diff options
| author | Julian Wollersberger <julian.wollersberger@gmx.at> | 2021-11-30 16:06:58 +0100 |
|---|---|---|
| committer | Julian Wollersberger <julian.wollersberger@gmx.at> | 2021-12-01 19:14:10 +0100 |
| commit | 1f147a2ed7671cacd8ab423d8979a1ccfa4443ab (patch) | |
| tree | 7ceb843b858469295039282f0417caac325bb010 /compiler/rustc_lexer/src/lib.rs | |
| parent | 72d66064e77281536588189a916af28a1819b313 (diff) | |
| download | rust-1f147a2ed7671cacd8ab423d8979a1ccfa4443ab.tar.gz rust-1f147a2ed7671cacd8ab423d8979a1ccfa4443ab.zip | |
Replace `nth_char(0)` with `next()` in `cursor.first()`
and optimize the iterator returned by `tokenize(). This improves lexer performance by 35%
Diffstat (limited to 'compiler/rustc_lexer/src/lib.rs')
| -rw-r--r-- | compiler/rustc_lexer/src/lib.rs | 20 |
1 files changed, 7 insertions, 13 deletions
diff --git a/compiler/rustc_lexer/src/lib.rs b/compiler/rustc_lexer/src/lib.rs index b64a891cb25..08cd2d29c41 100644 --- a/compiler/rustc_lexer/src/lib.rs +++ b/compiler/rustc_lexer/src/lib.rs @@ -225,14 +225,15 @@ pub fn first_token(input: &str) -> Token { } /// Creates an iterator that produces tokens from the input string. -pub fn tokenize(mut input: &str) -> impl Iterator<Item = Token> + '_ { +pub fn tokenize(input: &str) -> impl Iterator<Item = Token> + '_ { + let mut cursor = Cursor::new(input); std::iter::from_fn(move || { - if input.is_empty() { - return None; + if cursor.is_eof() { + None + } else { + cursor.reset_len_consumed(); + Some(cursor.advance_token()) } - let token = first_token(input); - input = &input[token.len..]; - Some(token) }) } @@ -808,11 +809,4 @@ impl Cursor<'_> { self.eat_while(is_id_continue); } - - /// Eats symbols while predicate returns true or until the end of file is reached. - fn eat_while(&mut self, mut predicate: impl FnMut(char) -> bool) { - while predicate(self.first()) && !self.is_eof() { - self.bump(); - } - } } |
