Skip to content

Commit

Permalink
parser: Use a lookup table for Delimiter::from_byte.
Browse files Browse the repository at this point in the history
It's faster.
  • Loading branch information
emilio committed Sep 11, 2023
1 parent d5631d8 commit 12ceb37
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,21 @@ impl Delimiters {

#[inline]
fn from_byte(byte: Option<u8>) -> Delimiters {
const TABLE: [Delimiters; 256] = {
let mut table = [Delimiter::None; 256];
table[b';' as usize] = Delimiter::Semicolon;
table[b'!' as usize] = Delimiter::Bang;
table[b',' as usize] = Delimiter::Comma;
table[b'{' as usize] = Delimiter::CurlyBracketBlock;
table[b'}' as usize] = ClosingDelimiter::CloseCurlyBracket;
table[b']' as usize] = ClosingDelimiter::CloseSquareBracket;
table[b')' as usize] = ClosingDelimiter::CloseParenthesis;
table
};

match byte {
Some(b';') => Delimiter::Semicolon,
Some(b'!') => Delimiter::Bang,
Some(b',') => Delimiter::Comma,
Some(b'{') => Delimiter::CurlyBracketBlock,
Some(b'}') => ClosingDelimiter::CloseCurlyBracket,
Some(b']') => ClosingDelimiter::CloseSquareBracket,
Some(b')') => ClosingDelimiter::CloseParenthesis,
_ => Delimiter::None,
None => Delimiter::None,
Some(b) => TABLE[b as usize],
}
}
}
Expand Down

0 comments on commit 12ceb37

Please sign in to comment.