Skip to content

Commit

Permalink
Avoid counting characters and add explanatory comment to test
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed May 21, 2018
1 parent c51f002 commit 2fa22ef
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/liballoc/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,13 @@ impl str {
reason = "return type may change to be an iterator",
issue = "27791")]
pub fn escape_debug(&self) -> String {
self.chars().enumerate().flat_map(|(i, c)| c.escape_debug_ext(i == 0)).collect()
let mut string = String::with_capacity(self.len());
let mut chars = self.chars();
if let Some(first) = chars.next() {
string.extend(first.escape_debug_ext(true))
}
string.extend(chars.flat_map(|c| c.escape_debug_ext(false)));
string
}

/// Escapes each char in `s` with [`char::escape_default`].
Expand Down
6 changes: 6 additions & 0 deletions src/liballoc/tests/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,12 @@ fn test_escape_unicode() {

#[test]
fn test_escape_debug() {
// Note that there are subtleties with the number of backslashes
// on the left- and right-hand sides. In particular, Unicode code points
// are usually escaped with two backslashes on the right-hand side, as
// they are escaped. However, when the character is unescaped (e.g. for
// printable characters), only a single backslash appears (as the character
// itself appears in the debug string).
assert_eq!("abc".escape_debug(), "abc");
assert_eq!("a c".escape_debug(), "a c");
assert_eq!("éèê".escape_debug(), "éèê");
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/unicode/unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# - UnicodeData.txt
#
# Since this should not require frequent updates, we just store this
# out-of-line and check the unicode.py file into git.
# out-of-line and check the tables.rs file into git.

import fileinput, re, os, sys, operator, math, datetime

Expand Down

0 comments on commit 2fa22ef

Please sign in to comment.