Debian Codegraph @

debian/missing-sources/jiff-static/src/shared/util/escape.rs

raw
1 // auto-generated by: jiff-cli generate shared
2
3 /*!
4 Provides convenience routines for escaping raw bytes.
5
6 This was copied from `regex-automata` with a few light edits.
7 */
8
9 use super::utf8;
10
11 /// Provides a convenient `Debug` implementation for a `u8`.
12 ///
13 /// The `Debug` impl treats the byte as an ASCII, and emits a human
14 /// readable representation of it. If the byte isn't ASCII, then it's
15 /// emitted as a hex escape sequence.
16 #[derive(Clone, Copy)]
17 pub(crate) struct Byte(pub u8);
18
19 impl core::fmt::Display for Byte {
20 #[inline(never)]
21 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
22 if self.0 == b' ' {
23 return write!(f, " ");
24 }
25 // 10 bytes is enough for any output from ascii::escape_default.
26 let mut bytes = [0u8; 10];
27 let mut len = 0;
28 for (i, mut b) in core::ascii::escape_default(self.0).enumerate() {
29 // capitalize \xab to \xAB
30 if i >= 2 && b'a' <= b && b <= b'f' {
31 b -= 32;
32 }
33 bytes[len] = b;
34 len += 1;
35 }
36 write!(f, "{}", core::str::from_utf8(&bytes[..len]).unwrap())
37 }
38 }
39
40 impl core::fmt::Debug for Byte {
41 #[inline(never)]
42 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
43 write!(f, "\"")?;
44 core::fmt::Display::fmt(self, f)?;
45 write!(f, "\"")?;
46 Ok(())
47 }
48 }
49
50 /// Provides a convenient `Debug` implementation for `&[u8]`.
51 ///
52 /// This generally works best when the bytes are presumed to be mostly
53 /// UTF-8, but will work for anything. For any bytes that aren't UTF-8,
54 /// they are emitted as hex escape sequences.
55 #[derive(Clone, Copy)]
56 pub(crate) struct Bytes<'a>(pub &'a [u8]);
57
58 impl<'a> core::fmt::Display for Bytes<'a> {
59 #[inline(never)]
60 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
61 // This is a sad re-implementation of a similar impl found in bstr.
62 let mut bytes = self.0;
63 while let Some(result) = utf8::decode(bytes) {
64 let ch = match result {
65 Ok(ch) => ch,
66 Err(err) => {
67 // The decode API guarantees `errant_bytes` is non-empty.
68 write!(f, r"\x{:02x}", err.as_slice()[0])?;
69 bytes = &bytes[1..];
70 continue;
71 }
72 };
73 bytes = &bytes[ch.len_utf8()..];
74 match ch {
75 '\0' => write!(f, "\\0")?,
76 '\x01'..='\x7f' => {
77 write!(f, "{}", (ch as u8).escape_ascii())?;
78 }
79 _ => write!(f, "{}", ch.escape_debug())?,
80 }
81 }
82 Ok(())
83 }
84 }
85
86 impl<'a> core::fmt::Debug for Bytes<'a> {
87 #[inline(never)]
88 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
89 write!(f, "\"")?;
90 core::fmt::Display::fmt(self, f)?;
91 write!(f, "\"")?;
92 Ok(())
93 }
94 }
95
96 /// A helper for repeating a single byte utilizing `Byte`.
97 ///
98 /// This is limited to repeating a byte up to `u8::MAX` times in order
99 /// to reduce its size overhead. And in practice, Jiff just doesn't
100 /// need more than this (at time of writing, 2025-11-29).
101 pub(crate) struct RepeatByte {
102 pub(crate) byte: u8,
103 pub(crate) count: u8,
104 }
105
106 impl core::fmt::Display for RepeatByte {
107 #[inline(never)]
108 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
109 for _ in 0..self.count {
110 write!(f, "{}", Byte(self.byte))?;
111 }
112 Ok(())
113 }
114 }
115
116 impl core::fmt::Debug for RepeatByte {
117 #[inline(never)]
118 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
119 write!(f, "\"")?;
120 core::fmt::Display::fmt(self, f)?;
121 write!(f, "\"")?;
122 Ok(())
123 }
124 }
125