deser-csv 0.9.1

CSV, TSV and other delimited text for deser
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
//! Splits delimited text into records and fields.
//!
//! The scanner finds the fields of a record in a single pass and remembers
//! their positions, the deserializer emits them from there.  It can be
//! suspended at any byte (when more input is needed) and continues where it
//! stopped once the input is longer.
use alloc::format;
use alloc::vec::Vec;
use deser_core::{Error, ErrorKind};

use crate::{Escape, Terminator};

// the classes of bytes
const OTHER: u8 = 0;
const DELIMITER: u8 = 1;
const QUOTE: u8 = 2;
const ESCAPE: u8 = 3;
const TERMINATOR: u8 = 4;
const CR: u8 = 5;

const LO: u64 = 0x0101_0101_0101_0101;
const HI: u64 = 0x8080_8080_8080_8080;

/// The field was quoted.
pub(crate) const QUOTED: u8 = 1;
/// The field contains escapes (or doubled quotes) that need to be decoded.
pub(crate) const UNESCAPE: u8 = 2;

/// The special characters of a dialect.
#[derive(Clone)]
pub(crate) struct Dialect {
    pub(crate) delimiter: u8,
    pub(crate) quote: Option<u8>,
    pub(crate) double_quote: bool,
    pub(crate) escape: Escape,
    pub(crate) comment: Option<u8>,
    classes: [u8; 256],
    // the special characters (repeated to fill the array) in every byte
    // of a word
    specials: [u64; 5],
}

impl core::fmt::Debug for Dialect {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Dialect")
            .field("delimiter", &self.delimiter)
            .field("quote", &self.quote)
            .finish_non_exhaustive()
    }
}

impl Dialect {
    /// Creates a dialect and checks that the special characters are ASCII
    /// and distinct.
    pub(crate) fn new(
        delimiter: u8,
        quote: Option<u8>,
        double_quote: bool,
        escape: Escape,
        terminator: Terminator,
        comment: Option<u8>,
    ) -> Result<Dialect, Error> {
        let mut classes = [OTHER; 256];
        let mut assign = |byte: u8, class: u8, what: &str| -> Result<(), Error> {
            if !byte.is_ascii() {
                return Err(config_error(format!(
                    "the {} must be an ASCII character",
                    what
                )));
            }
            if classes[byte as usize] != OTHER {
                return Err(config_error(format!(
                    "the {} conflicts with another special character",
                    what
                )));
            }
            classes[byte as usize] = class;
            Ok(())
        };
        match terminator {
            Terminator::Newline | Terminator::CrLf => {
                assign(b'\n', TERMINATOR, "terminator")?;
                assign(b'\r', CR, "terminator")?;
            }
            Terminator::Byte(byte) => assign(byte, TERMINATOR, "terminator")?,
        }
        assign(delimiter, DELIMITER, "delimiter")?;
        if let Some(quote) = quote {
            assign(quote, QUOTE, "quote character")?;
        }
        if let Some(escape) = escape.byte() {
            assign(escape, ESCAPE, "escape character")?;
        }
        if let Some(comment) = comment
            && (!comment.is_ascii() || classes[comment as usize] != OTHER)
        {
            return Err(config_error(
                "the comment character must be an ASCII character that is not special",
            ));
        }
        let (first, second) = match terminator {
            Terminator::Newline | Terminator::CrLf => (b'\n', b'\r'),
            Terminator::Byte(byte) => (byte, byte),
        };
        let specials = [
            delimiter,
            quote.unwrap_or(delimiter),
            escape.byte().unwrap_or(delimiter),
            first,
            second,
        ]
        .map(|byte| u64::from(byte) * LO);
        Ok(Dialect {
            delimiter,
            quote,
            double_quote,
            escape,
            comment,
            classes,
            specials,
        })
    }

    #[inline(always)]
    fn class(&self, byte: u8) -> u8 {
        self.classes[byte as usize]
    }

    /// Returns `true` if the byte is a special character.
    #[inline(always)]
    pub(crate) fn is_special(&self, byte: u8) -> bool {
        self.class(byte) != OTHER
    }

