diff options
| author | bors <bors@rust-lang.org> | 2016-07-28 11:20:33 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-07-28 11:20:33 -0700 |
| commit | d1df3fecdf8dc959fbd8901603a16e5bc0bfa21d (patch) | |
| tree | 52351065a422a440701808df99b4f476058eb2fb /src/libcollections | |
| parent | 748ecb1235d1cfa1aeaa0e5424c454837089f6cc (diff) | |
| parent | 3d09b4a0d58200da84fe19cd3b0003d61e5b1791 (diff) | |
| download | rust-d1df3fecdf8dc959fbd8901603a16e5bc0bfa21d.tar.gz rust-d1df3fecdf8dc959fbd8901603a16e5bc0bfa21d.zip | |
Auto merge of #34485 - tbu-:pr_unicode_debug_str, r=alexcrichton
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.
CC #34422.
[PEP 3138]: https://www.python.org/dev/peps/pep-3138/
Diffstat (limited to 'src/libcollections')
| -rw-r--r-- | src/libcollections/lib.rs | 1 | ||||
| -rw-r--r-- | src/libcollections/str.rs | 8 |
2 files changed, 9 insertions, 0 deletions
diff --git a/src/libcollections/lib.rs b/src/libcollections/lib.rs index f027d074cb6..7fc6e54d69f 100644 --- a/src/libcollections/lib.rs +++ b/src/libcollections/lib.rs @@ -33,6 +33,7 @@ #![feature(allow_internal_unstable)] #![feature(box_patterns)] #![feature(box_syntax)] +#![cfg_attr(not(test), feature(char_escape_debug))] #![feature(core_intrinsics)] #![feature(dropck_parametricity)] #![feature(fmt_internals)] diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index 55308a46f0a..4c64019de09 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -1697,6 +1697,14 @@ impl str { return s; } + /// Escapes each char in `s` with `char::escape_debug`. + #[unstable(feature = "str_escape", + reason = "return type may change to be an iterator", + issue = "27791")] + pub fn escape_debug(&self) -> String { + self.chars().flat_map(|c| c.escape_debug()).collect() + } + /// Escapes each char in `s` with `char::escape_default`. #[unstable(feature = "str_escape", reason = "return type may change to be an iterator", |
