From e3dc8ff28ecb1bd6fdcf59df9b3e828cdf785457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20S=C3=A1nchez=20Mu=C3=B1oz?= Date: Wed, 12 May 2021 16:56:19 +0200 Subject: [PATCH] swf: SwfStr: reimplement `Debug` with `std::ascii::escape_default` The string will now be surrounded with quotes (`"`), non-ASCII characters (UTF-8 or not) will be escaped in hexadecimal form (`\xNN`) and ASCII control characters will be escaped (`\x01`, `\n`, `\t`). --- swf/src/string.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/swf/src/string.rs b/swf/src/string.rs index c9b3b4489865..704f01f296bd 100644 --- a/swf/src/string.rs +++ b/swf/src/string.rs @@ -257,9 +257,18 @@ impl<'a, T: ?Sized + AsRef> PartialEq for SwfStr { impl fmt::Debug for SwfStr { /// Formats the `SwfStr` using the given formatter. /// - /// Note: this method assumes UTF-8 encoding; - /// other encodings like Shift-JIS will output gibberish. + /// Non-ASCII characters will be formatted in hexadecimal + /// form (`\xNN`). fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(&self.to_str_lossy(UTF_8)) + fmt::Write::write_char(f, '"')?; + for chr in self + .string + .iter() + .map(|&c| std::ascii::escape_default(c)) + .flatten() + { + fmt::Write::write_char(f, char::from(chr))?; + } + fmt::Write::write_char(f, '"') } }