zsv 1.4.3 → 1.4.4

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 (72) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +140 -2
  3. data/README.md +129 -275
  4. data/ext/zsv/ZSV_UPSTREAM +7 -0
  5. data/ext/zsv/common.h +0 -12
  6. data/ext/zsv/extconf.rb +70 -122
  7. data/ext/zsv/options.c +63 -62
  8. data/ext/zsv/options.h +8 -11
  9. data/ext/zsv/parser.c +390 -201
  10. data/ext/zsv/parser.h +35 -22
  11. data/ext/zsv/row.c +75 -28
  12. data/ext/zsv/row.h +15 -9
  13. data/ext/zsv/upstream.rb +55 -0
  14. data/ext/zsv/vendor/zsv/LICENSE +21 -0
  15. data/ext/zsv/vendor/zsv/VENDORED_FROM +2 -0
  16. data/ext/zsv/vendor/zsv/configure +1212 -0
  17. data/ext/zsv/vendor/zsv/include/zsv/api.h +375 -0
  18. data/ext/zsv/vendor/zsv/include/zsv/common.h +397 -0
  19. data/ext/zsv/vendor/zsv/include/zsv/ext/implementation.h +62 -0
  20. data/ext/zsv/vendor/zsv/include/zsv/ext/implementation_private.h +113 -0
  21. data/ext/zsv/vendor/zsv/include/zsv/ext/sheet.h +80 -0
  22. data/ext/zsv/vendor/zsv/include/zsv/ext.h +333 -0
  23. data/ext/zsv/vendor/zsv/include/zsv/utils/appname.h +51 -0
  24. data/ext/zsv/vendor/zsv/include/zsv/utils/arg.h +139 -0
  25. data/ext/zsv/vendor/zsv/include/zsv/utils/cache.h +49 -0
  26. data/ext/zsv/vendor/zsv/include/zsv/utils/clock.h +36 -0
  27. data/ext/zsv/vendor/zsv/include/zsv/utils/compiler.h +69 -0
  28. data/ext/zsv/vendor/zsv/include/zsv/utils/db.h +19 -0
  29. data/ext/zsv/vendor/zsv/include/zsv/utils/dirs.h +149 -0
  30. data/ext/zsv/vendor/zsv/include/zsv/utils/dl.h +22 -0
  31. data/ext/zsv/vendor/zsv/include/zsv/utils/emcc/fs_api.h +28 -0
  32. data/ext/zsv/vendor/zsv/include/zsv/utils/err.h +22 -0
  33. data/ext/zsv/vendor/zsv/include/zsv/utils/file-mem.h +18 -0
  34. data/ext/zsv/vendor/zsv/include/zsv/utils/file.h +125 -0
  35. data/ext/zsv/vendor/zsv/include/zsv/utils/jq.h +65 -0
  36. data/ext/zsv/vendor/zsv/include/zsv/utils/json.h +19 -0
  37. data/ext/zsv/vendor/zsv/include/zsv/utils/mem.h +19 -0
  38. data/ext/zsv/vendor/zsv/include/zsv/utils/memmem.h +13 -0
  39. data/ext/zsv/vendor/zsv/include/zsv/utils/os.h +145 -0
  40. data/ext/zsv/vendor/zsv/include/zsv/utils/output.h +34 -0
  41. data/ext/zsv/vendor/zsv/include/zsv/utils/overwrite.h +71 -0
  42. data/ext/zsv/vendor/zsv/include/zsv/utils/overwrite_writer.h +53 -0
  43. data/ext/zsv/vendor/zsv/include/zsv/utils/prop.h +119 -0
  44. data/ext/zsv/vendor/zsv/include/zsv/utils/rng.h +35 -0
  45. data/ext/zsv/vendor/zsv/include/zsv/utils/signal.h +25 -0
  46. data/ext/zsv/vendor/zsv/include/zsv/utils/sql.h +13 -0
  47. data/ext/zsv/vendor/zsv/include/zsv/utils/string.h +173 -0
  48. data/ext/zsv/vendor/zsv/include/zsv/utils/utf8.h +41 -0
  49. data/ext/zsv/vendor/zsv/include/zsv/utils/win/dl.h +25 -0
  50. data/ext/zsv/vendor/zsv/include/zsv/utils/writer.h +109 -0
  51. data/ext/zsv/vendor/zsv/include/zsv/zsv_export.h +33 -0
  52. data/ext/zsv/vendor/zsv/include/zsv.h.in +18 -0
  53. data/ext/zsv/vendor/zsv/src/Makefile +238 -0
  54. data/ext/zsv/vendor/zsv/src/vector_delim.c +60 -0
  55. data/ext/zsv/vendor/zsv/src/zsv.c +622 -0
  56. data/ext/zsv/vendor/zsv/src/zsv_internal.c +789 -0
  57. data/ext/zsv/vendor/zsv/src/zsv_scan_delim.c +285 -0
  58. data/ext/zsv/vendor/zsv/src/zsv_scan_delim_fast.c +741 -0
  59. data/ext/zsv/vendor/zsv/src/zsv_scan_fixed.c +88 -0
  60. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_avx2.h +89 -0
  61. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_neon.h +80 -0
  62. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_sse2.h +93 -0
  63. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_wasm.h +79 -0
  64. data/ext/zsv/vendor/zsv/src/zsv_strencode.c +84 -0
  65. data/ext/zsv/zsv_ext.c +176 -105
  66. data/lib/zsv/errors.rb +32 -0
  67. data/lib/zsv/input.rb +88 -0
  68. data/lib/zsv/options.rb +132 -0
  69. data/lib/zsv/strict_validator.rb +124 -0
  70. data/lib/zsv/version.rb +1 -1
  71. data/lib/zsv.rb +137 -35
  72. metadata +82 -11
