Skip to content

Commit 811c1a4

Browse files
committed
perf(parser): remove bounds check for getting strings from lexer
1 parent 0549ae5 commit 811c1a4

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

crates/oxc_parser/src/lexer/string.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,26 @@ impl<'a> Lexer<'a> {
255255
return self.escaped_strings[&token.start()];
256256
}
257257

258-
let raw = &self.source.whole()[token.start() as usize..token.end() as usize];
259-
match token.kind() {
258+
let start = token.start() as usize;
259+
let end = token.end() as usize;
260+
let (start, end) = match token.kind() {
260261
Kind::Str => {
261-
&raw[1..raw.len() - 1] // omit surrounding quotes
262+
// Omit surrounding quotes
263+
(start + 1, end - 1)
262264
}
263265
Kind::PrivateIdentifier => {
264-
&raw[1..] // omit leading `#`
266+
// Omit leading `#`
267+
(start + 1, end)
265268
}
266-
_ => raw,
267-
}
269+
_ => (start, end),
270+
};
271+
let whole_source = self.source.whole();
272+
debug_assert!(start <= whole_source.len());
273+
debug_assert!(end <= whole_source.len());
274+
debug_assert!(start <= end);
275+
debug_assert!(whole_source.is_char_boundary(start));
276+
debug_assert!(whole_source.is_char_boundary(end));
277+
// SAFETY: `token` is guaranteed to be within the source and on UTF-8 boundaries.
278+
unsafe { whole_source.get_unchecked(start..end) }
268279
}
269280
}

0 commit comments

Comments
 (0)