1/// A type that wraps a single byte with a convenient fmt::Debug impl that
2/// escapes the byte.
3pub(crate) struct Byte(pub(crate) u8);
45impl core::fmt::Debug for Byte {
6fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
7// Special case ASCII space. It's too hard to read otherwise, so
8 // put quotes around it. I sometimes wonder whether just '\x20' would
9 // be better...
10if self.0 == b' ' {
11return write!(f, "' '");
12 }
13// 10 bytes is enough to cover any output from ascii::escape_default.
14let mut bytes = [0u8; 10];
15let mut len = 0;
16for (i, mut b) in core::ascii::escape_default(self.0).enumerate() {
17// capitalize \xab to \xAB
18if i >= 2 && b'a' <= b && b <= b'f' {
19 b -= 32;
20 }
21 bytes[len] = b;
22 len += 1;
23 }
24write!(f, "{}", core::str::from_utf8(&bytes[..len]).unwrap())
25 }
26}
2728/// A type that provides a human readable debug impl for arbitrary bytes.
29///
30/// This generally works best when the bytes are presumed to be mostly UTF-8,
31/// but will work for anything.
32///
33/// N.B. This is copied nearly verbatim from regex-automata. Sigh.
34pub(crate) struct Bytes<'a>(pub(crate) &'a [u8]);
3536impl<'a> core::fmt::Debug for Bytes<'a> {
37fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
38write!(f, "\"")?;
39// This is a sad re-implementation of a similar impl found in bstr.
40let mut bytes = self.0;
41while let Some(result) = utf8_decode(bytes) {
42let ch = match result {
43Ok(ch) => ch,
44Err(byte) => {
45write!(f, r"\x{:02x}", byte)?;
46 bytes = &bytes[1..];
47continue;
48 }
49 };
50 bytes = &bytes[ch.len_utf8()..];
51match ch {
52'\0' => write!(f, "\\0")?,
53// ASCII control characters except \0, \n, \r, \t
54'\x01'..='\x08'
55| '\x0b'
56| '\x0c'
57| '\x0e'..='\x19'
58| '\x7f' => {
59write!(f, "\\x{:02x}", u32::from(ch))?;
60 }
61'\n' | '\r' | '\t' | _ => {
62write!(f, "{}", ch.escape_debug())?;
63 }
64 }
65 }
66write!(f, "\"")?;
67Ok(())
68 }
69}
7071/// Decodes the next UTF-8 encoded codepoint from the given byte slice.
72///
73/// If no valid encoding of a codepoint exists at the beginning of the given
74/// byte slice, then the first byte is returned instead.
75///
76/// This returns `None` if and only if `bytes` is empty.
77pub(crate) fn utf8_decode(bytes: &[u8]) -> Option<Result<char, u8>> {
78fn len(byte: u8) -> Option<usize> {
79if byte <= 0x7F {
80return Some(1);
81 } else if byte & 0b1100_0000 == 0b1000_0000 {
82return None;
83 } else if byte <= 0b1101_1111 {
84Some(2)
85 } else if byte <= 0b1110_1111 {
86Some(3)
87 } else if byte <= 0b1111_0111 {
88Some(4)
89 } else {
90None
91}
92 }
9394if bytes.is_empty() {
95return None;
96 }
97let len = match len(bytes[0]) {
98None => return Some(Err(bytes[0])),
99Some(len) if len > bytes.len() => return Some(Err(bytes[0])),
100Some(1) => return Some(Ok(char::from(bytes[0]))),
101Some(len) => len,
102 };
103match core::str::from_utf8(&bytes[..len]) {
104Ok(s) => Some(Ok(s.chars().next().unwrap())),
105Err(_) => Some(Err(bytes[0])),
106 }
107}