@@ -0,0 +1,622 @@
1
+ /*
2
+ * Copyright (C) 2021 Tai Chi Minh Ralph Eastwood (self), Matt Wong (Guarnerix Inc dba Liquidaty)
3
+ * All rights reserved
4
+ *
5
+ * This file is part of zsv/lib, distributed under the license defined at
6
+ * https://opensource.org/licenses/MIT
7
+ */
8
+
9
+ #ifndef ZSV_MIN_SCANNER_BUFFSIZE
10
+ #define ZSV_MIN_SCANNER_BUFFSIZE 4096
11
+ #endif
12
+
13
+ #include "zsv.h"
14
+ #include <zsv/utils/compiler.h>
15
+ #ifdef ZSV_EXTRAS
16
+ #include <zsv/utils/arg.h>
17
+ #endif
18
+
19
+ static struct zsv_cell zsv_get_cell_1(zsv_parser parser, size_t ix);
20
+ static struct zsv_cell zsv_get_cell_with_overwrite(zsv_parser parser, size_t col_ix);
21
+ #include "zsv_internal.c"
22
+
23
+ #ifndef ZSV_VERSION
24
+ #define ZSV_VERSION "unknown"
25
+ #endif
26
+
27
+ ZSV_EXPORT
28
+ const char *zsv_lib_version(void) {
29
+ return ZSV_VERSION;
30
+ }
31
+
32
+ #include "zsv_strencode.c"
33
+
34
+ /**
35
+ * When we parse a chunk, if it was not the first parse call, we might have a partial
36
+ * row at the end of our buffer that must be moved. The reason we do this at the beginning
37
+ * of a parse and not at the end of the prior parse is so that between chunks, the input
38
+ * chunk remains available in a contiguous block of one or more rows
39
+ */
40
+ // __attribute__((always_inline))
41
+ inline static enum zsv_status scanner_pre_parse(struct zsv_scanner *scanner, size_t *capacityp) {
42
+ scanner->last = '\0';
43
+ if (VERY_LIKELY(scanner->old_bytes_read)) {
44
+ scanner->last = scanner->buff.buff[scanner->old_bytes_read - 1];
45
+ if (scanner->row_start < scanner->old_bytes_read) {
46
+ size_t len = scanner->old_bytes_read - scanner->row_start;
47
+ memmove(scanner->buff.buff, scanner->buff.buff + scanner->row_start, len);
48
+ scanner->partial_row_length = len;
49
+ } else {
50
+ scanner->cell_start = 0;
51
+ scanner->row_start = 0;
52
+ zsv_clear_cell(scanner);
53
+ }
54
+ scanner->cell_start -= scanner->row_start;
55
+ for (size_t i2 = 0; i2 < scanner->row.used; i2++)
56
+ scanner->row.cells[i2].str -= scanner->row_start;
57
+ scanner->row_start = 0;
58
+ scanner->old_bytes_read = 0;
59
+ }
60
+
61
+ scanner->cum_scanned_length += scanner->scanned_length - scanner->partial_row_length;
62
+
63
+ size_t capacity = scanner->buff.size - scanner->partial_row_length;
64
+ if (VERY_UNLIKELY(capacity == 0)) {
65
+ // our row size was too small to fit a single row of data
66
+ scanner->errprintf(scanner->errf, "Warning: row %zu truncated\n", scanner->data_row_count);
67
+ if (scanner->mode == ZSV_MODE_FIXED) {
68
+ if (VERY_UNLIKELY(row_fx(scanner, scanner->buff.buff, 0, scanner->buff.size)))
69
+ return zsv_status_cancelled;
70
+ } else if (VERY_UNLIKELY(row_dl(scanner)))
71
+ return zsv_status_cancelled;
72
+
73
+ // throw away the next row end
74
+ scanner->buffer_exceeded = 1;
75
+ scanner->opts.row_handler = zsv_throwaway_row;
76
+ scanner->opts.ctx = scanner;
77
+
78
+ scanner->partial_row_length = 0;
79
+ capacity = scanner->buff.size;
80
+ }
81
+ if (capacityp)
82
+ *capacityp = capacity;
83
+ return zsv_status_ok;
84
+ }
85
+
86
+ /**
87
+ * apply --header-row option
88
+ */
89
+ static enum zsv_status zsv_insert_string(struct zsv_scanner *scanner) {
90
+ // to do: replace below with
91
+ // return parse_bytes(scanner, bytes, len);
92
+ size_t len = strlen(scanner->insert_string);
93
+ if (len > scanner->buff.size - scanner->partial_row_length)
94
+ len = scanner->buff.size - 1; // to do: throw an error instead
95
+ memcpy(scanner->buff.buff + scanner->partial_row_length, scanner->insert_string, len);
96
+ if (scanner->buff.buff[len] != '\n')
97
+ scanner->buff.buff[len] = '\n';
98
+ enum zsv_status stat = zsv_scan(scanner, scanner->buff.buff, len + 1);
99
+ scanner->insert_string = NULL;
100
+ return stat;
101
+ }
102
+
103
+ /**
104
+ * Read the next chunk of data from our input stream and parse it, calling our
105
+ * custom handlers as each cell and row are parsed
106
+ */
107
+ ZSV_EXPORT
108
+ enum zsv_status zsv_parse_more(struct zsv_scanner *scanner) {
109
+ if (VERY_UNLIKELY(scanner->insert_string != NULL))
110
+ zsv_insert_string(scanner);
111
+
112
+ size_t capacity;
113
+ enum zsv_status stat = scanner_pre_parse(scanner, &capacity);
114
+ if (stat != zsv_status_ok)
115
+ return stat;
116
+ size_t bytes_read;
117
+ if (VERY_UNLIKELY(scanner->checked_bom == 0)) {
118
+ #ifdef ZSV_EXTRAS
119
+ // initialize progress timer
120
+ if (scanner->opts.progress.seconds_interval)
121
+ scanner->progress.last_time = time(NULL);
122
+ #endif
123
+ size_t bom_len = strlen(ZSV_BOM);
124
+ scanner->checked_bom = 1;
125
+ if ((bytes_read = scanner->read(scanner->buff.buff, 1, bom_len, scanner->in)) == bom_len &&
126
+ !memcmp(scanner->buff.buff, ZSV_BOM, bom_len)) {
127
+ // have bom. disregard what we just read
128
+ bytes_read = scanner->read(scanner->buff.buff, 1, capacity, scanner->in);
129
+ scanner->had_bom = 1;
130
+ } else { // no BOM. keep the bytes we just read
131
+ // bytes_read = bom_len + scanner->read(scanner->buff.buff + bom_len, 1, capacity - bom_len, scanner->in);
132
+ if (bytes_read == bom_len) // maybe we only read < 3 bytes
133
+ bytes_read += scanner->read(scanner->buff.buff + bom_len, 1, capacity - bom_len, scanner->in);
134
+ }
135
+ } else // already checked bom. read as usual
136
+ bytes_read = scanner->read(scanner->buff.buff + scanner->partial_row_length, 1, capacity, scanner->in);
137
+ scanner->started = 1;
138
+ if (VERY_UNLIKELY(scanner->filter != NULL))
139
+ bytes_read = scanner->filter(scanner->filter_ctx, scanner->buff.buff + scanner->partial_row_length, bytes_read);
140
+ if (VERY_LIKELY(bytes_read))
141
+ return zsv_scan(scanner, scanner->buff.buff, bytes_read);
142
+
143
+ scanner->scanned_length = scanner->partial_row_length;
144
+ return zsv_status_no_more_input;
145
+ }
146
+
147
+ ZSV_EXPORT
148
+ enum zsv_status zsv_set_column_filter(zsv_parser parser, const unsigned int *col_indices, unsigned int count) {
149
+ if (parser->needed_cols) {
150
+ free(parser->needed_cols);
151
+ parser->needed_cols = NULL;
152
+ parser->needed_cols_count = 0;
153
+ }
154
+ if (!col_indices || !count)
155
+ return zsv_status_ok;
156
+
157
+ unsigned int max_col = parser->row.allocated;
158
+ parser->needed_cols = calloc(max_col, 1);
159
+ if (!parser->needed_cols)
160
+ return zsv_status_memory;
161
+ parser->needed_cols_count = max_col;
162
+ for (unsigned int i = 0; i < count; i++) {
163
+ if (col_indices[i] < max_col)
164
+ parser->needed_cols[col_indices[i]] = 1;
165
+ }
166
+ return zsv_status_ok;
167
+ }
168
+
169
+ ZSV_EXPORT
170
+ void zsv_set_skip_cells(zsv_parser parser, int skip) {
171
+ parser->skip_cells = skip ? 1 : 0;
172
+ }
173
+
174
+ ZSV_EXPORT
175
+ void zsv_abort(zsv_parser parser) {
176
+ parser->abort = 1;
177
+ }
178
+
179
+ ZSV_EXPORT
180
+ char zsv_row_is_blank(zsv_parser parser) {
181
+ return zsv_internal_row_is_blank(parser);
182
+ }
183
+
184
+ static void zsv_pull_row(void *ctx) {
185
+ zsv_parser parser = ctx;
186
+ parser->pull.now = 1;
187
+ parser->pull.row_used = parser->row.used;
188
+ }
189
+
190
+ /**
191
+ * For pull parsing, use zsv_next_row(). Not quite as fast as push parsing, but pretty close
192
+ * @return zsv_status_row on success
193
+ */
194
+ ZSV_EXPORT
195
+ enum zsv_status zsv_next_row(zsv_parser parser) {
196
+ if (VERY_UNLIKELY(!parser->pull.regs)) {
197
+ if (parser->started)
198
+ return zsv_status_error; // error: already started a push parser
199
+ if (!(parser->pull.regs = calloc(1, sizeof(*parser->pull.regs))))
200
+ return zsv_status_memory;
201
+ parser->mode = ZSV_MODE_DELIM_PULL;
202
+ zsv_set_row_handler(parser, zsv_pull_row);
203
+ zsv_set_context(parser, parser);
204
+ if (parser->insert_string != NULL)
205
+ parser->pull.stat = zsv_insert_string(parser);
206
+ if (parser->pull.stat == zsv_status_row)
207
+ return parser->pull.stat;
208
+ }
209
+ if (VERY_LIKELY(parser->pull.stat == zsv_status_row))
210
+ parser->pull.stat = zsv_scan_delim_pull(parser, parser->pull.buff, parser->pull.bytes_read);
211
+ if (VERY_UNLIKELY(parser->pull.stat == zsv_status_ok)) {
212
+ do {
213
+ parser->pull.stat = zsv_parse_more(parser); // should return zsv_status_row or zsv_status_no_more_input
214
+ } while (parser->pull.stat == zsv_status_ok);
215
+ if (VERY_LIKELY(parser->pull.stat == zsv_status_row))
216
+ return parser->pull.stat;
217
+ }
218
+
219
+ if (VERY_UNLIKELY(parser->pull.stat == zsv_status_no_more_input)) {
220
+ parser->pull.now = 0;
221
+ zsv_finish(parser);
222
+
223
+ parser->pull.stat = zsv_status_done;
224
+ if (parser->pull.now) {
225
+ parser->pull.now = 0;
226
+ parser->row.used = parser->pull.row_used;
227
+ return zsv_status_row;
228
+ }
229
+ }
230
+ #ifdef ZSV_EXTRAS
231
+ if (VERY_UNLIKELY(parser->pull.stat == zsv_status_max_rows_read)) {
232
+ parser->pull.now = 0;
233
+ parser->row.used = parser->pull.row_used;
234
+ parser->pull.stat = zsv_status_done;
235
+ return zsv_status_row;
236
+ }
237
+ #endif
238
+ return parser->pull.stat;
239
+ }
240
+
241
+ // to do: rename to zsv_column_count(). rename all other zsv_hand to just zsv_
242
+ ZSV_EXPORT
243
+ size_t zsv_cell_count(zsv_parser parser) {
244
+ return parser->row.used;
245
+ }
246
+
247
+ ZSV_EXPORT
248
+ void zsv_set_row_handler(zsv_parser parser, void (*row_handler)(void *ctx)) {
249
+ if (parser->opts.row_handler == parser->opts_orig.row_handler)
250
+ parser->opts.row_handler = row_handler;
251
+ parser->opts_orig.row_handler = row_handler;
252
+ }
253
+
254
+ ZSV_EXPORT
255
+ void zsv_set_context(zsv_parser parser, void *ctx) {
256
+ if (parser->opts.ctx == parser->opts_orig.ctx)
257
+ parser->opts.ctx = ctx;
258
+ parser->opts_orig.ctx = ctx;
259
+ }
260
+
261
+ ZSV_EXPORT
262
+ void zsv_set_read(zsv_parser parser, size_t (*read_func)(void *restrict, size_t n, size_t size, void *restrict)) {
263
+ parser->read = read_func;
264
+ }
265
+
266
+ ZSV_EXPORT
267
+ void zsv_set_input(zsv_parser parser, void *in) {
268
+ parser->in = in;
269
+ }
270
+
271
+ ZSV_EXPORT
272
+ char zsv_quoted(zsv_parser parser) {
273
+ return parser->quoted || parser->opts.no_quotes;
274
+ }
275
+
276
+ static struct zsv_cell zsv_get_cell_1(zsv_parser parser, size_t ix) {
277
+ if (VERY_LIKELY(ix < parser->row.used))
278
+ return parser->row.cells[ix];
279
+
280
+ struct zsv_cell c = {0, 0, 0, 0};
281
+ return c;
282
+ }
283
+
284
+ // to do: benchmark returning zsv_cell struct vs just a zsv_cell pointer
285
+ ZSV_EXPORT
286
+ struct zsv_cell zsv_get_cell(zsv_parser parser, size_t ix) {
287
+ return parser->get_cell(parser, ix);
288
+ }
289
+
290
+ /**
291
+ * `zsv_get_cell_len()` is not needed in most cases, but may be useful in
292
+ * restrictive cases such as when calling from Javascript into wasm
293
+ */
294
+ ZSV_EXPORT
295
+ size_t zsv_get_cell_len(zsv_parser parser, size_t ix) {
296
+ if (ix < parser->row.used)
297
+ return parser->row.cells[ix].len;
298
+ return 0;
299
+ }
300
+
301
+ ZSV_EXPORT
302
+ unsigned char *zsv_get_cell_str(zsv_parser parser, size_t ix) {
303
+ struct zsv_cell c = zsv_get_cell_1(parser, ix);
304
+ return c.len ? c.str : NULL;
305
+ }
306
+
307
+ ZSV_EXPORT enum zsv_status zsv_set_fixed_offsets(zsv_parser parser, size_t count, size_t *offsets) {
308
+ if (!count) {
309
+ parser->errprintf(parser->errf, "Fixed offset count must be greater than zero\n");
310
+ return zsv_status_invalid_option;
311
+ }
312
+ if (!offsets) {
313
+ parser->errprintf(parser->errf, "zsv_set_fixed_offsets: invalid NULL offsets argument\n");
314
+ return zsv_status_invalid_option;
315
+ }
316
+ if (offsets[0] == 0)
317
+ parser->errprintf(parser->errf, "Warning: first cell width is zero\n");
318
+ for (size_t i = 1; i < count; i++) {
319
+ if (offsets[i - 1] > offsets[i]) {
320
+ parser->errprintf(parser->errf, "Invalid offset %zu may not exceed prior offset %zu\n", offsets[i],
321
+ offsets[i - 1]);
322
+ return zsv_status_invalid_option;
323
+ } else if (offsets[i - 1] == offsets[i])
324
+ parser->errprintf(parser->errf, "Warning: offset %zu repeated, will always yield empty cell\n", offsets[i - 1]);
325
+ }
326
+
327
+ if (offsets[count - 1] > parser->buff.size) {
328
+ parser->errprintf(parser->errf, "Offset %zu exceeds total buffer size %zu\n", offsets[count - 1],
329
+ parser->buff.size);
330
+ return zsv_status_invalid_option;
331
+ }
332
+ if (parser->cum_scanned_length) {
333
+ parser->errprintf(parser->errf, "Scanner mode cannot be changed after parsing has begun\n");
334
+ return zsv_status_invalid_option;
335
+ }
336
+
337
+ free(parser->fixed.offsets);
338
+ parser->fixed.offsets = calloc(count, sizeof(*parser->fixed.offsets));
339
+ if (parser->fixed.offsets == NULL) {
340
+ parser->errprintf(parser->errf, "Out of memory!\n");
341
+ return zsv_status_memory;
342
+ }
343
+ parser->fixed.count = count;
344
+ for (unsigned i = 0; i < count; i++)
345
+ parser->fixed.offsets[i] = offsets[i];
346
+
347
+ parser->mode = ZSV_MODE_FIXED;
348
+ parser->checked_bom = 1;
349
+
350
+ set_callbacks(parser);
351
+
352
+ return zsv_status_ok;
353
+ }
354
+
355
+ ZSV_EXPORT
356
+ int zsv_peek(zsv_parser z) {
357
+ if (z->scanned_length + 1 < z->buff.size)
358
+ return z->buff.buff[z->scanned_length + 1];
359
+ return -1;
360
+ }
361
+
362
+ /**
363
+ * Create a zsv parser
364
+ * @param opts
365
+ * @returns parser handle
366
+ */
367
+ ZSV_EXPORT
368
+ zsv_parser zsv_new(struct zsv_opts *opts) {
369
+ struct zsv_opts tmp;
370
+ if (!opts) {
371
+ opts = &tmp;
372
+ memset(opts, 0, sizeof(*opts));
373
+ }
374
+
375
+ if (!opts->max_row_size)
376
+ opts->max_row_size = ZSV_ROW_MAX_SIZE_DEFAULT;
377
+ if (!opts->max_columns)
378
+ opts->max_columns = ZSV_MAX_COLS_DEFAULT;
379
+ if (opts->delimiter == '\n' || opts->delimiter == '\r' || opts->delimiter == '"') {
380
+ int (*errprintf)(void *, const char *fmt, ...) = opts->errprintf ? opts->errprintf : zsv_generic_fprintf;
381
+ errprintf(opts->errf ? opts->errf : stderr, "Invalid delimiter\n");
382
+ return NULL;
383
+ }
384
+ struct zsv_scanner *scanner = calloc(1, sizeof(*scanner));
385
+ if (scanner) {
386
+ if (zsv_scanner_init(scanner, opts)) {
387
+ zsv_delete(scanner);
388
+ scanner = NULL;
389
+ }
390
+ }
391
+ return scanner;
392
+ }
393
+
394
+ ZSV_EXPORT
395
+ enum zsv_status zsv_finish(struct zsv_scanner *scanner) {
396
+ enum zsv_status stat = zsv_status_ok;
397
+ if (!scanner)
398
+ return zsv_status_error;
399
+ if (!scanner->abort) {
400
+ if (scanner->mode == ZSV_MODE_FIXED) {
401
+ if (scanner->partial_row_length && memchr("\n\r", scanner->buff.buff[scanner->partial_row_length - 1], 2))
402
+ scanner->partial_row_length--;
403
+ if (scanner->partial_row_length)
404
+ return row_fx(scanner, scanner->buff.buff, 0, scanner->partial_row_length);
405
+ return zsv_status_ok;
406
+ }
407
+
408
+ /* EOF fix-up: if we ran out of input while still inside an unclosed
409
+ * quoted cell, virtually close it so cell_dl sees a consistent state.
410
+ * `pending` is the cell length signaled by whichever counter is set:
411
+ * partial_row_length (multi-chunk parses carrying data forward) or
412
+ * scanned_length (single-chunk parses where partial_row_length is 0).
413
+ *
414
+ * For a stray single `"` (pending < 2) the input is malformed; drop
415
+ * the quoted flag so the byte is emitted as plain content.
416
+ *
417
+ * Otherwise we synthesize a virtual closing quote and let cell_dl
418
+ * strip the surrounding quotes via its easy fast-path. cell_dl's
419
+ * easy path only adjusts pointer/length (no reads past the cell),
420
+ * so the synthesized closing quote at offset `pending` is never
421
+ * actually accessed.
422
+ *
423
+ * For the EMBEDDED case, however, cell_dl's `""`-collapse loop
424
+ * reads up to s[n-1] — i.e. one byte past the original valid input
425
+ * — which causes a heap-buffer-overflow on a tightly-sized scanner
426
+ * buffer. To produce the same cell content the easy path would
427
+ * produce when the over-read happened to land on a zero byte (no
428
+ * spurious extra `""` match), we do the de-escape ourselves in
429
+ * place, then have cell_dl run the easy fast-path on a synthetic
430
+ * envelope sized to the de-escaped content. The cell's quoted flags
431
+ * are preserved (CLOSED|NEEDED) so downstream writers re-quote on
432
+ * output. */
433
+ if (scanner->quoted & ZSV_PARSER_QUOTE_UNCLOSED) {
434
+ size_t pending = scanner->partial_row_length > scanner->cell_start
435
+ ? scanner->partial_row_length - scanner->cell_start
436
+ : scanner->scanned_length > scanner->cell_start ? scanner->scanned_length - scanner->cell_start
437
+ : 0;
438
+ if (pending > 0) {
439
+ if (pending < 2) {
440
+ scanner->quoted = 0;
441
+ } else if (scanner->quoted & ZSV_PARSER_QUOTE_EMBEDDED) {
442
+ /* In-place de-escape of `""` pairs in the cell interior
443
+ * (everything after the leading `"`). After this, cell_dl's
444
+ * easy fast-path sees a `"<de-escaped>"`-shaped envelope of
445
+ * length 2+out and strips both surrounding quotes via
446
+ * `s++; n -= 2`, yielding a cell view of exactly <de-escaped>
447
+ * bytes. The synthetic trailing `"` is never read. */
448
+ unsigned char *inner = scanner->buff.buff + scanner->cell_start + 1;
449
+ size_t inner_len = pending - 1;
450
+ size_t out = 0;
451
+ for (size_t i = 0; i < inner_len; i++) {
452
+ inner[out++] = inner[i];
453
+ if (i + 1 < inner_len && inner[i] == '"' && inner[i + 1] == '"')
454
+ i++;
455
+ }
456
+ scanner->scanned_length = scanner->cell_start + 2 + out;
457
+ scanner->quote_close_position = 1 + out;
458
+ /* Drop EMBEDDED so cell_dl picks the easy path; keep CLOSED
459
+ * and NEEDED so writers see the cell as needing re-quoting. */
460
+ scanner->quoted = (scanner->quoted | ZSV_PARSER_QUOTE_CLOSED | ZSV_PARSER_QUOTE_NEEDED) &
461
+ ~(ZSV_PARSER_QUOTE_UNCLOSED | ZSV_PARSER_QUOTE_PENDING | ZSV_PARSER_QUOTE_EMBEDDED);
462
+ } else {
463
+ int quote = '"';
464
+ scanner->quoted |= ZSV_PARSER_QUOTE_CLOSED;
465
+ scanner->quoted &= ~ZSV_PARSER_QUOTE_UNCLOSED;
466
+ scanner->quote_close_position = pending;
467
+ if (scanner->last != quote)
468
+ scanner->scanned_length++;
469
+ }
470
+ }
471
+ }
472
+ if (scanner->quoted != 0 && scanner->quote_close_position > 0 &&
473
+ scanner->cell_start + scanner->quote_close_position >= scanner->buff.size) {
474
+ // the below does not work if !scanner->free_buff
475
+ // use scanner_pre_parse() instead to shift the row over
476
+ // then append '"' to the end
477
+ size_t new_end = scanner->buff.size - scanner->row_start;
478
+ if (new_end >= scanner->buff.size) {
479
+ // row_start == 0: buffer entirely full, nothing to shift; truncate in place
480
+ scanner->errprintf(scanner->errf, "Warning: row %zu truncated\n", scanner->data_row_count);
481
+ new_end = scanner->buff.size - 1;
482
+ } else {
483
+ stat = scanner_pre_parse(scanner, NULL);
484
+ if (stat != zsv_status_ok)
485
+ return stat;
486
+ }
487
+ // qcp is relative to cell_start; scanned_length must not exceed new_end+1
488
+ // so that cell_dl's easy path (qcp+1 == n) is taken and no read past buffer
489
+ if (scanner->scanned_length > new_end + 1)
490
+ scanner->scanned_length = new_end + 1;
491
+ scanner->quote_close_position = new_end - scanner->cell_start;
492
+ scanner->buff.buff[new_end] = '"';
493
+ /*
494
+ size_t new_size = scanner->cell_start + scanner->quote_close_position + 1;
495
+
496
+ void *mem = realloc(scanner->buff.buff, new_size);
497
+ if (!mem)
498
+ return zsv_status_memory;
499
+ scanner->buff.buff = mem;
500
+ scanner->buff.size = new_size;
501
+ */
502
+ }
503
+ }
504
+
505
+ if (!scanner->finished) {
506
+ scanner->finished = 1;
507
+ if (!scanner->abort) {
508
+ if (scanner->scanned_length > 0 && scanner->scanned_length >= scanner->cell_start)
509
+ cell_dl(scanner, scanner->buff.buff + scanner->cell_start, scanner->scanned_length - scanner->cell_start);
510
+ if (scanner->have_cell) {
511
+ if (row_dl(scanner))
512
+ stat = zsv_status_cancelled;
513
+ }
514
+ } else
515
+ stat = zsv_status_cancelled;
516
+ #ifdef ZSV_EXTRAS
517
+ if (scanner->opts.completed.callback)
518
+ scanner->opts.completed.callback(scanner->opts.completed.ctx, stat);
519
+ #endif
520
+ }
521
+ return stat;
522
+ }
523
+
524
+ ZSV_EXPORT
525
+ enum zsv_status zsv_delete(zsv_parser parser) {
526
+ if (parser) {
527
+ if (parser->free_buff && parser->buff.buff)
528
+ free(parser->buff.buff);
529
+
530
+ free(parser->row.cells);
531
+ free(parser->fixed.offsets);
532
+ free(parser->needed_cols);
533
+ collate_header_destroy(&parser->collate_header);
534
+ free(parser->pull.regs);
535
+
536
+ #ifdef ZSV_EXTRAS
537
+ if (parser->overwrite.close) {
538
+ parser->overwrite.close(parser->overwrite.ctx);
539
+ parser->overwrite.ctx = NULL;
540
+ }
541
+ #endif
542
+
543
+ if (parser->errclose)
544
+ parser->errclose(parser->errf);
545
+ free(parser);
546
+ }
547
+ return zsv_status_ok;
548
+ }
549
+
550
+ ZSV_EXPORT
551
+ const unsigned char *zsv_parse_status_desc(enum zsv_status status) {
552
+ switch (status) {
553
+ case zsv_status_ok:
554
+ return (unsigned char *)"OK";
555
+ case zsv_status_cancelled:
556
+ return (unsigned char *)"User cancelled";
557
+ case zsv_status_no_more_input:
558
+ return (unsigned char *)"No more input";
559
+ case zsv_status_invalid_option:
560
+ return (unsigned char *)"Invalid option";
561
+ case zsv_status_memory:
562
+ return (unsigned char *)"Out of memory";
563
+ case zsv_status_done:
564
+ return (unsigned char *)"No more rows to pull";
565
+ case zsv_status_row:
566
+ return (unsigned char *)"Row successfully pulled";
567
+ case zsv_status_error:
568
+ return (unsigned char *)"Unexpected error";
569
+ case zsv_status_nonstandard_csv:
570
+ return (unsigned char *)"Non-standard CSV detected by fast SIMD engine; retry with ZSV_MODE_COMPAT";
571
+ #ifdef ZSV_EXTRAS
572
+ case zsv_status_max_rows_read:
573
+ return (unsigned char *)"Maximum specified rows have been parsed";
574
+ #endif
575
+ }
576
+ return (unsigned char *)"Unknown";
577
+ }
578
+
579
+ ZSV_EXPORT
580
+ size_t zsv_scanned_length(zsv_parser parser) {
581
+ return parser->scanned_length;
582
+ }
583
+
584
+ ZSV_EXPORT
585
+ size_t zsv_cum_scanned_length(zsv_parser parser) {
586
+ return parser->cum_scanned_length + (parser->finished ? 0 : parser->scanned_length) +
587
+ (parser->had_bom ? strlen(ZSV_BOM) : 0);
588
+ }
589
+
590
+ ZSV_EXPORT
591
+ size_t zsv_row_length_raw_bytes(zsv_parser parser) {
592
+ return parser->scanned_length - parser->row_start;
593
+ }
594
+
595
+ /**
596
+ * @param parser parser handle
597
+ * @param buff the input buffer. Note: this buffer may not overlap with
598
+ * the parser buffer!
599
+ * @param len length of the input to parse
600
+ */
601
+ enum zsv_status zsv_parse_bytes(struct zsv_scanner *scanner, const unsigned char *bytes, size_t len) {
602
+ if (VERY_UNLIKELY(scanner->abort || scanner->finished))
603
+ // e.g. from nonstandard-CSV detection, etc
604
+ return zsv_status_cancelled;
605
+ enum zsv_status stat = zsv_status_ok;
606
+ const unsigned char *cursor = bytes;
607
+ while (len && stat == zsv_status_ok) {
608
+ size_t capacity;
609
+ if ((stat = scanner_pre_parse(scanner, &capacity)) != zsv_status_ok)
610
+ break;
611
+ size_t this_chunk_size = len > capacity ? capacity : len;
612
+ memcpy(scanner->buff.buff + scanner->partial_row_length, cursor, this_chunk_size);
613
+ cursor += this_chunk_size;
614
+ len -= this_chunk_size;
615
+ if (scanner->filter)
616
+ this_chunk_size =
617
+ scanner->filter(scanner->filter_ctx, scanner->buff.buff + scanner->partial_row_length, this_chunk_size);
618
+ if (this_chunk_size)
619
+ stat = zsv_scan(scanner, scanner->buff.buff, this_chunk_size);
620
+ }
621
+ return stat;
622
+ }