zsv 1.4.4-x86_64-linux-gnu

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +187 -0
  3. data/LICENSE +21 -0
  4. data/README.md +203 -0
  5. data/ext/zsv/ZSV_UPSTREAM +7 -0
  6. data/ext/zsv/common.h +22 -0
  7. data/ext/zsv/extconf.rb +102 -0
  8. data/ext/zsv/options.c +126 -0
  9. data/ext/zsv/options.h +28 -0
  10. data/ext/zsv/options_internal.h +8 -0
  11. data/ext/zsv/parser.c +503 -0
  12. data/ext/zsv/parser.h +75 -0
  13. data/ext/zsv/row.c +169 -0
  14. data/ext/zsv/row.h +45 -0
  15. data/ext/zsv/upstream.rb +55 -0
  16. data/ext/zsv/vendor/zsv/LICENSE +21 -0
  17. data/ext/zsv/vendor/zsv/VENDORED_FROM +2 -0
  18. data/ext/zsv/vendor/zsv/configure +1212 -0
  19. data/ext/zsv/vendor/zsv/include/zsv/api.h +375 -0
  20. data/ext/zsv/vendor/zsv/include/zsv/common.h +397 -0
  21. data/ext/zsv/vendor/zsv/include/zsv/ext/implementation.h +62 -0
  22. data/ext/zsv/vendor/zsv/include/zsv/ext/implementation_private.h +113 -0
  23. data/ext/zsv/vendor/zsv/include/zsv/ext/sheet.h +80 -0
  24. data/ext/zsv/vendor/zsv/include/zsv/ext.h +333 -0
  25. data/ext/zsv/vendor/zsv/include/zsv/utils/appname.h +51 -0
  26. data/ext/zsv/vendor/zsv/include/zsv/utils/arg.h +139 -0
  27. data/ext/zsv/vendor/zsv/include/zsv/utils/cache.h +49 -0
  28. data/ext/zsv/vendor/zsv/include/zsv/utils/clock.h +36 -0
  29. data/ext/zsv/vendor/zsv/include/zsv/utils/compiler.h +69 -0
  30. data/ext/zsv/vendor/zsv/include/zsv/utils/db.h +19 -0
  31. data/ext/zsv/vendor/zsv/include/zsv/utils/dirs.h +149 -0
  32. data/ext/zsv/vendor/zsv/include/zsv/utils/dl.h +22 -0
  33. data/ext/zsv/vendor/zsv/include/zsv/utils/emcc/fs_api.h +28 -0
  34. data/ext/zsv/vendor/zsv/include/zsv/utils/err.h +22 -0
  35. data/ext/zsv/vendor/zsv/include/zsv/utils/file-mem.h +18 -0
  36. data/ext/zsv/vendor/zsv/include/zsv/utils/file.h +125 -0
  37. data/ext/zsv/vendor/zsv/include/zsv/utils/jq.h +65 -0
  38. data/ext/zsv/vendor/zsv/include/zsv/utils/json.h +19 -0
  39. data/ext/zsv/vendor/zsv/include/zsv/utils/mem.h +19 -0
  40. data/ext/zsv/vendor/zsv/include/zsv/utils/memmem.h +13 -0
  41. data/ext/zsv/vendor/zsv/include/zsv/utils/os.h +145 -0
  42. data/ext/zsv/vendor/zsv/include/zsv/utils/output.h +34 -0
  43. data/ext/zsv/vendor/zsv/include/zsv/utils/overwrite.h +71 -0
  44. data/ext/zsv/vendor/zsv/include/zsv/utils/overwrite_writer.h +53 -0
  45. data/ext/zsv/vendor/zsv/include/zsv/utils/prop.h +119 -0
  46. data/ext/zsv/vendor/zsv/include/zsv/utils/rng.h +35 -0
  47. data/ext/zsv/vendor/zsv/include/zsv/utils/signal.h +25 -0
  48. data/ext/zsv/vendor/zsv/include/zsv/utils/sql.h +13 -0
  49. data/ext/zsv/vendor/zsv/include/zsv/utils/string.h +173 -0
  50. data/ext/zsv/vendor/zsv/include/zsv/utils/utf8.h +41 -0
  51. data/ext/zsv/vendor/zsv/include/zsv/utils/win/dl.h +25 -0
  52. data/ext/zsv/vendor/zsv/include/zsv/utils/writer.h +109 -0
  53. data/ext/zsv/vendor/zsv/include/zsv/zsv_export.h +33 -0
  54. data/ext/zsv/vendor/zsv/include/zsv.h.in +18 -0
  55. data/ext/zsv/vendor/zsv/src/Makefile +238 -0
  56. data/ext/zsv/vendor/zsv/src/vector_delim.c +60 -0
  57. data/ext/zsv/vendor/zsv/src/zsv.c +622 -0
  58. data/ext/zsv/vendor/zsv/src/zsv_internal.c +789 -0
  59. data/ext/zsv/vendor/zsv/src/zsv_scan_delim.c +285 -0
  60. data/ext/zsv/vendor/zsv/src/zsv_scan_delim_fast.c +741 -0
  61. data/ext/zsv/vendor/zsv/src/zsv_scan_fixed.c +88 -0
  62. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_avx2.h +89 -0
  63. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_neon.h +80 -0
  64. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_sse2.h +93 -0
  65. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_wasm.h +79 -0
  66. data/ext/zsv/vendor/zsv/src/zsv_strencode.c +84 -0
  67. data/ext/zsv/zsv_ext.c +417 -0
  68. data/lib/zsv/3.3/zsv.so +0 -0
  69. data/lib/zsv/3.4/zsv.so +0 -0
  70. data/lib/zsv/4.0/zsv.so +0 -0
  71. data/lib/zsv/errors.rb +32 -0
  72. data/lib/zsv/input.rb +88 -0
  73. data/lib/zsv/options.rb +132 -0
  74. data/lib/zsv/strict_validator.rb +124 -0
  75. data/lib/zsv/version.rb +5 -0
  76. data/lib/zsv.rb +183 -0
  77. metadata +196 -0
