Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/jiff-0.2.20/src/util/escape.rs
Line
Count
Source
1
/*!
2
Provides convenience routines for escaping raw bytes.
3
4
This was copied from `regex-automata` with a few light edits.
5
*/
6
7
use super::utf8;
8
9
/// 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(Clone, Copy)]
15
pub(crate) struct Byte(pub u8);
16
17
impl core::fmt::Display for Byte {
18
    #[inline(never)]
19
0
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
20
0
        if self.0 == b' ' {
21
0
            return f.write_str(" ");
22
0
        }
23
        // 10 bytes is enough for any output from ascii::escape_default.
24
0
        let mut bytes = [0u8; 10];
25
0
        let mut len = 0;
26
0
        for (i, mut b) in core::ascii::escape_default(self.0).enumerate() {
27
            // capitalize \xab to \xAB
28
0
            if i >= 2 && b'a' <= b && b <= b'f' {
29
0
                b -= 32;
30
0
            }
31
0
            bytes[len] = b;
32
0
            len += 1;
33
        }
34
0
        f.write_str(core::str::from_utf8(&bytes[..len]).unwrap())
35
0
    }
36
}
37
38
impl core::fmt::Debug for Byte {
39
    #[inline(never)]
40
0
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
41
0
        f.write_str("\"")?;
42
0
        core::fmt::Display::fmt(self, f)?;
43
0
        f.write_str("\"")?;
44
0
        Ok(())
45
0
    }
46
}
47
48
/// 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(Clone, Copy)]
54
pub(crate) struct Bytes<'a>(pub &'a [u8]);
55
56
impl<'a> core::fmt::Display for Bytes<'a> {
57
    #[inline(never)]
58
0
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
59
        // This is a sad re-implementation of a similar impl found in bstr.
60
0
        let mut bytes = self.0;
61
0
        while let Some(result) = utf8::decode(bytes) {
62
0
            let ch = match result {
63
0
                Ok(ch) => ch,
64
0
                Err(err) => {
65
                    // The decode API guarantees `errant_bytes` is non-empty.
66
0
                    write!(f, r"\x{:02x}", err.as_slice()[0])?;
67
0
                    bytes = &bytes[1..];
68
0
                    continue;
69
                }
70
            };
71
0
            bytes = &bytes[ch.len_utf8()..];
72
0
            match ch {
73
0
                '\0' => f.write_str(r"\0")?,
74
0
                '\x01'..='\x7f' => {
75
0
                    core::fmt::Display::fmt(&(ch as u8).escape_ascii(), f)?;
76
                }
77
                _ => {
78
0
                    core::fmt::Display::fmt(&ch.escape_debug(), f)?;
79
                }
80
            }
81
        }
82
0
        Ok(())
83
0
    }
84
}
85
86
impl<'a> core::fmt::Debug for Bytes<'a> {
87
    #[inline(never)]
88
0
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
89
0
        f.write_str("\"")?;
90
0
        core::fmt::Display::fmt(self, f)?;
91
0
        f.write_str("\"")?;
92
0
        Ok(())
93
0
    }
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
0
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
109
0
        for _ in 0..self.count {
110
0
            core::fmt::Display::fmt(&Byte(self.byte), f)?;
111
        }
112
0
        Ok(())
113
0
    }
114
}
115
116
impl core::fmt::Debug for RepeatByte {
117
    #[inline(never)]
118
0
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
119
0
        f.write_str("\"")?;
120
0
        core::fmt::Display::fmt(self, f)?;
121
0
        f.write_str("\"")?;
122
0
        Ok(())
123
0
    }
124
}