Skip to content

Commit

Permalink
swf: SwfStr: reimplement Debug with std::ascii::escape_default
Browse files Browse the repository at this point in the history
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`).
  • Loading branch information
eduardosm authored and Herschel committed May 12, 2021
1 parent 9538647 commit e3dc8ff
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions swf/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, '"')
}
}

0 comments on commit e3dc8ff

Please sign in to comment.