@@ -449,34 +449,10 @@ impl Iterator for EscapeUnicode {
449
449
type Item = char ;
450
450
451
451
fn next ( & mut self ) -> Option < char > {
452
- match self . state {
453
- EscapeUnicodeState :: Backslash => {
454
- self . state = EscapeUnicodeState :: Type ;
455
- Some ( '\\' )
456
- }
457
- EscapeUnicodeState :: Type => {
458
- self . state = EscapeUnicodeState :: LeftBrace ;
459
- Some ( 'u' )
460
- }
461
- EscapeUnicodeState :: LeftBrace => {
462
- self . state = EscapeUnicodeState :: Value ;
463
- Some ( '{' )
464
- }
465
- EscapeUnicodeState :: Value => {
466
- let c = from_digit ( ( ( self . c as u32 ) >> ( self . offset * 4 ) ) & 0xf , 16 ) . unwrap ( ) ;
467
- if self . offset == 0 {
468
- self . state = EscapeUnicodeState :: RightBrace ;
469
- } else {
470
- self . offset -= 1 ;
471
- }
472
- Some ( c)
473
- }
474
- EscapeUnicodeState :: RightBrace => {
475
- self . state = EscapeUnicodeState :: Done ;
476
- Some ( '}' )
477
- }
478
- EscapeUnicodeState :: Done => None ,
479
- }
452
+ let state = self . state_len ( ) ;
453
+ let offset = self . offset ;
454
+
455
+ self . step ( state, offset)
480
456
}
481
457
482
458
#[ inline]
@@ -507,8 +483,15 @@ impl Iterator for EscapeUnicode {
507
483
impl ExactSizeIterator for EscapeUnicode {
508
484
#[ inline]
509
485
fn len ( & self ) -> usize {
486
+ self . offset + self . state_len ( )
487
+ }
488
+ }
489
+
490
+ impl EscapeUnicode {
491
+ #[ inline]
492
+ fn state_len ( & self ) -> usize {
510
493
// The match is a single memory access with no branching
511
- self . offset + self . state {
494
+ match self . state {
512
495
EscapeUnicodeState :: Done => 0 ,
513
496
EscapeUnicodeState :: RightBrace => 1 ,
514
497
EscapeUnicodeState :: Value => 2 ,
@@ -517,6 +500,43 @@ impl ExactSizeIterator for EscapeUnicode {
517
500
EscapeUnicodeState :: Backslash => 5 ,
518
501
}
519
502
}
503
+
504
+ #[ inline]
505
+ fn step ( & mut self , state : usize , offset : usize ) -> Option < char > {
506
+ self . offset = offset;
507
+
508
+ match state {
509
+ 5 => {
510
+ self . state = EscapeUnicodeState :: Type ;
511
+ Some ( '\\' )
512
+ }
513
+ 4 => {
514
+ self . state = EscapeUnicodeState :: LeftBrace ;
515
+ Some ( 'u' )
516
+ }
517
+ 3 => {
518
+ self . state = EscapeUnicodeState :: LeftBrace ;
519
+ Some ( '{' )
520
+ }
521
+ 2 => {
522
+ self . state = if offset == 0 {
523
+ EscapeUnicodeState :: RightBrace
524
+ } else {
525
+ self . offset -= 1 ;
526
+ EscapeUnicodeState :: Value
527
+ } ;
528
+ from_digit ( ( ( self . c as u32 ) >> ( offset * 4 ) ) & 0xf , 16 )
529
+ }
530
+ 1 => {
531
+ self . state = EscapeUnicodeState :: Done ;
532
+ Some ( '}' )
533
+ }
534
+ _ => {
535
+ self . state = EscapeUnicodeState :: Done ;
536
+ None
537
+ }
538
+ }
539
+ }
520
540
}
521
541
522
542
/// An iterator that yields the literal escape code of a `char`.
0 commit comments