Skip to content

Commit fb39348

Browse files
committed
Escape combining characters in escape_debug
Although combining characters are technically printable, they make little sense to print on their own with `Debug`: it'd be better to escape them like non-printable characters.
1 parent 75af15e commit fb39348

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/libcore/char.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,17 @@ pub fn from_digit(num: u32, radix: u32) -> Option<char> {
363363
}
364364
}
365365

366+
fn is_combining_char(c: char) -> bool {
367+
match c as u32 {
368+
0x0300..=0x036f |
369+
0x1ab0..=0x1aff |
370+
0x1dc0..=0x1dff |
371+
0x20d0..=0x20ff |
372+
0xfe20..=0xfe2f => true,
373+
_ => false
374+
}
375+
}
376+
366377
// NB: the stabilization and documentation for this trait is in
367378
// unicode/char.rs, not here
368379
#[allow(missing_docs)] // docs in libunicode/u_char.rs
@@ -451,8 +462,8 @@ impl CharExt for char {
451462
'\r' => EscapeDefaultState::Backslash('r'),
452463
'\n' => EscapeDefaultState::Backslash('n'),
453464
'\\' | '\'' | '"' => EscapeDefaultState::Backslash(self),
454-
c if is_printable(c) => EscapeDefaultState::Char(c),
455-
c => EscapeDefaultState::Unicode(c.escape_unicode()),
465+
_ if is_printable(self) && !is_combining_char(self) => EscapeDefaultState::Char(self),
466+
_ => EscapeDefaultState::Unicode(self.escape_unicode()),
456467
};
457468
EscapeDebug(EscapeDefault { state: init_state })
458469
}

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#![feature(custom_attribute)]
7777
#![feature(doc_cfg)]
7878
#![feature(doc_spotlight)]
79+
#![cfg_attr(stage0, feature(dotdoteq_in_patterns))]
7980
#![feature(fn_must_use)]
8081
#![feature(fundamental)]
8182
#![feature(i128_type)]

src/libcore/tests/char.rs

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ fn test_escape_debug() {
181181
assert_eq!(string('\u{ff}'), "\u{ff}");
182182
assert_eq!(string('\u{11b}'), "\u{11b}");
183183
assert_eq!(string('\u{1d4b6}'), "\u{1d4b6}");
184+
assert_eq!(string('\u{301}'), "\\u{301}"); // combining character
184185
assert_eq!(string('\u{200b}'),"\\u{200b}"); // zero width space
185186
assert_eq!(string('\u{e000}'), "\\u{e000}"); // private use 1
186187
assert_eq!(string('\u{100000}'), "\\u{100000}"); // private use 2

0 commit comments

Comments
 (0)