Debug print char 0 as '\0' rather than '\u{0}'

This commit is contained in:
David Tolnay 2022-03-26 13:35:58 -07:00
parent 100f12d170
commit 2ac9efbe95
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
5 changed files with 7 additions and 6 deletions

View File

@ -69,7 +69,7 @@ fn test_format_macro_interface() {
t!(format!("{:?}", "true"), "\"true\""); t!(format!("{:?}", "true"), "\"true\"");
t!(format!("{:?}", "foo\nbar"), "\"foo\\nbar\""); t!(format!("{:?}", "foo\nbar"), "\"foo\\nbar\"");
t!(format!("{:?}", "foo\n\"bar\"\r\n\'baz\'\t\\qux\\"), r#""foo\n\"bar\"\r\n'baz'\t\\qux\\""#); t!(format!("{:?}", "foo\n\"bar\"\r\n\'baz\'\t\\qux\\"), r#""foo\n\"bar\"\r\n'baz'\t\\qux\\""#);
t!(format!("{:?}", "foo\0bar\x01baz\u{7f}q\u{75}x"), r#""foo\u{0}bar\u{1}baz\u{7f}qux""#); t!(format!("{:?}", "foo\0bar\x01baz\u{7f}q\u{75}x"), r#""foo\0bar\u{1}baz\u{7f}qux""#);
t!(format!("{:o}", 10_usize), "12"); t!(format!("{:o}", 10_usize), "12");
t!(format!("{:x}", 10_usize), "a"); t!(format!("{:x}", 10_usize), "a");
t!(format!("{:X}", 10_usize), "A"); t!(format!("{:X}", 10_usize), "A");

View File

@ -1116,7 +1116,7 @@ fn test_escape_debug() {
assert_eq!("abc".escape_debug().to_string(), "abc"); assert_eq!("abc".escape_debug().to_string(), "abc");
assert_eq!("a c".escape_debug().to_string(), "a c"); assert_eq!("a c".escape_debug().to_string(), "a c");
assert_eq!("éèê".escape_debug().to_string(), "éèê"); assert_eq!("éèê".escape_debug().to_string(), "éèê");
assert_eq!("\r\n\t".escape_debug().to_string(), "\\r\\n\\t"); assert_eq!("\0\r\n\t".escape_debug().to_string(), "\\0\\r\\n\\t");
assert_eq!("'\"\\".escape_debug().to_string(), "\\'\\\"\\\\"); assert_eq!("'\"\\".escape_debug().to_string(), "\\'\\\"\\\\");
assert_eq!("\u{7f}\u{ff}".escape_debug().to_string(), "\\u{7f}\u{ff}"); assert_eq!("\u{7f}\u{ff}".escape_debug().to_string(), "\\u{7f}\u{ff}");
assert_eq!("\u{100}\u{ffff}".escape_debug().to_string(), "\u{100}\\u{ffff}"); assert_eq!("\u{100}\u{ffff}".escape_debug().to_string(), "\u{100}\\u{ffff}");

View File

@ -421,6 +421,7 @@ impl char {
#[inline] #[inline]
pub(crate) fn escape_debug_ext(self, args: EscapeDebugExtArgs) -> EscapeDebug { pub(crate) fn escape_debug_ext(self, args: EscapeDebugExtArgs) -> EscapeDebug {
let init_state = match self { let init_state = match self {
'\0' => EscapeDefaultState::Backslash('0'),
'\t' => EscapeDefaultState::Backslash('t'), '\t' => EscapeDefaultState::Backslash('t'),
'\r' => EscapeDefaultState::Backslash('r'), '\r' => EscapeDefaultState::Backslash('r'),
'\n' => EscapeDefaultState::Backslash('n'), '\n' => EscapeDefaultState::Backslash('n'),

View File

@ -197,7 +197,7 @@ fn test_escape_debug() {
assert_eq!(string('~'), "~"); assert_eq!(string('~'), "~");
assert_eq!(string('é'), "é"); assert_eq!(string('é'), "é");
assert_eq!(string('文'), ""); assert_eq!(string('文'), "");
assert_eq!(string('\x00'), "\\u{0}"); assert_eq!(string('\x00'), "\\0");
assert_eq!(string('\x1f'), "\\u{1f}"); assert_eq!(string('\x1f'), "\\u{1f}");
assert_eq!(string('\x7f'), "\\u{7f}"); assert_eq!(string('\x7f'), "\\u{7f}");
assert_eq!(string('\u{80}'), "\\u{80}"); assert_eq!(string('\u{80}'), "\\u{80}");

View File

@ -50,17 +50,17 @@ LL ~ match $s { $($t)+ => {}
LL ~ '\u{10fffe}'..='\u{10ffff}' => todo!() } LL ~ '\u{10fffe}'..='\u{10ffff}' => todo!() }
| |
error[E0004]: non-exhaustive patterns: `'\u{0}'` not covered error[E0004]: non-exhaustive patterns: `'\0'` not covered
--> $DIR/half-open-range-pats-exhaustive-fail.rs:28:8 --> $DIR/half-open-range-pats-exhaustive-fail.rs:28:8
| |
LL | m!('a', ALMOST_MIN..); LL | m!('a', ALMOST_MIN..);
| ^^^ pattern `'\u{0}'` not covered | ^^^ pattern `'\0'` not covered
| |
= note: the matched value is of type `char` = note: the matched value is of type `char`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
| |
LL ~ match $s { $($t)+ => {} LL ~ match $s { $($t)+ => {}
LL ~ '\u{0}' => todo!() } LL ~ '\0' => todo!() }
| |
error[E0004]: non-exhaustive patterns: `'\u{10ffff}'` not covered error[E0004]: non-exhaustive patterns: `'\u{10ffff}'` not covered