@@ -300,15 +300,18 @@ impl CharExt for char {
300
300
#[ inline]
301
301
fn escape_unicode ( self ) -> EscapeUnicode {
302
302
let c = self as u32 ;
303
+
303
304
// or-ing 1 ensures that for c==0 the code computes that one
304
305
// digit should be printed and (which is the same) avoids the
305
306
// (31 - 32) underflow
306
307
let msb = 31 - ( c | 1 ) . leading_zeros ( ) ;
307
- let msdigit = msb / 4 ;
308
+
309
+ // the index of the most significant hex digit
310
+ let ms_hex_digit = msb / 4 ;
308
311
EscapeUnicode {
309
312
c : self ,
310
313
state : EscapeUnicodeState :: Backslash ,
311
- offset : msdigit as usize ,
314
+ hex_digit_idx : ms_hex_digit as usize ,
312
315
}
313
316
}
314
317
@@ -431,7 +434,11 @@ pub fn encode_utf16_raw(mut ch: u32, dst: &mut [u16]) -> Option<usize> {
431
434
pub struct EscapeUnicode {
432
435
c : char ,
433
436
state : EscapeUnicodeState ,
434
- offset : usize ,
437
+
438
+ // The index of the next hex digit to be printed (0 if none),
439
+ // i.e. the number of remaining hex digits to be printed;
440
+ // increasing from the least significant digit: 0x543210
441
+ hex_digit_idx : usize ,
435
442
}
436
443
437
444
#[ derive( Clone ) ]
@@ -463,11 +470,11 @@ impl Iterator for EscapeUnicode {
463
470
Some ( '{' )
464
471
}
465
472
EscapeUnicodeState :: Value => {
466
- let c = from_digit ( ( ( self . c as u32 ) >> ( self . offset * 4 ) ) & 0xf , 16 ) . unwrap ( ) ;
467
- if self . offset == 0 {
473
+ let c = from_digit ( ( ( self . c as u32 ) >> ( self . hex_digit_idx * 4 ) ) & 0xf , 16 ) . unwrap ( ) ;
474
+ if self . hex_digit_idx == 0 {
468
475
self . state = EscapeUnicodeState :: RightBrace ;
469
476
} else {
470
- self . offset -= 1 ;
477
+ self . hex_digit_idx -= 1 ;
471
478
}
472
479
Some ( c)
473
480
}
@@ -488,7 +495,7 @@ impl Iterator for EscapeUnicode {
488
495
EscapeUnicodeState :: RightBrace => 1 ,
489
496
EscapeUnicodeState :: Done => 0 ,
490
497
} ;
491
- let n = n + self . offset ;
498
+ let n = n + self . hex_digit_idx ;
492
499
( n, Some ( n) )
493
500
}
494
501
}
0 commit comments