Skip to content

Escape control #22928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,13 @@ pub trait StrExt: Index<RangeFull, Output = str> {
self.chars().flat_map(|c| c.escape_default()).collect()
}

/// Escapes each char in `s` with `char::escape_control`.
#[unstable(feature = "collections",
reason = "return type may change to be an iterator")]
fn escape_control(&self) -> String {
self.chars().flat_map(|c| c.escape_control()).collect()
}

/// Escapes each char in `s` with `char::escape_unicode`.
#[unstable(feature = "collections",
reason = "return type may change to be an iterator")]
Expand Down Expand Up @@ -2240,6 +2247,17 @@ mod tests {
String::from_str("\\u{1d4ea}\\r"));
}

#[test]
fn test_escape_control() {
assert_eq!("abc".escape_control(), String::from_str("abc"));
assert_eq!("öbµ".escape_control(), String::from_str("öbµ"));
assert_eq!("a c".escape_control(), String::from_str("a c"));
assert_eq!("\r\n\t".escape_control(), String::from_str("\\r\\n\\t"));
assert_eq!("'\"\\".escape_control(), String::from_str("\\'\\\"\\\\"));
assert_eq!("\u{100}".escape_control(),
String::from_str("\u{100}"));
}

#[test]
fn test_total_ord() {
"1234".cmp("123") == Greater;
Expand Down
54 changes: 54 additions & 0 deletions src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,41 @@ pub trait CharExt {
#[stable(feature = "rust1", since = "1.0.0")]
fn escape_default(self) -> EscapeDefault;

/// Escapes all C0 and C1 control characters (ISO 646 (ASCII), ISO 6429).
/// This method is called when printing a string with `{:?}`. The exact
/// rules for escaping are:
///
/// * Tab, CR and LF are escaped as '\t', '\r' and '\n' respectively.
/// * Single-quote, double-quote and backslash chars are backslash-
/// escaped.
/// * Any other C0 ([0x00, 0x1f]) and C1 ([0x80, 0x9f]) chars are escaped
/// with the Rust unicode syntax: `\\u{NNNN}`.
/// * Any other chars are not escaped
///
/// Note: 0x7f (delete) is often considered a control character, but is not
/// escaped!
///
/// # Examples
///
/// ```
/// for c in "ä\n☃".chars() {
/// for i in c.escape_control() {
/// print!("{}", i);
/// }
/// println!("");
/// }
/// ```
///
/// This prints:
///
/// ```text
/// ä
/// \n
/// ☃
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn escape_control(self) -> EscapeDefault;

/// Returns the number of bytes this character would need if encoded in UTF-8.
///
/// # Examples
Expand Down Expand Up @@ -394,6 +429,25 @@ impl CharExt for char {
EscapeDefault { state: init_state }
}

#[stable(feature = "rust1", since = "1.0.0")]
fn escape_control(self) -> EscapeDefault {
let init_state = match self {
// Backslash-escape special control character.
'\t' => EscapeDefaultState::Backslash('t'),
'\r' => EscapeDefaultState::Backslash('r'),
'\n' => EscapeDefaultState::Backslash('n'),
'\\' => EscapeDefaultState::Backslash('\\'),
'\'' => EscapeDefaultState::Backslash('\''),
'"' => EscapeDefaultState::Backslash('"'),
// Unicode-escape other C0 or C1 control character.
'\x00' ... '\x1f' | '\u{80}' ... '\u{9f}' =>
EscapeDefaultState::Unicode(self.escape_unicode()),
// Don't escape anything else.
_ => EscapeDefaultState::Char(self),
};
EscapeDefault { state: init_state }
}

#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn len_utf8(self) -> usize {
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ impl Display for bool {
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()) {
for c in self.chars().flat_map(|c| c.escape_control()) {
try!(write!(f, "{}", c));
}
write!(f, "\"")
Expand All @@ -679,7 +679,7 @@ impl Debug for char {
fn fmt(&self, f: &mut Formatter) -> Result {
use char::CharExt;
try!(write!(f, "'"));
for c in self.escape_default() {
for c in self.escape_control() {
try!(write!(f, "{}", c));
}
write!(f, "'")
Expand Down
31 changes: 31 additions & 0 deletions src/libcoretest/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,37 @@ fn test_escape_default() {
assert_eq!(s, "\\u{1d4b6}");
}

#[test]
fn test_escape_control() {
fn string(c: char) -> String {
c.escape_control().collect()
}
let s = string('\n');
assert_eq!(s, "\\n");
let s = string('\r');
assert_eq!(s, "\\r");
let s = string('\'');
assert_eq!(s, "\\'");
let s = string('"');
assert_eq!(s, "\\\"");
let s = string(' ');
assert_eq!(s, " ");
let s = string('a');
assert_eq!(s, "a");
let s = string('ä');
assert_eq!(s, "ä");
let s = string('~');
assert_eq!(s, "~");
let s = string('\x00');
assert_eq!(s, "\\u{0}");
let s = string('\x1f');
assert_eq!(s, "\\u{1f}");
let s = string('\u{ff}');
assert_eq!(s, "\u{ff}");
let s = string('\u{11b}');
assert_eq!(s, "\u{11b}");
}

#[test]
fn test_escape_unicode() {
fn string(c: char) -> String { c.escape_unicode().collect() }
Expand Down
37 changes: 37 additions & 0 deletions src/libunicode/u_char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,41 @@ pub trait CharExt {
#[stable(feature = "rust1", since = "1.0.0")]
fn escape_default(self) -> char::EscapeDefault;

/// Escapes all C0 and C1 control characters (ISO 646 (ASCII), ISO 6429).
/// This method is called when printing a string with `{:?}`. The exact
/// rules for escaping are:
///
/// * Tab, CR and LF are escaped as '\t', '\r' and '\n' respectively.
/// * Single-quote, double-quote and backslash chars are backslash-
/// escaped.
/// * Any other C0 ([0x00, 0x1f]) and C1 ([0x80, 0x9f]) chars are escaped
/// with the Rust unicode syntax: `\\u{NNNN}`.
/// * Any other chars are not escaped
///
/// Note: 0x7f (delete) is often considered a control character, but is not
/// escaped!
///
/// # Examples
///
/// ```
/// for c in "ä\n☃".chars() {
/// for i in c.escape_control() {
/// print!("{}", i);
/// }
/// println!("");
/// }
/// ```
///
/// This prints:
///
/// ```text
/// ä
/// \n
/// ☃
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn escape_control(self) -> char::EscapeDefault;

/// Returns the amount of bytes this character would need if encoded in
/// UTF-8.
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -232,6 +267,8 @@ impl CharExt for char {
#[stable(feature = "rust1", since = "1.0.0")]
fn escape_default(self) -> char::EscapeDefault { C::escape_default(self) }
#[stable(feature = "rust1", since = "1.0.0")]
fn escape_control(self) -> char::EscapeDefault { C::escape_control(self) }
#[stable(feature = "rust1", since = "1.0.0")]
fn len_utf8(self) -> usize { C::len_utf8(self) }
#[stable(feature = "rust1", since = "1.0.0")]
fn len_utf16(self) -> usize { C::len_utf16(self) }
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/ifmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn main() {
t!(format!("{}", '☃'), "☃");
t!(format!("{}", 10), "10");
t!(format!("{}", 10_usize), "10");
t!(format!("{:?}", '☃'), "'\\u{2603}'");
t!(format!("{:?}", '☃'), "''");
t!(format!("{:?}", 10), "10");
t!(format!("{:?}", 10_usize), "10");
t!(format!("{:?}", "true"), "\"true\"");
Expand Down