Implement size_hint
for EscapeDefault
This commit is contained in:
parent
025ca11ab9
commit
d2d08721be
@ -147,7 +147,6 @@ pub trait CharExt {
|
||||
fn to_digit(self, radix: u32) -> Option<u32>;
|
||||
fn escape_unicode(self) -> EscapeUnicode;
|
||||
fn escape_default(self) -> EscapeDefault;
|
||||
fn needs_escape_default(self) -> bool;
|
||||
fn len_utf8(self) -> usize;
|
||||
fn len_utf16(self) -> usize;
|
||||
fn encode_utf8(self, dst: &mut [u8]) -> Option<usize>;
|
||||
@ -186,24 +185,13 @@ impl CharExt for char {
|
||||
'\t' => EscapeDefaultState::Backslash('t'),
|
||||
'\r' => EscapeDefaultState::Backslash('r'),
|
||||
'\n' => EscapeDefaultState::Backslash('n'),
|
||||
'\\' => EscapeDefaultState::Backslash('\\'),
|
||||
'\'' => EscapeDefaultState::Backslash('\''),
|
||||
'"' => EscapeDefaultState::Backslash('"'),
|
||||
'\\' | '\'' | '"' => EscapeDefaultState::Backslash(self),
|
||||
'\x20' ... '\x7e' => EscapeDefaultState::Char(self),
|
||||
_ => EscapeDefaultState::Unicode(self.escape_unicode())
|
||||
};
|
||||
EscapeDefault { state: init_state }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn needs_escape_default(self) -> bool {
|
||||
match self {
|
||||
'\\' | '\'' | '"' => true,
|
||||
'\x20' ... '\x7e' => false,
|
||||
_ => true
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn len_utf8(self) -> usize {
|
||||
let code = self as u32;
|
||||
@ -390,4 +378,13 @@ impl Iterator for EscapeDefault {
|
||||
EscapeDefaultState::Unicode(ref mut iter) => iter.next()
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
match self.state {
|
||||
EscapeDefaultState::Char(_) => (1, Some(1)),
|
||||
EscapeDefaultState::Backslash(_) => (2, Some(2)),
|
||||
EscapeDefaultState::Unicode(_) => (0, Some(10)),
|
||||
_ => (0, Some(0))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1313,11 +1313,12 @@ impl Debug for str {
|
||||
try!(f.write_char('"'));
|
||||
let mut from = 0;
|
||||
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 c.needs_escape_default() {
|
||||
if esc.size_hint().0 != 1 {
|
||||
try!(f.write_str(&self[from..i]));
|
||||
for e in c.escape_default() {
|
||||
try!(f.write_char(e));
|
||||
for c in esc {
|
||||
try!(f.write_char(c));
|
||||
}
|
||||
from = i + c.len_utf8();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user