Skip to content

Commit b349778

Browse files
committed
refactor(lexer): shorten code for parsing hex digit
1 parent d023335 commit b349778

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

crates/oxc_parser/src/lexer/unicode.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -163,29 +163,29 @@ impl<'a> Lexer<'a> {
163163
}
164164

165165
fn hex_digit(&mut self) -> Option<u32> {
166+
let b = self.peek_byte()?;
167+
166168
// Reduce instructions and remove 1 branch by comparing against `A-F` and `a-f` simultaneously
167169
// https://godbolt.org/z/9caMMzvP3
168-
let value = if let Some(b) = self.peek_byte() {
169-
if b.is_ascii_digit() {
170-
b - b'0'
170+
let value = if b.is_ascii_digit() {
171+
b - b'0'
172+
} else {
173+
// Match `A-F` or `a-f`. `b | 32` converts uppercase letters to lowercase,
174+
// but leaves lowercase as they are
175+
let lower_case = b | 32;
176+
if matches!(lower_case, b'a'..=b'f') {
177+
lower_case + 10 - b'a'
171178
} else {
172-
// Match `A-F` or `a-f`. `b | 32` converts uppercase letters to lowercase,
173-
// but leaves lowercase as they are
174-
let lower_case = b | 32;
175-
if matches!(lower_case, b'a'..=b'f') {
176-
lower_case + 10 - b'a'
177-
} else {
178-
return None;
179-
}
179+
return None;
180180
}
181-
} else {
182-
return None;
183181
};
182+
184183
// Because of `b | 32` above, compiler cannot deduce that next byte is definitely ASCII
185184
// so `next_byte_unchecked` is necessary to produce compact assembly, rather than `consume_char`.
186185
// SAFETY: This code is only reachable if there is a byte remaining, and it's ASCII.
187186
// Therefore it's safe to consume that byte, and will leave position on a UTF-8 char boundary.
188187
unsafe { self.source.next_byte_unchecked() };
188+
189189
Some(u32::from(value))
190190
}
191191

0 commit comments

Comments
 (0)