diff --git a/src/impls.rs b/src/impls.rs index 1c9614d..de25392 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -525,6 +525,9 @@ mod bstr { for (s, e, ch) in self.char_indices() { match ch { '\0' => write!(f, "\\0")?, + '\x01'..='\x7f' => { + write!(f, "{}", (ch as u8).escape_ascii())?; + } '\u{FFFD}' => { let bytes = self[s..e].as_bytes(); if bytes == b"\xEF\xBF\xBD" { @@ -535,17 +538,6 @@ mod bstr { } } } - // ASCII control characters except \0, \n, \r, \t - '\x01'..='\x08' - | '\x0b' - | '\x0c' - | '\x0e'..='\x19' - | '\x7f' => { - write!(f, "\\x{:02x}", ch as u32)?; - } - '\n' | '\r' | '\t' => { - write!(f, "{}", ch.escape_debug())?; - } _ => { write!(f, "{}", ch.escape_debug())?; } @@ -1305,7 +1297,12 @@ fn test_debug() { // Before fixing #188, the output here would be: // \\xED\\xA0\\x80Aa\\x7f\\x0b B(&format!("{:?}", b"\xed\xa0\x80Aa\x7f\x0b".as_bstr())).as_bstr(), - ) + ); + + assert_eq!( + r#""\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x11\x12\r\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f \x7f\x80\x81\xfe\xff""#, + format!("{:?}", b"\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x11\x12\r\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f \x7f\x80\x81\xfe\xff".as_bstr()), + ); } // See: https://github.com/BurntSushi/bstr/issues/82