Debian Codegraph @

third_party/rust/unix_path/src/lossy.rs

raw
1 use core::char;
2 use core::fmt::{self, Write};
3 use core::mem;
4 use core::str as core_str;
5
6 // https://tools.ietf.org/html/rfc3629
7 static UTF8_CHAR_WIDTH: [u8; 256] = [
8 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9 1, // 0x1F
10 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11 1, // 0x3F
12 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
13 1, // 0x5F
14 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
15 1, // 0x7F
16 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17 0, // 0x9F
18 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19 0, // 0xBF
20 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
21 2, // 0xDF
22 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xEF
23 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xFF
24 ];
25
26 /// Given a first byte, determines how many bytes are in this UTF-8 character.
27 #[inline]
28 pub fn utf8_char_width(b: u8) -> usize {
29 UTF8_CHAR_WIDTH[b as usize] as usize
30 }
31
32 /// Lossy UTF-8 string.
33 pub struct Utf8Lossy {
34 bytes: [u8],
35 }
36
37 impl Utf8Lossy {
38 pub fn from_bytes(bytes: &[u8]) -> &Utf8Lossy {
39 // SAFETY: Both use the same memory layout, and UTF-8 correctness isn't required.
40 unsafe { mem::transmute(bytes) }
41 }
42
43 pub fn chunks(&self) -> Utf8LossyChunksIter<'_> {
44 Utf8LossyChunksIter {
45 source: &self.bytes,
46 }
47 }
48 }
49
50 /// Iterator over lossy UTF-8 string
51 pub struct Utf8LossyChunksIter<'a> {
52 source: &'a [u8],
53 }
54
55 #[derive(PartialEq, Eq, Debug)]
56 pub struct Utf8LossyChunk<'a> {
57 /// Sequence of valid chars.
58 /// Can be empty between broken UTF-8 chars.
59 pub valid: &'a str,
60 /// Single broken char, empty if none.
61 /// Empty iff iterator item is last.
62 pub broken: &'a [u8],
63 }
64
65 impl<'a> Iterator for Utf8LossyChunksIter<'a> {
66 type Item = Utf8LossyChunk<'a>;
67
68 fn next(&mut self) -> Option<Utf8LossyChunk<'a>> {
69 if self.source.is_empty() {
70 return None;
71 }
72
73 const TAG_CONT_U8: u8 = 128;
74 fn safe_get(xs: &[u8], i: usize) -> u8 {
75 *xs.get(i).unwrap_or(&0)
76 }
77
78 let mut i = 0;
79 while i < self.source.len() {
80 let i_ = i;
81
82 // SAFETY: `i` starts at `0`, is less than `self.source.len()`, and
83 // only increases, so `0 <= i < self.source.len()`.
84 let byte = unsafe { *self.source.get_unchecked(i) };
85 i += 1;
86
87 if byte < 128 {
88 } else {
89 let w = utf8_char_width(byte);
90
91 macro_rules! error {
92 () => {{
93 // SAFETY: We have checked up to `i` that source is valid UTF-8.
94 unsafe {
95 let r = Utf8LossyChunk {
96 valid: core_str::from_utf8_unchecked(&self.source[0..i_]),
97 broken: &self.source[i_..i],
98 };
99 self.source = &self.source[i..];
100 return Some(r);
101 }
102 }};
103 }
104
105 match w {
106 2 => {
107 if safe_get(self.source, i) & 192 != TAG_CONT_U8 {
108 error!();
109 }
110 i += 1;
111 }
112 3 => {
113 match (byte, safe_get(self.source, i)) {
114 (0xE0, 0xA0..=0xBF) => (),
115 (0xE1..=0xEC, 0x80..=0xBF) => (),
116 (0xED, 0x80..=0x9F) => (),
117 (0xEE..=0xEF, 0x80..=0xBF) => (),
118 _ => {
119 error!();
120 }
121 }
122 i += 1;
123 if safe_get(self.source, i) & 192 != TAG_CONT_U8 {
124 error!();
125 }
126 i += 1;
127 }
128 4 => {
129 match (byte, safe_get(self.source, i)) {
130 (0xF0, 0x90..=0xBF) => (),
131 (0xF1..=0xF3, 0x80..=0xBF) => (),
132 (0xF4, 0x80..=0x8F) => (),
133 _ => {
134 error!();
135 }
136 }
137 i += 1;
138 if safe_get(self.source, i) & 192 != TAG_CONT_U8 {
139 error!();
140 }
141 i += 1;
142 if safe_get(self.source, i) & 192 != TAG_CONT_U8 {
143 error!();
144 }
145 i += 1;
146 }
147 _ => {
148 error!();
149 }
150 }
151 }
152 }
153
154 let r = Utf8LossyChunk {
155 // SAFETY: We have checked that the entire source is valid UTF-8.
156 valid: unsafe { core_str::from_utf8_unchecked(self.source) },
157 broken: &[],
158 };
159 self.source = &[];
160 Some(r)
161 }
162 }
163
164 impl fmt::Display for Utf8Lossy {
165 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
166 // If we're the empty string then our iterator won't actually yield
167 // anything, so perform the formatting manually
168 if self.bytes.is_empty() {
169 return "".fmt(f);
170 }
171
172 for Utf8LossyChunk { valid, broken } in self.chunks() {
173 // If we successfully decoded the whole chunk as a valid string then
174 // we can return a direct formatting of the string which will also
175 // respect various formatting flags if possible.
176 if valid.len() == self.bytes.len() {
177 assert!(broken.is_empty());
178 return valid.fmt(f);
179 }
180
181 f.write_str(valid)?;
182 if !broken.is_empty() {
183 f.write_char(char::REPLACEMENT_CHARACTER)?;
184 }
185 }
186 Ok(())
187 }
188 }