Skip to content

Commit

Permalink
Fix formatting of control characters \x1a through \x1f
Browse files Browse the repository at this point in the history
Due to an incorrect range, control characters \x1a through \x1f get
formatted as Unicode \u escapes rather than hex \x escapes.

Fix the range, and add a test.

Without the fix, the test fails like this:

```
  left: "\"\\0\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x11\\x12\\r\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f \\x7f\\x80\\x81\\xfe\\xff\""
 right: "\"\\0\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x11\\x12\\r\\x14\\x15\\x16\\x17\\x18\\x19\\u{1a}\\u{1b}\\u{1c}\\u{1d}\\u{1e}\\u{1f} \\x7f\\x80\\x81\\xfe\\xff\""
```
  • Loading branch information
joshtriplett committed Jan 2, 2025
1 parent de55623 commit 0d04e28
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ mod bstr {
'\x01'..='\x08'
| '\x0b'
| '\x0c'
| '\x0e'..='\x19'
| '\x0e'..='\x1f'
| '\x7f' => {
write!(f, "\\x{:02x}", ch as u32)?;
}
Expand Down Expand Up @@ -1305,7 +1305,12 @@ fn test_debug() {
// Before fixing #188, the output here would be:
// \\xED\\xA0\\x80Aa\\x7f\\x0b
B(&format!("{:?}", b"\xed\xa0\x80Aa\x7f\x0b".as_bstr())).as_bstr(),
)
);

assert_eq!(
r#""\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x11\x12\r\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f \x7f\x80\x81\xfe\xff""#,
format!("{:?}", b"\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x11\x12\r\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f \x7f\x80\x81\xfe\xff".as_bstr()),
);
}

// See: https://github.com/BurntSushi/bstr/issues/82
Expand Down

0 comments on commit 0d04e28

Please sign in to comment.