Debian Codegraph @

src/shared/util/escape.rs

raw symbols SCIP
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 superrust-analyzer cargo jiff-static 0.2.16 shared/util/::utf8rust-analyzer cargo jiff-static 0.2.16 shared/util/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 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)]
17 pub(craterust-analyzer cargo jiff-static 0.2.16 crate/) struct Byterust-analyzer cargo jiff-static 0.2.16 shared/util/escape/Byte#(pub u8);
18
19 impl corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Byterust-analyzer cargo jiff-static 0.2.16 shared/util/escape/Byte# {
20 fn fmtrust-analyzer cargo jiff-static 0.2.16 shared/util/escape/impl#[Byte][Display]fmt().(&self, f: &mut corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
21 if self.0rust-analyzer cargo jiff-static 0.2.16 shared/util/escape/Byte#0. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b' ' {
22 return writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, " ");
23 }
24 // 10 bytes is enough for any output from ascii::escape_default.
25 let mut bytes = [0u8; 10];
26 let mut len = 0;
27 for (i, mut b) in corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::asciirust-analyzer cargo core https://github.com/rust-lang/rust/library/core ascii/::escape_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ascii/escape_default().(self.0rust-analyzer cargo jiff-static 0.2.16 shared/util/escape/Byte#0.).enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() {
28 // capitalize \xab to \xAB
29 if i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().2 && b'a' rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]le().b && b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]le().b'f' {
30 b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`SubAssign<Self>`]sub_assign().32;
31 }
32 bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut(). = b;
33 len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
34 }
35 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{}", corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(&bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().())
36 }
37 }
38
39 impl corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for Byterust-analyzer cargo jiff-static 0.2.16 shared/util/escape/Byte# {
40 fn fmtrust-analyzer cargo jiff-static 0.2.16 shared/util/escape/impl#[Byte][Debug]fmt().(&self, f: &mut corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
41 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "\"")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
42 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#::fmtrust-analyzer cargo jiff-static 0.2.16 shared/util/escape/impl#[Byte][Display]fmt().(self, f)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
43 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "\"")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
44 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
45 }
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 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)]
54 pub(craterust-analyzer cargo jiff-static 0.2.16 crate/) struct Bytesrust-analyzer cargo jiff-static 0.2.16 shared/util/escape/Bytes#<'a>(pub &'a [u8]);
55
56 impl<'a> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Bytesrust-analyzer cargo jiff-static 0.2.16 shared/util/escape/Bytes#<'a> {
57 fn fmtrust-analyzer cargo jiff-static 0.2.16 shared/util/escape/impl#[`Bytes<'a>`][Display]fmt().(&self, f: &mut corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
58 // This is a sad re-implementation of a similar impl found in bstr.
59 let mut bytes = self.0rust-analyzer cargo jiff-static 0.2.16 shared/util/escape/Bytes#0.;
60 while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(result) = utf8rust-analyzer cargo jiff-static 0.2.16 shared/util/utf8/::decoderust-analyzer cargo jiff-static 0.2.16 shared/util/utf8/decode().(bytes) {
61 let ch = match result {
62 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ch) => ch,
63 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(errant_bytes) => {
64 // The decode API guarantees `errant_bytes` is non-empty.
65 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, r"\x{:02x}", errant_bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
66 bytes = &bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
67 continue;
68 }
69 };
70 bytes = &bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().ch.len_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]len_utf8().()..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
71 match ch {
72 '\0' => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "\\0")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
73 '\x01'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'\x7f' => {
74 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{}", (ch as u8).escape_asciirust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]escape_ascii().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
75 }
76 _ => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{}", ch.escape_debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]escape_debug().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
77 }
78 }
79 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
80 }
81 }
82
83 impl<'a> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for Bytesrust-analyzer cargo jiff-static 0.2.16 shared/util/escape/Bytes#<'a> {
84 fn fmtrust-analyzer cargo jiff-static 0.2.16 shared/util/escape/impl#[`Bytes<'a>`][Debug]fmt().(&self, f: &mut corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
85 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "\"")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
86 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#::fmtrust-analyzer cargo jiff-static 0.2.16 shared/util/escape/impl#[`Bytes<'a>`][Display]fmt().(self, f)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
87 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "\"")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
88 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
89 }
90 }