1/*!
2Provides convenience routines for escaping raw bytes.
34This was copied from `regex-automata` with a few light edits.
5*/
67use super::utf8;
89/// Provides a convenient `Debug` implementation for a `u8`.
10///
11/// The `Debug` impl treats the byte as an ASCII, and emits a human
12/// readable representation of it. If the byte isn't ASCII, then it's
13/// emitted as a hex escape sequence.
14#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
for Byte {
}
#[automatically_derived]impl ::core::clone::Clone for Byte {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<u8>;
*self
}
}Clone, #[automatically_derived]impl ::core::marker::Copy for Byte { }Copy)]
15pub(crate) struct Byte(pub u8);
1617impl core::fmt::Displayfor Byte {
18#[inline(never)]
19fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
20if self.0 == b' ' {
21return f.write_str(" ");
22 }
23// 10 bytes is enough for any output from ascii::escape_default.
24let mut bytes = [0u8; 10];
25let mut len = 0;
26for (i, mut b) in core::ascii::escape_default(self.0).enumerate() {
27// capitalize \xab to \xAB
28if i >= 2 && b'a' <= b && b <= b'f' {
29 b -= 32;
30 }
31 bytes[len] = b;
32 len += 1;
33 }
34f.write_str(core::str::from_utf8(&bytes[..len]).unwrap())
35 }
36}
3738impl core::fmt::Debugfor Byte {
39#[inline(never)]
40fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
41 f.write_str("\"")?;
42 core::fmt::Display::fmt(self, f)?;
43 f.write_str("\"")?;
44Ok(())
45 }
46}
4748/// Provides a convenient `Debug` implementation for `&[u8]`.
49///
50/// This generally works best when the bytes are presumed to be mostly
51/// UTF-8, but will work for anything. For any bytes that aren't UTF-8,
52/// they are emitted as hex escape sequences.
53#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl<'a>
::core::clone::TrivialClone for Bytes<'a> {
}
#[automatically_derived]impl<'a> ::core::clone::Clone for Bytes<'a> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<&'a [u8]>;
*self
}
}Clone, #[automatically_derived]impl<'a> ::core::marker::Copy for Bytes<'a> { }Copy)]
54pub(crate) struct Bytes<'a>(pub &'a [u8]);
5556impl<'a> core::fmt::Displayfor Bytes<'a> {
57#[inline(never)]
58fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
59// This is a sad re-implementation of a similar impl found in bstr.
60let mut bytes = self.0;
61while let Some(result) = utf8::decode(bytes) {
62let ch = match result {
63Ok(ch) => ch,
64Err(err) => {
65// The decode API guarantees `errant_bytes` is non-empty.
66f.write_fmt(format_args!("\\x{0:02x}", err.as_slice()[0]))write!(f, r"\x{:02x}", err.as_slice()[0])?;
67 bytes = &bytes[1..];
68continue;
69 }
70 };
71 bytes = &bytes[ch.len_utf8()..];
72match ch {
73'\0' => f.write_str(r"\0")?,
74'\x01'..='\x7f' => {
75 core::fmt::Display::fmt(&(ch as u8).escape_ascii(), f)?;
76 }
77_ => {
78 core::fmt::Display::fmt(&ch.escape_debug(), f)?;
79 }
80 }
81 }
82Ok(())
83 }
84}
8586impl<'a> core::fmt::Debugfor Bytes<'a> {
87#[inline(never)]
88fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
89 f.write_str("\"")?;
90 core::fmt::Display::fmt(self, f)?;
91 f.write_str("\"")?;
92Ok(())
93 }
94}
9596/// 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).
101pub(crate) struct RepeatByte {
102pub(crate) byte: u8,
103pub(crate) count: u8,
104}
105106impl core::fmt::Displayfor RepeatByte {
107#[inline(never)]
108fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
109for _ in 0..self.count {
110 core::fmt::Display::fmt(&Byte(self.byte), f)?;
111 }
112Ok(())
113 }
114}
115116impl core::fmt::Debugfor RepeatByte {
117#[inline(never)]
118fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
119 f.write_str("\"")?;
120 core::fmt::Display::fmt(self, f)?;
121 f.write_str("\"")?;
122Ok(())
123 }
124}