bytes2chars 0.2.1

lazy utf-8 decoder iterator with rich errors
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
use core::mem;

use inner::Utf8State;

use crate::{Error, ErrorKind, Result};

mod inner {
    use core::ops::RangeInclusive;

    use crate::ErrorKind;

    type Utf8Result<T> = core::result::Result<T, ErrorKind>;

    // utf8[depends validate.expected-continuation]
    const CONTINUATION_MASK: u8 = 0b1100_0000;
    const CONTINUATION_PREFIX: u8 = 0b1000_0000;
    const CONTINUATION_PAYLOAD_MASK: u8 = 0b0011_1111;
    // utf8[depends encoding.two-byte]
    const TWO_BYTE_LEAD_PAYLOAD_MASK: u8 = 0b0001_1111;
    // utf8[depends encoding.three-byte]
    const THREE_BYTE_LEAD_PAYLOAD_MASK: u8 = 0b0000_1111;
    // utf8[depends encoding.four-byte]
    const FOUR_BYTE_LEAD_PAYLOAD_MASK: u8 = 0b0000_0111;

    // utf8[depends validate.no-surrogates]
    const SURROGATE_RANGE: RangeInclusive<u32> = 0xD800..=0xDFFF;

    // utf8[depends validate.max-codepoint]
    const MAX_CODEPOINT: u32 = 0x0010_FFFF;

    trait Utf8ByteExt: Copy {
        fn is_utf8_continuation(self) -> bool;
        fn utf8_seq_len(self) -> Utf8Result<u8>;
    }

    impl Utf8ByteExt for u8 {
        // utf8[depends validate.expected-continuation]
        fn is_utf8_continuation(self) -> bool {
            self & CONTINUATION_MASK == CONTINUATION_PREFIX
        }

        fn utf8_seq_len(self) -> Utf8Result<u8> {
            match self {
                0x00..=0x7F => unreachable!(
                    "ASCII bytes are handled in Utf8Decoder::push before utf8_seq_len is called"
                ),
                0x80..=0xBF => Err(ErrorKind::InvalidLead(self)), // utf8[impl validate.invalid-lead]
                0xC0..=0xC1 => Err(ErrorKind::InvalidLead(self)), // utf8[impl validate.no-overlong]
                0xC2..=0xDF => Ok(2),                             // utf8[impl encoding.two-byte]
                0xE0..=0xEF => Ok(3),                             // utf8[impl encoding.three-byte]
                0xF0..=0xF4 => Ok(4),                             // utf8[impl encoding.four-byte]
                0xF5..=0xF7 => Err(ErrorKind::InvalidLead(self)), // utf8[impl validate.max-codepoint]
                0xF8..=0xFF => Err(ErrorKind::InvalidSequenceLength(self)), // utf8[impl validate.max-sequence-length]
            }
        }
    }

    #[derive(Clone, Debug, PartialEq, Eq)]
    pub(super) struct UtfStateInner {
        codepoint: u32,
        bytes: u8,
        max_bytes: u8,
    }

    impl UtfStateInner {
        fn push(&mut self, b: u8) {
            debug_assert!(self.bytes < self.max_bytes);

            if self.bytes == 0 {
                self.codepoint = Self::lead_payload_bits(b, self.max_bytes);
            } else {
                self.codepoint = (self.codepoint << 6) | Self::continuation_payload_bits(b);
            }

            self.bytes += 1;
        }

        fn lead_payload_bits(b: u8, max_bytes: u8) -> u32 {
            let mask = match max_bytes {
                2 => TWO_BYTE_LEAD_PAYLOAD_MASK,
                3 => THREE_BYTE_LEAD_PAYLOAD_MASK,
                4 => FOUR_BYTE_LEAD_PAYLOAD_MASK,
                _ => {
                    unreachable!("max_bytes is always 2, 3, or 4 per utf8_seq_len, got {max_bytes}")
                }
            };
            u32::from(b & mask)
        }

        fn continuation_payload_bits(b: u8) -> u32 {
            u32::from(b & CONTINUATION_PAYLOAD_MASK)
        }

        fn is_done(&self) -> bool {
            self.bytes == self.max_bytes
        }

        fn new(max_bytes: u8) -> Self {
            Self {
                codepoint: 0,
                bytes: 0,
                max_bytes,
            }
        }

        fn from_byte(b: u8, remaining_bytes: u8) -> Self {
            let mut s = Self::new(remaining_bytes);
            s.push(b);
            s
        }
    }

    #[derive(Clone, Debug, PartialEq, Eq, Default)]
    pub(super) enum Utf8State {
        #[default]
        Idle,
        Expecting(UtfStateInner),
        Done(char),
    }

