about summary refs log tree commit diff
path: root/src/libcore/fmt
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-10-02 22:49:37 +0000
committerbors <bors@rust-lang.org>2015-10-02 22:49:37 +0000
commitbfb26033af68949646081429040db606a044db22 (patch)
tree312d5abd0a61c9971eef77f3ce82cc369bd33b98 /src/libcore/fmt
parent669fc7f7d12b85c8da6e0b0cfe2de0c31dc3d47f (diff)
parent0294098d8f3e2926cf0e6adba9a96e47099c3c0f (diff)
downloadrust-bfb26033af68949646081429040db606a044db22.tar.gz
rust-bfb26033af68949646081429040db606a044db22.zip
Auto merge of #28662 - semmaz:fmt-debug, r=alexcrichton
fixes #26920
Diffstat (limited to 'src/libcore/fmt')
-rw-r--r--src/libcore/fmt/mod.rs23
1 files changed, 16 insertions, 7 deletions
diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs
index 69fa0fa1609..f6053a75f1f 100644
--- a/src/libcore/fmt/mod.rs
+++ b/src/libcore/fmt/mod.rs
@@ -1332,11 +1332,21 @@ impl Display for bool {
 #[stable(feature = "rust1", since = "1.0.0")]
 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!(f.write_char(c))
+        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 esc.size_hint() != (1, Some(1)) {
+                try!(f.write_str(&self[from..i]));
+                for c in esc {
+                    try!(f.write_char(c));
+                }
+                from = i + c.len_utf8();
+            }
         }
-        write!(f, "\"")
+        try!(f.write_str(&self[from..]));
+        f.write_char('"')
     }
 }
 
@@ -1350,12 +1360,11 @@ impl Display for str {
 #[stable(feature = "rust1", since = "1.0.0")]
 impl Debug for char {
     fn fmt(&self, f: &mut Formatter) -> Result {
-        use char::CharExt;
-        try!(write!(f, "'"));
+        try!(f.write_char('\''));
         for c in self.escape_default() {
             try!(f.write_char(c))
         }
-        write!(f, "'")
+        f.write_char('\'')
     }
 }