Skip to content

Commit 2fa22ef

Browse files
committed
Avoid counting characters and add explanatory comment to test
1 parent c51f002 commit 2fa22ef

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

src/liballoc/str.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,13 @@ impl str {
380380
reason = "return type may change to be an iterator",
381381
issue = "27791")]
382382
pub fn escape_debug(&self) -> String {
383-
self.chars().enumerate().flat_map(|(i, c)| c.escape_debug_ext(i == 0)).collect()
383+
let mut string = String::with_capacity(self.len());
384+
let mut chars = self.chars();
385+
if let Some(first) = chars.next() {
386+
string.extend(first.escape_debug_ext(true))
387+
}
388+
string.extend(chars.flat_map(|c| c.escape_debug_ext(false)));
389+
string
384390
}
385391

386392
/// Escapes each char in `s` with [`char::escape_default`].

src/liballoc/tests/str.rs

+6
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,12 @@ fn test_escape_unicode() {
989989

990990
#[test]
991991
fn test_escape_debug() {
992+
// Note that there are subtleties with the number of backslashes
993+
// on the left- and right-hand sides. In particular, Unicode code points
994+
// are usually escaped with two backslashes on the right-hand side, as
995+
// they are escaped. However, when the character is unescaped (e.g. for
996+
// printable characters), only a single backslash appears (as the character
997+
// itself appears in the debug string).
992998
assert_eq!("abc".escape_debug(), "abc");
993999
assert_eq!("a c".escape_debug(), "a c");
9941000
assert_eq!("éèê".escape_debug(), "éèê");

src/libcore/unicode/unicode.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# - UnicodeData.txt
2222
#
2323
# Since this should not require frequent updates, we just store this
24-
# out-of-line and check the unicode.py file into git.
24+
# out-of-line and check the tables.rs file into git.
2525

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

0 commit comments

Comments
 (0)