csveee 0.2.1

A very fast, parallel CSV parser.
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
//! DFA-side of `parse_from`: per-byte state machine + record emission.
//!
//! Plugs into [`crate::parser::driver::ChunkDriver`] via [`ChunkStepper`].
//! The driver owns fill/consume, UTF-8 validation, and offset shifts;
//! this file owns the inner hot loop, write-cursor compaction, field
//! accumulation, and EOF finalization for terminal DFA states.
//!
//! # Buffer access discipline
//!
//! Pointers cannot outlive a call (a grow relocates the buffer, a
//! consume can wrap it), so a record still open on return is demoted to
//! offsets in `pending_spans` and promoted back on the next entry.

use std::ops::ControlFlow;

use super::table::{Dfa, ERROR_STATE, FIRST_ACTION, RECORD_END_STATE, State, StructuralNeedles};
use crate::config::Config;
use crate::parser::driver::{ChunkStepper, StepCtx, StepResult};
use crate::parser::output::{self, Output};

/// DFA stepper. Holds all state that must survive between driver iterations.
pub(super) struct DfaStepper<'a> {
    /// Transition table and structural-byte needles for the configured dialect.
    dfa: &'a Dfa,
    /// Strip leading and trailing ASCII whitespace from each field before emitting.
    trim_fields: bool,
    /// Current DFA state, carried across refills so records may span them.
    dfa_state: State,
    /// Buffer offset where the current field started.
    field_start: usize,
    /// In-place compaction write cursor; lags the read pos once escapes are removed.
    write_pos: usize,
    /// Buffer offset of the record in flight; caps what the driver may consume.
    record_buf_start: usize,
    /// Fields of the record in flight; live only within one `step` / `finalize`.
    refs: Vec<(*mut u8, usize)>,
    /// `refs` demoted to offsets across a refill; never both non-empty.
    pending_spans: Vec<(usize, usize)>,
    /// Absolute file offset just past the last completed record.
    last_record_end: Option<usize>,
    /// Absolute file offset of the current record, for error reporting only.
    record_in_start: usize,
    /// The same, for the field in flight.
    field_in_start: usize,
    /// Arity the callback declares; `None` in variadic mode. Checked per record.
    expected_fields: Option<usize>,
}

impl<'a> DfaStepper<'a> {
    pub(super) fn new(dfa: &'a Dfa, config: &Config, start_offset: usize) -> Self {
        Self {
            dfa,
            trim_fields: config.trim,
            dfa_state: State::RecordStart,
            field_start: 0,
            write_pos: 0,
            record_buf_start: 0,
            record_in_start: start_offset,
            field_in_start: start_offset,
            refs: Vec::with_capacity(128),
            pending_spans: Vec::with_capacity(128),
            last_record_end: None,
            expected_fields: config.field_count,
        }
    }

    /// Promote spans carried across a refill to pointers against the
    /// current `base`. No-op in the steady state.
    #[inline]
    fn resume(&mut self, base: *mut u8) {
        if self.pending_spans.is_empty() {
            return;
        }
        self.refs.extend(
            self.pending_spans
                .drain(..)
                // SAFETY: `consume` tracks every consume, so each offset is still in bounds
                .map(|(off, len)| (unsafe { base.add(off) }, len)),
        );
    }

    /// Demote the record in flight to offsets before suspending back to
    /// the driver, whose next `fill` / `consume` may move the buffer.
    #[inline]
    fn suspend(&mut self, base: *mut u8) {
        if self.refs.is_empty() {
            return;
        }
        self.pending_spans.extend(
            self.refs
                .drain(..)
                // SAFETY: both derive from `base`, so the distance is in range and non-negative.
                .map(|(p, len)| (unsafe { p.offset_from(base) } as usize, len)),
        );
    }

    /// Push the current field span (`field_start..write_pos`, optionally
    /// trimmed) onto `refs`. Caller advances the cursors afterward.
    ///
    /// # Safety
    ///
    /// `base` must be the current buffer base and the span lie within it.
    #[inline]
    unsafe fn push_field(&mut self, base: *mut u8) {
        debug_assert!(self.field_start <= self.write_pos);
        let (mut off, mut len) = (self.field_start, self.write_pos - self.field_start);
        if self.trim_fields {
            // SAFETY: the span lies within the buffer and is not held across a write
            let s = unsafe { std::slice::from_raw_parts(base.add(off), len) };
            let t = s.trim_ascii();
            off += t.as_ptr() as usize - s.as_ptr() as usize;
            len = t.len();
        }
        self.refs.push((unsafe { base.add(off) }, len));
    }

    /// Point every field cursor at `pos`: the next field starts here
    /// and nothing has been written for it yet.
    #[inline]
    fn start_field_at(&mut self, total_consumed: usize, pos: usize) {
        self.field_start = pos;
        self.field_in_start = total_consumed + pos;
        self.write_pos = pos;
    }

