1//! Helper code for character escaping.
2
3use crate::ascii;
4use crate::num::NonZero;
5use crate::ops::Range;
6
7const HEX_DIGITS: [ascii::Char; 16] = *b"0123456789abcdef".as_ascii().unwrap();
8
9/// Escapes a byte into provided buffer; returns length of escaped
10/// representation.
11pub(crate) fn escape_ascii_into(output: &mut [ascii::Char; 4], byte: u8) -> Range<u8> {
12 #[inline]
13 fn backslash(a: ascii::Char) -> ([ascii::Char; 4], u8) {
14 ([ascii::Char::ReverseSolidus, a, ascii::Char::Null, ascii::Char::Null], 2)
15 }
16
17 let (data, len) = match byte {
18 b'\t' => backslash(ascii::Char::SmallT),
19 b'\r' => backslash(ascii::Char::SmallR),
20 b'\n' => backslash(ascii::Char::SmallN),
21 b'\\' => backslash(ascii::Char::ReverseSolidus),
22 b'\'' => backslash(ascii::Char::Apostrophe),
23 b'\"' => backslash(ascii::Char::QuotationMark),
24 _ => {
25 if let Some(a) = byte.as_ascii()
26 && !byte.is_ascii_control()
27 {
28 ([a, ascii::Char::Null, ascii::Char::Null, ascii::Char::Null], 1)
29 } else {
30 let hi = HEX_DIGITS[usize::from(byte >> 4)];
31 let lo = HEX_DIGITS[usize::from(byte & 0xf)];
32 ([ascii::Char::ReverseSolidus, ascii::Char::SmallX, hi, lo], 4)
33 }
34 }
35 };
36 *output = data;
37 0..len
38}
39
40/// Escapes a character into provided buffer using `\u{NNNN}` representation.
41pub(crate) fn escape_unicode_into(output: &mut [ascii::Char; 10], ch: char) -> Range<u8> {
42 output[9] = ascii::Char::RightCurlyBracket;
43
44 let ch: u32 = ch as u32;
45 output[3] = HEX_DIGITS[((ch >> 20) & 15) as usize];
46 output[4] = HEX_DIGITS[((ch >> 16) & 15) as usize];
47 output[5] = HEX_DIGITS[((ch >> 12) & 15) as usize];
48 output[6] = HEX_DIGITS[((ch >> 8) & 15) as usize];
49 output[7] = HEX_DIGITS[((ch >> 4) & 15) as usize];
50 output[8] = HEX_DIGITS[((ch >> 0) & 15) as usize];
51
52 // or-ing 1 ensures that for ch==0 the code computes that one digit should
53 // be printed.
54 let start: usize = (ch | 1).leading_zeros() as usize / 4 - 2;
55 const UNICODE_ESCAPE_PREFIX: &[ascii::Char; 3] = b"\\u{".as_ascii().unwrap();
56 output[start..][..3].copy_from_slice(UNICODE_ESCAPE_PREFIX);
57
58 (start as u8)..10
59}
60
61/// An iterator over an fixed-size array.
62///
63/// This is essentially equivalent to array’s IntoIter except that indexes are
64/// limited to u8 to reduce size of the structure.
65#[derive(Clone, Debug)]
66pub(crate) struct EscapeIterInner<const N: usize> {
67 // The element type ensures this is always ASCII, and thus also valid UTF-8.
68 pub(crate) data: [ascii::Char; N],
69
70 // Invariant: alive.start <= alive.end <= N.
71 pub(crate) alive: Range<u8>,
72}
73
74impl<const N: usize> EscapeIterInner<N> {
75 pub fn new(data: [ascii::Char; N], alive: Range<u8>) -> Self {
76 const { assert!(N < 256) };
77 debug_assert!(alive.start <= alive.end && usize::from(alive.end) <= N, "{alive:?}");
78 Self { data, alive }
79 }
80
81 pub fn from_array<const M: usize>(array: [ascii::Char; M]) -> Self {
82 const { assert!(M <= N) };
83
84 let mut data = [ascii::Char::Null; N];
85 data[..M].copy_from_slice(&array);
86 Self::new(data, 0..M as u8)
87 }
88
89 pub fn as_ascii(&self) -> &[ascii::Char] {
90 &self.data[usize::from(self.alive.start)..usize::from(self.alive.end)]
91 }
92
93 pub fn as_str(&self) -> &str {
94 self.as_ascii().as_str()
95 }
96
97 pub fn len(&self) -> usize {
98 usize::from(self.alive.end - self.alive.start)
99 }
100
101 pub fn next(&mut self) -> Option<u8> {
102 self.alive.next().map(|i| self.data[usize::from(i)].to_u8())
103 }
104
105 pub fn next_back(&mut self) -> Option<u8> {
106 self.alive.next_back().map(|i| self.data[usize::from(i)].to_u8())
107 }
108
109 pub fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
110 self.alive.advance_by(n)
111 }
112
113 pub fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
114 self.alive.advance_back_by(n)
115 }
116}
117