    impl Utf8State {
        pub(super) fn process(self, b: u8) -> Utf8Result<Self> {
            match self {
                Utf8State::Idle => {
                    let seq_len = b.utf8_seq_len()?;
                    match seq_len {
                        2..=4 => Ok(Utf8State::Expecting(UtfStateInner::from_byte(b, seq_len))),
                        _ => unreachable!("`utf8_seq_len` only returns 2..=4 for non-ASCII bytes"),
                    }
                }
                Utf8State::Expecting(mut state) => {
                    // utf8[impl validate.expected-continuation]
                    if !b.is_utf8_continuation() {
                        return Err(ErrorKind::ExpectedContinuation(b));
                    }

                    state.push(b);

                    if state.is_done() {
                        validate_utf8_sequence(&state)?;
                        Ok(Utf8State::Done(
                            // SAFETY: byte sequence is validated by `validate_utf8_sequence`
                            unsafe { char::from_u32_unchecked(state.codepoint) },
                        ))
                    } else {
                        Ok(Utf8State::Expecting(state))
                    }
                }
                Utf8State::Done(_) => {
                    unreachable!(
                        "`Utf8Decoder::push` resets `Done` to `Idle` before calling `process`"
                    )
                }
            }
        }
    }

    fn validate_utf8_sequence(bytes: &UtfStateInner) -> Utf8Result<()> {
        let codepoint = bytes.codepoint;

        // utf8[impl validate.no-surrogates]
        if SURROGATE_RANGE.contains(&codepoint) {
            return Err(ErrorKind::InvalidSurrogate(codepoint));
        }

        // utf8[impl validate.max-codepoint]
        if codepoint > MAX_CODEPOINT {
            return Err(ErrorKind::OutOfRange(codepoint));
        }

        // utf8[impl validate.no-overlong]
        if bytes.bytes != min_bytes_for_code_point(codepoint) {
            return Err(ErrorKind::Overlong(codepoint));
        }

        Ok(())
    }

    // utf8[depends validate.no-overlong]
    fn min_bytes_for_code_point(cp: u32) -> u8 {
        match cp {
            0x0000..=0x007F => 1,       // utf8[depends encoding.ascii]
            0x0080..=0x07FF => 2,       // utf8[depends encoding.two-byte]
            0x0800..=0xFFFF => 3,       // utf8[depends encoding.three-byte]
            0x10000..=0x0010_FFFF => 4, // utf8[depends encoding.four-byte]
            _ => unreachable!(
                "code points above U+10FFFF are rejected by validate.max-codepoint before reaching this function"
            ),
        }
    }
}

/// push based UTF-8 decoder that tracks byte positions
///
/// # Examples
///
/// decode a valid character
///
/// ```
/// # use bytes2chars::{Utf8Decoder, Result};
/// # fn main() -> Result<()> {
/// let mut decoder = Utf8Decoder::new(0);
/// assert_eq!(decoder.push(0xF0), None); // accumulating
/// assert_eq!(decoder.push(0x9F), None);
/// assert_eq!(decoder.push(0xA6), None);
/// assert_eq!(decoder.push(0x80), Some(Ok((0, '🦀')))); // complete
/// decoder.finish()?; // check for truncated sequence
///
/// # Ok(())
/// # }
/// ```
///
/// keeps going after an error. offending bytes are thrown in the garbage can
///
/// ```
/// # use bytes2chars::{Error, ErrorKind, Utf8Decoder};
/// let mut decoder = Utf8Decoder::new(0);
/// assert_eq!(decoder.push(b'a'), Some(Ok((0, 'a'))));
/// assert_eq!(decoder.push(0xC3), None);
/// assert_eq!(
///     decoder.push(0xC3),
///     Some(Err(Error {
///         range: 1..3,
///         kind: ErrorKind::ExpectedContinuation(0xC3),
///     }))
/// );
/// assert_eq!(decoder.push(b'b'), Some(Ok((3, 'b'))));
/// ```
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct Utf8Decoder {
    state: Utf8State,
    /// offset of the current sequence
    sequence_offset: usize,
    /// byte offset of the next byte to be consumed
    offset: usize,
}

impl Utf8Decoder {
    /// create a decoder starting at byte offset `offset`
    pub fn new(offset: usize) -> Self {
        Self {
            state: Utf8State::Idle,
            sequence_offset: offset,
            offset,
        }
    }