    /// Scan-and-copy fast path for the two "content" states, where the
    /// DFA self-loops with `has_output = true` on every non-structural
    /// byte. Returns the new read position.
    ///
    /// # Safety
    ///
    /// `pos <= scan_end` must hold and `..scan_end` be readable and
    /// writable through `base`.
    #[inline(always)]
    unsafe fn scan_run(
        &mut self,
        base: *mut u8,
        pos: usize,
        scan_end: usize,
        needles: &StructuralNeedles,
    ) -> usize {
        // SAFETY: `pos <= scan_end`; not held across any write through `base`
        let haystack = unsafe { std::slice::from_raw_parts(base.add(pos), scan_end - pos) };
        let next_pos = match needles.find(haystack) {
            Some(off) => pos + off,
            None => scan_end,
        };
        let run_len = next_pos - pos;
        if run_len != 0 && self.write_pos != pos {
            // SAFETY: both ranges lie within `..scan_end`
            unsafe { std::ptr::copy(base.add(pos), base.add(self.write_pos), run_len) };
        }
        self.write_pos += run_len;
        next_pos
    }

    /// Check the field count of the in-flight record against [`Self::expected_fields`],
    #[inline]
    fn check_arity(&self) -> Result<(), crate::Error> {
        let Some(expected) = self.expected_fields else {
            return Ok(());
        };
        let found = self.refs.len();
        if found == expected {
            return Ok(());
        }
        Err(crate::Error::NumberOfFields {
            expected,
            found,
            position: crate::error::Position {
                byte_offset: self.record_in_start,
            },
        })
    }

    /// Arity-check the record in flight, then hand it to `emit`.
    #[inline]
    fn emit_record<O, E>(&mut self, emit: &mut E) -> Result<(), crate::Error>
    where
        O: Output + ?Sized,
        E: FnMut(&mut [&mut O]) -> Result<(), Box<dyn std::error::Error + Send + Sync>>,
    {
        self.check_arity()?;
        // SAFETY: a `&mut str` here is known-good UTF-8
        let res = unsafe { output::emit_record::<O, _>(&mut self.refs, emit) };
        self.refs.clear();
        res.map_err(crate::Error::from_user)
    }

    /// Close out the field the action state `s` landed on, and — for
    /// `RECORD_END_STATE` — the record with it. `pos` is one past the
    /// byte that triggered the action.
    ///
    /// `Break` ends the chunk: a parse error, or a record that closed on
    /// the chunk boundary. Nothing is suspended on the error path — the
    /// driver hands the error straight out of `run` and drops the stepper.
    ///
    /// # Safety
    ///
    /// `field_start <= write_pos <= pos <= scan_end`, all within `base`.
    #[inline(always)]
    unsafe fn apply_action<O, E>(
        &mut self,
        s: u8,
        base: *mut u8,
        pos: usize,
        remaining_in_chunk: usize,
        total_consumed: usize,
        emit: &mut E,
    ) -> ControlFlow<StepResult>
    where
        O: Output + ?Sized,
        E: FnMut(&mut [&mut O]) -> Result<(), Box<dyn std::error::Error + Send + Sync>>,
    {
        if s == ERROR_STATE {
            return ControlFlow::Break(StepResult::Error(crate::Error::InvalidQuote {
                position: crate::error::Position {
                    byte_offset: total_consumed + pos - 1,
                },
            }));
        }

        // SAFETY: `field_start <= write_pos <= scan_end`.
        unsafe { self.push_field(base) };
        self.start_field_at(total_consumed, pos);

        if s != RECORD_END_STATE {
            // FieldEnd — next field.
            self.dfa_state = State::FieldStart;
            return ControlFlow::Continue(());
        }

        if let Err(e) = self.emit_record::<O, E>(emit) {
            return ControlFlow::Break(StepResult::Error(e));
        }

        let anchor = pos - 1;

        let end = total_consumed + anchor;
        self.last_record_end = Some(end);
        self.record_buf_start = pos;
        self.start_field_at(total_consumed, pos);
        self.dfa_state = State::RecordStart;

        // The byte `find_record_start` stops on from the other side.
        if anchor >= remaining_in_chunk {
            debug_assert!(self.refs.is_empty(), "record in flight after emit");
            return ControlFlow::Break(StepResult::Done(end));
        }
        ControlFlow::Continue(())
    }

    /// How far the driver may consume once the scan suspends at `pos`.
    #[inline]
    fn consumable(&self, pos: usize) -> usize {
        if self.pending_spans.is_empty() && self.field_start == self.write_pos {
            pos
        } else {
            self.record_buf_start
        }
    }
}

