diff --git a/src/string.rs b/src/string.rs index 0cc48a7..734cd81 100644 --- a/src/string.rs +++ b/src/string.rs @@ -143,23 +143,8 @@ impl fmt::Debug for String { } // Format as bytes - write!(f, "b\"")?; - for &b in bytes { - // https://doc.rust-lang.org/reference/tokens.html#byte-escapes - match b { - b'\n' => write!(f, "\\n")?, - b'\r' => write!(f, "\\r")?, - b'\t' => write!(f, "\\t")?, - b'\\' | b'"' => write!(f, "\\{}", b as char)?, - b'\0' => write!(f, "\\0")?, - // ASCII printable - 0x20..=0x7e => write!(f, "{}", b as char)?, - _ => write!(f, "\\x{b:02x}")?, - } - } - write!(f, "\"")?; - - Ok(()) + write!(f, "b")?; + ::fmt(bstr::BStr::new(&bytes), f) } } diff --git a/tests/string.rs b/tests/string.rs index a986c29..456ad84 100644 --- a/tests/string.rs +++ b/tests/string.rs @@ -86,7 +86,7 @@ fn test_string_debug() -> Result<()> { // Invalid utf8 let s = lua.create_string(b"hello\0world\r\n\t\xF0\x90\x80")?; - assert_eq!(format!("{s:?}"), r#"b"hello\0world\r\n\t\xf0\x90\x80""#); + assert_eq!(format!("{s:?}"), r#"b"hello\0world\r\n\t\xF0\x90\x80""#); Ok(()) }