    /// Returns `true` if the text contains a special character.
    #[inline]
    pub(crate) fn has_special(&self, text: &[u8]) -> bool {
        // eight bytes at a time: a byte of `x` is zero where the text has
        // the special character
        let (chunks, rest) = text.as_chunks::<8>();
        for chunk in chunks {
            let word = u64::from_ne_bytes(*chunk);
            let mut found = 0;
            for special in self.specials {
                let x = word ^ special;
                found |= x.wrapping_sub(LO) & !x & HI;
            }
            if found != 0 {
                return true;
            }
        }
        rest.iter().any(|&b| self.is_special(b))
    }

    /// Returns `true` if the byte ends a record.
    pub(crate) fn is_terminator(&self, byte: u8) -> bool {
        matches!(self.class(byte), TERMINATOR | CR)
    }
}

#[cold]
fn config_error<M: Into<alloc::borrow::Cow<'static, str>>>(msg: M) -> Error {
    Error::new(ErrorKind::Unexpected, msg)
}

/// A field of a record.  The positions are relative to the record.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Field {
    /// The start of the field (including quotes and whitespace).
    pub(crate) span_start: usize,
    /// The end of the field (including quotes and whitespace).
    pub(crate) span_end: usize,
    /// The start of the text (without quotes).
    pub(crate) start: usize,
    /// The end of the text (without quotes).
    pub(crate) end: usize,
    pub(crate) flags: u8,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Mode {
    /// Before the first byte of a record.
    RecordStart,
    /// In a comment line.
    Comment,
    /// Before the first byte of a field.
    FieldStart,
    Unquoted,
    UnquotedEscape,
    Quoted,
    QuotedEscape,
    /// After a quote in a quoted field (it ends the field or is doubled).
    QuotedQuote,
    /// After the closing quote.
    AfterQuoted,
}

/// The result of a scan.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Scan {
    /// A record ends at `end`, the first `consumed` bytes of the input are
    /// used (including the terminator).  The fields are in the scanner.
    Record { end: usize, consumed: usize },
    /// A blank line or comment was skipped.
    Skip { consumed: usize },
    /// More input is needed.
    Incomplete,
    /// There are no more records.
    End,
}

/// How a record is scanned.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Options {
    /// Whitespace around fields (and quotes) is not part of the fields.
    pub(crate) trim: bool,
    pub(crate) skip_blank_lines: bool,
    /// Quotes in unquoted fields and characters after quoted fields are
    /// accepted.
    pub(crate) lenient_quotes: bool,
    pub(crate) max_record_len: usize,
}