impl<'a, O: Output + ?Sized> ChunkStepper<O> for DfaStepper<'a> {
    fn step<E>(&mut self, ctx: StepCtx<'_>, emit: &mut E) -> StepResult
    where
        E: FnMut(&mut [&mut O]) -> Result<(), Box<dyn std::error::Error + Send + Sync>>,
    {
        let StepCtx {
            buf,
            scan_start,
            scan_end,
            remaining_in_chunk,
            total_consumed,
        } = ctx;
        debug_assert!(scan_end <= buf.len());
        let base = buf.as_mut_ptr();
        let dfa = self.dfa;
        let mut pos = scan_start;

        self.resume(base);

        while pos < scan_end {
            match self.dfa_state {
                // SAFETY: `pos < scan_end <= buf.len()` from the loop condition
                State::InField => {
                    pos = unsafe { self.scan_run(base, pos, scan_end, dfa.in_field_needles()) }
                }
                State::InQuotedField => {
                    pos = unsafe { self.scan_run(base, pos, scan_end, dfa.in_quoted_needles()) }
                }

                // Bulk-skip empty-line terminators without going through the table
                State::RecordStart => {
                    let mask = dfa.record_start_structural();
                    let run_start = pos;
                    // SAFETY: `pos < scan_end` on every read.
                    while pos < scan_end && !mask[unsafe { *base.add(pos) } as usize] {
                        pos += 1;
                    }

                    let anchor = remaining_in_chunk.max(run_start);
                    if anchor < pos {
                        debug_assert!(self.refs.is_empty(), "record in flight at RecordStart");
                        let end = total_consumed + anchor;
                        self.last_record_end = Some(end);
                        return StepResult::Done(end);
                    }

                    self.record_in_start = total_consumed + pos;
                    self.field_in_start = total_consumed + pos;
                }
                _ => {}
            }
            // Only the arms above can land on `scan_end`; for the rest the
            // loop condition already rules it out.
            if pos >= scan_end {
                break;
            }

            // ── Main DFA step ────────────────────────────────────
            // SAFETY: `pos < scan_end` from the loop condition
            let byte = unsafe { *base.add(pos) };
            let trans = dfa.transition(self.dfa_state, byte);
            if trans.has_output() {
                if self.write_pos != pos {
                    // SAFETY: `write_pos <= pos < scan_end`.
                    unsafe { *base.add(self.write_pos) = byte };
                }
                self.write_pos += 1;
            }
            pos += 1;

            // Action states are never stored: `apply_action` installs the
            // interior state that follows, and the error path returns.
            let s = trans.next_raw();
            if s < FIRST_ACTION {
                self.dfa_state = trans.next_state();
                continue;
            }

            // SAFETY: `field_start <= write_pos <= pos <= scan_end`.
            if let ControlFlow::Break(result) = unsafe {
                self.apply_action::<O, E>(s, base, pos, remaining_in_chunk, total_consumed, emit)
            } {
                return result;
            }
        }

        // demote
        self.suspend(base);

        StepResult::Suspended {
            resume_at: pos,
            consumable: self.consumable(pos),
            last_record_end: self.last_record_end,
        }
    }

    fn consume(&mut self, n: usize) {
        self.field_start = self.field_start.saturating_sub(n);
        self.write_pos = self.write_pos.saturating_sub(n);
        self.record_buf_start = self.record_buf_start.saturating_sub(n);
        for span in self.pending_spans.iter_mut() {
            debug_assert!(span.0 >= n, "consumed past a pending field span");
            span.0 = span.0.saturating_sub(n);
        }
    }

    fn finalize<E>(&mut self, ctx: StepCtx<'_>, emit: &mut E) -> Result<Option<usize>, crate::Error>
    where
        E: FnMut(&mut [&mut O]) -> Result<(), Box<dyn std::error::Error + Send + Sync>>,
    {
        let StepCtx {
            buf,
            scan_end,
            total_consumed,
            ..
        } = ctx;

        match self.dfa_state {
            State::RecordStart | State::InComment => Ok(None),

            // Not `field_start`: a write cursor, so it lags a skipped run
            State::InQuotedField | State::InEscapedQuote => Err(crate::Error::UnclosedQuote {
                position: crate::error::Position {
                    byte_offset: self.field_in_start,
                },
            }),

            State::FieldStart | State::InField | State::AfterClosingQuote => {
                let base = buf.as_mut_ptr();
                self.resume(base);
                // SAFETY: `field_start <= write_pos <= scan_end`
                unsafe { self.push_field(base) };
                self.emit_record(emit)?;
                Ok(Some(total_consumed + scan_end))
            }

            // Action states never leak out of the hot loop — they're
            // handled inline and converted back to interior states.
            State::FieldEnd | State::RecordEnd | State::Error => {
                debug_assert!(
                    false,
                    "action state leaked past hot loop: {:?}",
                    self.dfa_state
                );
                Ok(None)
            }
        }
    }
}