We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
EscapeUnicode
1 parent 2fd2670 commit 7b33d39Copy full SHA for 7b33d39
src/libcore/char.rs
@@ -299,14 +299,16 @@ impl CharExt for char {
299
300
#[inline]
301
fn escape_unicode(self) -> EscapeUnicode {
302
- let mut n = 0;
303
- while (self as u32) >> (4 * (n + 1)) != 0 {
304
- n += 1;
305
- }
+ let c = self as u32;
+ // or-ing 1 ensures that for c==0 the code computes that one
+ // digit should be printed and (which is the same) avoids the
+ // (31 - 32) underflow
306
+ let msb = 31 - (c | 1).leading_zeros();
307
+ let msdigit = msb / 4;
308
EscapeUnicode {
309
c: self,
310
state: EscapeUnicodeState::Backslash,
- offset: n,
311
+ offset: msdigit as usize,
312
}
313
314
0 commit comments