/// Finds the fields of records.
#[derive(Debug)]
pub(crate) struct Scanner {
    mode: Mode,
    pos: usize,
    span_start: usize,
    start: usize,
    end: usize,
    flags: u8,
    /// The fields of the last record.
    pub(crate) fields: Vec<Field>,
    /// The first error of the last record (offset and message).  The
    /// record can still be split, which is why this does not end the
    /// stream.
    pub(crate) error: Option<(usize, &'static str)>,
    /// A record ended with a CR at the end of the input, a LF that follows
    /// belongs to it.
    pending_lf: bool,
}

impl Default for Scanner {
    fn default() -> Scanner {
        Scanner {
            mode: Mode::RecordStart,
            pos: 0,
            span_start: 0,
            start: 0,
            end: 0,
            flags: 0,
            fields: Vec::new(),
            error: None,
            pending_lf: false,
        }
    }
}

impl Scanner {
    /// Scans the next record.
    ///
    /// The input starts at the start of the record (or of what is skipped
    /// before it).  After [`Scan::Incomplete`] the scanner has to be invoked
    /// again with the same input plus more data.
    pub(crate) fn scan(
        &mut self,
        dialect: &Dialect,
        input: &[u8],
        eof: bool,
        options: Options,
    ) -> Result<Scan, Error> {
        if self.mode == Mode::RecordStart {
            if self.pending_lf {
                match input.first() {
                    None if eof => return Ok(Scan::End),
                    None => return Ok(Scan::Incomplete),
                    Some(b'\n') => {
                        self.pending_lf = false;
                        return Ok(Scan::Skip { consumed: 1 });
                    }
                    Some(_) => self.pending_lf = false,
                }
            }
            let Some(&first) = input.first() else {
                return Ok(if eof { Scan::End } else { Scan::Incomplete });
            };
            self.fields.clear();
            self.error = None;
            self.pos = 0;
            if Some(first) == dialect.comment {
                self.mode = Mode::Comment;
            } else if dialect.is_terminator(first) {
                if options.skip_blank_lines {
                    let consumed = self.terminator(dialect, input, 0, eof);
                    return Ok(Scan::Skip { consumed });
                }
                // a record with one empty field
                self.fields.push(Field {
                    span_start: 0,
                    span_end: 0,
                    start: 0,
                    end: 0,
                    flags: 0,
                });
                return Ok(self.end_record(dialect, input, 0, eof));
            } else {
                self.start_field(0);
            }
        }

        let len = input.len();
        let mut pos = self.pos;
        while pos < len {
            match self.mode {
                Mode::Comment => {
                    match input[pos..].iter().position(|&b| dialect.is_terminator(b)) {
                        Some(index) => {
                            let consumed = self.terminator(dialect, input, pos + index, eof);
                            self.mode = Mode::RecordStart;
                            return Ok(Scan::Skip { consumed });
                        }
                        None => pos = len,
                    }
                }
                Mode::FieldStart => {
                    let byte = input[pos];
                    match dialect.class(byte) {
                        OTHER if options.trim && is_space(byte) => pos += 1,
                        OTHER => {
                            self.start = pos;
                            self.mode = Mode::Unquoted;
                            pos += 1;
                        }
                        QUOTE => {
                            self.flags = QUOTED;
                            self.start = pos + 1;
                            self.mode = Mode::Quoted;
                            pos += 1;
                        }
                        ESCAPE => {
                            self.start = pos;
                            self.flags = UNESCAPE;
                            self.mode = Mode::UnquotedEscape;
                            pos += 1;
                        }
                        DELIMITER => {
                            self.start = pos;
                            self.push_unquoted(input, pos, options);
                            pos += 1;
                            self.start_field(pos);
                        }
                        _ => {
                            self.start = pos;
                            self.push_unquoted(input, pos, options);
                            return Ok(self.end_record(dialect, input, pos, eof));
                        }
                    }
                }
                Mode::Unquoted => {
                    while pos < len && dialect.class(input[pos]) == OTHER {
                        pos += 1;
                    }
                    let Some(&byte) = input.get(pos) else {
                        break;
                    };
                    match dialect.class(byte) {
                        DELIMITER => {
                            self.push_unquoted(input, pos, options);
                            pos += 1;
                            self.start_field(pos);
                        }
                        QUOTE => {
                            if !options.lenient_quotes {
                                self.set_error(pos, "unexpected quote in an unquoted field");
                            }
                            pos += 1;
                        }
                        ESCAPE => {
                            self.flags |= UNESCAPE;
                            self.mode = Mode::UnquotedEscape;
                            pos += 1;
                        }
                        _ => {
                            self.push_unquoted(input, pos, options);
                            return Ok(self.end_record(dialect, input, pos, eof));
                        }
                    }
                }
                Mode::UnquotedEscape => {
                    pos += 1;
                    self.mode = Mode::Unquoted;
                }
                Mode::Quoted => {
                    while pos < len && !matches!(dialect.class(input[pos]), QUOTE | ESCAPE) {
                        pos += 1;
                    }
                    let Some(&byte) = input.get(pos) else {
                        break;
                    };
                    if dialect.class(byte) == QUOTE {
                        self.mode = Mode::QuotedQuote;
                    } else {
                        self.flags |= UNESCAPE;
                        self.mode = Mode::QuotedEscape;
                    }
                    pos += 1;
                }
                Mode::QuotedEscape => {
                    pos += 1;
                    self.mode = Mode::Quoted;
                }
                Mode::QuotedQuote => {
                    if dialect.double_quote && dialect.class(input[pos]) == QUOTE {
                        self.flags |= UNESCAPE;
                        self.mode = Mode::Quoted;
                        pos += 1;
                    } else {
                        self.end = pos - 1;
                        self.mode = Mode::AfterQuoted;
                    }
                }
                Mode::AfterQuoted => {
                    let byte = input[pos];
                    match dialect.class(byte) {
                        DELIMITER => {
                            self.push_quoted(pos);
                            pos += 1;
                            self.start_field(pos);
                        }
                        TERMINATOR | CR => {
                            self.push_quoted(pos);
                            return Ok(self.end_record(dialect, input, pos, eof));
                        }
                        OTHER if options.trim && is_space(byte) => pos += 1,
                        _ => {
                            // the rest of the field is taken as it is, the
                            // quotes are removed when the field is decoded
                            if !options.lenient_quotes {
                                self.set_error(pos, "unexpected character after a quoted field");
                            }
                            self.flags |= UNESCAPE;
                            self.mode = Mode::Unquoted;
                        }
                    }
                }
                Mode::RecordStart => unreachable!("records are started above"),
            }
        }

        self.pos = pos;
        if !eof {
            if pos > options.max_record_len {
                return Err(Error::new(
                    ErrorKind::Unexpected,
                    format!(
                        "record is longer than the maximum of {} bytes",
                        options.max_record_len
                    ),
                )
                .with_offset(0));
            }
            return Ok(Scan::Incomplete);
        }

        // the end of the input ends the record
        match self.mode {
            Mode::Comment => {
                self.mode = Mode::RecordStart;
                return Ok(Scan::Skip { consumed: len });
            }
            Mode::FieldStart => {
                self.start = len;
                self.push_unquoted(input, len, options);
            }
            Mode::Unquoted => self.push_unquoted(input, len, options),
            Mode::UnquotedEscape => {
                self.set_error(len - 1, "escape character at the end of the input");
                self.push_unquoted(input, len, options);
            }
            Mode::Quoted | Mode::QuotedEscape => {
                self.set_error(self.start - 1, "unterminated quoted field");
                self.end = len;
                self.push_quoted(len);
            }
            Mode::QuotedQuote => {
                self.end = len - 1;
                self.push_quoted(len);
            }
            Mode::AfterQuoted => self.push_quoted(len),
            Mode::RecordStart => unreachable!("records are started above"),
        }
        self.mode = Mode::RecordStart;
        Ok(Scan::Record {
            end: len,
            consumed: len,
        })
    }

