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`).
This commit is contained in:
Eduardo Sánchez Muñoz 2021-05-12 16:56:19 +02:00 committed by Mike Welsh
parent 9538647422
commit e3dc8ff28e
1 changed files with 12 additions and 3 deletions

View File

@ -257,9 +257,18 @@ impl<'a, T: ?Sized + AsRef<str>> PartialEq<T> 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, '"')
}
}