@@ -0,0 +1,741 @@
1
+ /*
2
+ * Branchless fast CSV scanner.
3
+ *
4
+ * Uses 64-byte SIMD processing to find delimiters and row-ends in bulk,
5
+ * with prefix-XOR carry propagation for branchless quote state tracking.
6
+ *
7
+ * In the common case (no quotes), cells are stored directly without
8
+ * going through cell_dl(), eliminating per-cell function call and
9
+ * branch overhead. Quoted cells still use cell_dl() for normalization.
10
+ *
11
+ * Platform-specific SIMD intrinsics are isolated in zsv_scan_simd_*.h
12
+ * headers, each providing a uniform interface:
13
+ * fast_vec_t — broadcast vector type
14
+ * fast_vec_set1(c) — broadcast byte c to all lanes
15
+ * fast_cmpeq_64(p,v)— compare 64 bytes at p against v, return 64-bit mask
16
+ *
17
+ * Falls back to the compat/scalar engine on unsupported platforms.
18
+ */
19
+
20
+ #if defined(__wasm_simd128__)
21
+ #include "zsv_scan_simd_wasm.h"
22
+ #define ZSV_FAST_PARSER_AVAILABLE 1
23
+ #pragma message "Using WebAssembly SIMD for fast CSV parsing"
24
+ #elif defined(__aarch64__)
25
+ #include "zsv_scan_simd_neon.h"
26
+ #define ZSV_FAST_PARSER_AVAILABLE 1
27
+ #elif defined(__AVX2__)
28
+ #include "zsv_scan_simd_avx2.h"
29
+ #define ZSV_FAST_PARSER_AVAILABLE 1
30
+ #elif defined(__x86_64__) || defined(_M_X64)
31
+ #include "zsv_scan_simd_sse2.h"
32
+ #define ZSV_FAST_PARSER_AVAILABLE 1
33
+ #endif
34
+
35
+ #ifdef ZSV_FAST_PARSER_AVAILABLE
36
+
37
+ /*
38
+ * Set scanner->quoted and scanner->quote_close_position for a cell
39
+ * by scanning its content. This replicates the quote tracking that
40
+ * the compat/scalar engine does character-by-character.
41
+ */
42
+ __attribute__((always_inline)) static inline void fast_set_quote_flags(struct zsv_scanner *scanner, unsigned char *s,
43
+ size_t n) {
44
+ scanner->quote_close_position = 0;
45
+ if (n == 0 || *s != '"') {
46
+ /* Not a quoted cell. Check for embedded quotes (quote in unquoted cell) */
47
+ if (n > 0 && memchr(s, '"', n))
48
+ scanner->quoted = ZSV_PARSER_QUOTE_EMBEDDED;
49
+ return;
50
+ }
51
+
52
+ /* Cell starts with a quote — find closing quote */
53
+ scanner->quoted = ZSV_PARSER_QUOTE_UNCLOSED;
54
+ scanner->quote_close_position = 0;
55
+
56
+ for (size_t i = 1; i < n; i++) {
57
+ if (s[i] == '"') {
58
+ if (i + 1 < n && s[i + 1] == '"') {
59
+ /* Embedded "" pair */
60
+ scanner->quoted |= ZSV_PARSER_QUOTE_NEEDED;
61
+ scanner->quoted |= ZSV_PARSER_QUOTE_EMBEDDED;
62
+ i++; /* skip second quote */
63
+ } else {
64
+ /* Closing quote */
65
+ scanner->quoted |= ZSV_PARSER_QUOTE_CLOSED;
66
+ scanner->quoted &= ~ZSV_PARSER_QUOTE_UNCLOSED;
67
+ scanner->quote_close_position = i;
68
+ break;
69
+ }
70
+ }
71
+ }
72
+ }
73
+
74
+ /*
75
+ * Out-of-line slow path for cell storage: handles column filtering,
76
+ * UTF-8 replacement, and cell_handler callbacks. Kept out of the hot
77
+ * SIMD loop to reduce code size and register pressure on x86-64.
78
+ */
79
+ __attribute__((noinline)) static void fast_store_cell_slow(struct zsv_scanner *scanner, unsigned char *s, size_t n) {
80
+ /* Column filter: skip unneeded columns */
81
+ if (scanner->needed_cols) {
82
+ if (scanner->row.used >= scanner->needed_cols_count || !scanner->needed_cols[scanner->row.used]) {
83
+ if (VERY_LIKELY(scanner->row.used < scanner->row.allocated)) {
84
+ struct zsv_cell c = {s, n, scanner->opts.no_quotes ? 1 : 0, 0};
85
+ scanner->row.cells[scanner->row.used++] = c;
86
+ } else
87
+ scanner->row.overflow++;
88
+ scanner->have_cell = 1;
89
+ return;
90
+ }
91
+ }
92
+ if (scanner->opts.malformed_utf8_replace || scanner->opts.malformed_utf8_handler)
93
+ n = zsv_cell_encode_utf8(scanner, s, n);
94
+ if (UNLIKELY(scanner->opts.cell_handler != NULL))
95
+ scanner->opts.cell_handler(scanner->opts.ctx, s, n);
96
+ if (VERY_LIKELY(scanner->row.used < scanner->row.allocated)) {
97
+ struct zsv_cell c = {s, n, scanner->opts.no_quotes ? 1 : 0, 0};
98
+ scanner->row.cells[scanner->row.used++] = c;
99
+ } else
100
+ scanner->row.overflow++;
101
+ scanner->have_cell = 1;
102
+ }
103
+
104
+ /*
105
+ * Store a cell directly into the row, bypassing cell_dl().
106
+ * Tiny inline fast path: just store pointer+length when no special
107
+ * processing is needed. Falls back to out-of-line slow path for
108
+ * column filtering, UTF-8 replacement, and cell_handler callbacks.
109
+ *
110
+ * The `need_slow` and `no_quotes` flags are pre-computed once at
111
+ * zsv_scan_delim_fast() entry and passed in to avoid re-checking
112
+ * scanner fields on every cell.
113
+ */
114
+ __attribute__((always_inline)) static inline void fast_store_cell(struct zsv_scanner *scanner, unsigned char *s,
115
+ size_t n, int need_slow, unsigned char no_quotes) {
116
+ if (UNLIKELY(need_slow)) {
117
+ fast_store_cell_slow(scanner, s, n);
118
+ return;
119
+ }
120
+ if (VERY_LIKELY(scanner->row.used < scanner->row.allocated)) {
121
+ struct zsv_cell c = {s, n, no_quotes, 0};
122
+ scanner->row.cells[scanner->row.used++] = c;
123
+ } else
124
+ scanner->row.overflow++;
125
+ scanner->have_cell = 1;
126
+ }
127
+
128
+ /*
129
+ * Cache-friendly cell store using local variables.
130
+ * Stores directly into the cells array via a cached pointer and index,
131
+ * avoiding repeated loads of scanner->row.used, scanner->row.cells,
132
+ * and scanner->row.allocated through the scanner pointer (which the
133
+ * compiler cannot hoist due to aliasing).
134
+ */
135
+ __attribute__((always_inline)) static inline void fast_store_cell_cached(struct zsv_cell *cells, size_t *used_p,
136
+ size_t allocated, unsigned char *s, size_t n,
137
+ unsigned char no_quotes) {
138
+ size_t u = *used_p;
139
+ if (VERY_LIKELY(u < allocated)) {
140
+ struct zsv_cell c = {s, n, no_quotes, 0};
141
+ cells[u] = c;
142
+ *used_p = u + 1;
143
+ }
144
+ /* overflow and have_cell handled by caller at row-end */
145
+ }
146
+
147
+ /*
148
+ * Out-of-line row-end handler: overflow warning, progress tracking,
149
+ * max_rows check. Kept out of the hot loop.
150
+ */
151
+ __attribute__((noinline)) static enum zsv_status fast_row_end_slow(struct zsv_scanner *scanner) {
152
+ if (VERY_UNLIKELY(scanner->row.overflow)) {
153
+ scanner->errprintf(scanner->errf, "Warning: number of columns (%zu) exceeds row max (%zu)\n",
154
+ scanner->row.allocated + scanner->row.overflow, scanner->row.allocated);
155
+ scanner->row.overflow = 0;
156
+ }
157
+ if (VERY_LIKELY(scanner->opts.row_handler != NULL))
158
+ scanner->opts.row_handler(scanner->opts.ctx);
159
+
160
+ #ifdef ZSV_EXTRAS
161
+ scanner->progress.cum_row_count++;
162
+ if (VERY_UNLIKELY(scanner->opts.progress.rows_interval &&
163
+ scanner->progress.cum_row_count % scanner->opts.progress.rows_interval == 0)) {
164
+ char ok;
165
+ if (!scanner->opts.progress.seconds_interval)
166
+ ok = 1;
167
+ else {
168
+ time_t now = time(NULL);
169
+ if (now > scanner->progress.last_time &&
170
+ (unsigned int)(now - scanner->progress.last_time) >= scanner->opts.progress.seconds_interval) {
171
+ ok = 1;
172
+ scanner->progress.last_time = now;
173
+ } else
174
+ ok = 0;
175
+ }
176
+ if (ok && scanner->opts.progress.callback)
177
+ scanner->abort = scanner->opts.progress.callback(scanner->opts.progress.ctx, scanner->progress.cum_row_count);
178
+ }
179
+ if (VERY_UNLIKELY(scanner->progress.max_rows > 0)) {
180
+ if (VERY_UNLIKELY(scanner->progress.cum_row_count == scanner->progress.max_rows)) {
181
+ scanner->abort = 1;
182
+ scanner->row.used = 0;
183
+ return zsv_status_max_rows_read;
184
+ }
185
+ }
186
+ #endif
187
+
188
+ if (VERY_UNLIKELY(scanner->abort))
189
+ return zsv_status_cancelled;
190
+ scanner->have_cell = 0;
191
+ scanner->row.used = 0;
192
+ return zsv_status_ok;
193
+ }
194
+
195
+ /*
196
+ * Store a cell and invoke the row handler, bypassing cell_dl()/row_dl().
197
+ * Returns non-zero status on abort/cancellation.
198
+ */
199
+ __attribute__((always_inline)) static inline enum zsv_status fast_store_cell_and_row(struct zsv_scanner *scanner,
200
+ unsigned char *s, size_t n,
201
+ int need_slow,
202
+ unsigned char no_quotes) {
203
+ fast_store_cell(scanner, s, n, need_slow, no_quotes);
204
+ return fast_row_end_slow(scanner);
205
+ }
206
+
207
+ /*
208
+ * Handle a row-end for an unquoted cell (fast path).
209
+ * Skips cell_dl()/row_dl() overhead.
210
+ */
211
+ #define FAST_ROWEND_NOQUOTE(scanner, buff, idx, is_cr, need_slow_, no_quotes_) \
212
+ do { \
213
+ if (!(is_cr)) { \
214
+ char prev = (idx) > 0 ? (buff)[(idx)-1] : (scanner)->last; \
215
+ if (prev == '\r') { \
216
+ (scanner)->cell_start = (idx) + 1; \
217
+ (scanner)->row_start = (idx) + 1; \
218
+ break; \
219
+ } \
220
+ } \
221
+ (scanner)->scanned_length = (idx); \
222
+ enum zsv_status stat_ = fast_store_cell_and_row((scanner), (buff) + (scanner)->cell_start, \
223
+ (idx) - (scanner)->cell_start, (need_slow_), (no_quotes_)); \
224
+ if (VERY_UNLIKELY(stat_)) \
225
+ return stat_; \
226
+ (scanner)->cell_start = (idx) + 1; \
227
+ (scanner)->row_start = (idx) + 1; \
228
+ (scanner)->data_row_count++; \
229
+ } while (0)
230
+
231
+ /*
232
+ * Handle a row-end for a potentially quoted cell.
233
+ * Sets quote flags, then calls cell_and_row_dl().
234
+ */
235
+ #define FAST_ROWEND_QUOTED(scanner, buff, idx, is_cr, quote_char) \
236
+ do { \
237
+ if (!(is_cr)) { \
238
+ char prev = (idx) > 0 ? (buff)[(idx)-1] : (scanner)->last; \
239
+ if (prev == '\r') { \
240
+ (scanner)->cell_start = (idx) + 1; \
241
+ (scanner)->row_start = (idx) + 1; \
242
+ break; \
243
+ } \
244
+ } \
245
+ (scanner)->scanned_length = (idx); \
246
+ if ((quote_char) > 0) \
247
+ fast_set_quote_flags((scanner), (buff) + (scanner)->cell_start, (idx) - (scanner)->cell_start); \
248
+ enum zsv_status stat_ = cell_and_row_dl((scanner), (buff) + (scanner)->cell_start, (idx) - (scanner)->cell_start); \
249
+ if (VERY_UNLIKELY(stat_)) \
250
+ return stat_; \
251
+ (scanner)->cell_start = (idx) + 1; \
252
+ (scanner)->row_start = (idx) + 1; \
253
+ (scanner)->data_row_count++; \
254
+ } while (0)
255
+
256
+ static enum zsv_status zsv_scan_delim_fast(struct zsv_scanner *scanner, unsigned char *buff, size_t bytes_read) {
257
+ /* Guard: fall back for unsupported configurations */
258
+ if (0
259
+ #ifndef ZSV_NO_ONLY_CRLF
260
+ || scanner->opts.only_crlf_rowend /* only-crlf mode — not yet supported */
261
+ #endif
262
+ ) {
263
+ return zsv_scan_delim(scanner, buff, bytes_read);
264
+ }
265
+
266
+ int quote_char = scanner->opts.no_quotes > 0 ? -1 : '"';
267
+
268
+ /* Pre-compute per-cell flags once, avoiding repeated field access in the hot loop. */
269
+ int need_slow = (scanner->needed_cols || scanner->opts.malformed_utf8_replace ||
270
+ scanner->opts.malformed_utf8_handler || scanner->opts.cell_handler)
271
+ ? 1
272
+ : 0;
273
+ unsigned char no_quotes = scanner->opts.no_quotes ? 1 : 0;
274
+
275
+ size_t i = scanner->partial_row_length;
276
+
277
+ /* Reset scanned_length so zsv_cum_scanned_length() doesn't double-count
278
+ * the previous buffer's value (which was already absorbed into
279
+ * cum_scanned_length at the start of zsv_parse_more). */
280
+ scanner->scanned_length = i;
281
+
282
+ /* If the entire buffer fits in the scalar tail and contains quotes,
283
+ * use the compat/scalar engine. The scalar tail's simplified quote handling
284
+ * combined with cell_dl's in-place memmove can produce incorrect results
285
+ * for small inputs with complex quoting. No performance impact since
286
+ * this only triggers for inputs < 64 bytes after partial row data. */
287
+
288
+ bytes_read += i;
289
+ scanner->partial_row_length = 0;
290
+ scanner->buffer_end = bytes_read;
291
+
292
+ /* Handle ZSV_PARSER_QUOTE_PENDING from previous buffer */
293
+ if (scanner->quoted & ZSV_PARSER_QUOTE_PENDING) {
294
+ scanner->quoted -= ZSV_PARSER_QUOTE_PENDING;
295
+ if (buff[i] != '"') {
296
+ scanner->quoted |= ZSV_PARSER_QUOTE_CLOSED;
297
+ scanner->quoted &= ~ZSV_PARSER_QUOTE_UNCLOSED;
298
+ scanner->quote_close_position = i - scanner->cell_start - 1;
299
+ } else {
300
+ scanner->quoted |= ZSV_PARSER_QUOTE_NEEDED;
301
+ scanner->quoted |= ZSV_PARSER_QUOTE_EMBEDDED;
302
+ i++;
303
+ }
304
+ }
305
+
306
+ int inside_quote = (scanner->quoted & ZSV_PARSER_QUOTE_UNCLOSED) ? 1 : 0;
307
+ char delimiter = scanner->opts.delimiter;
308
+
309
+ fast_vec_t v_comma = fast_vec_set1((unsigned char)delimiter);
310
+ fast_vec_t v_nl = fast_vec_set1('\n');
311
+ fast_vec_t v_cr = fast_vec_set1('\r');
312
+ fast_vec_t v_qt = fast_vec_set1(quote_char > 0 ? (unsigned char)quote_char : 0);
313
+
314
+ /*
315
+ * Skip-cells mode: no cell storage, just count rows.
316
+ * Used when the caller doesn't need cell data (e.g. count, skip-head).
317
+ *
318
+ * Uses SIMD to find row-ends and quotes in bulk. For blocks without
319
+ * quotes (common case), counts row-ends via popcount. For blocks
320
+ * with quotes, falls through to scalar processing.
321
+ */
322
+ if (scanner->skip_cells) {
323
+ while (i + 64 <= bytes_read) {
324
+ uint64_t commas, newlines, crs, quotes;
325
+ fast_scan_block(buff + i, v_comma, v_nl, v_cr, v_qt, &commas, &newlines, &crs, &quotes);
326
+ if (quote_char <= 0)
327
+ quotes = 0;
328
+
329
+ /* Unified path: prefix-XOR handles both quoted and unquoted blocks.
330
+ * When quotes==0 and !inside_quote: B=0, state_mask=0, all delims valid.
331
+ * When quotes or inside_quote: state_mask masks out delims inside quotes.
332
+ * This eliminates the branch between quoted/unquoted paths, reducing
333
+ * code size and improving branch prediction. */
334
+ {
335
+ uint64_t B = fast_prefix_xor(quotes);
336
+ uint64_t state_mask = inside_quote ? ~B : B;
337
+ inside_quote = (state_mask >> 63) & 1;
338
+
339
+ uint64_t valid_nl = newlines & ~state_mask;
340
+ uint64_t valid_cr = crs & ~state_mask;
341
+ uint64_t crlf_n = (valid_cr << 1) & valid_nl;
342
+ uint64_t row_ends = valid_cr | (valid_nl & ~crlf_n);
343
+
344
+ if (row_ends) {
345
+ /* Extract individual row-end positions via bit extraction so
346
+ * scanned_length and row_start are accurate per-row. This is
347
+ * needed for parallel boundary checks (zsv_cum_scanned_length). */
348
+ uint64_t re = row_ends;
349
+ while (re) {
350
+ int bit = __builtin_ctzll(re);
351
+ size_t rowend_pos = i + bit;
352
+ scanner->scanned_length = rowend_pos + 1;
353
+ scanner->row_start = rowend_pos + 1;
354
+ scanner->data_row_count++;
355
+ if (VERY_LIKELY(scanner->opts.row_handler != NULL))
356
+ scanner->opts.row_handler(scanner->opts.ctx);
357
+ scanner->have_cell = 0;
358
+ scanner->row.used = 0;
359
+ #ifdef ZSV_EXTRAS
360
+ scanner->progress.cum_row_count++;
361
+ if (VERY_UNLIKELY(scanner->progress.max_rows > 0 &&
362
+ scanner->progress.cum_row_count == scanner->progress.max_rows)) {
363
+ scanner->abort = 1;
364
+ return zsv_status_max_rows_read;
365
+ }
366
+ #endif
367
+ if (VERY_UNLIKELY(scanner->abort))
368
+ return zsv_status_cancelled;
369
+ re &= re - 1; /* clear lowest set bit */
370
+ }
371
+ int last_bit = 63 - __builtin_clzll(row_ends);
372
+ size_t last_rowend_pos = i + last_bit;
373
+ scanner->cell_start = last_rowend_pos + 1;
374
+ }
375
+
376
+ i += 64;
377
+ if (!scanner->skip_cells)
378
+ break;
379
+ continue;
380
+ }
381
+ }
382
+
383
+ /* Scalar tail for skip_cells: handles remaining bytes */
384
+ if (scanner->skip_cells) {
385
+ for (; i < bytes_read; i++) {
386
+ unsigned char c = buff[i];
387
+ if (c == '"' && quote_char > 0) {
388
+ if (inside_quote) {
389
+ if (i + 1 < bytes_read && buff[i + 1] == '"')
390
+ i++;
391
+ else
392
+ inside_quote = 0;
393
+ } else {
394
+ inside_quote = 1;
395
+ }
396
+ continue;
397
+ }
398
+ if (inside_quote)
399
+ continue;
400
+ if (c == '\r' || (c == '\n' && (i == 0 ? scanner->last != '\r' : buff[i - 1] != '\r'))) {
401
+ scanner->data_row_count++;
402
+ scanner->cell_start = i + 1;
403
+ scanner->row_start = i + 1;
404
+ scanner->scanned_length = i + 1;
405
+ if (VERY_LIKELY(scanner->opts.row_handler != NULL))
406
+ scanner->opts.row_handler(scanner->opts.ctx);
407
+ scanner->have_cell = 0;
408
+ scanner->row.used = 0;
409
+ #ifdef ZSV_EXTRAS
410
+ scanner->progress.cum_row_count++;
411
+ if (VERY_UNLIKELY(scanner->progress.max_rows > 0 &&
412
+ scanner->progress.cum_row_count == scanner->progress.max_rows)) {
413
+ scanner->abort = 1;
414
+ return zsv_status_max_rows_read;
415
+ }
416
+ #endif
417
+ if (VERY_UNLIKELY(scanner->abort))
418
+ return zsv_status_cancelled;
419
+ if (!scanner->skip_cells)
420
+ break;
421
+ } else if (c == '\n') {
422
+ scanner->cell_start = i + 1;
423
+ scanner->row_start = i + 1;
424
+ }
425
+ }
426
+ }
427
+
428
+ if (!scanner->skip_cells && i < bytes_read) {
429
+ /* Sync scanner->quoted with the locally-tracked inside_quote state.
430
+ * The SIMD skip_cells loop updates inside_quote via prefix-XOR but
431
+ * does not update scanner->quoted until function exit. When skip_cells
432
+ * is cleared mid-buffer (e.g. at a parallel chunk boundary), we must
433
+ * persist the current state before transitioning to normal_parse,
434
+ * which reads scanner->quoted for its own quote tracking. */
435
+ if (inside_quote)
436
+ scanner->quoted |= ZSV_PARSER_QUOTE_UNCLOSED;
437
+ else
438
+ scanner->quoted &= ~ZSV_PARSER_QUOTE_UNCLOSED;
439
+ goto normal_parse;
440
+ }
441
+
442
+ if (inside_quote)
443
+ scanner->quoted |= ZSV_PARSER_QUOTE_UNCLOSED;
444
+ else
445
+ scanner->quoted &= ~ZSV_PARSER_QUOTE_UNCLOSED;
446
+ scanner->scanned_length = i;
447
+ scanner->old_bytes_read = bytes_read;
448
+ return zsv_status_ok;
449
+ }
450
+
451
+ normal_parse:
452
+ /* Process 64 bytes at a time */
453
+ while (i + 64 <= bytes_read) {
454
+ uint64_t commas, newlines, crs, quotes;
455
+ fast_scan_block(buff + i, v_comma, v_nl, v_cr, v_qt, &commas, &newlines, &crs, &quotes);
456
+ if (quote_char <= 0)
457
+ quotes = 0;
458
+
459
+ uint64_t all_delims = commas | newlines | crs;
460
+
461
+ if (LIKELY(quotes == 0 && !inside_quote)) {
462
+ /*
463
+ * Fast path: no quotes in this 64-byte chunk and not inside a quoted
464
+ * cell. Store cells directly without going through cell_dl().
465
+ */
466
+ if (LIKELY(all_delims == 0)) {
467
+ i += 64;
468
+ continue;
469
+ }
470
+
471
+ size_t base = i;
472
+ i += 64;
473
+
474
+ /* Cache scanner fields in locals to avoid aliasing-induced reloads.
475
+ * The compiler can't hoist these because stores to cells[] could
476
+ * alias any scanner field through the same pointer. */
477
+ size_t cell_start_local = scanner->cell_start;
478
+ if (LIKELY(!need_slow)) {
479
+ struct zsv_cell *cells = scanner->row.cells;
480
+ size_t row_used = scanner->row.used;
481
+ size_t row_allocated = scanner->row.allocated;
482
+
483
+ while (all_delims) {
484
+ int bit = __builtin_ctzll(all_delims);
485
+ size_t idx = base + bit;
486
+ uint64_t bitmask = 1ULL << bit;
487
+ all_delims = fast_clear_lowest(all_delims);
488
+
489
+ if (LIKELY(bitmask & commas)) {
490
+ fast_store_cell_cached(cells, &row_used, row_allocated, buff + cell_start_local, idx - cell_start_local,
491
+ no_quotes);
492
+ cell_start_local = idx + 1;
493
+ } else if (bitmask & crs) {
494
+ scanner->row.used = row_used;
495
+ scanner->cell_start = cell_start_local;
496
+ FAST_ROWEND_NOQUOTE(scanner, buff, idx, 1, need_slow, no_quotes);
497
+ row_used = scanner->row.used;
498
+ cell_start_local = scanner->cell_start;
499
+ } else {
500
+ scanner->row.used = row_used;
501
+ scanner->cell_start = cell_start_local;
502
+ FAST_ROWEND_NOQUOTE(scanner, buff, idx, 0, need_slow, no_quotes);
503
+ row_used = scanner->row.used;
504
+ cell_start_local = scanner->cell_start;
505
+ }
506
+ }
507
+ scanner->row.used = row_used;
508
+ scanner->have_cell = 1;
509
+ } else {
510
+ /* Slow path: need_slow is set, use original per-cell function */
511
+ while (all_delims) {
512
+ int bit = __builtin_ctzll(all_delims);
513
+ size_t idx = base + bit;
514
+ uint64_t bitmask = 1ULL << bit;
515
+ all_delims = fast_clear_lowest(all_delims);
516
+
517
+ if (LIKELY(bitmask & commas)) {
518
+ scanner->scanned_length = idx;
519
+ scanner->cell_start = cell_start_local;
520
+ fast_store_cell_slow(scanner, buff + cell_start_local, idx - cell_start_local);
521
+ cell_start_local = idx + 1;
522
+ } else if (bitmask & crs) {
523
+ scanner->cell_start = cell_start_local;
524
+ FAST_ROWEND_QUOTED(scanner, buff, idx, 1, quote_char);
525
+ cell_start_local = scanner->cell_start;
526
+ } else {
527
+ scanner->cell_start = cell_start_local;
528
+ FAST_ROWEND_QUOTED(scanner, buff, idx, 0, quote_char);
529
+ cell_start_local = scanner->cell_start;
530
+ }
531
+ }
532
+ }
533
+ scanner->cell_start = cell_start_local;
534
+ continue;
535
+ }
536
+
537
+ /*
538
+ * Quote-aware path: use prefix-XOR to compute state_mask.
539
+ * Every quote toggles the inside/outside state.
540
+ * PCLMULQDQ on x86 reduces this to a single instruction. */
541
+ {
542
+ uint64_t B = fast_prefix_xor(quotes);
543
+ uint64_t state_mask = inside_quote ? ~B : B;
544
+ inside_quote = (state_mask >> 63) & 1;
545
+
546
+ uint64_t valid_delims = all_delims & ~state_mask;
547
+
548
+ /* Non-standard-CSV detection (COMPAT-parity check).
549
+ * COMPAT only treats `"` as a quote-toggle when the `"` is at a cell
550
+ * boundary (zsv_scan_delim.c:239 — i.e. immediately after a delim,
551
+ * \r, \n, or at scanner->cell_start). Prefix-XOR toggles on every `"`.
552
+ * A `"` that opens a quoted region per prefix-XOR but is NOT at a
553
+ * cell boundary signals non-standard input — the two engines will
554
+ * diverge on the rest of the buffer. Flag it so the cold path can
555
+ * fall back to COMPAT for byte-for-byte parity.
556
+ *
557
+ * opening_quotes: bits where state transitions outside→inside
558
+ * (= quotes & state_mask in current state convention).
559
+ * valid_prev: bits whose preceding byte is a delim/CR/LF, plus
560
+ * the cell_start position itself.
561
+ *
562
+ * Cost: ~4 bitops + a branch per block. The check piggybacks on
563
+ * already-loaded data (quotes, all_delims, state_mask). */
564
+ #ifndef ZSV_NO_NONSTANDARD_CHECK
565
+ if (UNLIKELY(quotes)) {
566
+ /* A bit is a "valid open position" if its preceding byte is a delim,
567
+ * CR, LF, OR another quote (the `""` escape sequence inside a quoted
568
+ * cell — prefix-XOR sees it as close+open, but it's standard CSV).
569
+ * Also valid: scanner->cell_start position itself. */
570
+ uint64_t opening_quotes = quotes & state_mask;
571
+ uint64_t valid_prev = (all_delims | quotes) << 1;
572
+ unsigned char prev_byte = i > 0 ? buff[i - 1] : (unsigned char)scanner->last;
573
+ if (prev_byte == (unsigned char)delimiter || prev_byte == '\r' || prev_byte == '\n' || prev_byte == '"')
574
+ valid_prev |= 1ULL;
575
+ if (scanner->cell_start >= i && scanner->cell_start < i + 64)
576
+ valid_prev |= 1ULL << (scanner->cell_start - i);
577
+ if (UNLIKELY(opening_quotes & ~valid_prev)) {
578
+ /* Non-standard input: opening quote not at a cell boundary.
579
+ * The fast engine cannot safely continue — bail out before any
580
+ * cells from this block are emitted, so already-delivered rows
581
+ * remain valid. Caller should switch to ZSV_MODE_COMPAT. */
582
+ scanner->nonstandard = 1;
583
+ scanner->abort = 1;
584
+ scanner->scanned_length = i;
585
+ scanner->old_bytes_read = bytes_read;
586
+ return zsv_status_nonstandard_csv;
587
+ }
588
+ }
589
+ #endif
590
+
591
+ if (LIKELY(valid_delims == 0)) {
592
+ i += 64;
593
+ continue;
594
+ }
595
+
596
+ size_t base = i;
597
+ i += 64;
598
+
599
+ /* Standard CSV: store cells raw (zero-copy passthrough).
600
+ * Prefix-XOR already identified correct delimiter positions,
601
+ * so cell data from cell_start to idx is the raw field content
602
+ * including any original quoting. The writer outputs it as-is,
603
+ * avoiding the normalize-then-re-quote overhead. */
604
+ size_t cell_start_q = scanner->cell_start;
605
+ if (LIKELY(!need_slow)) {
606
+ struct zsv_cell *cells = scanner->row.cells;
607
+ size_t row_used = scanner->row.used;
608
+ size_t row_allocated = scanner->row.allocated;
609
+
610
+ while (valid_delims) {
611
+ int bit = __builtin_ctzll(valid_delims);
612
+ size_t idx = base + bit;
613
+ uint64_t bitmask = 1ULL << bit;
614
+ valid_delims = fast_clear_lowest(valid_delims);
615
+
616
+ if (LIKELY(bitmask & commas)) {
617
+ fast_store_cell_cached(cells, &row_used, row_allocated, buff + cell_start_q, idx - cell_start_q, no_quotes);
618
+ cell_start_q = idx + 1;
619
+ } else if (bitmask & crs) {
620
+ scanner->row.used = row_used;
621
+ scanner->cell_start = cell_start_q;
622
+ FAST_ROWEND_NOQUOTE(scanner, buff, idx, 1, need_slow, no_quotes);
623
+ row_used = scanner->row.used;
624
+ cell_start_q = scanner->cell_start;
625
+ } else {
626
+ scanner->row.used = row_used;
627
+ scanner->cell_start = cell_start_q;
628
+ FAST_ROWEND_NOQUOTE(scanner, buff, idx, 0, need_slow, no_quotes);
629
+ row_used = scanner->row.used;
630
+ cell_start_q = scanner->cell_start;
631
+ }
632
+ }
633
+ scanner->row.used = row_used;
634
+ scanner->have_cell = 1;
635
+ } else {
636
+ while (valid_delims) {
637
+ int bit = __builtin_ctzll(valid_delims);
638
+ size_t idx = base + bit;
639
+ uint64_t bitmask = 1ULL << bit;
640
+ valid_delims = fast_clear_lowest(valid_delims);
641
+
642
+ if (LIKELY(bitmask & commas)) {
643
+ scanner->scanned_length = idx;
644
+ scanner->cell_start = cell_start_q;
645
+ fast_store_cell_slow(scanner, buff + cell_start_q, idx - cell_start_q);
646
+ cell_start_q = idx + 1;
647
+ } else if (bitmask & crs) {
648
+ scanner->cell_start = cell_start_q;
649
+ FAST_ROWEND_QUOTED(scanner, buff, idx, 1, quote_char);
650
+ cell_start_q = scanner->cell_start;
651
+ } else {
652
+ scanner->cell_start = cell_start_q;
653
+ FAST_ROWEND_QUOTED(scanner, buff, idx, 0, quote_char);
654
+ cell_start_q = scanner->cell_start;
655
+ }
656
+ }
657
+ }
658
+ scanner->cell_start = cell_start_q;
659
+ }
660
+ }
661
+
662
+ /* Scalar tail — process remaining bytes one at a time. */
663
+ for (; i < bytes_read; i++) {
664
+ unsigned char c = buff[i];
665
+
666
+ if (c == '"' && quote_char > 0) {
667
+ if (inside_quote) {
668
+ if (i + 1 < bytes_read && buff[i + 1] == '"') {
669
+ i++; /* skip escaped quote */
670
+ } else {
671
+ inside_quote = 0;
672
+ }
673
+ } else {
674
+ #ifndef ZSV_NO_NONSTANDARD_CHECK
675
+ /* Non-standard check: opening quote must be at a cell boundary OR
676
+ * immediately after another `"` (the `""` escape inside a quoted
677
+ * cell — closed at the previous byte, reopens here). */
678
+ if (UNLIKELY(i != scanner->cell_start)) {
679
+ unsigned char prev = i > 0 ? buff[i - 1] : (unsigned char)scanner->last;
680
+ if (prev != (unsigned char)delimiter && prev != '\r' && prev != '\n' && prev != '"') {
681
+ scanner->nonstandard = 1;
682
+ scanner->abort = 1;
683
+ scanner->scanned_length = i;
684
+ scanner->old_bytes_read = bytes_read;
685
+ return zsv_status_nonstandard_csv;
686
+ }
687
+ }
688
+ #endif
689
+ inside_quote = 1;
690
+ }
691
+ continue;
692
+ }
693
+
694
+ if (inside_quote)
695
+ continue;
696
+
697
+ if (c == delimiter) {
698
+ scanner->scanned_length = i;
699
+ fast_store_cell(scanner, buff + scanner->cell_start, i - scanner->cell_start, need_slow, no_quotes);
700
+ scanner->cell_start = i + 1;
701
+ } else if (c == '\r') {
702
+ FAST_ROWEND_NOQUOTE(scanner, buff, i, 1, need_slow, no_quotes);
703
+ } else if (c == '\n') {
704
+ FAST_ROWEND_NOQUOTE(scanner, buff, i, 0, need_slow, no_quotes);
705
+ }
706
+ }
707
+
708
+ /* Carry quote state across buffer boundaries.
709
+ *
710
+ * COMPAT-parity gate: COMPAT only treats `"` as a state-toggling quote
711
+ * when it appears AT the cell start (zsv_scan_delim.c:239); a `"` in the
712
+ * middle of an unquoted cell is a literal. Prefix-XOR cannot distinguish
713
+ * the two inside a 64-byte block and may end the buffer with
714
+ * inside_quote=1 even though the current cell never opened with a quote
715
+ * (e.g. cell starts `D~E.F.FD"..."""..`). Letting that spurious UNCLOSED
716
+ * out of this scan call is what drives zsv_finish's EOF fix-up into the
717
+ * cell_dl memmove path that overruns the buffer (zsv.c:465 / zsv_internal.c:312).
718
+ *
719
+ * Gate the carry on COMPAT's invariant: UNCLOSED is only valid when the
720
+ * current cell actually begins with the quote char. This runs once per
721
+ * scan call (cold path); the hot SIMD loop is untouched. */
722
+ if (inside_quote && scanner->cell_start < bytes_read && buff[scanner->cell_start] == (unsigned char)quote_char)
723
+ scanner->quoted |= ZSV_PARSER_QUOTE_UNCLOSED;
724
+ else
725
+ scanner->quoted &= ~ZSV_PARSER_QUOTE_UNCLOSED;
726
+
727
+ scanner->scanned_length = i;
728
+ scanner->old_bytes_read = bytes_read;
729
+ return zsv_status_ok;
730
+ }
731
+
732
+ #undef FAST_ROWEND_NOQUOTE
733
+ #undef FAST_ROWEND_QUOTED
734
+
735
+ #else /* !ZSV_FAST_PARSER_AVAILABLE */
736
+ /* Unsupported platform: fall back to compat/scalar engine */
737
+ #pragma message "Fast parser not available on this platform, falling back to compat/scalar engine"
738
+ static enum zsv_status zsv_scan_delim_fast(struct zsv_scanner *scanner, unsigned char *buff, size_t bytes_read) {
739
+ return zsv_scan_delim(scanner, buff, bytes_read);
740
+ }
741
+ #endif