diff options
| author | Tobias Bucher <tobiasbucher5991@gmail.com> | 2016-07-26 01:39:54 +0200 |
|---|---|---|
| committer | Tobias Bucher <tobiasbucher5991@gmail.com> | 2016-07-26 15:15:00 +0200 |
| commit | 68efea08fa1cf800b3b76683992ec77a89323d53 (patch) | |
| tree | f9c7cee686d99efacdf80cbbaf477bd6ff19f12f /src/libcore | |
| parent | 0685900fbd1ea1f6be5c3454dcde753ac3484c01 (diff) | |
| download | rust-68efea08fa1cf800b3b76683992ec77a89323d53.tar.gz rust-68efea08fa1cf800b3b76683992ec77a89323d53.zip | |
Restore `char::escape_default` and add `char::escape` instead
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/char.rs | 38 | ||||
| -rw-r--r-- | src/libcore/fmt/mod.rs | 4 |
2 files changed, 39 insertions, 3 deletions
diff --git a/src/libcore/char.rs b/src/libcore/char.rs index 0d39217bd72..3e435b47110 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -264,6 +264,8 @@ pub trait CharExt { fn escape_unicode(self) -> EscapeUnicode; #[stable(feature = "core", since = "1.6.0")] fn escape_default(self) -> EscapeDefault; + #[unstable(feature = "char_escape", issue = "0")] + fn escape(self) -> Escape; #[stable(feature = "core", since = "1.6.0")] fn len_utf8(self) -> usize; #[stable(feature = "core", since = "1.6.0")] @@ -321,10 +323,23 @@ 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()) + }; + EscapeDefault { state: init_state } + } + + #[inline] + fn escape(self) -> Escape { + let init_state = match self { + '\t' => EscapeDefaultState::Backslash('t'), + '\r' => EscapeDefaultState::Backslash('r'), + '\n' => EscapeDefaultState::Backslash('n'), + '\\' | '\'' | '"' => EscapeDefaultState::Backslash(self), c if is_printable(c) => EscapeDefaultState::Char(c), c => EscapeDefaultState::Unicode(c.escape_unicode()), }; - EscapeDefault { state: init_state } + Escape(EscapeDefault { state: init_state }) } #[inline] @@ -601,6 +616,27 @@ impl ExactSizeIterator for EscapeDefault { } } +/// An iterator that yields the literal escape code of a `char`. +/// +/// This `struct` is created by the [`escape()`] method on [`char`]. See its +/// documentation for more. +/// +/// [`escape()`]: ../../std/primitive.char.html#method.escape +/// [`char`]: ../../std/primitive.char.html +#[unstable(feature = "char_escape", issue = "0")] +#[derive(Clone, Debug)] +pub struct Escape(EscapeDefault); + +#[unstable(feature = "char_escape", issue = "0")] +impl Iterator for Escape { + type Item = char; + fn next(&mut self) -> Option<char> { self.0.next() } + fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() } +} + +#[unstable(feature = "char_escape", issue = "0")] +impl ExactSizeIterator for Escape { } + /// An iterator over `u8` entries represending the UTF-8 encoding of a `char` /// value. /// diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index e5eb8f21382..3bcdce57af0 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -1383,7 +1383,7 @@ impl Debug for str { f.write_char('"')?; let mut from = 0; for (i, c) in self.char_indices() { - let esc = c.escape_default(); + let esc = c.escape(); // If char needs escaping, flush backlog so far and write, else skip if esc.len() != 1 { f.write_str(&self[from..i])?; @@ -1409,7 +1409,7 @@ impl Display for str { impl Debug for char { fn fmt(&self, f: &mut Formatter) -> Result { f.write_char('\'')?; - for c in self.escape_default() { + for c in self.escape() { f.write_char(c)? } f.write_char('\'') |
