Skip to content

Commit

Permalink
Add ASCII fast path from rust-lang
Browse files Browse the repository at this point in the history
See rust-lang/rust's `src/librustc_lexer/src/lib.rs`

Idea came from unicode-rs#13
  • Loading branch information
Ed Page committed Apr 28, 2021
1 parent 4c762c1 commit 122b387
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,19 @@ pub trait UnicodeXID {
impl UnicodeXID for char {
#[inline]
fn is_xid_start(self) -> bool {
derived_property::XID_Start(self)
// Fast-path for ascii idents
('a' <= self && self <= 'z')
|| ('A' <= self && self <= 'Z')
|| (self > '\x7f' && derived_property::XID_Start(self))
}

#[inline]
fn is_xid_continue(self) -> bool {
derived_property::XID_Continue(self)
// Fast-path for ascii idents
('a' <= self && self <= 'z')
|| ('A' <= self && self <= 'Z')
|| ('0' <= self && self <= '9')
|| self == '_'
|| (self > '\x7f' && derived_property::XID_Continue(self))
}
}

0 comments on commit 122b387

Please sign in to comment.