about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-07-28 10:28:45 +0000
committerbors <bors@rust-lang.org>2015-07-28 10:28:45 +0000
commit4c371bb6de7a1c21b7403b86a66a8ce3318ff003 (patch)
tree06b78d6597cda66e5e96f7e3da90fe895f5379f5
parent5b72fa42d434e92e5fbb9fbd3b0995cc936ddcb9 (diff)
parentaa8950427e522794dc5c0201c5e0d5bd2da1f0db (diff)
downloadrust-4c371bb6de7a1c21b7403b86a66a8ce3318ff003.tar.gz
rust-4c371bb6de7a1c21b7403b86a66a8ce3318ff003.zip
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.
-rw-r--r--src/libcore/fmt/mod.rs4
-rw-r--r--src/libstd/sys/common/wtf8.rs40
2 files changed, 27 insertions, 17 deletions
diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs
index 29a2f76ef29..02b23c6c7e7 100644
--- a/src/libcore/fmt/mod.rs
+++ b/src/libcore/fmt/mod.rs
@@ -1287,7 +1287,7 @@ impl Debug for str {
     fn fmt(&self, f: &mut Formatter) -> Result {
         try!(write!(f, "\""));
         for c in self.chars().flat_map(|c| c.escape_default()) {
-            try!(write!(f, "{}", c));
+            try!(f.write_char(c))
         }
         write!(f, "\"")
     }
@@ -1306,7 +1306,7 @@ impl Debug for char {
         use char::CharExt;
         try!(write!(f, "'"));
         for c in self.escape_default() {
-            try!(write!(f, "{}", c));
+            try!(f.write_char(c))
         }
         write!(f, "'")
     }
diff --git a/src/libstd/sys/common/wtf8.rs b/src/libstd/sys/common/wtf8.rs
index 6f15d606724..3d5d1f5e0eb 100644
--- a/src/libstd/sys/common/wtf8.rs
+++ b/src/libstd/sys/common/wtf8.rs
@@ -426,26 +426,36 @@ impl Ord for Wtf8 {
 /// and surrogates as `\u` followed by four hexadecimal digits.
 /// Example: `"a\u{D800}"` for a slice with code points [U+0061, U+D800]
 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("\""));
         let mut pos = 0;
         loop {
             match self.next_surrogate(pos) {
                 None => break,
                 Some((surrogate_pos, surrogate)) => {
-                    try!(formatter.write_str(unsafe {
-                        // the data in this slice is valid UTF-8, transmute to &str
-                        mem::transmute(&self.bytes[pos .. surrogate_pos])
-                    }));
+                    try!(write_str_escaped(
+                        formatter,
+                        unsafe { str::from_utf8_unchecked(
+                            &self.bytes[pos .. surrogate_pos]
+                        )},
+                    ));
                     try!(write!(formatter, "\\u{{{:X}}}", surrogate));
                     pos = surrogate_pos + 3;
                 }
             }
         }
-        try!(formatter.write_str(unsafe {
-            // the data in this slice is valid UTF-8, transmute to &str
-            mem::transmute(&self.bytes[pos..])
-        }));
+        try!(write_str_escaped(
+            formatter,
+            unsafe { str::from_utf8_unchecked(&self.bytes[pos..]) },
+        ));
         formatter.write_str("\"")
     }
 }
@@ -1083,9 +1093,9 @@ mod tests {
 
     #[test]
     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());
-        assert_eq!(format!("{:?}", string), r#""aé 💩\u{D800}""#);
+        assert_eq!(format!("{:?}", string), r#""a\t\u{e9} \u{1f4a9}\r\u{D800}""#);
     }
 
     #[test]
@@ -1094,10 +1104,10 @@ mod tests {
     }
 
     #[test]
-    fn wtf8_show() {
-        let mut string = Wtf8Buf::from_str("aé 💩");
-        string.push(CodePoint::from_u32(0xD800).unwrap());
-        assert_eq!(format!("{:?}", string), r#""aé 💩\u{D800}""#);
+    fn wtf8buf_show_str() {
+        let text = "a\té 💩\r";
+        let mut string = Wtf8Buf::from_str(text);
+        assert_eq!(format!("{:?}", text), format!("{:?}", string));
     }
 
     #[test]