From 4dddf3c18d4590f7c92214fa592c3faca3d2c812 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Thu, 26 Sep 2024 22:46:02 +0100 Subject: [PATCH] Use `fmt::Debug` implementation for Lua string from `bstr` --- src/string.rs | 19 ++----------------- tests/string.rs | 2 +- 2 files changed, 3 insertions(+), 18 deletions(-) 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(()) }