@@ -48,6 +48,7 @@ impl Token {
48
48
}
49
49
50
50
/// Enum representing common lexeme types.
51
+ // perf note: Changing all `usize` to `u32` doesn't change performance. See #77629
51
52
#[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord ) ]
52
53
pub enum TokenKind {
53
54
// Multi-char tokens:
@@ -160,6 +161,7 @@ pub enum LiteralKind {
160
161
/// - `r##~"abcde"##`: `InvalidStarter`
161
162
/// - `r###"abcde"##`: `NoTerminator { expected: 3, found: 2, possible_terminator_offset: Some(11)`
162
163
/// - Too many `#`s (>65535): `TooManyDelimiters`
164
+ // perf note: It doesn't matter that this makes `Token` 36 bytes bigger. See #77629
163
165
#[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord ) ]
164
166
pub enum RawStrError {
165
167
/// Non `#` characters exist between `r` and `"` eg. `r#~"..`
@@ -689,7 +691,12 @@ impl Cursor<'_> {
689
691
let mut max_hashes = 0 ;
690
692
691
693
// Count opening '#' symbols.
692
- let n_start_hashes = self . eat_while ( |c| c == '#' ) ;
694
+ let mut eaten = 0 ;
695
+ while self . first ( ) == '#' {
696
+ eaten += 1 ;
697
+ self . bump ( ) ;
698
+ }
699
+ let n_start_hashes = eaten;
693
700
694
701
// Check that string is started.
695
702
match self . bump ( ) {
@@ -724,16 +731,11 @@ impl Cursor<'_> {
724
731
// Note that this will not consume extra trailing `#` characters:
725
732
// `r###"abcde"####` is lexed as a `RawStr { n_hashes: 3 }`
726
733
// followed by a `#` token.
727
- let mut hashes_left = n_start_hashes;
728
- let is_closing_hash = |c| {
729
- if c == '#' && hashes_left != 0 {
730
- hashes_left -= 1 ;
731
- true
732
- } else {
733
- false
734
- }
735
- } ;
736
- let n_end_hashes = self . eat_while ( is_closing_hash) ;
734
+ let mut n_end_hashes = 0 ;
735
+ while self . first ( ) == '#' && n_end_hashes < n_start_hashes {
736
+ n_end_hashes += 1 ;
737
+ self . bump ( ) ;
738
+ }
737
739
738
740
if n_end_hashes == n_start_hashes {
739
741
return ( n_start_hashes, None ) ;
@@ -807,17 +809,9 @@ impl Cursor<'_> {
807
809
}
808
810
809
811
/// Eats symbols while predicate returns true or until the end of file is reached.
810
- /// Returns amount of eaten symbols.
811
- fn eat_while < F > ( & mut self , mut predicate : F ) -> usize
812
- where
813
- F : FnMut ( char ) -> bool ,
814
- {
815
- let mut eaten: usize = 0 ;
812
+ fn eat_while ( & mut self , mut predicate : impl FnMut ( char ) -> bool ) {
816
813
while predicate ( self . first ( ) ) && !self . is_eof ( ) {
817
- eaten += 1 ;
818
814
self . bump ( ) ;
819
815
}
820
-
821
- eaten
822
816
}
823
817
}
0 commit comments