    fn start_field(&mut self, pos: usize) {
        self.span_start = pos;
        self.flags = 0;
        self.mode = Mode::FieldStart;
    }

    fn set_error(&mut self, offset: usize, msg: &'static str) {
        if self.error.is_none() {
            self.error = Some((offset, msg));
        }
    }

    /// Adds an unquoted field that ends at `pos`.
    fn push_unquoted(&mut self, input: &[u8], pos: usize, options: Options) {
        let mut end = pos;
        if options.trim && self.flags & UNESCAPE == 0 {
            while end > self.start && is_space(input[end - 1]) {
                end -= 1;
            }
        }
        // for the rest of a field after the closing quote (with lenient
        // quotes) the start is after the opening quote
        self.fields.push(Field {
            span_start: self.span_start,
            span_end: pos,
            start: self.start,
            end,
            flags: self.flags,
        });
    }

    /// Adds a quoted field whose closing quote is at `self.end`.
    fn push_quoted(&mut self, pos: usize) {
        self.fields.push(Field {
            span_start: self.span_start,
            span_end: pos,
            start: self.start,
            end: self.end,
            flags: self.flags,
        });
    }

    /// Ends the record at the terminator at `pos` (or the end of the input).
    fn end_record(&mut self, dialect: &Dialect, input: &[u8], pos: usize, eof: bool) -> Scan {
        let consumed = self.terminator(dialect, input, pos, eof);
        self.mode = Mode::RecordStart;
        Scan::Record { end: pos, consumed }
    }

