Closed
Description
The Debug
formatter for str
outputs one byte at a time to the underlying writer.
We could improve this, for example we could skip forward while no escaping is needed, and if we find a char to escape, write the backlog and then write escape for the current char, and continue.
Exploration impl:
impl Debug for str {
fn fmt(&self, f: &mut Formatter) -> Result {
try!(write!(f, "\""));
let mut write_from = 0;
for (index, ch) in self.char_indices() {
// if char needs escaping, flush backlog so far and write, else skip
let mut escaped = ch.escape_default();
if let Some(first) = escaped.next() {
if let Some(second) = escaped.next() {
if write_from != index {
try!(write!(f, "{}", &self[write_from..index]));
}
write_from = index + ch.len_utf8();
try!(write!(f, "{}{}", first, second));
for ech in escaped {
try!(write!(f, "{}", ech));
}
}
}
}
try!(write!(f, "{}", &self[write_from..]));
write!(f, "\"")
}
}