    /// process a single byte
    ///
    /// # Examples
    ///
    /// ```
    /// # use bytes2chars::{Error, ErrorKind, Utf8Decoder, Result};
    /// let mut decoder = Utf8Decoder::default();
    ///
    /// assert_eq!(decoder.push(0xC3), None); // accumulating
    /// assert_eq!(decoder.push(0xA9), Some(Ok((0, 'é')))); // complete
    ///
    /// // error
    /// let expected = Some(Err(Error {
    ///     range: 2..3,
    ///     kind: ErrorKind::InvalidLead(0x80),
    /// }));
    /// assert_eq!(decoder.push(0x80), expected);
    ///
    /// // after error, decoder is reset to idle and continues
    /// assert_eq!(decoder.push(b'b'), Some(Ok((3, 'b'))));
    /// assert_eq!(decoder.finish(), Ok(4)); // no truncated sequence
    /// ```
    pub fn push(&mut self, b: u8) -> Option<Result<(usize, char)>> {
        let curr_idx = self.offset;
        self.offset += 1;

        if matches!(self.state, Utf8State::Idle) {
            // utf8[impl encoding.ascii]
            if b.is_ascii() {
                return Some(Ok((curr_idx, char::from(b))));
            }
            self.sequence_offset = curr_idx;
        }

        match mem::take(&mut self.state).process(b) {
            Err(kind) => {
                return Some(Err(Error {
                    range: self.sequence_offset..curr_idx + 1,
                    kind,
                }));
            }
            Ok(state) => self.state = state,
        }

        if let Utf8State::Done(c) = self.state {
            self.state = Utf8State::Idle;
            Some(Ok((self.sequence_offset, c)))
        } else {
            None
        }
    }

    /// flush the decoder when there are no more bytes left
    ///
    /// on success, returns the total number of bytes consumed
    ///
    /// # Errors
    ///
    /// Returns an error of kind [`ErrorKind::UnfinishedSequence`] when current byte sequence is truncated
    ///
    /// # Examples
    ///
    /// ```
    /// # use bytes2chars::{ErrorKind, Utf8Decoder};
    /// // idle decoder is all good
    /// assert_eq!(Utf8Decoder::new(0).finish(), Ok(0));
    ///
    /// // incomplete sequence returns `UnfinishedSequence`
    /// let mut decoder = Utf8Decoder::new(0);
    /// assert_eq!(decoder.push(0xC3), None);
    /// assert_eq!(
    ///     decoder.finish().unwrap_err().kind,
    ///     ErrorKind::UnfinishedSequence
    /// );
    /// ```
    pub fn finish(self) -> Result<usize> {
        // utf8[impl validate.unfinished]
        if matches!(self.state, Utf8State::Expecting(_)) {
            Err(Error {
                range: self.sequence_offset..self.offset,
                kind: ErrorKind::UnfinishedSequence,
            })
        } else {
            Ok(self.offset)
        }
    }
}

#[cfg(test)]
mod tests {
    extern crate alloc;

    use alloc::vec::Vec;

    use super::*;

    fn collect_from_bytes(bytes: &[u8]) -> Vec<Result<(usize, char)>> {
        let mut decoder = Utf8Decoder::new(0);
        bytes
            .iter()
            .copied()
            .filter_map(|b| decoder.push(b))
            .collect()
    }

    fn collect_string(bytes: &[u8]) -> Result<alloc::string::String> {
        collect_from_bytes(bytes)
            .into_iter()
            .map(|r| r.map(|(_, c)| c))
            .collect()
    }

    // utf8[verify encoding.ascii]
    #[test]
    fn ascii() {
        assert_eq!(collect_from_bytes(b"a"), Vec::from([Ok((0, 'a'))]));
    }

    // utf8[verify encoding.two-byte]
    #[test]
    fn valid_2byte() {
        assert_eq!(collect_from_bytes(&[0xC3, 0xA9]), Vec::from([Ok((0, 'é'))]));
    }

    // utf8[verify encoding.three-byte]
    #[test]
    fn valid_3byte() {
        assert_eq!(
            collect_from_bytes(&[0xE2, 0x82, 0xAC]),
            Vec::from([Ok((0, '€'))])
        );
    }

    // utf8[verify encoding.four-byte]
    #[test]
    fn valid_4byte() {
        assert_eq!(
            collect_from_bytes(&[0xF0, 0x9F, 0x98, 0x80]),
            Vec::from([Ok((0, '😀'))])
        );
    }

    // utf8[verify validate.invalid-lead]
    #[test]
    fn invalid_lead_byte() {
        assert_eq!(
            collect_from_bytes(&[0x80]),
            Vec::from([Err(Error {
                range: 0..1,
                kind: ErrorKind::InvalidLead(0x80),
            })])
        );
    }

