Skip to content

Commit

Permalink
Better (shorter, clearer) debug implementation
Browse files Browse the repository at this point in the history
Fixes #56
  • Loading branch information
tailhook committed Jun 11, 2020
1 parent 2122e1e commit cd1db3b
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,22 @@ mod bstr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "\"")?;
for (s, e, ch) in self.char_indices() {
if ch == '\u{FFFD}' {
for &b in self[s..e].as_bytes() {
write!(f, r"\x{:X}", b)?;
match ch {
'\0' => write!(f, "\\0")?,
'\u{FFFD}' => {
for &b in self[s..e].as_bytes() {
write!(f, r"\x{:02X}", b)?;
}
}
} else {
write!(f, "{}", ch.escape_debug())?;
}
// ASCII control characters except \0 \n, \r, \t
'\x01'..='\x08' | '\x0b' | '\x0c' | '\x0e'..='\x19' |
'\x7f' => {
write!(f, "\\x{:02x}", ch as u32)?;
}
'\n' | '\r' | '\t' | _ => {
write!(f, "{}", ch.escape_debug())?;
}
}
}
write!(f, "\"")?;
Ok(())
Expand Down Expand Up @@ -759,3 +768,12 @@ mod bstring_arbitrary {
}
}
}

#[test]
fn test_debug() {
use crate::ByteSlice;
assert_eq!(
r#""\0\0\0 ftypisom\0\0\x02\0isomiso2avc1mp""#,
format!("{:?}", b"\0\0\0 ftypisom\0\0\x02\0isomiso2avc1mp".as_bstr()));
}

0 comments on commit cd1db3b

Please sign in to comment.