    /// Returns the end of the terminator at `pos`.
    fn terminator(&mut self, dialect: &Dialect, input: &[u8], pos: usize, eof: bool) -> usize {
        if pos >= input.len() {
            return input.len();
        }
        if dialect.class(input[pos]) == CR {
            match input.get(pos + 1) {
                Some(b'\n') => return pos + 2,
                None if !eof => self.pending_lf = true,
                _ => {}
            }
        }
        pos + 1
    }
}

#[inline(always)]
fn is_space(byte: u8) -> bool {
    byte == b' ' || byte == b'\t'
}

/// Decodes the escapes (and doubled quotes) of a field into `out`.
pub(crate) fn unescape(dialect: &Dialect, text: &[u8], quoted: bool, out: &mut Vec<u8>) {
    out.clear();
    let escape = dialect.escape.byte();
    let mut pos = 0;
    while pos < text.len() {
        let byte = text[pos];
        pos += 1;
        if Some(byte) == escape {
            match text.get(pos) {
                Some(&next) => {
                    pos += 1;
                    out.push(match (dialect.escape, next) {
                        (Escape::Backslash, b't') => b'\t',
                        (Escape::Backslash, b'n') => b'\n',
                        (Escape::Backslash, b'r') => b'\r',
                        (Escape::Backslash, b'0') => b'\0',
                        (_, next) => next,
                    });
                }
                // an escape at the end of the input is taken as it is
                None => out.push(byte),
            }
        } else if quoted && Some(byte) == dialect.quote {
            // doubled quotes are a quote, single quotes (which only exist
            // with lenient quotes) are dropped
            if dialect.double_quote && text.get(pos) == Some(&byte) {
                pos += 1;
                out.push(byte);
            }
        } else {
            out.push(byte);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Returns the chunk sizes to feed an input of `len` bytes in.
    ///
    /// Miri is too slow for all sizes, it checks chunks of a byte (which
    /// split the input at every position), odd and aligned sizes and the
    /// whole input.
    fn chunk_sizes(len: usize) -> impl Iterator<Item = usize> {
        (1..=len).filter(move |&size| !cfg!(miri) || matches!(size, 1 | 3 | 8) || size == len)
    }

    fn dialect() -> Dialect {
        Dialect::new(
            b',',
            Some(b'"'),
            true,
            Escape::None,
            Terminator::Newline,
            None,
        )
        .unwrap()
    }

    const OPTIONS: Options = Options {
        trim: false,
        skip_blank_lines: true,
        lenient_quotes: false,
        max_record_len: usize::MAX,
    };

    /// Scans all records of an input given in chunks of `size` bytes.
    fn scan_all(input: &[u8], size: usize) -> Vec<Vec<String>> {
        let dialect = dialect();
        let mut scanner = Scanner::default();
        let mut rv = Vec::new();
        let mut start = 0;
        let mut available = size.min(input.len());
        loop {
            let eof = available == input.len();
            let chunk = &input[start..available];
            match scanner.scan(&dialect, chunk, eof, OPTIONS).unwrap() {
                Scan::Record { end: _, consumed } => {
                    let mut out = Vec::new();
                    rv.push(
                        scanner
                            .fields
                            .iter()
                            .map(|field| {
                                let text = &chunk[field.start..field.end];
                                if field.flags & UNESCAPE != 0 {
                                    unescape(&dialect, text, field.flags & QUOTED != 0, &mut out);
                                    String::from_utf8(out.clone()).unwrap()
                                } else {
                                    String::from_utf8(text.to_vec()).unwrap()
                                }
                            })
                            .collect(),
                    );
                    start += consumed;
                }
                Scan::Skip { consumed } => start += consumed,
                Scan::Incomplete => available = (available + size).min(input.len()),
                Scan::End => return rv,
            }
        }
    }

    #[test]
    fn test_scan_in_chunks() {
        let input = b"a,\"b\"\"c\",d\r\n\r\n\"x\ny\",,z\ry";
        let expected = vec![
            vec!["a".to_string(), "b\"c".into(), "d".into()],
            vec!["x\ny".into(), "".into(), "z".into()],
            vec!["y".into()],
        ];
        for size in chunk_sizes(input.len()) {
            assert_eq!(scan_all(input, size), expected, "size {}", size);
        }
    }

    #[test]
    fn test_has_special() {
        let dialect = dialect();
        assert!(!dialect.has_special(b""));
        assert!(!dialect.has_special(b"abcdefghijklmnop\x80\xff"));
        for special in *b",\"\n\r" {
            for len in 1..20 {
                for pos in 0..len {
                    let mut text = vec![b'x'; len];
                    text[pos] = special;
                    assert!(dialect.has_special(&text), "{:?}", text);
                }
            }
        }
    }

    #[test]
    fn test_invalid_dialects() {
        let err = Dialect::new(
            b',',
            Some(b','),
            true,
            Escape::None,
            Terminator::Newline,
            None,
        )
        .unwrap_err();
        assert_eq!(
            err.message(),
            "the quote character conflicts with another special character"
        );
        let err =
            Dialect::new(0xa7, None, true, Escape::None, Terminator::Newline, None).unwrap_err();
        assert_eq!(err.message(), "the delimiter must be an ASCII character");
    }
}