Fix escaping of characters in Debug for OsStr

Fixes #27211

Fix Debug for {char, str} in core::fmt
This commit is contained in:
diaphore 2015-07-26 19:32:13 +02:00
parent 0469be1eb7
commit aa8950427e
2 changed files with 27 additions and 17 deletions

View File

@ -1287,7 +1287,7 @@ impl Debug for str {
fn fmt(&self, f: &mut Formatter) -> Result { fn fmt(&self, f: &mut Formatter) -> Result {
try!(write!(f, "\"")); try!(write!(f, "\""));
for c in self.chars().flat_map(|c| c.escape_default()) { for c in self.chars().flat_map(|c| c.escape_default()) {
try!(write!(f, "{}", c)); try!(f.write_char(c))
} }
write!(f, "\"") write!(f, "\"")
} }
@ -1306,7 +1306,7 @@ impl Debug for char {
use char::CharExt; use char::CharExt;
try!(write!(f, "'")); try!(write!(f, "'"));
for c in self.escape_default() { for c in self.escape_default() {
try!(write!(f, "{}", c)); try!(f.write_char(c))
} }
write!(f, "'") write!(f, "'")
} }

View File

@ -426,26 +426,36 @@ impl Ord for Wtf8 {
/// and surrogates as `\u` followed by four hexadecimal digits. /// and surrogates as `\u` followed by four hexadecimal digits.
/// Example: `"a\u{D800}"` for a slice with code points [U+0061, U+D800] /// Example: `"a\u{D800}"` for a slice with code points [U+0061, U+D800]
impl fmt::Debug for Wtf8 { impl fmt::Debug for Wtf8 {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
fn write_str_escaped(f: &mut fmt::Formatter, s: &str) -> fmt::Result {
use fmt::Write;
for c in s.chars().flat_map(|c| c.escape_default()) {
try!(f.write_char(c))
}
Ok(())
}
try!(formatter.write_str("\"")); try!(formatter.write_str("\""));
let mut pos = 0; let mut pos = 0;
loop { loop {
match self.next_surrogate(pos) { match self.next_surrogate(pos) {
None => break, None => break,
Some((surrogate_pos, surrogate)) => { Some((surrogate_pos, surrogate)) => {
try!(formatter.write_str(unsafe { try!(write_str_escaped(
// the data in this slice is valid UTF-8, transmute to &str formatter,
mem::transmute(&self.bytes[pos .. surrogate_pos]) unsafe { str::from_utf8_unchecked(
})); &self.bytes[pos .. surrogate_pos]
)},
));
try!(write!(formatter, "\\u{{{:X}}}", surrogate)); try!(write!(formatter, "\\u{{{:X}}}", surrogate));
pos = surrogate_pos + 3; pos = surrogate_pos + 3;
} }
} }
} }
try!(formatter.write_str(unsafe { try!(write_str_escaped(
// the data in this slice is valid UTF-8, transmute to &str formatter,
mem::transmute(&self.bytes[pos..]) unsafe { str::from_utf8_unchecked(&self.bytes[pos..]) },
})); ));
formatter.write_str("\"") formatter.write_str("\"")
} }
} }
@ -1083,9 +1093,9 @@ mod tests {
#[test] #[test]
fn wtf8buf_show() { fn wtf8buf_show() {
let mut string = Wtf8Buf::from_str("aé 💩"); let mut string = Wtf8Buf::from_str("a\té 💩\r");
string.push(CodePoint::from_u32(0xD800).unwrap()); string.push(CodePoint::from_u32(0xD800).unwrap());
assert_eq!(format!("{:?}", string), r#""aé 💩\u{D800}""#); assert_eq!(format!("{:?}", string), r#""a\t\u{e9} \u{1f4a9}\r\u{D800}""#);
} }
#[test] #[test]
@ -1094,10 +1104,10 @@ mod tests {
} }
#[test] #[test]
fn wtf8_show() { fn wtf8buf_show_str() {
let mut string = Wtf8Buf::from_str("aé 💩"); let text = "a\té 💩\r";
string.push(CodePoint::from_u32(0xD800).unwrap()); let mut string = Wtf8Buf::from_str(text);
assert_eq!(format!("{:?}", string), r#""aé 💩\u{D800}""#); assert_eq!(format!("{:?}", text), format!("{:?}", string));
} }
#[test] #[test]