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,375 @@
1
+ /*
2
+ * Copyright (C) 2021 Tai Chi Minh Ralph Eastwood, Matt Wong and Guarnerix 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_API_H
10
+ #define ZSV_API_H
11
+
12
+ #define ZSV_ROW_MAX_SIZE_DEFAULT 65536
13
+ #define ZSV_ROW_MAX_SIZE_DEFAULT_S "64k"
14
+
15
+ #define ZSV_MAX_COLS_DEFAULT 1024
16
+
17
+ #define ZSV_ROW_MAX_SIZE_MIN 1024
18
+ #define ZSV_ROW_MAX_SIZE_MIN_S "1024"
19
+
20
+ #define ZSV_MIN_SCANNER_BUFFSIZE 4096
21
+ #define ZSV_DEFAULT_SCANNER_BUFFSIZE (1 << 18) // 256k
22
+
23
+ #include "zsv_export.h"
24
+ /*****************************************************************************
25
+ * libzsv API
26
+ *
27
+ * Functions provided by the zsv library are described herein. This document is
28
+ * organized into three sections:
29
+ * - required functions. any time libzsv is used and any input at all is parsed,
30
+ * each of these functions should be used (usually, exactly once)
31
+ * - minimal access functions. these functions are generally necessary to use
32
+ * libzsv for any non-trivial task
33
+ * - other functions
34
+ ******************************************************************************/
35
+
36
+ /******************************************************************************
37
+ * Required functions:
38
+ * - zsv_new(): allocate a parser
39
+ * - zsv_parse_more(): parse some data
40
+ * - zsv_finish(): tie up loose ends
41
+ * - zsv_delete(): dispose the parser
42
+ ******************************************************************************/
43
+
44
+ ZSV_EXPORT
45
+ int zsv_peek(zsv_parser);
46
+
47
+ /**
48
+ * Create a zsv parser. Typically, passed options will at least include a
49
+ * a `row_handler()` callback. Many, but not all, options can be subsequently
50
+ * set or modified after calling `zsv_new()`
51
+ *
52
+ * @param options see `struct zsv_opts` in common.h
53
+ * @returns zsv parser handle
54
+ */
55
+ ZSV_EXPORT
56
+ zsv_parser zsv_new(struct zsv_opts *opts);
57
+
58
+ /**
59
+ * Parse the next chunk of data from the input stream:
60
+ * - Immediately after a cell (column) delimiter is parsed, the configured
61
+ * `cell_handler()` callback, if any, is called
62
+ * - Immediately after a row delimiter is parsed, the configured
63
+ * `row_handler()` callback, if any, is called.
64
+ *
65
+ * @param parser
66
+ * @returns zsv_status_ok if more data remains to be parsed,
67
+ * zsv_status_no_more_input if the stream's EOF has been reached,
68
+ * or other zsv status code in the event of error or cancellation
69
+ */
70
+ ZSV_EXPORT enum zsv_status zsv_parse_more(zsv_parser parser);
71
+
72
+ /**
73
+ * Finish any remaining processing, after all input has been read
74
+ */
75
+ ZSV_EXPORT enum zsv_status zsv_finish(zsv_parser);
76
+
77
+ /**
78
+ * Dispose of a parser that was created with `zsv_new()` or `zsv_new_with_properties()`
79
+ */
80
+ ZSV_EXPORT enum zsv_status zsv_delete(zsv_parser);
81
+
82
+ /******************************************************************************
83
+ * minimal access functions:
84
+ * - zsv_cell_count(): get the number of cells in the row
85
+ * - zsv_get_cell(): retrieve a cell value
86
+ ******************************************************************************/
87
+
88
+ /**
89
+ * Get the number of cells in the row that was just parsed. This function
90
+ * is typically called from within your `row_handler()` callback. In the event
91
+ * that the last row did not contain a single cell delimiter, returns 1
92
+ *
93
+ * @param parser
94
+ * @returns number, >= 1, of cells in the row that was just parsed
95
+ */
96
+ ZSV_EXPORT
97
+ size_t zsv_cell_count(zsv_parser parser);
98
+
99
+ /**
100
+ * Get the contents of a cell in the row that was just parsed. See `struct zsv_cell`
101
+ * in common.h for further details
102
+ *
103
+ * @param parser
104
+ * @param index zero-based index of the cell to fetch
105
+ * @return `zsv_cell` structure with the bytes and length of this cell value
106
+ *
107
+ * An example of a `row_handler()` loop to print each cell in a row might be:
108
+ * ```
109
+ * size_t cell_count = zsv_cell_count(parser);
110
+ * for(size_t i = 0; i < cell_count; i++) {
111
+ * struct zsv_cell c = zsv_get_cell(parser, i);
112
+ * printf("%.*s", c.len, (const char *)c.str);
113
+ * }
114
+ * ```
115
+ */
116
+ struct zsv_cell zsv_get_cell(zsv_parser parser, size_t index);
117
+
118
+ /**
119
+ * `zsv_get_cell_len()` is not needed in most cases, but may be useful in
120
+ * restrictive cases such as when calling from Javascript into wasm
121
+ *
122
+ * @param parser
123
+ * @param ix 0-based index of the cell to copy
124
+ * @return length of cell contents (0 if cell is empty)
125
+ */
126
+ ZSV_EXPORT
127
+ size_t zsv_get_cell_len(zsv_parser parser, size_t ix);
128
+
129
+ /**
130
+ * get a pointer to the cell contents (not NULL-terminated)
131
+ * @param parser
132
+ * @param ix 0-based index of the cell to copy. Caller must ensure validity
133
+ * @return pointer to the cell contents, or NULL if contents are empty
134
+ */
135
+ ZSV_EXPORT
136
+ unsigned char *zsv_get_cell_str(zsv_parser parser, size_t ix);
137
+
138
+ /******************************************************************************
139
+ * other functions
140
+ ******************************************************************************/
141
+
142
+ /**
143
+ * Get the library version
144
+ */
145
+ ZSV_EXPORT
146
+ const char *zsv_lib_version(void);
147
+
148
+ /**
149
+ * Change a parser's row handler. This function may be called at any time
150
+ * during the parsing process to change the row handler that is called each
151
+ * time a row is parsed
152
+ *
153
+ * @param parser
154
+ * @param row_handler new callback value
155
+ */
156
+ ZSV_EXPORT void zsv_set_row_handler(zsv_parser, void (*row_handler)(void *ctx));
157
+
158
+ /**
159
+ * Check if the row we just parsed consisted entirely of blank data
160
+ *
161
+ * @param parser
162
+ * @return non-zero if blank, 0 if non-blank
163
+ */
164
+ ZSV_EXPORT
165
+ char zsv_row_is_blank(zsv_parser parser);
166
+
167
+ /**
168
+ * Set the context pointer that is passed to our callbacks
169
+ * @param parser
170
+ * @param ctx new context pointer value
171
+ */
172
+ ZSV_EXPORT
173
+ void zsv_set_context(zsv_parser parser, void *ctx);
174
+
175
+ /**
176
+ * Set the read function that is invoked by `zsv_parse_more()` to fetch more data.
177
+ * If not explicitly set, defaults to fread
178
+ *
179
+ * @param parser
180
+ * @param read_function
181
+ * @param stream value that is passed to read_function when it is called
182
+ */
183
+ ZSV_EXPORT
184
+ void zsv_set_read(zsv_parser parser, size_t (*read_func)(void *restrict, size_t n, size_t size, void *restrict));
185
+
186
+ /**
187
+ * Set the input stream our parser reads from. If not explicity set, defaults to
188
+ * stdin. This can be used to read multiple inputs as a single combined input
189
+ * by calling `zsv_set_input()` after `zsv_parse_more()` returns
190
+ * `zsv_status_no_more_input`
191
+ */
192
+ ZSV_EXPORT
193
+ void zsv_set_input(zsv_parser, void *in);
194
+
195
+ /**
196
+ * Insert a filter to process or modify, before parsing, the next chunk of raw
197
+ * bytes read from the input stream. For example, to save a copy of the raw
198
+ * input to a file, `zsv_set_scan_filter()` could be called with
199
+ * `zsv_filter_write` passed as the filter argument, and the target FILE *
200
+ * passed as the context pointer.
201
+ *
202
+ * @param parser
203
+ * @param filter callback that is called on each chunk that is read from the
204
+ * input stream, before the chunk is parsed. The callback may
205
+ * modify the contents of the buffer so long as its return value
206
+ * does not exceed the bufflen it was passed
207
+ */
208
+ ZSV_EXPORT enum zsv_status zsv_set_scan_filter(zsv_parser parser,
209
+ size_t (*filter)(void *ctx, unsigned char *buff, size_t bufflen),
210
+ void *ctx);
211
+
212
+ /**
213
+ * Set parsing mode to fixed-width. Once set to fixed mode, a parser may not be
214
+ * set back to CSV mode
215
+ * @return status code
216
+ * @param parser parser handle
217
+ * @param count number of elements in offsets
218
+ * @param offsets array of cell-end offsets. offsets[0] should be the length of the first cell
219
+ */
220
+ ZSV_EXPORT enum zsv_status zsv_set_fixed_offsets(zsv_parser parser, size_t count, size_t *offsets);
221
+
222
+ /**
223
+ * Parse a buffer of bytes. This function is usually not needed, but
224
+ * can be used to parse in a push instead of pull manner
225
+ *
226
+ * @param parser
227
+ * @param buff the input buffer to parse. This buffer may not overlap with
228
+ * the parser buffer!
229
+ * @param len length of the input to parse
230
+ */
231
+ ZSV_EXPORT enum zsv_status zsv_parse_bytes(zsv_parser parser, const unsigned char *restrict buff, size_t len);
232
+
233
+ /**
234
+ * Get a text description of a status code
235
+ */
236
+ ZSV_EXPORT
237
+ const unsigned char *zsv_parse_status_desc(enum zsv_status status);
238
+
239
+ /**
240
+ * Set a column filter for the fast scanner engine. When set, only the
241
+ * specified columns receive full processing (quote normalization, UTF-8
242
+ * encoding). Other columns are stored as raw pointers into the input
243
+ * buffer without normalization. Has no effect on the standard engine.
244
+ *
245
+ * @param parser
246
+ * @param col_indices array of 0-based column indices to fully process
247
+ * @param count number of elements in col_indices
248
+ * @return zsv_status_ok on success
249
+ */
250
+ ZSV_EXPORT
251
+ enum zsv_status zsv_set_column_filter(zsv_parser parser, const unsigned int *col_indices, unsigned int count);
252
+
253
+ /**
254
+ * Hint to the parser that cell data is not needed. When set, the fast
255
+ * scanner engine skips per-cell storage and uses popcount-only row
256
+ * counting. The row_handler callback is still invoked for each row.
257
+ * Has no effect on the standard scanner engine.
258
+ *
259
+ * @param parser
260
+ * @param skip non-zero to skip cell storage, 0 to resume normal parsing
261
+ */
262
+ ZSV_EXPORT
263
+ void zsv_set_skip_cells(zsv_parser parser, int skip);
264
+
265
+ /**
266
+ * Abort parsing. After this function is called, no further
267
+ * `row_handler()` or `cell_handler()` calls will be made, and parse functions
268
+ * will return zsv_status_cancelled
269
+ */
270
+ ZSV_EXPORT
271
+ void zsv_abort(zsv_parser);
272
+
273
+ /**
274
+ * @return number of bytes scanned from the last zsv_parse_more() invocation
275
+ */
276
+ ZSV_EXPORT size_t zsv_scanned_length(zsv_parser);
277
+
278
+ /**
279
+ * @return cumulative number of bytes scanned across all requests by this parser
280
+ */
281
+ ZSV_EXPORT size_t zsv_cum_scanned_length(zsv_parser parser);
282
+
283
+ /**
284
+ * @return number of raw bytes scanned from the beginning of the row
285
+ * to the end of this row. Subtract from zsv_cum_scanned_length() to get the
286
+ * position of the beginning of the row
287
+ */
288
+ ZSV_EXPORT size_t zsv_row_length_raw_bytes(zsv_parser parser);
289
+
290
+ /**
291
+ * Check the quoted status of the last cell that was read. This function is only
292
+ * applicable when called from within a cell_handler() callback. Furthermore, this
293
+ * function is generally only useful when the cell value will subsequent be
294
+ * output in CSV format
295
+ *
296
+ * @returns 0 if value will *not* need to be quoted when output as CSV, or
297
+ * 1 if it might need to be quoted
298
+ */
299
+ ZSV_EXPORT
300
+ char zsv_quoted(zsv_parser parser);
301
+
302
+ /**
303
+ * Create a zsv_opts structure and return its handle
304
+ *
305
+ * This is only necessary in environments where structures cannot be directly
306
+ * instantiated such as web assembly. Otherwise, you should avoid this function
307
+ * and just create a `struct zsv_opts` on the stack
308
+ *
309
+ * Each argument to `zsv_opts_new()` corresponds to the same-named `struct zsv_opts` element
310
+ * See common.h for details
311
+ */
312
+ ZSV_EXPORT struct zsv_opts *zsv_opts_new(void (*row_handler)(void *ctx),
313
+ void (*cell_handler)(void *ctx, unsigned char *utf8_value, size_t len),
314
+ void *ctx, zsv_generic_read read, void *stream, unsigned char *buff,
315
+ size_t buffsize, unsigned max_columns, unsigned max_row_size, char delimiter,
316
+ char no_quotes
317
+ #ifdef ZSV_EXTRAS
318
+ ,
319
+ size_t max_rows
320
+ #endif
321
+ );
322
+
323
+ /**
324
+ * Destroy an option structure that was created by zsv_opts_new()
325
+ */
326
+ ZSV_EXPORT void zsv_opts_delete(struct zsv_opts *);
327
+
328
+ /******************************************************************************
329
+ * Pull parsing functions
330
+ ******************************************************************************/
331
+
332
+ /**
333
+ * To use pull parsing, do not use row or cell handlers, handler context
334
+ * or zsv_parse_more(). Instead, call zsv_next_row() until a non-ok result
335
+ * @param parser parser handle
336
+ * @return zsv_status_ok on success, other status code on error
337
+ */
338
+ ZSV_EXPORT
339
+ enum zsv_status zsv_next_row(zsv_parser parser);
340
+
341
+ /******************************************************************************
342
+ * Miscellaneous functions used by the parser that may have standalone utility
343
+ ******************************************************************************/
344
+
345
+ /**
346
+ * Force a string to conform to UTF8 encoding. Replaces any non-conforming utf8
347
+ * with the specified char, or removes from the string (and shortens the string)
348
+ * if replace = 0
349
+ * @param s input string. invalid UTF8 bytes will be overwritten
350
+ * @param n length (in bytes) of input
351
+ * @param replace the character to replace any malformed UTF8 bytes with, or 0
352
+ * to remove and shorten the result
353
+ * @param callback optional callback invoked upon scanning malformed UTF8
354
+ * @param ctx context pointer passed to callback
355
+ * @return length of the valid string
356
+ */
357
+ ZSV_EXPORT
358
+ size_t zsv_strencode(unsigned char *s, size_t n, unsigned char replace,
359
+ int (*malformed_handler)(void *, const unsigned char *s, size_t n, size_t offset),
360
+ void *handler_ctx);
361
+
362
+ /**
363
+ * Scan a string for malformed UTF8 without modifying it
364
+ * @param s input string (read-only)
365
+ * @param n length (in bytes) of input
366
+ * @param callback optional callback invoked upon scanning malformed UTF8
367
+ * @param ctx context pointer passed to callback
368
+ * @return number of malformed UTF8 sequences found
369
+ */
370
+ ZSV_EXPORT
371
+ size_t zsv_strencode_validate(const unsigned char *s, size_t n,
372
+ int (*malformed_handler)(void *, const unsigned char *s, size_t n, size_t offset),
373
+ void *handler_ctx);
374
+
375
+ #endif