Skip to content

Commit 4c371bb

Browse files
committed
Auto merge of #27319 - diaphore:pr_debug_osstr_escape, r=alexcrichton
I had to modify some tests : since `wtf8buf_show` and `wtf8_show` were doing the exact same thing, I repurposed `wtf8_show` to `wtf8buf_show_str` which ensures `Wtf8Buf` `Debug`-formats the same as `str`. `write_str_escaped` might also be shared amongst other `fmt` but I just left it there within `Wtf8::fmt` for review.
2 parents 5b72fa4 + aa89504 commit 4c371bb

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/libcore/fmt/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,7 @@ impl Debug for str {
12871287
fn fmt(&self, f: &mut Formatter) -> Result {
12881288
try!(write!(f, "\""));
12891289
for c in self.chars().flat_map(|c| c.escape_default()) {
1290-
try!(write!(f, "{}", c));
1290+
try!(f.write_char(c))
12911291
}
12921292
write!(f, "\"")
12931293
}
@@ -1306,7 +1306,7 @@ impl Debug for char {
13061306
use char::CharExt;
13071307
try!(write!(f, "'"));
13081308
for c in self.escape_default() {
1309-
try!(write!(f, "{}", c));
1309+
try!(f.write_char(c))
13101310
}
13111311
write!(f, "'")
13121312
}

src/libstd/sys/common/wtf8.rs

+25-15
Original file line numberDiff line numberDiff line change
@@ -426,26 +426,36 @@ impl Ord for Wtf8 {
426426
/// and surrogates as `\u` followed by four hexadecimal digits.
427427
/// Example: `"a\u{D800}"` for a slice with code points [U+0061, U+D800]
428428
impl fmt::Debug for Wtf8 {
429-
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
429+
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
430+
fn write_str_escaped(f: &mut fmt::Formatter, s: &str) -> fmt::Result {
431+
use fmt::Write;
432+
for c in s.chars().flat_map(|c| c.escape_default()) {
433+
try!(f.write_char(c))
434+
}
435+
Ok(())
436+
}
437+
430438
try!(formatter.write_str("\""));
431439
let mut pos = 0;
432440
loop {
433441
match self.next_surrogate(pos) {
434442
None => break,
435443
Some((surrogate_pos, surrogate)) => {
436-
try!(formatter.write_str(unsafe {
437-
// the data in this slice is valid UTF-8, transmute to &str
438-
mem::transmute(&self.bytes[pos .. surrogate_pos])
439-
}));
444+
try!(write_str_escaped(
445+
formatter,
446+
unsafe { str::from_utf8_unchecked(
447+
&self.bytes[pos .. surrogate_pos]
448+
)},
449+
));
440450
try!(write!(formatter, "\\u{{{:X}}}", surrogate));
441451
pos = surrogate_pos + 3;
442452
}
443453
}
444454
}
445-
try!(formatter.write_str(unsafe {
446-
// the data in this slice is valid UTF-8, transmute to &str
447-
mem::transmute(&self.bytes[pos..])
448-
}));
455+
try!(write_str_escaped(
456+
formatter,
457+
unsafe { str::from_utf8_unchecked(&self.bytes[pos..]) },
458+
));
449459
formatter.write_str("\"")
450460
}
451461
}
@@ -1083,9 +1093,9 @@ mod tests {
10831093

10841094
#[test]
10851095
fn wtf8buf_show() {
1086-
let mut string = Wtf8Buf::from_str("aé 💩");
1096+
let mut string = Wtf8Buf::from_str("a\té 💩\r");
10871097
string.push(CodePoint::from_u32(0xD800).unwrap());
1088-
assert_eq!(format!("{:?}", string), r#""aé 💩\u{D800}""#);
1098+
assert_eq!(format!("{:?}", string), r#""a\t\u{e9} \u{1f4a9}\r\u{D800}""#);
10891099
}
10901100

10911101
#[test]
@@ -1094,10 +1104,10 @@ mod tests {
10941104
}
10951105

10961106
#[test]
1097-
fn wtf8_show() {
1098-
let mut string = Wtf8Buf::from_str("aé 💩");
1099-
string.push(CodePoint::from_u32(0xD800).unwrap());
1100-
assert_eq!(format!("{:?}", string), r#""aé 💩\u{D800}""#);
1107+
fn wtf8buf_show_str() {
1108+
let text = "a\té 💩\r";
1109+
let mut string = Wtf8Buf::from_str(text);
1110+
assert_eq!(format!("{:?}", text), format!("{:?}", string));
11011111
}
11021112

11031113
#[test]

0 commit comments

Comments
 (0)