    // utf8[verify validate.max-sequence-length]
    #[test]
    fn five_plus_byte_lead() {
        assert_eq!(
            collect_from_bytes(&[0xF8]),
            Vec::from([Err(Error {
                range: 0..1,
                kind: ErrorKind::InvalidSequenceLength(0xF8),
            })])
        );
    }

    #[test]
    fn missing_continuation() {
        // offending bytes are thrown in the garbage can — 'a' lands at offset 2, not 1
        assert_eq!(
            collect_from_bytes(&[0xC3, 0xC3, b'a']),
            Vec::from([
                Err(Error {
                    range: 0..2,
                    kind: ErrorKind::ExpectedContinuation(0xC3),
                }),
                Ok((2, 'a')),
            ])
        );
    }

    // utf8[verify validate.expected-continuation]
    #[test]
    fn invalid_continuation() {
        assert_eq!(
            collect_from_bytes(&[0xC3, 0x40]),
            Vec::from([Err(Error {
                range: 0..2,
                kind: ErrorKind::ExpectedContinuation(0x40),
            })])
        );
    }

    // utf8[verify validate.no-overlong]
    #[test]
    fn e0_overlong() {
        assert_eq!(
            collect_from_bytes(&[0xE0, 0x80, 0x80]),
            Vec::from([Err(Error {
                range: 0..3,
                kind: ErrorKind::Overlong(0x0000),
            })])
        );
    }

    // utf8[verify validate.no-surrogates]
    #[test]
    fn ed_surrogate_rejected() {
        assert_eq!(
            collect_from_bytes(&[0xED, 0xA0, 0x80]),
            Vec::from([Err(Error {
                range: 0..3,
                kind: ErrorKind::InvalidSurrogate(0xD800),
            })])
        );
    }

    // utf8[verify validate.no-overlong]
    #[test]
    fn f0_overlong() {
        assert_eq!(
            collect_from_bytes(&[0xF0, 0x80, 0x80, 0x80]),
            Vec::from([Err(Error {
                range: 0..4,
                kind: ErrorKind::Overlong(0x0000),
            })])
        );
    }

    // utf8[verify validate.max-codepoint]
    #[test]
    fn f4_out_of_range() {
        assert_eq!(
            collect_from_bytes(&[0xF4, 0x90, 0x80, 0x80]),
            Vec::from([Err(Error {
                range: 0..4,
                kind: ErrorKind::OutOfRange(0x0011_0000),
            })])
        );
    }

    // utf8[verify validate.unfinished]
    #[test]
    fn incomplete_sequence_errors_on_finish() {
        let mut decoder = Utf8Decoder::new(0);
        assert!(decoder.push(0xC3).is_none()); // 2-byte sequence start
        assert_eq!(
            decoder.finish(),
            Err(Error {
                range: 0..1,
                kind: ErrorKind::UnfinishedSequence
            })
        );

        let mut decoder = Utf8Decoder::new(0);
        assert!(decoder.push(0xF0).is_none()); // 4-byte sequence start
        assert_eq!(
            decoder.finish(),
            Err(Error {
                range: 0..1,
                kind: ErrorKind::UnfinishedSequence
            })
        );
    }

    #[test]
    fn chars_collect() {
        assert_eq!(collect_string(b"\xF0\x9F\xA6\x80 rust").unwrap(), "🦀 rust");
    }

    #[test]
    fn done_starts_fresh_sequence() {
        let mut decoder = Utf8Decoder::new(0);
        assert_eq!(decoder.push(0xE2), None);
        assert_eq!(decoder.push(0x82), None);
        assert_eq!(decoder.push(0xAC), Some(Ok((0, '€'))));

        // now in Done state, next byte should start new sequence
        assert_eq!(decoder.push(b'a'), Some(Ok((3, 'a'))));
        assert_eq!(decoder.push(0xC3), None);
        // 0xC3 0x80 = U+00C0 (À), valid 2-byte sequence
        assert_eq!(decoder.push(0x80), Some(Ok((4, 'À'))));
    }

    #[test]
    fn new_at_offsets_indices() {
        // decoder starts mid-stream at byte 10
        let mut decoder = Utf8Decoder::new(10);
        // ASCII byte at effective position 10
        assert_eq!(decoder.push(b'x'), Some(Ok((10, 'x'))));
        // 2-byte sequence starting at effective position 11
        assert_eq!(decoder.push(0xC3), None);
        assert_eq!(decoder.push(0xA9), Some(Ok((11, 'é'))));
        // Error range is also offset
        assert_eq!(
            decoder.push(0x80),
            Some(Err(Error {
                range: 13..14,
                kind: ErrorKind::InvalidLead(0x80),
            }))
        );
    }
}