@@ -361,65 +361,18 @@ impl Cursor<'_> {
361361 } ,
362362
363363 // Byte literal, byte string literal, raw byte string literal or identifier.
364- 'b' => match ( self . first ( ) , self . second ( ) ) {
365- ( '\'' , _) => {
366- self . bump ( ) ;
367- let terminated = self . single_quoted_string ( ) ;
368- let suffix_start = self . pos_within_token ( ) ;
369- if terminated {
370- self . eat_literal_suffix ( ) ;
371- }
372- let kind = Byte { terminated } ;
373- Literal { kind, suffix_start }
374- }
375- ( '"' , _) => {
376- self . bump ( ) ;
377- let terminated = self . double_quoted_string ( ) ;
378- let suffix_start = self . pos_within_token ( ) ;
379- if terminated {
380- self . eat_literal_suffix ( ) ;
381- }
382- let kind = ByteStr { terminated } ;
383- Literal { kind, suffix_start }
384- }
385- ( 'r' , '"' ) | ( 'r' , '#' ) => {
386- self . bump ( ) ;
387- let res = self . raw_double_quoted_string ( 2 ) ;
388- let suffix_start = self . pos_within_token ( ) ;
389- if res. is_ok ( ) {
390- self . eat_literal_suffix ( ) ;
391- }
392- let kind = RawByteStr { n_hashes : res. ok ( ) } ;
393- Literal { kind, suffix_start }
394- }
395- _ => self . ident_or_unknown_prefix ( ) ,
396- } ,
364+ 'b' => self . c_or_byte_string (
365+ |terminated| ByteStr { terminated } ,
366+ |n_hashes| RawByteStr { n_hashes } ,
367+ Some ( |terminated| Byte { terminated } ) ,
368+ ) ,
397369
398- // TODO deduplicate this code
399370 // c-string literal, raw c-string literal or identifier.
400- 'c' => match ( self . first ( ) , self . second ( ) ) {
401- ( '"' , _) => {
402- self . bump ( ) ;
403- let terminated = self . double_quoted_string ( ) ;
404- let suffix_start = self . pos_within_token ( ) ;
405- if terminated {
406- self . eat_literal_suffix ( ) ;
407- }
408- let kind = CStr { terminated } ;
409- Literal { kind, suffix_start }
410- }
411- ( 'r' , '"' ) | ( 'r' , '#' ) => {
412- self . bump ( ) ;
413- let res = self . raw_double_quoted_string ( 2 ) ;
414- let suffix_start = self . pos_within_token ( ) ;
415- if res. is_ok ( ) {
416- self . eat_literal_suffix ( ) ;
417- }
418- let kind = RawCStr { n_hashes : res. ok ( ) } ;
419- Literal { kind, suffix_start }
420- }
421- _ => self . ident_or_unknown_prefix ( ) ,
422- } ,
371+ 'c' => self . c_or_byte_string (
372+ |terminated| CStr { terminated } ,
373+ |n_hashes| RawCStr { n_hashes } ,
374+ None ,
375+ ) ,
423376
424377 // Identifier (this should be checked after other variant that can
425378 // start as identifier).
@@ -583,6 +536,47 @@ impl Cursor<'_> {
583536 }
584537 }
585538
539+ fn c_or_byte_string (
540+ & mut self ,
541+ mk_kind : impl FnOnce ( bool ) -> LiteralKind ,
542+ mk_kind_raw : impl FnOnce ( Option < u8 > ) -> LiteralKind ,
543+ single_quoted : Option < fn ( bool ) -> LiteralKind > ,
544+ ) -> TokenKind {
545+ match ( self . first ( ) , self . second ( ) , single_quoted) {
546+ ( '\'' , _, Some ( mk_kind) ) => {
547+ self . bump ( ) ;
548+ let terminated = self . single_quoted_string ( ) ;
549+ let suffix_start = self . pos_within_token ( ) ;
550+ if terminated {
551+ self . eat_literal_suffix ( ) ;
552+ }
553+ let kind = mk_kind ( terminated) ;
554+ Literal { kind, suffix_start }
555+ }
556+ ( '"' , _, _) => {
557+ self . bump ( ) ;
558+ let terminated = self . double_quoted_string ( ) ;
559+ let suffix_start = self . pos_within_token ( ) ;
560+ if terminated {
561+ self . eat_literal_suffix ( ) ;
562+ }
563+ let kind = mk_kind ( terminated) ;
564+ Literal { kind, suffix_start }
565+ }
566+ ( 'r' , '"' , _) | ( 'r' , '#' , _) => {
567+ self . bump ( ) ;
568+ let res = self . raw_double_quoted_string ( 2 ) ;
569+ let suffix_start = self . pos_within_token ( ) ;
570+ if res. is_ok ( ) {
571+ self . eat_literal_suffix ( ) ;
572+ }
573+ let kind = mk_kind_raw ( res. ok ( ) ) ;
574+ Literal { kind, suffix_start }
575+ }
576+ _ => self . ident_or_unknown_prefix ( ) ,
577+ }
578+ }
579+
586580 fn number ( & mut self , first_digit : char ) -> LiteralKind {
587581 debug_assert ! ( '0' <= self . prev( ) && self . prev( ) <= '9' ) ;
588582 let mut base = Base :: Decimal ;
0 commit comments