Implement `size_hint` for `EscapeUnicode`
This commit is contained in:
parent
d2d08721be
commit
0294098d8f
|
@ -342,6 +342,22 @@ impl Iterator for EscapeUnicode {
|
|||
EscapeUnicodeState::Done => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let mut n = 0;
|
||||
while (self.c as usize) >> (4 * (n + 1)) != 0 {
|
||||
n += 1;
|
||||
}
|
||||
let n = match self.state {
|
||||
EscapeUnicodeState::Backslash => n + 5,
|
||||
EscapeUnicodeState::Type => n + 4,
|
||||
EscapeUnicodeState::LeftBrace => n + 3,
|
||||
EscapeUnicodeState::Value(offset) => offset + 2,
|
||||
EscapeUnicodeState::RightBrace => 1,
|
||||
EscapeUnicodeState::Done => 0,
|
||||
};
|
||||
(n, Some(n))
|
||||
}
|
||||
}
|
||||
|
||||
/// An iterator over the characters that represent a `char`, escaped
|
||||
|
@ -375,7 +391,7 @@ impl Iterator for EscapeDefault {
|
|||
Some(c)
|
||||
}
|
||||
EscapeDefaultState::Done => None,
|
||||
EscapeDefaultState::Unicode(ref mut iter) => iter.next()
|
||||
EscapeDefaultState::Unicode(ref mut iter) => iter.next(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -383,8 +399,8 @@ impl Iterator for EscapeDefault {
|
|||
match self.state {
|
||||
EscapeDefaultState::Char(_) => (1, Some(1)),
|
||||
EscapeDefaultState::Backslash(_) => (2, Some(2)),
|
||||
EscapeDefaultState::Unicode(_) => (0, Some(10)),
|
||||
_ => (0, Some(0))
|
||||
EscapeDefaultState::Unicode(ref iter) => iter.size_hint(),
|
||||
EscapeDefaultState::Done => (0, Some(0)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1315,7 +1315,7 @@ impl Debug for str {
|
|||
for (i, c) in self.char_indices() {
|
||||
let esc = c.escape_default();
|
||||
// If char needs escaping, flush backlog so far and write, else skip
|
||||
if esc.size_hint().0 != 1 {
|
||||
if esc.size_hint() != (1, Some(1)) {
|
||||
try!(f.write_str(&self[from..i]));
|
||||
for c in esc {
|
||||
try!(f.write_char(c));
|
||||
|
|
Loading…
Reference in New Issue