about summary refs log tree commit diff
path: root/src/libcore/char.rs
diff options
context:
space:
mode:
authorTobias Bucher <tobiasbucher5991@gmail.com>2016-06-26 15:11:48 +0200
committerTobias Bucher <tobiasbucher5991@gmail.com>2016-07-23 00:18:44 +0200
commite7d16580f5856ecb0c515c7cdcabd2c10ba91547 (patch)
tree68359f4a107dc3d3c2fbc21d17e4ad006688f4c5 /src/libcore/char.rs
parentad264f7f39b6113fb87a489d72d31abbc03ab148 (diff)
downloadrust-e7d16580f5856ecb0c515c7cdcabd2c10ba91547.tar.gz
rust-e7d16580f5856ecb0c515c7cdcabd2c10ba91547.zip
Escape fewer Unicode codepoints in `Debug` impl of `str`
Use the same procedure as Python to determine whether a character is
printable, described in [PEP 3138]. In particular, this means that the
following character classes are escaped:

- Cc (Other, Control)
- Cf (Other, Format)
- Cs (Other, Surrogate), even though they can't appear in Rust strings
- Co (Other, Private Use)
- Cn (Other, Not Assigned)
- Zl (Separator, Line)
- Zp (Separator, Paragraph)
- Zs (Separator, Space), except for the ASCII space `' '` (`0x20`)

This allows for user-friendly inspection of strings that are not
English (e.g. compare `"\u{e9}\u{e8}\u{ea}"` to `"éèê"`).

Fixes #34318.

[PEP 3138]: https://www.python.org/dev/peps/pep-3138/
Diffstat (limited to 'src/libcore/char.rs')
-rw-r--r--src/libcore/char.rs5
1 files changed, 3 insertions, 2 deletions
diff --git a/src/libcore/char.rs b/src/libcore/char.rs
index 0e7f04c7758..0d39217bd72 100644
--- a/src/libcore/char.rs
+++ b/src/libcore/char.rs
@@ -17,6 +17,7 @@
 
 use prelude::v1::*;
 
+use char_private::is_printable;
 use mem::transmute;
 
 // UTF-8 ranges and tags for encoding characters
@@ -320,8 +321,8 @@ impl CharExt for char {
             '\r' => EscapeDefaultState::Backslash('r'),
             '\n' => EscapeDefaultState::Backslash('n'),
             '\\' | '\'' | '"' => EscapeDefaultState::Backslash(self),
-            '\x20' ... '\x7e' => EscapeDefaultState::Char(self),
-            _ => EscapeDefaultState::Unicode(self.escape_unicode())
+            c if is_printable(c) => EscapeDefaultState::Char(c),
+            c => EscapeDefaultState::Unicode(c.escape_unicode()),
         };
         EscapeDefault { state: init_state }
     }