Skip to content

Commit

Permalink
perf(parser): speed up parsing octal literals
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jul 14, 2024
1 parent 79516c6 commit 05a9c4d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions crates/oxc_parser/src/lexer/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ fn parse_int_without_underscores(s: &str, kind: Kind) -> Result<f64, &'static st
Kind::Decimal => Ok(parse_decimal(s)),
Kind::Binary => Ok(parse_binary(&s[2..])),
Kind::Octal => {
let s = if s.starts_with("0o") || s.starts_with("0O") {
&s[2..]
// Octals always begin with `0`. Trim off leading `0`, `0o` or `0O`.
let second_byte = s.as_bytes()[1];
let s = if second_byte == b'o' || second_byte == b'O' {
// SAFETY: We just checked that 2nd byte is ASCII, so slicing off 2 bytes
// must be in bounds and on a UTF-8 character boundary.
unsafe { s.get_unchecked(2..) }
} else {
s // legacy octal
&s[1..] // legacy octal
};
Ok(parse_octal(s))
}
Expand Down

0 comments on commit 05a9c4d

Please sign in to comment.