-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreader.go
More file actions
464 lines (390 loc) · 13.8 KB
/
reader.go
File metadata and controls
464 lines (390 loc) · 13.8 KB
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
//go:build goexperiment.simd && amd64
// Package simdcsv provides a high-performance CSV parser using SIMD instructions.
// It is API-compatible with the standard library's encoding/csv package.
package simdcsv
import "io"
// ============================================================================
// Public Types
// ============================================================================
// Reader reads records from a CSV-encoded file.
//
// As returned by NewReader, a Reader expects input conforming to RFC 4180.
// The exported fields can be changed to customize the details before the
// first call to Read or ReadAll.
//
// # Configuration (Policy)
//
// Public fields control parsing behavior:
// - Comma, Comment: delimiter configuration
// - FieldsPerRecord: field count validation mode
// - LazyQuotes, TrimLeadingSpace: quote and whitespace handling
// - ReuseRecord: memory allocation strategy
//
// # Implementation (Mechanism)
//
// Internal state handles the actual parsing using SIMD-accelerated scanning
// and field extraction.
type Reader struct {
// Comma is the field delimiter (set to ',' by NewReader).
// Must be a valid rune and must not be \r, \n, or the Unicode replacement character (0xFFFD).
Comma rune
// Comment, if not 0, is the comment character.
// Lines beginning with Comment (without preceding whitespace) are ignored.
// With leading whitespace, the Comment character becomes part of the field,
// even if TrimLeadingSpace is true.
// Must be a valid rune, not \r, \n, 0xFFFD, and not equal to Comma.
Comment rune
// FieldsPerRecord is the number of expected fields per record.
// - Positive: Read requires each record to have exactly this many fields.
// - Zero: Read sets it to the first record's field count; subsequent records must match.
// - Negative: No check is made; records may have variable field counts.
FieldsPerRecord int
// LazyQuotes enables lenient parsing of quoted fields.
// If true, a quote may appear in an unquoted field and a
// non-doubled quote may appear in a quoted field.
LazyQuotes bool
// TrimLeadingSpace causes leading whitespace in fields to be ignored.
// This applies even if Comma is whitespace.
TrimLeadingSpace bool
// ReuseRecord controls whether Read may return a slice sharing
// the backing array of the previous call's returned slice.
// By default, each call returns newly allocated memory.
ReuseRecord bool
// source is the underlying data source.
source io.Reader
// state holds all mutable parsing state.
state readerState
// opts holds extended configuration options.
opts extendedOptions
}
// ReaderOptions contains extended configuration for Reader.
type ReaderOptions struct {
// SkipBOM removes UTF-8 BOM (EF BB BF) from the beginning of input if present.
SkipBOM bool
// MaxInputSize is the maximum allowed input size in bytes.
// - 0: Use DefaultMaxInputSize (2GB)
// - -1: Unlimited (not recommended for untrusted input)
// - >0: Custom limit
MaxInputSize int64
// BufferSize is the internal buffer size hint (not yet implemented).
BufferSize int
// ChunkSize is the parallel processing chunk size (not yet implemented).
ChunkSize int
// ZeroCopy enables zero-copy optimization (not yet implemented).
ZeroCopy bool
}
// ============================================================================
// Internal Types
// ============================================================================
// readerState holds the mutable state during parsing.
// Separating state from configuration makes the Reader easier to understand.
type readerState struct {
// Input state
offset int64
rawBuffer []byte
// Field position tracking for FieldPos()
fieldPositions []position
// Record reuse for ReuseRecord option
lastRecord []string
// Batch string allocation buffers
recordBuffer []byte
fieldEnds []int
// SIMD processing state
scanResult *scanResult
parseResult *parseResult
currentRecordIndex int
nonCommentRecordCount int
initialized bool
// Fast path flags from SIMD scan
hasQuotes bool
hasCR bool
chunkHasQuote []bool
}
// extendedOptions holds configuration beyond the standard encoding/csv API.
type extendedOptions struct {
skipBOM bool
maxInputSize int64
// Reserved for future streaming/chunked processing
bufferSize int
chunkSize int
zeroCopy bool
}
// position represents a position in the input.
type position struct {
line int
column int
}
// ============================================================================
// Constructors
// ============================================================================
// NewReader returns a new Reader that reads from r.
func NewReader(r io.Reader) *Reader {
return &Reader{
Comma: ',',
source: r,
}
}
// NewReaderWithOptions creates a Reader with extended options.
func NewReaderWithOptions(r io.Reader, opts ReaderOptions) *Reader {
reader := NewReader(r)
reader.opts = extendedOptions{
skipBOM: opts.SkipBOM,
maxInputSize: opts.MaxInputSize,
bufferSize: opts.BufferSize,
chunkSize: opts.ChunkSize,
zeroCopy: opts.ZeroCopy,
}
return reader
}
// ============================================================================
// Public API - Reading Records
// ============================================================================
// Read reads one record (a slice of fields) from r.
//
// Returns:
// - On unexpected field count: the record and ErrFieldCount
// - On parse error: a partial record (fields before the error) and the error
// - On EOF: nil and io.EOF
//
// If ReuseRecord is true, the returned slice may be shared between calls.
func (r *Reader) Read() (record []string, err error) {
if err := r.ensureInitialized(); err != nil {
return nil, err
}
return r.readNextRecord()
}
// ReadAll reads all remaining records from r.
// A successful call returns err == nil, not io.EOF.
// Empty input returns nil with no error (matching encoding/csv behavior).
func (r *Reader) ReadAll() (records [][]string, err error) {
if err := r.ensureInitialized(); err != nil {
return nil, err
}
for {
record, err := r.readNextRecord()
if err == io.EOF {
return records, nil
}
if err != nil {
return records, err
}
// Defer allocation until we have a record
if records == nil && r.state.parseResult != nil {
records = make([][]string, 0, len(r.state.parseResult.rows))
}
records = append(records, record)
}
}
// ============================================================================
// Public API - Position and Resource Management
// ============================================================================
// FieldPos returns the line and column (1-indexed) of the field at the given index
// in the most recently returned record. Columns are counted in bytes, not runes.
// Panics if the index is out of range.
func (r *Reader) FieldPos(field int) (line, column int) {
if field < 0 || field >= len(r.state.fieldPositions) {
panic("out of range index passed to FieldPos")
}
p := r.state.fieldPositions[field]
return p.line, p.column
}
// InputOffset returns the byte offset of the end of the most recently read row.
func (r *Reader) InputOffset() int64 {
return r.state.offset
}
// ============================================================================
// Internal - Record Reading
// ============================================================================
// readNextRecord reads and returns the next non-comment record.
// Returns io.EOF when no more records are available.
func (r *Reader) readNextRecord() ([]string, error) {
for {
if r.isAtEnd() {
return nil, io.EOF
}
rowIdx := r.state.currentRecordIndex
rowInfo := r.state.parseResult.rows[rowIdx]
r.state.currentRecordIndex++
// Skip comment lines
if r.Comment != 0 && r.isCommentLine(rowInfo, rowIdx) {
continue
}
// Fast path: no quotes anywhere, so no unescape/validation needed.
if !r.state.hasQuotes {
record := r.buildRecordNoQuotes(rowInfo)
if err := r.validateFieldCount(record, rowInfo); err != nil {
return record, err
}
r.state.nonCommentRecordCount++
return record, nil
}
record, err := r.buildRecordWithValidation(rowInfo, rowIdx)
if err != nil {
return record, err
}
if err := r.validateFieldCount(record, rowInfo); err != nil {
return record, err
}
r.state.nonCommentRecordCount++
return record, nil
}
}
// isAtEnd reports whether all records have been read.
func (r *Reader) isAtEnd() bool {
return r.state.parseResult == nil || r.state.currentRecordIndex >= len(r.state.parseResult.rows)
}
// ============================================================================
// Internal - Field Count Validation
// ============================================================================
// validateFieldCount checks if record has the expected number of fields.
//
// Policy modes:
// - Positive: strict validation against the configured count
// - Zero: auto-detect from first record, then enforce
// - Negative: no validation (variable field counts allowed)
func (r *Reader) validateFieldCount(record []string, rowInfo rowInfo) error {
// No validation mode
if r.FieldsPerRecord < 0 {
return nil
}
// Auto-detect mode: set expected count from first record
if r.FieldsPerRecord == 0 && r.isFirstNonCommentRecord() {
r.FieldsPerRecord = len(record)
return nil
}
// Validate against expected count
if len(record) != r.FieldsPerRecord {
return r.fieldCountError(rowInfo.lineNum)
}
return nil
}
// fieldCountError creates a ParseError for field count mismatch.
func (r *Reader) fieldCountError(lineNum int) *ParseError {
return &ParseError{
StartLine: lineNum,
Line: lineNum,
Column: 1,
Err: ErrFieldCount,
}
}
// ============================================================================
// Internal - Initialization
// ============================================================================
// ensureInitialized performs lazy initialization on first read.
func (r *Reader) ensureInitialized() error {
if r.state.initialized {
return nil
}
return r.initialize()
}
// initialize reads all input and runs SIMD scanning and parsing.
// This is a one-time operation that processes the entire input.
func (r *Reader) initialize() error {
r.state.initialized = true
if err := r.readInput(); err != nil {
return err
}
r.skipUTF8BOM()
// Empty input: no records
if len(r.state.rawBuffer) == 0 {
r.state.parseResult = parseResultPool.Get().(*parseResult)
r.state.parseResult.reset()
return nil
}
// Scan: structural analysis using SIMD (generates bitmasks)
r.state.scanResult = scanBuffer(r.state.rawBuffer, byte(r.Comma))
// Copy scan flags for fast path optimizations
r.state.hasQuotes = r.state.scanResult.hasQuotes
r.state.hasCR = r.state.scanResult.hasCR
r.copyChunkHasQuote()
// Parse: extract fields and rows from scan result
r.state.parseResult = parseBuffer(r.state.rawBuffer, r.state.scanResult)
// Release scanResult (no longer needed after parsing)
r.state.scanResult.release()
r.state.scanResult = nil
r.state.offset = int64(len(r.state.rawBuffer))
return nil
}
// ============================================================================
// Internal - Input Reading
// ============================================================================
// readInput reads the entire input into rawBuffer with size limiting.
func (r *Reader) readInput() error {
maxSize := r.opts.maxInputSize
if maxSize == 0 {
maxSize = DefaultMaxInputSize
}
// Try to determine input size for pre-allocation
var initialCap int64
if seeker, ok := r.source.(io.Seeker); ok {
if size, err := seeker.Seek(0, io.SeekEnd); err == nil {
initialCap = size
_, _ = seeker.Seek(0, io.SeekStart)
}
}
var err error
if maxSize > 0 {
// Enforce size limit
limited := io.LimitReader(r.source, maxSize+1)
r.state.rawBuffer, err = readAllWithPool(limited, initialCap)
if err != nil {
return err
}
if int64(len(r.state.rawBuffer)) > maxSize {
return ErrInputTooLarge
}
} else {
// No limit (maxSize == -1)
r.state.rawBuffer, err = readAllWithPool(r.source, initialCap)
}
return err
}
// readAllWithPool reads all data from r, pre-allocating if size is known.
// Returns a slice that may be from a pool (caller should return via releaseRawBuffer).
func readAllWithPool(r io.Reader, initialCap int64) ([]byte, error) {
// Try to determine size from common reader types
if initialCap == 0 {
switch sr := r.(type) {
case interface{ Len() int }:
initialCap = int64(sr.Len()) // strings.Reader, bytes.Reader, bytes.Buffer
case interface{ Size() int64 }:
initialCap = sr.Size() // strings.Reader also has Size()
}
}
// Pre-allocate if size is known
if initialCap > 0 {
buf := make([]byte, initialCap)
n, err := io.ReadFull(r, buf)
if err == io.ErrUnexpectedEOF || err == io.EOF {
return buf[:n], nil
}
return buf[:n], err
}
return io.ReadAll(r)
}
// ============================================================================
// Internal - BOM and Chunk Processing
// ============================================================================
// skipUTF8BOM removes the UTF-8 BOM (EF BB BF) from the beginning of rawBuffer if present.
func (r *Reader) skipUTF8BOM() {
if !r.opts.skipBOM || len(r.state.rawBuffer) < 3 {
return
}
if r.state.rawBuffer[0] == 0xEF && r.state.rawBuffer[1] == 0xBB && r.state.rawBuffer[2] == 0xBF {
r.state.rawBuffer = r.state.rawBuffer[3:]
}
}
// copyChunkHasQuote copies per-chunk quote presence flags for validation fast path.
func (r *Reader) copyChunkHasQuote() {
srcLen := len(r.state.scanResult.chunkHasQuote)
if srcLen == 0 {
r.state.chunkHasQuote = nil
return
}
if cap(r.state.chunkHasQuote) < srcLen {
r.state.chunkHasQuote = make([]bool, srcLen)
} else {
r.state.chunkHasQuote = r.state.chunkHasQuote[:srcLen]
}
copy(r.state.chunkHasQuote, r.state.scanResult.chunkHasQuote)
}