@@ -300,15 +300,18 @@ impl CharExt for char {
300300 #[ inline]
301301 fn escape_unicode ( self ) -> EscapeUnicode {
302302 let c = self as u32 ;
303+
303304 // or-ing 1 ensures that for c==0 the code computes that one
304305 // digit should be printed and (which is the same) avoids the
305306 // (31 - 32) underflow
306307 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 ;
308311 EscapeUnicode {
309312 c : self ,
310313 state : EscapeUnicodeState :: Backslash ,
311- offset : msdigit as usize ,
314+ hex_digit_idx : ms_hex_digit as usize ,
312315 }
313316 }
314317
@@ -431,7 +434,11 @@ pub fn encode_utf16_raw(mut ch: u32, dst: &mut [u16]) -> Option<usize> {
431434pub struct EscapeUnicode {
432435 c : char ,
433436 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 ,
435442}
436443
437444#[ derive( Clone ) ]
@@ -463,11 +470,11 @@ impl Iterator for EscapeUnicode {
463470 Some ( '{' )
464471 }
465472 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 {
468475 self . state = EscapeUnicodeState :: RightBrace ;
469476 } else {
470- self . offset -= 1 ;
477+ self . hex_digit_idx -= 1 ;
471478 }
472479 Some ( c)
473480 }
@@ -488,7 +495,7 @@ impl Iterator for EscapeUnicode {
488495 EscapeUnicodeState :: RightBrace => 1 ,
489496 EscapeUnicodeState :: Done => 0 ,
490497 } ;
491- let n = n + self . offset ;
498+ let n = n + self . hex_digit_idx ;
492499 ( n, Some ( n) )
493500 }
494501}
0 commit comments