Skip to content

Commit

Permalink
Simplify escaping logic using escape_ascii
Browse files Browse the repository at this point in the history
Replace manual ranges of characters to escape with a call to
`escape_ascii` for anything in the ASCII range.
  • Loading branch information
joshtriplett committed Jan 2, 2025
1 parent 0d04e28 commit 93da841
Showing 1 changed file with 1 addition and 11 deletions.
12 changes: 1 addition & 11 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ mod bstr {
for (s, e, ch) in self.char_indices() {
match ch {
'\0' => write!(f, "\\0")?,
'\x01'..='\x7f' => write!(f, "{}", (ch as u8).escape_ascii())?,
'\u{FFFD}' => {
let bytes = self[s..e].as_bytes();
if bytes == b"\xEF\xBF\xBD" {
Expand All @@ -535,17 +536,6 @@ mod bstr {
}
}
}
// ASCII control characters except \0, \n, \r, \t
'\x01'..='\x08'
| '\x0b'
| '\x0c'
| '\x0e'..='\x1f'
| '\x7f' => {
write!(f, "\\x{:02x}", ch as u32)?;
}
'\n' | '\r' | '\t' => {
write!(f, "{}", ch.escape_debug())?;
}
_ => {
write!(f, "{}", ch.escape_debug())?;
}
Expand Down

0 comments on commit 93da841

Please sign in to comment.