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,789 @@
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
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+ #include <string.h>
12
+ #include <ctype.h>
13
+ #include <stdint.h> // uint32_t
14
+
15
+ #ifdef ZSV_EXTRAS
16
+ #include <time.h>
17
+ #endif
18
+
19
+ #include <zsv/utils/utf8.h>
20
+ #include <zsv/utils/compiler.h>
21
+ #include <zsv/utils/string.h>
22
+
23
+ #if !defined(__AVX2__) // -mavx2 compiler flag not present
24
+ #define ZSV_NO_AVX
25
+ #define zsv_mask_t uint16_t
26
+ #define VECTOR_BYTES 16
27
+ #define NEXT_BIT __builtin_ffs
28
+ #if defined(__AVX__)
29
+ #include <emmintrin.h>
30
+ #define zsv_mask_t uint16_t
31
+ #define VECTOR_BYTES 16
32
+ #define NEXT_BIT __builtin_ffs
33
+ #define movemask_pseudo(x) _mm_movemask_epi8((__m128i)x)
34
+ #endif
35
+ #elif defined(HAVE_AVX512)
36
+ #ifndef __AVX512BW__
37
+ #error AVX512 requested, but __AVX512BW__ macro not defined
38
+ #else
39
+ #include <immintrin.h>
40
+ #define VECTOR_BYTES 64
41
+ #define zsv_mask_t uint64_t
42
+ #define movemask_pseudo(x) _mm512_movepi8_mask((__m512i)x)
43
+ #define NEXT_BIT __builtin_ffsl
44
+ #endif
45
+ #elif defined(__AVX2__) // have avx2, not avx512
46
+ #include <immintrin.h>
47
+ #define VECTOR_BYTES 32
48
+ #define zsv_mask_t uint32_t
49
+ #define movemask_pseudo(x) _mm256_movemask_epi8((__m256i)x)
50
+ #define NEXT_BIT __builtin_ffs
51
+ #else
52
+ #define ZSV_NO_AVX
53
+ #define zsv_mask_t uint16_t
54
+ #define VECTOR_BYTES 16
55
+ #define NEXT_BIT __builtin_ffs
56
+ #endif
57
+
58
+ typedef unsigned char zsv_uc_vector __attribute__((vector_size(VECTOR_BYTES)));
59
+
60
+ struct zsv_row {
61
+ size_t used, allocated, overflow;
62
+ struct zsv_cell *cells;
63
+ };
64
+
65
+ struct collate_header {
66
+ struct {
67
+ unsigned char *buff;
68
+ size_t used;
69
+ } buff;
70
+ size_t *lengths; // length PLUS 1 of each cell
71
+ size_t column_count;
72
+ };
73
+
74
+ struct zsv_scan_delim_regs {
75
+ size_t i;
76
+ size_t bytes_chunk_end;
77
+ size_t bytes_read;
78
+ char delimiter;
79
+ unsigned char c;
80
+ char skip_next_delim;
81
+ int quote;
82
+ size_t mask_total_offset;
83
+ zsv_mask_t mask;
84
+ int mask_last_start;
85
+ unsigned char location;
86
+ };
87
+
88
+ struct zsv_scan_fixed_regs {
89
+ char xx; // to do
90
+ };
91
+
92
+ #ifdef ZSV_EXTRAS
93
+ #include <inttypes.h>
94
+ #include <sqlite3.h>
95
+
96
+ struct zsv_overwrite {
97
+ struct zsv_overwrite_data odata;
98
+ void *ctx;
99
+ enum zsv_status (*next)(void *ctx, struct zsv_overwrite_data *odata);
100
+ enum zsv_status (*close)(void *ctx);
101
+ };
102
+ #endif
103
+
104
+ struct zsv_scanner {
105
+ char last;
106
+ struct {
107
+ unsigned char *buff; // provided by caller
108
+ size_t size; // provided by caller
109
+ } buff;
110
+
111
+ size_t cell_start;
112
+ unsigned char quoted : 7; // bitfield of ZSV_PARSER_QUOTE_XXX flags
113
+ unsigned char buffer_exceeded : 1;
114
+
115
+ unsigned char waiting_for_end : 1;
116
+ unsigned char checked_bom : 1;
117
+ unsigned char free_buff : 1;
118
+ unsigned char finished : 1;
119
+ unsigned char had_bom : 1;
120
+ unsigned char abort : 1;
121
+ unsigned char have_cell : 1;
122
+ unsigned char started : 1;
123
+
124
+ unsigned char skip_cells : 1; // fast engine: skip cell storage, just count rows
125
+ unsigned char nonstandard : 1; // fast engine: set when input violates RFC 4180
126
+ // (e.g. quote mid-unquoted-cell). Cold-path falls back
127
+ // to COMPAT to match its byte-for-byte interpretation.
128
+
129
+ /* Column filter for the fast engine. When non-NULL, only columns
130
+ * with needed_cols[col_ix] != 0 get full processing (quote normalization,
131
+ * UTF-8 encoding, cell_handler). Other columns get a raw placeholder.
132
+ * Array length must be >= max_columns. */
133
+ unsigned char *needed_cols;
134
+ unsigned int needed_cols_count; // length of needed_cols array
135
+
136
+ size_t quote_close_position;
137
+ struct zsv_opts opts;
138
+
139
+ size_t row_start;
140
+ struct zsv_row row;
141
+
142
+ size_t scanned_length;
143
+ size_t cum_scanned_length;
144
+ size_t partial_row_length;
145
+
146
+ size_t (*read)(void *buff, size_t n, size_t size, void *in);
147
+ void *in;
148
+
149
+ size_t (*filter)(void *ctx, unsigned char *buff, size_t bytes_read);
150
+ void *filter_ctx;
151
+
152
+ size_t buffer_end;
153
+ size_t old_bytes_read; // only non-zero if we must shift upon next parse_more()
154
+
155
+ const char *insert_string;
156
+
157
+ size_t empty_header_rows;
158
+
159
+ struct zsv_opts opts_orig;
160
+
161
+ #define ZSV_MODE_DELIM 0
162
+ #define ZSV_MODE_FIXED 1
163
+ #define ZSV_MODE_DELIM_PULL 2
164
+ #define ZSV_MODE_DELIM_FAST 3
165
+ #define ZSV_MODE_COMPAT 255
166
+ unsigned char mode;
167
+ struct {
168
+ unsigned *offsets; // 0-based position of each cell end. offset[0] = end of first cell
169
+ unsigned count; // number of offsets
170
+ } fixed;
171
+
172
+ struct collate_header *collate_header;
173
+ size_t data_row_count; /* 0 = in header row; 1 = first data row */
174
+ struct zsv_cell (*get_cell)(zsv_parser parser, size_t ix);
175
+
176
+ struct {
177
+ union {
178
+ struct zsv_scan_delim_regs delim;
179
+ struct zsv_scan_fixed_regs fixed;
180
+ } *regs;
181
+ enum zsv_status stat; // last status
182
+ unsigned char *buff;
183
+ size_t bytes_read;
184
+ size_t row_used;
185
+ unsigned char now;
186
+ } pull;
187
+
188
+ int (*errprintf)(void *ctx, const char *format, ...);
189
+ void *errf;
190
+ int (*errclose)(void *ctx);
191
+
192
+ #ifdef ZSV_EXTRAS
193
+ struct {
194
+ size_t cum_row_count; /* total number of rows read */
195
+ time_t last_time; /* last time from which to check seconds_interval */
196
+ size_t max_rows; /* max rows to read, including header row(s) */
197
+ } progress;
198
+ struct zsv_overwrite overwrite;
199
+ #endif
200
+ };
201
+
202
+ void collate_header_destroy(struct collate_header **chp) {
203
+ if (*chp) {
204
+ struct collate_header *ch = *chp;
205
+ free(ch->buff.buff);
206
+ free(ch->lengths);
207
+ free(ch);
208
+ *chp = NULL;
209
+ }
210
+ }
211
+
212
+ /* collate_header_append(): return err */
213
+ static int collate_header_append(struct zsv_scanner *scanner, struct collate_header **chp) {
214
+ if (!*chp) {
215
+ if ((*chp = calloc(1, sizeof(struct collate_header))))
216
+ (*chp)->lengths = calloc(scanner->row.allocated, sizeof(*(*chp)->lengths));
217
+ if (!(*chp) || !(*chp)->lengths) {
218
+ free(*chp);
219
+ scanner->errprintf(scanner->errf, "Out of memory!\n");
220
+ return -1;
221
+ }
222
+ }
223
+ struct collate_header *ch = *chp;
224
+ size_t this_row_size = 0;
225
+ size_t column_count = zsv_cell_count(scanner);
226
+ for (size_t i = 0, j = column_count; i < j; i++) {
227
+ struct zsv_cell c = zsv_get_cell_1(scanner, i);
228
+ if (c.len)
229
+ this_row_size += c.len + 1; // +1: terminating null or delim
230
+ }
231
+ size_t new_row_size = ch->buff.used + this_row_size;
232
+ if (new_row_size > 0) {
233
+ unsigned char *new_row = realloc(ch->buff.buff, new_row_size);
234
+ if (!new_row) {
235
+ scanner->errprintf(scanner->errf, "Out of memory!\n");
236
+ return -1;
237
+ }
238
+
239
+ // now: splice the new row into the old row, starting with the last cell
240
+ // e.g. prior row = A1.B1.C1.
241
+ // this row = A2.B2.C2.
242
+ // new_row = A1.B1.C1..........
243
+ // starting with last cell in this row, move the old data, then splice new:
244
+ // new_row = A1.B1.C1.......C2.
245
+ // new_row = A1.B1.C1....C1 C2.
246
+ // new_row = A1.B1.C1.B2.C1 C2.
247
+ // new_row = A1.B1.B1 B2.C1 C2.
248
+ // new_row = A1.A2.B1 B2.C1 C2.
249
+ // new_row = A1 A2.B1 B2.C1 C2.
250
+
251
+ size_t new_row_end = ch->buff.used + this_row_size;
252
+ size_t old_row_end = ch->buff.used;
253
+ ch->buff.used += this_row_size;
254
+ ch->buff.buff = new_row;
255
+
256
+ for (size_t i = column_count; i > 0; i--) {
257
+ struct zsv_cell c = zsv_get_cell_1(scanner, i - 1);
258
+ // copy new row's cell value to end
259
+ if (c.len) {
260
+ memcpy(new_row + new_row_end - c.len - 1, c.str, c.len);
261
+ new_row[new_row_end - 1] = ' ';
262
+ new_row_end = new_row_end - c.len - 1;
263
+ }
264
+
265
+ // move prior cell value
266
+ size_t old_cell_len = ch->lengths[i - 1]; // old_cell_len includes delim
267
+ if (old_cell_len) {
268
+ // need memmove, not memcpy
269
+ memmove(new_row + new_row_end - old_cell_len, new_row + old_row_end - old_cell_len, old_cell_len);
270
+ old_row_end -= old_cell_len;
271
+ new_row_end -= old_cell_len;
272
+ }
273
+ if (c.len)
274
+ ch->lengths[i - 1] += c.len + 1;
275
+ }
276
+ }
277
+ if (column_count > ch->column_count)
278
+ ch->column_count = column_count;
279
+ return 0;
280
+ }
281
+
282
+ __attribute__((always_inline)) static inline void zsv_clear_cell(struct zsv_scanner *scanner) {
283
+ scanner->quoted = 0;
284
+ }
285
+
286
+ static int zsv_count_malformed(void *ctx, const unsigned char *s, size_t n, size_t offset) {
287
+ (void)s;
288
+ (void)n;
289
+ (void)offset;
290
+ ++*(size_t *)ctx;
291
+ return 0;
292
+ }
293
+
294
+ /* Apply the malformed-UTF8 policy to one cell and fire the per-cell handler.
295
+ * Callers gate on (malformed_utf8_replace || malformed_utf8_handler).
296
+ * malformed_utf8_replace holds negative sentinels in a plain char, which is
297
+ * unsigned on some targets (e.g. gcc/ARM Linux): compare via signed char.
298
+ * The handler is not fired for overflow cells (scanner->row.used is the index
299
+ * this cell is about to occupy), though replacement still applies to them. */
300
+ static inline size_t zsv_cell_encode_utf8(struct zsv_scanner *scanner, unsigned char *s, size_t n) {
301
+ signed char replace = (signed char)scanner->opts.malformed_utf8_replace;
302
+ char count_it = scanner->opts.malformed_utf8_handler != NULL && scanner->row.used < scanner->row.allocated;
303
+ size_t bad = 0;
304
+ if (replace) // a replacement char, or REMOVE (DO_NOT_REPLACE was normalized to 0 at init)
305
+ n = zsv_strencode(s, n, replace < 0 ? 0 : (unsigned char)replace, count_it ? zsv_count_malformed : NULL, &bad);
306
+ else if (count_it) // leave the bytes untouched, but still count
307
+ bad = zsv_strencode_validate(s, n, NULL, NULL);
308
+ if (bad && count_it)
309
+ scanner->opts.malformed_utf8_handler(scanner->opts.malformed_utf8_handler_ctx, scanner->row.used, bad);
310
+ return n;
311
+ }
312
+
313
+ // always_inline has a noticeable impact. do not remove without benchmarking!
314
+ __attribute__((always_inline)) static inline void cell_dl(struct zsv_scanner *scanner, unsigned char *s, size_t n) {
315
+ // handle quoting
316
+ if (VERY_LIKELY(!scanner->buffer_exceeded)) {
317
+ if (UNLIKELY(scanner->quoted > 0)) {
318
+ if (LIKELY(scanner->quote_close_position + 1 == n)) {
319
+ if (LIKELY((scanner->quoted & ZSV_PARSER_QUOTE_EMBEDDED) == 0)) {
320
+ // easy and usual case: no embedded double-quotes
321
+ // just remove surrounding quotes from content (n >= 2 enforced
322
+ // by EOF fix-up in zsv_finish, and by the parser state machine
323
+ // in scan-loop call sites)
324
+ s++;
325
+ n -= 2;
326
+ } else { // embedded dbl-quotes to remove
327
+ s++;
328
+ n--;
329
+ // remove dbl-quotes. TO DO: consider adding option to skip this
330
+ for (size_t i = 0; i + 1 < n; i++) {
331
+ if (s[i] == '"' && s[i + 1] == '"') {
332
+ if (n > i + 2)
333
+ memmove(s + i + 1, s + i + 2, n - i - 2);
334
+ n--;
335
+ }
336
+ }
337
+ n--;
338
+ }
339
+ } else {
340
+ if (scanner->quote_close_position) {
341
+ // the first char was a quote, and we have content after the closing quote
342
+ // the solution below is a generalized on that will work
343
+ // for the easy and usual case, but by handling separately
344
+ // we avoid the memmove in the easy / usual case
345
+ memmove(s + 1, s, scanner->quote_close_position);
346
+ s += 2;
347
+ n -= 2;
348
+ if (UNLIKELY((scanner->quoted & ZSV_PARSER_QUOTE_EMBEDDED) != 0)) {
349
+ // remove dbl-quotes
350
+ for (size_t i = 0; i + 1 < n; i++) {
351
+ if (s[i] == '"' && s[i + 1] == '"') {
352
+ if (n > i + 2)
353
+ memmove(s + i + 1, s + i + 2, n - i - 2);
354
+ n--;
355
+ }
356
+ }
357
+ }
358
+ }
359
+ }
360
+ } else if (UNLIKELY(scanner->opts.delimiter != ',')) {
361
+ if (memchr(s, ',', n))
362
+ scanner->quoted = ZSV_PARSER_QUOTE_NEEDED;
363
+ }
364
+ // end quote handling
365
+
366
+ if (scanner->opts.malformed_utf8_replace || scanner->opts.malformed_utf8_handler)
367
+ n = zsv_cell_encode_utf8(scanner, s, n);
368
+ }
369
+ if (UNLIKELY(scanner->opts.cell_handler != NULL))
370
+ scanner->opts.cell_handler(scanner->opts.ctx, s, n);
371
+ if (VERY_LIKELY(scanner->row.used < scanner->row.allocated)) {
372
+ struct zsv_row *row = &scanner->row;
373
+ struct zsv_cell c = {s, n, scanner->opts.no_quotes ? 1 : scanner->quoted, 0};
374
+ row->cells[row->used++] = c;
375
+ } else
376
+ scanner->row.overflow++;
377
+ scanner->have_cell = 1;
378
+
379
+ zsv_clear_cell(scanner);
380
+ }
381
+
382
+ __attribute__((always_inline)) static inline enum zsv_status row_dl(struct zsv_scanner *scanner) {
383
+ if (VERY_UNLIKELY(scanner->row.overflow)) {
384
+ scanner->errprintf(scanner->errf, "Warning: number of columns (%zu) exceeds row max (%zu)\n",
385
+ scanner->row.allocated + scanner->row.overflow, scanner->row.allocated);
386
+ scanner->row.overflow = 0;
387
+ }
388
+ if (VERY_LIKELY(scanner->opts.row_handler != NULL)) // TO DO: disallow row_handler to be null; if null, set to dummy
389
+ scanner->opts.row_handler(scanner->opts.ctx);
390
+ // Note: scanner->data_row_count will be incremented AFTER this call
391
+ // in order to accommodate pull parsing, in which case incrementing here
392
+ // would be too early
393
+ #ifdef ZSV_EXTRAS
394
+ scanner->progress.cum_row_count++;
395
+ if (VERY_UNLIKELY(scanner->opts.progress.rows_interval &&
396
+ scanner->progress.cum_row_count % scanner->opts.progress.rows_interval == 0)) {
397
+ char ok;
398
+ if (!scanner->opts.progress.seconds_interval)
399
+ ok = 1;
400
+ else {
401
+ // using timer_create() would be better, but is not currently supported on
402
+ // all platforms, so the fallback is to poll
403
+ time_t now = time(NULL);
404
+ if (now > scanner->progress.last_time &&
405
+ (unsigned int)(now - scanner->progress.last_time) >= scanner->opts.progress.seconds_interval) {
406
+ ok = 1;
407
+ scanner->progress.last_time = now;
408
+ } else
409
+ ok = 0;
410
+ }
411
+ if (ok && scanner->opts.progress.callback)
412
+ scanner->abort = scanner->opts.progress.callback(scanner->opts.progress.ctx, scanner->progress.cum_row_count);
413
+ #ifndef NDEBUG
414
+ if (scanner->abort)
415
+ scanner->errprintf(scanner->errf, "ZSV parsing aborted at %zu\n", scanner->progress.cum_row_count);
416
+ #endif
417
+ }
418
+ if (VERY_UNLIKELY(scanner->progress.max_rows > 0)) {
419
+ if (VERY_UNLIKELY(scanner->progress.cum_row_count == scanner->progress.max_rows)) {
420
+ scanner->abort = 1;
421
+ scanner->row.used = 0;
422
+ return zsv_status_max_rows_read;
423
+ }
424
+ }
425
+
426
+ #endif
427
+ if (VERY_UNLIKELY(scanner->abort))
428
+ return zsv_status_cancelled;
429
+ scanner->have_cell = 0;
430
+ scanner->row.used = 0;
431
+ return zsv_status_ok;
432
+ }
433
+
434
+ __attribute__((always_inline)) static inline enum zsv_status cell_and_row_dl(struct zsv_scanner *scanner,
435
+ unsigned char *s, size_t n) {
436
+ cell_dl(scanner, s, n);
437
+ return row_dl(scanner);
438
+ }
439
+
440
+ #ifndef movemask_pseudo
441
+ /*
442
+ provide our own pseudo-movemask, which sets the 1 bit for each corresponding
443
+ non-zero value in the vector (as opposed to real movemask which sets the bit
444
+ only for each corresponding non-zero highest-bit value in the vector)
445
+ */
446
+
447
+ #if defined(__EMSCRIPTEN__) && defined(__SSE2__)
448
+ #include <wasm_simd128.h>
449
+ #define movemask_pseudo(x) wasm_i8x16_bitmask(x)
450
+
451
+ #elif defined(__ARM_NEON) || defined(__ARM_NEON__)
452
+ #include <arm_neon.h>
453
+ static inline zsv_mask_t movemask_pseudo(zsv_uc_vector v) {
454
+ // see https://stackoverflow.com/questions/11870910/
455
+ static const uint8_t __attribute__((aligned(16)))
456
+ _powers[16] = {1, 2, 4, 8, 16, 32, 64, 128, 1, 2, 4, 8, 16, 32, 64, 128};
457
+ uint8x16_t mm_powers = vld1q_u8(_powers);
458
+
459
+ // compute the mask from the input
460
+ uint64x2_t imask = vpaddlq_u32(vpaddlq_u16(vpaddlq_u8(vandq_u8(v, mm_powers))));
461
+
462
+ // Get the resulting bytes
463
+ uint16_t mask;
464
+ vst1q_lane_u8((uint8_t *)&mask + 0, (uint8x16_t)imask, 0);
465
+ vst1q_lane_u8((uint8_t *)&mask + 1, (uint8x16_t)imask, 8);
466
+ return mask;
467
+ }
468
+
469
+ #elif defined(__SSE2__)
470
+
471
+ typedef char zsv_c_vector __attribute__((vector_size(VECTOR_BYTES)));
472
+ #define movemask_pseudo(x) __builtin_ia32_pmovmskb128((zsv_c_vector)x)
473
+
474
+ #else
475
+
476
+ // slow path
477
+ #if defined(__EMSCRIPTEN__)
478
+ #warning \
479
+ "Compiling with emscripten, without using SIMD. To use SIMD, compile with -msse2 -msimd128 -experimental-wasm-simd and -I/path/to/emsdk/upstream/lib/clang/16.0.0/include"
480
+ #endif
481
+
482
+ static inline zsv_mask_t movemask_pseudo(zsv_uc_vector v) {
483
+ zsv_mask_t mask = 0, tmp = 1;
484
+ for (size_t i = 0; i < sizeof(zsv_uc_vector); i++) {
485
+ mask |= (v[i] ? tmp : 0);
486
+ tmp <<= 1;
487
+ }
488
+
489
+ return mask;
490
+ }
491
+
492
+ #endif // __EMSCRIPTEN__
493
+ #endif // ndef movemask_pseudo
494
+
495
+ #include "vector_delim.c"
496
+
497
+ #ifdef ZSV_SUPPORT_PULL_PARSER
498
+ #undef ZSV_SUPPORT_PULL_PARSER
499
+ #endif
500
+
501
+ #define ZSV_SCAN_DELIM zsv_scan_delim
502
+ #include "zsv_scan_delim.c"
503
+ #undef ZSV_SCAN_DELIM
504
+ #undef scanner_last
505
+
506
+ #define ZSV_SUPPORT_PULL_PARSER 1
507
+ #define ZSV_SCAN_DELIM zsv_scan_delim_pull
508
+ #include "zsv_scan_delim.c"
509
+
510
+ #include "zsv_scan_fixed.c"
511
+ #include "zsv_scan_delim_fast.c"
512
+
513
+ static enum zsv_status zsv_scan(struct zsv_scanner *scanner, unsigned char *buff, size_t bytes_read) {
514
+ switch (scanner->mode) {
515
+ case ZSV_MODE_FIXED:
516
+ return zsv_scan_fixed(scanner, buff, bytes_read);
517
+ case ZSV_MODE_DELIM_PULL:
518
+ // return zsv_status_row or zsv_status_ok (next call to parse_more)
519
+ return zsv_scan_delim_pull(scanner, buff, bytes_read);
520
+ case ZSV_MODE_DELIM_FAST:
521
+ return zsv_scan_delim_fast(scanner, buff, bytes_read);
522
+ default:
523
+ return zsv_scan_delim(scanner, buff, bytes_read);
524
+ }
525
+ }
526
+
527
+ #define ZSV_BOM "\xef\xbb\xbf"
528
+
529
+ // optional: set a filter function to filter data before it is processed
530
+ // function should return the number of bytes to process. this may be smaller
531
+ // but may not be larger than the original number of bytes, and any data modification
532
+ // must be done in-place to *buff
533
+ enum zsv_status zsv_set_scan_filter(struct zsv_scanner *scanner,
534
+ size_t (*filter)(void *ctx, unsigned char *buff, size_t bytes_read), void *ctx) {
535
+ scanner->filter = filter;
536
+ scanner->filter_ctx = ctx;
537
+ return zsv_status_ok;
538
+ }
539
+
540
+ static void apply_callbacks(struct zsv_scanner *scanner) {
541
+ if (UNLIKELY(scanner->opts.cell_handler != NULL)) {
542
+ // call the user-provided cell() callback on each cell
543
+ unsigned char saved_quoted = scanner->quoted;
544
+ for (size_t i = 0, j = zsv_cell_count(scanner); i < j; i++) {
545
+ struct zsv_cell c = zsv_get_cell_1(scanner, i);
546
+ scanner->quoted = c.quoted;
547
+ scanner->opts.cell_handler(scanner->opts.ctx, c.str, c.len);
548
+ }
549
+ scanner->quoted = saved_quoted;
550
+ }
551
+ // call the user-provided row() callback
552
+ if (VERY_LIKELY(scanner->opts.row_handler != NULL))
553
+ scanner->opts.row_handler(scanner->opts.ctx);
554
+ }
555
+
556
+ static void set_callbacks(struct zsv_scanner *scanner);
557
+
558
+ static char zsv_internal_row_is_blank(zsv_parser parser) {
559
+ for (unsigned int i = 0; i < parser->row.used; i++)
560
+ if (parser->row.cells[i].len)
561
+ return 0;
562
+ return 1;
563
+ }
564
+
565
+ static void skip_to_first_row_w_data(void *ctx) {
566
+ struct zsv_scanner *scanner = ctx;
567
+ if (LIKELY(zsv_internal_row_is_blank(scanner) == 0)) {
568
+ scanner->opts.keep_empty_header_rows = 1;
569
+ if (scanner->empty_header_rows) {
570
+ scanner->errprintf(scanner->errf, "Warning: skipped %zu empty header rows; suggest using:\n --skip-head %zu\n",
571
+ scanner->empty_header_rows, scanner->empty_header_rows + scanner->opts_orig.rows_to_ignore);
572
+ }
573
+ set_callbacks(scanner);
574
+ apply_callbacks(scanner);
575
+ } else // entire row was empty
576
+ scanner->empty_header_rows++;
577
+ }
578
+
579
+ static void ignore_header_rows(void *ctx) {
580
+ struct zsv_scanner *scanner = ctx;
581
+ if (scanner->opts.rows_to_ignore)
582
+ scanner->opts.rows_to_ignore--;
583
+ if (!scanner->opts.rows_to_ignore)
584
+ set_callbacks(scanner);
585
+ }
586
+
587
+ static void collate_header_row(void *ctx) {
588
+ struct zsv_scanner *scanner = ctx;
589
+ if (scanner->opts.header_span) {
590
+ --scanner->opts.header_span;
591
+
592
+ // save this row
593
+
594
+ // first, make sure this row has at least as many cells as the largest prior row
595
+ if (scanner->collate_header) {
596
+ for (size_t i = zsv_cell_count(scanner); i < scanner->row.allocated && i < scanner->collate_header->column_count;
597
+ i++)
598
+ memset(&scanner->row.cells[i], 0, sizeof(scanner->row.cells[i]));
599
+ scanner->row.used = scanner->collate_header->column_count;
600
+ }
601
+
602
+ if (collate_header_append(scanner, &scanner->collate_header))
603
+ scanner->abort = 1;
604
+ }
605
+
606
+ if (!scanner->opts.header_span) {
607
+ // finished with header; combine all rows into a single row
608
+ set_callbacks(scanner);
609
+ if (scanner->collate_header) {
610
+ size_t offset = 0;
611
+ for (size_t i = 0; i < scanner->collate_header->column_count; i++) {
612
+ size_t len_plus1 = scanner->collate_header->lengths[i];
613
+ scanner->row.cells[i].str = scanner->collate_header->buff.buff + offset;
614
+ if (len_plus1) {
615
+ scanner->row.cells[i].len = len_plus1 - 1;
616
+ scanner->row.cells[i].quoted = 1;
617
+ } else
618
+ scanner->row.cells[i].len = 0;
619
+ offset += len_plus1;
620
+ }
621
+ }
622
+
623
+ apply_callbacks(scanner);
624
+ if (scanner->mode != ZSV_MODE_DELIM_PULL)
625
+ collate_header_destroy(&scanner->collate_header);
626
+ }
627
+ }
628
+
629
+ static void set_callbacks(struct zsv_scanner *scanner) {
630
+ if (scanner->opts.rows_to_ignore) {
631
+ scanner->opts.row_handler = ignore_header_rows;
632
+ scanner->opts.cell_handler = NULL;
633
+ scanner->opts.ctx = scanner;
634
+ } else if (scanner->mode != ZSV_MODE_FIXED && !scanner->opts.keep_empty_header_rows) {
635
+ scanner->opts.row_handler = skip_to_first_row_w_data;
636
+ scanner->opts.cell_handler = NULL;
637
+ scanner->opts.ctx = scanner;
638
+ } else if (scanner->opts.header_span > 1) {
639
+ scanner->opts.row_handler = collate_header_row;
640
+ scanner->opts.cell_handler = NULL;
641
+ scanner->opts.ctx = scanner;
642
+ } else {
643
+ #ifdef ZSV_EXTRAS
644
+ if (scanner->overwrite.odata.have)
645
+ scanner->get_cell = zsv_get_cell_with_overwrite;
646
+ else
647
+ #endif
648
+ scanner->get_cell = zsv_get_cell_1;
649
+ scanner->data_row_count = 0;
650
+ scanner->skip_cells = 0;
651
+ scanner->opts.row_handler = scanner->opts_orig.row_handler;
652
+ scanner->opts.cell_handler = scanner->opts_orig.cell_handler;
653
+ scanner->opts.ctx = scanner->opts_orig.ctx;
654
+ }
655
+ }
656
+
657
+ static void zsv_throwaway_row(void *ctx) {
658
+ struct zsv_scanner *scanner = ctx;
659
+ if (scanner->opts.overflow_row_handler != NULL) {
660
+ if (zsv_cell_count(scanner) > 1 || zsv_get_cell_1(scanner, 0).len > 0)
661
+ scanner->opts.overflow_row_handler(ctx);
662
+ }
663
+ scanner->buffer_exceeded = 0;
664
+ set_callbacks(ctx);
665
+ }
666
+
667
+ #ifdef ZSV_EXTRAS
668
+
669
+ static int zsv_have_overwrite(zsv_parser parser, size_t row_ix, size_t col_ix) {
670
+ struct zsv_overwrite *overwrite = &parser->overwrite;
671
+ while (overwrite->odata.have && overwrite->odata.row_ix < row_ix)
672
+ overwrite->next(overwrite->ctx, &overwrite->odata);
673
+ while (overwrite->odata.have && overwrite->odata.row_ix == row_ix && overwrite->odata.col_ix < col_ix)
674
+ overwrite->next(overwrite->ctx, &overwrite->odata);
675
+ if (!overwrite->odata.have)
676
+ parser->get_cell = zsv_get_cell_1;
677
+ return overwrite->odata.have && overwrite->odata.row_ix == row_ix && overwrite->odata.col_ix == col_ix;
678
+ }
679
+
680
+ static struct zsv_cell zsv_get_cell_with_overwrite(zsv_parser parser, size_t col_ix) {
681
+ if (VERY_LIKELY(col_ix < parser->row.used)) {
682
+ size_t row_ix = parser->data_row_count;
683
+ if (!zsv_have_overwrite(parser, row_ix, col_ix))
684
+ return parser->row.cells[col_ix];
685
+
686
+ struct zsv_cell c = parser->overwrite.odata.val;
687
+ c.overwritten = 1;
688
+ return c;
689
+ }
690
+ struct zsv_cell c = {0, 0, 0, 0};
691
+ return c;
692
+ }
693
+ #endif
694
+
695
+ static int zsv_scanner_init(struct zsv_scanner *scanner, struct zsv_opts *opts) {
696
+ size_t need_buff_size = 0;
697
+ scanner->errprintf = opts->errprintf ? opts->errprintf : zsv_generic_fprintf;
698
+ scanner->errf = opts->errf ? opts->errf : stderr;
699
+ scanner->errclose = opts->errclose;
700
+ // via signed char: the sentinel is negative and plain char is unsigned on
701
+ // some targets (e.g. gcc/ARM Linux), where a bare == -2 never matches
702
+ if ((signed char)opts->malformed_utf8_replace == ZSV_MALFORMED_UTF8_DO_NOT_REPLACE)
703
+ opts->malformed_utf8_replace = 0;
704
+ if (opts->buffsize < opts->max_row_size * 2)
705
+ need_buff_size = opts->max_row_size * 2;
706
+ opts->delimiter = opts->delimiter ? opts->delimiter : ',';
707
+ if (opts->delimiter == '\n' || opts->delimiter == '\r' || opts->delimiter == '"') {
708
+ scanner->errprintf(scanner->errf, "warning: ignoring illegal delimiter\n");
709
+ opts->delimiter = ',';
710
+ }
711
+
712
+ if (opts->insert_header_row)
713
+ scanner->insert_string = opts->insert_header_row;
714
+
715
+ if (need_buff_size < ZSV_MIN_SCANNER_BUFFSIZE)
716
+ need_buff_size = ZSV_MIN_SCANNER_BUFFSIZE;
717
+ if (opts->buffsize < need_buff_size) {
718
+ if (opts->buffsize > 0) {
719
+ if (need_buff_size == ZSV_MIN_SCANNER_BUFFSIZE)
720
+ scanner->errprintf(scanner->errf, "Increasing --buff-size to minimum %zu\n", need_buff_size);
721
+ else
722
+ scanner->errprintf(scanner->errf, "Increasing --buff-size to %zu to accommodate max-row-size of %u\n",
723
+ need_buff_size, opts->max_row_size);
724
+ }
725
+ opts->buffsize = need_buff_size;
726
+ }
727
+ if (opts->buffsize == 0)
728
+ opts->buffsize = ZSV_DEFAULT_SCANNER_BUFFSIZE;
729
+ else if (opts->buffsize < ZSV_MIN_SCANNER_BUFFSIZE)
730
+ opts->buffsize = ZSV_MIN_SCANNER_BUFFSIZE;
731
+
732
+ if (opts->scan_engine == ZSV_MODE_COMPAT)
733
+ scanner->mode = ZSV_MODE_DELIM; /* force compat/standard engine */
734
+ else if (opts->scan_engine)
735
+ scanner->mode = opts->scan_engine;
736
+ #if defined(ZSV_FAST_PARSER_AVAILABLE) && defined(ZSV_DEFAULT_PARSER_FAST)
737
+ else
738
+ scanner->mode = ZSV_MODE_DELIM_FAST;
739
+ #endif
740
+
741
+ scanner->in = opts->stream;
742
+ if (!opts->read) {
743
+ scanner->read = (zsv_generic_read)fread;
744
+ if (!opts->stream)
745
+ scanner->in = stdin;
746
+ } else {
747
+ scanner->read = opts->read;
748
+ scanner->in = opts->stream;
749
+ }
750
+ scanner->buff.buff = opts->buff;
751
+ scanner->buff.size = opts->buffsize;
752
+
753
+ if (opts->buffsize && !opts->buff) {
754
+ scanner->buff.buff = malloc(opts->buffsize);
755
+ scanner->free_buff = 1;
756
+ }
757
+
758
+ #ifdef ZSV_EXTRAS
759
+ if (opts->max_rows)
760
+ scanner->progress.max_rows = opts->max_rows;
761
+ #endif
762
+ if (scanner->buff.buff) {
763
+ scanner->opts = *opts;
764
+ scanner->opts_orig = *opts;
765
+ if (!scanner->opts.max_columns)
766
+ scanner->opts.max_columns = 1024;
767
+ set_callbacks(scanner);
768
+ if ((scanner->row.allocated = scanner->opts.max_columns) &&
769
+ (scanner->row.cells = calloc(scanner->row.allocated, sizeof(*scanner->row.cells)))) {
770
+ #ifdef ZSV_EXTRAS
771
+ // initialize overwrites
772
+ if (scanner->opts.overwrite.open && !scanner->opts.overwrite.cancel) {
773
+ if (scanner->opts.overwrite.open(scanner->opts.overwrite.ctx) == zsv_status_ok) {
774
+ scanner->overwrite.odata.have = 1;
775
+ scanner->overwrite.next = scanner->opts.overwrite.next;
776
+ scanner->overwrite.close = scanner->opts.overwrite.close;
777
+ scanner->overwrite.ctx = scanner->opts.overwrite.ctx;
778
+ // load the first overwrite
779
+ scanner->overwrite.next(scanner->overwrite.ctx, &scanner->overwrite.odata);
780
+ return 0;
781
+ }
782
+ return 1;
783
+ }
784
+ #endif
785
+ return 0;
786
+ }
787
+ }
788
+ return 1;
789
+ }