1use core::iter::FusedIterator;
26
27use utf_types::utf8::Ascii;
28use utf_types::utf8::FourByteSequence;
29use utf_types::utf8::PreparedThreeBytes;
30use utf_types::utf8::PreparedTwoBytes;
31use utf_types::utf8::ThreeByteSequence;
32use utf_types::utf8::TwoByteSequence;
33
34const SEQUENCE_FOR_REPLACEMENT_CHARACTER: ThreeByteSequence = const {
35 if let Ok(sequence) = ThreeByteSequence::try_new(0xEF, 0xBF, 0xBD) {
36 sequence
37 } else {
38 ::core::panicking::panic_default_msg()panic!()
39 }
40};
41
42pub trait Utf8Handler {
45 type Output;
48
49 fn single_byte(&self, ascii: Ascii) -> Self::Output;
57
58 fn two_byte(&self, sequence: TwoByteSequence) -> Self::Output;
66
67 fn three_byte(&self, sequence: ThreeByteSequence) -> Self::Output;
75
76 fn four_byte(&self, sequence: FourByteSequence) -> Self::Output;
84
85 #[inline(always)]
100 fn error(&self) -> Self::Output {
101 self.three_byte(SEQUENCE_FOR_REPLACEMENT_CHARACTER)
102 }
103}
104
105#[derive(#[automatically_derived]impl<'a, H: ::core::fmt::Debug> ::core::fmt::Debug for
Utf8CharsWithHandler<'a, H> where H: Utf8Handler {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"Utf8CharsWithHandler remaining handler", &self.remaining,
&&self.handler)
}
}Debug)]
109pub struct Utf8CharsWithHandler<'a, H>
110where
111 H: Utf8Handler,
112{
113 remaining: &'a [u8],
114 handler: H,
115}
116
117impl<'a, H> Utf8CharsWithHandler<'a, H>
118where
119 H: Utf8Handler,
120{
121 #[inline(always)]
122 pub fn new(bytes: &'a [u8], handler: H) -> Self {
124 Utf8CharsWithHandler::<'a, H> {
125 remaining: bytes,
126 handler,
127 }
128 }
129
130 #[inline(always)]
133 pub fn as_slice(&self) -> &'a [u8] {
134 self.remaining
135 }
136
137 #[inline(always)]
139 pub fn handler(&self) -> &H {
140 &self.handler
141 }
142
143 #[cold]
144 fn error(&self) -> H::Output {
145 self.handler.error()
146 }
147
148 #[cold]
154 #[inline(never)]
155 fn next_fallback(&mut self) -> Option<H::Output> {
156 let (&first, after_one) = self.remaining.split_first()?;
157 if let Ok(ascii) = Ascii::try_new(first) {
158 self.remaining = after_one;
159 return Some(self.handler.single_byte(ascii));
160 }
161 if let Some((&second, after_two)) = after_one.split_first() {
162 if let Ok(two_byte) = TwoByteSequence::try_new(first, second) {
163 self.remaining = after_two;
164 return Some(self.handler.two_byte(two_byte));
165 }
166 let prepared_two = PreparedTwoBytes::new(first, second);
167 if let Some((&third, after_three)) = after_two.split_first() {
168 let prepared_three = PreparedThreeBytes::new_with_prepared(prepared_two, third);
169 if let Ok(three_byte) = ThreeByteSequence::try_new_with_prepared(prepared_three) {
170 self.remaining = after_three;
171 return Some(self.handler.three_byte(three_byte));
172 }
173 if prepared_three.prefix_of_four_byte() {
180 self.remaining = after_three;
181 return Some(self.error());
182 }
183 }
184 if prepared_two.sequence_prefix() {
190 self.remaining = after_two;
191 return Some(self.error());
192 }
193 }
194 self.remaining = after_one;
195 Some(self.error())
196 }
197
198 #[cold]
199 #[inline(never)]
200 fn next_back_fallback(&mut self) -> Option<H::Output> {
201 let (&last, before_one) = self.remaining.split_last()?;
202 if let Ok(ascii) = Ascii::try_new(last) {
203 self.remaining = before_one;
204 return Some(self.handler.single_byte(ascii));
205 }
206 if let Some((&second_last, before_two)) = before_one.split_last() {
207 if let Ok(two_byte) = TwoByteSequence::try_new(second_last, last) {
208 self.remaining = before_two;
209 return Some(self.handler.two_byte(two_byte));
210 }
211 let prepared_two = PreparedTwoBytes::new(second_last, last);
212 if let Some((&third_last, before_three)) = before_two.split_last() {
213 let prepared_three = PreparedThreeBytes::new(third_last, second_last, last);
214 if let Ok(three_byte) = ThreeByteSequence::try_new_with_prepared(prepared_three) {
215 self.remaining = before_three;
216 return Some(self.handler.three_byte(three_byte));
217 }
218 if prepared_three.prefix_of_four_byte() {
220 self.remaining = before_three;
221 return Some(self.error());
222 }
223 }
224 if prepared_two.sequence_prefix() {
230 self.remaining = before_two;
231 return Some(self.error());
232 }
233 }
234 self.remaining = before_one;
235 Some(self.error())
236 }
237}
238
239impl<'a, H> Iterator for Utf8CharsWithHandler<'a, H>
240where
241 H: Utf8Handler,
242{
243 type Item = H::Output;
244
245 #[inline(always)]
246 fn next(&mut self) -> Option<Self::Item> {
247 if self.remaining.len() >= 4 {
248 let (&first, after_one) = self.remaining.split_first().unwrap();
250 if let Ok(ascii) = Ascii::try_new(first) {
251 self.remaining = after_one;
252 return Some(self.handler.single_byte(ascii));
253 }
254 let (&second, after_two) = after_one.split_first().unwrap();
256 if let Ok(two_byte) = TwoByteSequence::try_new(first, second) {
257 self.remaining = after_two;
258 return Some(self.handler.two_byte(two_byte));
259 }
260 let (&third, after_three) = after_two.split_first().unwrap();
262 let prepared_three = PreparedThreeBytes::new(first, second, third);
263 if let Ok(three_byte) = ThreeByteSequence::try_new_with_prepared(prepared_three) {
264 self.remaining = after_three;
265 return Some(self.handler.three_byte(three_byte));
266 }
267 let (&fourth, after_four) = after_three.split_first().unwrap();
269 if let Ok(four_byte) = FourByteSequence::try_new_with_prepared(prepared_three, fourth) {
270 self.remaining = after_four;
271 return Some(self.handler.four_byte(four_byte));
272 }
273 }
274 self.next_fallback()
275 }
276
277 #[inline(always)]
280 fn size_hint(&self) -> (usize, Option<usize>) {
281 let len = self.remaining.len();
282 (len.div_ceil(4), Some(len))
283 }
284
285 #[inline(always)]
286 fn last(mut self) -> Option<Self::Item> {
287 self.next_back()
289 }
290}
291
292impl<'a, H> DoubleEndedIterator for Utf8CharsWithHandler<'a, H>
293where
294 H: Utf8Handler,
295{
296 #[inline(always)]
297 fn next_back(&mut self) -> Option<H::Output> {
298 if self.remaining.len() >= 4 {
299 let (&last, before_one) = self.remaining.split_last().unwrap();
301 if let Ok(ascii) = Ascii::try_new(last) {
302 self.remaining = before_one;
303 return Some(self.handler.single_byte(ascii));
304 }
305 let (&second_last, before_two) = before_one.split_last().unwrap();
307 if let Ok(two_byte) = TwoByteSequence::try_new(second_last, last) {
308 self.remaining = before_two;
309 return Some(self.handler.two_byte(two_byte));
310 }
311 let (&third_last, before_three) = before_two.split_last().unwrap();
313 if let Ok(three_byte) = ThreeByteSequence::try_new(third_last, second_last, last) {
314 self.remaining = before_three;
315 return Some(self.handler.three_byte(three_byte));
316 }
317 let (&fourth_last, before_four) = before_three.split_last().unwrap();
319 if let Ok(four_byte) =
320 FourByteSequence::try_new(fourth_last, third_last, second_last, last)
321 {
322 self.remaining = before_four;
323 return Some(self.handler.four_byte(four_byte));
324 }
325 }
326 self.next_back_fallback()
327 }
328}
329
330impl<H> FusedIterator for Utf8CharsWithHandler<'_, H> where H: Utf8Handler {}
331
332impl<'a, H> Clone for Utf8CharsWithHandler<'a, H>
333where
334 H: Utf8Handler + Clone,
335{
336 fn clone(&self) -> Self {
337 Self {
338 remaining: self.remaining,
339 handler: self.handler.clone(),
340 }
341 }
342}
343
344