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 6, 2023
1 parent 9c53e4f commit daaa60d
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,19 @@ impl Delimiters {

#[inline]
fn from_byte(byte: Option<u8>) -> Delimiters {
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,
}
let Some(b) = byte else { return Delimiter::None };
const TABLE: [Delimiters; 255] = {
let mut table = [Delimiter::None; 255];
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
};
TABLE[b as usize]
}
}

Expand Down

0 comments on commit daaa60d

Please sign in to comment.