Improve computation of EscapeUnicode offset field

Instead of iteratively scanning the bits, use `leading_zeros`.
This commit is contained in:
Andrea Canciani 2015-12-30 22:03:35 +01:00
parent 2fd2670ea0
commit 7b33d39da9

View file

@ -299,14 +299,16 @@ impl CharExt for char {
#[inline]
fn escape_unicode(self) -> EscapeUnicode {
let mut n = 0;
while (self as u32) >> (4 * (n + 1)) != 0 {
n += 1;
}
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
let msb = 31 - (c | 1).leading_zeros();
let msdigit = msb / 4;
EscapeUnicode {
c: self,
state: EscapeUnicodeState::Backslash,
offset: n,
offset: msdigit as usize,
}
}