vsql-node 0.0.0-stage → 1.0.0

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 (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +203 -2
  3. package/binding.gyp +61 -0
  4. package/index.d.ts +133 -0
  5. package/internal/amalgamation/LICENSE +21 -0
  6. package/internal/amalgamation/LICENSE-V +21 -0
  7. package/internal/amalgamation/afunix.h +26 -0
  8. package/internal/amalgamation/atomic.h +571 -0
  9. package/internal/amalgamation/core_windows.c +25 -0
  10. package/internal/amalgamation/core_windows.h +9 -0
  11. package/internal/amalgamation/execute_capture_nix.c +7 -0
  12. package/internal/amalgamation/execute_capture_nix.h +266 -0
  13. package/internal/amalgamation/gc/cord.h +426 -0
  14. package/internal/amalgamation/gc/cord_pos.h +151 -0
  15. package/internal/amalgamation/gc/ec.h +91 -0
  16. package/internal/amalgamation/gc/gc.h +2826 -0
  17. package/internal/amalgamation/gc/gc_allocator.h +480 -0
  18. package/internal/amalgamation/gc/gc_backptr.h +109 -0
  19. package/internal/amalgamation/gc/gc_config_macros.h +548 -0
  20. package/internal/amalgamation/gc/gc_disclaim.h +85 -0
  21. package/internal/amalgamation/gc/gc_gcj.h +123 -0
  22. package/internal/amalgamation/gc/gc_inline.h +261 -0
  23. package/internal/amalgamation/gc/gc_mark.h +516 -0
  24. package/internal/amalgamation/gc/gc_pthread_redirects.h +140 -0
  25. package/internal/amalgamation/gc/gc_tiny_fl.h +107 -0
  26. package/internal/amalgamation/gc/gc_typed.h +196 -0
  27. package/internal/amalgamation/gc/gc_version.h +55 -0
  28. package/internal/amalgamation/gc/javaxfc.h +76 -0
  29. package/internal/amalgamation/gc/leak_detector.h +120 -0
  30. package/internal/amalgamation/gc.c +45932 -0
  31. package/internal/amalgamation/gc.h +4 -0
  32. package/internal/amalgamation/gc_boehm_helpers.c +64 -0
  33. package/internal/amalgamation/gc_debugger_linux.h +61 -0
  34. package/internal/amalgamation/getrandom_linux.h +33 -0
  35. package/internal/amalgamation/ipv6_v6only.h +5 -0
  36. package/internal/amalgamation/libvsql.c +27 -0
  37. package/internal/amalgamation/libvsql.h +1772 -0
  38. package/internal/amalgamation/libvsql_linux_amd64.c +106591 -0
  39. package/internal/amalgamation/libvsql_linux_arm64.c +106571 -0
  40. package/internal/amalgamation/libvsql_macos_amd64.c +106728 -0
  41. package/internal/amalgamation/libvsql_macos_arm64.c +106708 -0
  42. package/internal/amalgamation/libvsql_windows_amd64.c +108235 -0
  43. package/internal/amalgamation/segfault_handler_nix.c +56 -0
  44. package/internal/amalgamation/segfault_handler_nix.h +449 -0
  45. package/internal/amalgamation/segfault_handler_windows.h +52 -0
  46. package/internal/amalgamation/thread_helper.c +12 -0
  47. package/internal/amalgamation/thread_helper.h +96 -0
  48. package/internal/amalgamation/ticks_windows.c +3 -0
  49. package/internal/amalgamation/ticks_windows.h +31 -0
  50. package/internal/amalgamation/ticks_windows_api.h +6 -0
  51. package/internal/amalgamation/vsql_runtime.c +14 -0
  52. package/internal/amalgamation/vsql_runtime_linux.c +6 -0
  53. package/internal/amalgamation/vsql_runtime_macos.c +6 -0
  54. package/internal/amalgamation/vsql_runtime_windows.c +6 -0
  55. package/lib/binding.js +13 -0
  56. package/lib/connection.js +492 -0
  57. package/lib/convert.js +213 -0
  58. package/lib/errors.js +17 -0
  59. package/lib/index.js +41 -0
  60. package/libvsql_flags.gypi +30 -0
  61. package/package.json +45 -4
  62. package/prebuilds/darwin-arm64/vsql-node.glibc.node +0 -0
  63. package/prebuilds/darwin-x64/vsql-node.glibc.node +0 -0
  64. package/prebuilds/linux-arm64/vsql-node.glibc.node +0 -0
  65. package/prebuilds/linux-x64/vsql-node.glibc.node +0 -0
  66. package/prebuilds/win32-x64/vsql-node.glibc.node +0 -0
  67. package/src/binding.c +773 -0
  68. package/src/runtime.c +20 -0
@@ -0,0 +1,266 @@
1
+ #ifndef V_OS_EXECUTE_CAPTURE_NIX_H
2
+ #define V_OS_EXECUTE_CAPTURE_NIX_H
3
+
4
+ #ifndef V_OS_EXECUTE_SCOPE
5
+ #define V_OS_EXECUTE_SCOPE static inline
6
+ #endif
7
+
8
+ // v_os_execute_set_cloexec marks an fd as close-on-exec. When multiple threads
9
+ // each call os.execute, every pipe() they create is briefly visible to all of
10
+ // them; without FD_CLOEXEC, one thread's spawned child can inherit another
11
+ // thread's pipe write end, keeping that pipe open after the intended writer
12
+ // exits. The reader then never observes EOF and the captured output ends up
13
+ // empty (seen on macOS arm64 in CI). Setting CLOEXEC closes the fd
14
+ // automatically across exec, which fixes the race for both fork/execvp and
15
+ // posix_spawn paths.
16
+ V_OS_EXECUTE_SCOPE void v_os_execute_set_cloexec(int fd) {
17
+ int flags = fcntl(fd, F_GETFD, 0);
18
+ if (flags >= 0) {
19
+ fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
20
+ }
21
+ }
22
+
23
+ #if defined(__ANDROID__) && (!defined(__ANDROID_API__) || __ANDROID_API__ < 28)
24
+ // Android API levels below 28 do not provide posix_spawn(). Fall back to
25
+ // fork()/execvp() with a pipe; this is what popen() does internally and
26
+ // is sufficient for capturing the merged stdout+stderr of a shell command.
27
+ V_OS_EXECUTE_SCOPE int v_os_execute_capture_start(const char *cmd, int *child_pid, int *read_fd) {
28
+ int pipefd[2];
29
+ if (pipe(pipefd) != 0) {
30
+ return -1;
31
+ }
32
+ v_os_execute_set_cloexec(pipefd[0]);
33
+ v_os_execute_set_cloexec(pipefd[1]);
34
+ pid_t pid = fork();
35
+ if (pid < 0) {
36
+ close(pipefd[0]);
37
+ close(pipefd[1]);
38
+ return -1;
39
+ }
40
+ if (pid == 0) {
41
+ // child: redirect stdout+stderr to the pipe, then exec the shell.
42
+ // dup2 clears FD_CLOEXEC on the destination, so STDOUT/STDERR stay
43
+ // open across exec while the original pipe fds are auto-closed.
44
+ dup2(pipefd[1], STDOUT_FILENO);
45
+ dup2(pipefd[1], STDERR_FILENO);
46
+ close(pipefd[0]);
47
+ close(pipefd[1]);
48
+ execlp("sh", "sh", "-c", cmd, (char *)NULL);
49
+ _exit(127);
50
+ }
51
+ close(pipefd[1]);
52
+ *child_pid = (int)pid;
53
+ *read_fd = pipefd[0];
54
+ return 0;
55
+ }
56
+
57
+ V_OS_EXECUTE_SCOPE int v_os_exec_capture_start(char *const argv[], int *child_pid, int *read_fd) {
58
+ if (argv == NULL || argv[0] == NULL) {
59
+ return -1;
60
+ }
61
+ int pipefd[2];
62
+ if (pipe(pipefd) != 0) {
63
+ return -1;
64
+ }
65
+ v_os_execute_set_cloexec(pipefd[0]);
66
+ v_os_execute_set_cloexec(pipefd[1]);
67
+ pid_t pid = fork();
68
+ if (pid < 0) {
69
+ close(pipefd[0]);
70
+ close(pipefd[1]);
71
+ return -1;
72
+ }
73
+ if (pid == 0) {
74
+ dup2(pipefd[1], STDOUT_FILENO);
75
+ dup2(pipefd[1], STDERR_FILENO);
76
+ close(pipefd[0]);
77
+ close(pipefd[1]);
78
+ execvp(argv[0], argv);
79
+ _exit(127);
80
+ }
81
+ close(pipefd[1]);
82
+ *child_pid = (int)pid;
83
+ *read_fd = pipefd[0];
84
+ return 0;
85
+ }
86
+ #else
87
+ // Use opaque void* declarations for posix_spawn instead of #include <spawn.h>.
88
+ // Including <spawn.h> transitively pulls in <features.h>/<sys/cdefs.h>, which
89
+ // breaks under musl-gcc on the Ubuntu docker image where <sys/cdefs.h> is not
90
+ // on the include path. The 128-byte buffer is comfortably larger than the
91
+ // real posix_spawn_file_actions_t on glibc (~80 bytes) and musl (~40 bytes);
92
+ // posix_spawn_file_actions_init() initializes the buffer, so its true layout
93
+ // is not needed here. We pass NULL for posix_spawnattr_t, so it is not
94
+ // declared. Calling these via void* is ABI-compatible: pointer parameters
95
+ // are passed identically regardless of pointee type.
96
+ typedef struct { unsigned char _opaque[128]; } v_posix_spawn_file_actions_t;
97
+ #ifdef __cplusplus
98
+ extern "C" {
99
+ #endif
100
+ extern int posix_spawn(pid_t *, const char *, const void *, const void *, char *const [], char *const []);
101
+ extern int posix_spawnp(pid_t *, const char *, const void *, const void *, char *const [], char *const []);
102
+ extern int posix_spawn_file_actions_init(void *);
103
+ extern int posix_spawn_file_actions_destroy(void *);
104
+ extern int posix_spawn_file_actions_adddup2(void *, int, int);
105
+ extern int posix_spawn_file_actions_addclose(void *, int);
106
+
107
+ extern char **environ;
108
+ #ifdef __cplusplus
109
+ }
110
+ #endif
111
+
112
+ V_OS_EXECUTE_SCOPE int v_os_execute_capture_start(const char *cmd, int *child_pid, int *read_fd) {
113
+ int pipefd[2];
114
+ if (pipe(pipefd) != 0) {
115
+ return -1;
116
+ }
117
+ v_os_execute_set_cloexec(pipefd[0]);
118
+ v_os_execute_set_cloexec(pipefd[1]);
119
+ v_posix_spawn_file_actions_t actions;
120
+ if (posix_spawn_file_actions_init(&actions) != 0) {
121
+ close(pipefd[0]);
122
+ close(pipefd[1]);
123
+ return -1;
124
+ }
125
+ int err = 0;
126
+ if ((err = posix_spawn_file_actions_adddup2(&actions, pipefd[1], STDOUT_FILENO)) != 0
127
+ || (err = posix_spawn_file_actions_adddup2(&actions, pipefd[1], STDERR_FILENO)) != 0
128
+ || (err = posix_spawn_file_actions_addclose(&actions, pipefd[0])) != 0
129
+ || (err = posix_spawn_file_actions_addclose(&actions, pipefd[1])) != 0) {
130
+ posix_spawn_file_actions_destroy(&actions);
131
+ close(pipefd[0]);
132
+ close(pipefd[1]);
133
+ return -1;
134
+ }
135
+ char *const argv[] = {(char *)"/bin/sh", (char *)"-c", (char *)cmd, NULL};
136
+ err = posix_spawn(child_pid, "/bin/sh", &actions, NULL, argv, environ);
137
+ posix_spawn_file_actions_destroy(&actions);
138
+ if (err != 0) {
139
+ close(pipefd[0]);
140
+ close(pipefd[1]);
141
+ return -1;
142
+ }
143
+ close(pipefd[1]);
144
+ *read_fd = pipefd[0];
145
+ return 0;
146
+ }
147
+
148
+ V_OS_EXECUTE_SCOPE int v_os_exec_capture_start(char *const argv[], int *child_pid, int *read_fd) {
149
+ if (argv == NULL || argv[0] == NULL) {
150
+ return -1;
151
+ }
152
+ int pipefd[2];
153
+ if (pipe(pipefd) != 0) {
154
+ return -1;
155
+ }
156
+ v_os_execute_set_cloexec(pipefd[0]);
157
+ v_os_execute_set_cloexec(pipefd[1]);
158
+ v_posix_spawn_file_actions_t actions;
159
+ if (posix_spawn_file_actions_init(&actions) != 0) {
160
+ close(pipefd[0]);
161
+ close(pipefd[1]);
162
+ return -1;
163
+ }
164
+ int err = 0;
165
+ if ((err = posix_spawn_file_actions_adddup2(&actions, pipefd[1], STDOUT_FILENO)) != 0
166
+ || (err = posix_spawn_file_actions_adddup2(&actions, pipefd[1], STDERR_FILENO)) != 0
167
+ || (err = posix_spawn_file_actions_addclose(&actions, pipefd[0])) != 0
168
+ || (err = posix_spawn_file_actions_addclose(&actions, pipefd[1])) != 0) {
169
+ posix_spawn_file_actions_destroy(&actions);
170
+ close(pipefd[0]);
171
+ close(pipefd[1]);
172
+ return -1;
173
+ }
174
+ err = posix_spawnp(child_pid, argv[0], &actions, NULL, argv, environ);
175
+ posix_spawn_file_actions_destroy(&actions);
176
+ if (err != 0) {
177
+ close(pipefd[0]);
178
+ close(pipefd[1]);
179
+ return -1;
180
+ }
181
+ close(pipefd[1]);
182
+ *read_fd = pipefd[0];
183
+ return 0;
184
+ }
185
+ #endif
186
+
187
+ // v_os_fd_write_all writes the whole buffer to the fd, retrying short writes.
188
+ // It is defined on all nix platforms because fastc references it
189
+ // unconditionally (see vlib/v/gen/fastc/unit_compile_nix.c.v); guarding it
190
+ // behind __APPLE__ left it undefined at link time on Linux.
191
+ V_OS_EXECUTE_SCOPE void v_os_fd_write_all(int fd, const char *data, size_t len) {
192
+ while (len > 0) {
193
+ ssize_t written = write(fd, data, len);
194
+ if (written <= 0) {
195
+ return;
196
+ }
197
+ data += written;
198
+ len -= (size_t)written;
199
+ }
200
+ }
201
+
202
+ #if defined(__APPLE__)
203
+ // v_os_exec_capture_input_start is the argv-based capture helper with an
204
+ // additional pipe connected to the child's stdin. F_SETNOSIGPIPE makes a
205
+ // writer observe EPIPE instead of terminating the parent when a child exits
206
+ // after reporting an early input error.
207
+ V_OS_EXECUTE_SCOPE int v_os_exec_capture_input_start(char *const argv[], int *child_pid, int *read_fd, int *write_fd) {
208
+ if (argv == NULL || argv[0] == NULL) {
209
+ return -1;
210
+ }
211
+ int output_pipe[2];
212
+ int input_pipe[2];
213
+ if (pipe(output_pipe) != 0) {
214
+ return -1;
215
+ }
216
+ if (pipe(input_pipe) != 0) {
217
+ close(output_pipe[0]);
218
+ close(output_pipe[1]);
219
+ return -1;
220
+ }
221
+ v_os_execute_set_cloexec(output_pipe[0]);
222
+ v_os_execute_set_cloexec(output_pipe[1]);
223
+ v_os_execute_set_cloexec(input_pipe[0]);
224
+ v_os_execute_set_cloexec(input_pipe[1]);
225
+ fcntl(input_pipe[1], F_SETNOSIGPIPE, 1);
226
+ v_posix_spawn_file_actions_t actions;
227
+ if (posix_spawn_file_actions_init(&actions) != 0) {
228
+ close(output_pipe[0]);
229
+ close(output_pipe[1]);
230
+ close(input_pipe[0]);
231
+ close(input_pipe[1]);
232
+ return -1;
233
+ }
234
+ int err = 0;
235
+ if ((err = posix_spawn_file_actions_adddup2(&actions, input_pipe[0], STDIN_FILENO)) != 0
236
+ || (err = posix_spawn_file_actions_adddup2(&actions, output_pipe[1], STDOUT_FILENO)) != 0
237
+ || (err = posix_spawn_file_actions_adddup2(&actions, output_pipe[1], STDERR_FILENO)) != 0
238
+ || (err = posix_spawn_file_actions_addclose(&actions, input_pipe[0])) != 0
239
+ || (err = posix_spawn_file_actions_addclose(&actions, input_pipe[1])) != 0
240
+ || (err = posix_spawn_file_actions_addclose(&actions, output_pipe[0])) != 0
241
+ || (err = posix_spawn_file_actions_addclose(&actions, output_pipe[1])) != 0) {
242
+ posix_spawn_file_actions_destroy(&actions);
243
+ close(output_pipe[0]);
244
+ close(output_pipe[1]);
245
+ close(input_pipe[0]);
246
+ close(input_pipe[1]);
247
+ return -1;
248
+ }
249
+ err = posix_spawnp(child_pid, argv[0], &actions, NULL, argv, environ);
250
+ posix_spawn_file_actions_destroy(&actions);
251
+ if (err != 0) {
252
+ close(output_pipe[0]);
253
+ close(output_pipe[1]);
254
+ close(input_pipe[0]);
255
+ close(input_pipe[1]);
256
+ return -1;
257
+ }
258
+ close(output_pipe[1]);
259
+ close(input_pipe[0]);
260
+ *read_fd = output_pipe[0];
261
+ *write_fd = input_pipe[1];
262
+ return 0;
263
+ }
264
+ #endif
265
+
266
+ #endif // V_OS_EXECUTE_CAPTURE_NIX_H
@@ -0,0 +1,426 @@
1
+ /*
2
+ * Copyright (c) 1993-1994 by Xerox Corporation. All rights reserved.
3
+ *
4
+ * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5
+ * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6
+ *
7
+ * Permission is hereby granted to use or copy this program
8
+ * for any purpose, provided the above notices are retained on all copies.
9
+ * Permission to modify the code and to distribute modified code is granted,
10
+ * provided the above notices are retained, and a notice that the code was
11
+ * modified is included with the above copyright notice.
12
+ */
13
+
14
+ /*
15
+ * Cords are immutable character strings. A number of operations on long
16
+ * cords are much more efficient than their counterpart in platform
17
+ * `strings.h` file. In particular, concatenation takes constant time
18
+ * independent of the length of the arguments. (Cords are represented as
19
+ * trees, with internal nodes representing concatenation and leaves
20
+ * consisting of either C strings or a functional description of the string.)
21
+ *
22
+ * The following are reasonable applications of cords. They would perform
23
+ * unacceptably if C strings were used:
24
+ * - A compiler that produces assembly language output by repeatedly
25
+ * concatenating instructions onto a cord representing the output file;
26
+ * - A text editor that converts the input file to a cord, and then
27
+ * performs editing operations by producing a new cord representing the
28
+ * file after each character change (and keeping the old ones in an edit
29
+ * history).
30
+ *
31
+ * For optimal performance, cords should be built by concatenating short
32
+ * sections. This interface is designed for maximum compatibility with
33
+ * C strings. ASCII NUL characters may be embedded in cords using
34
+ * `CORD_from_fn`. This is handled correctly, but `CORD_to_char_star` will
35
+ * produce a string with embedded NUL characters when given such a cord.
36
+ *
37
+ * This interface is fairly big, largely for performance reasons.
38
+ * The most basic constants, functions and types:
39
+ *
40
+ * - `CORD` - the type of a cord;
41
+ * - `CORD_EMPTY` - empty cord;
42
+ * - `CORD_len(cord)` - length of a cord;
43
+ * - `CORD_cat(cord1, cord2)` - concatenation of two cords;
44
+ * - `CORD_substr(cord, start, len)` - substring (or subcord);
45
+ * - `CORD_pos i; CORD_FOR(i, cord) { ... CORD_pos_fetch(i) ... }` - examine
46
+ * each character in a cord (`CORD_pos_fetch(i)` is the `char`);
47
+ * - `CORD_fetch(i)` - retrieve `i`'th character (slowly);
48
+ * - `CORD_cmp(cord1, cord2)` - compare two cords;
49
+ * - `CORD_from_file(FILE *f)` - turn a read-only file into a cord;
50
+ * - `CORD_to_char_star(cord)` - convert to C string (non-`NULL` C constant
51
+ * strings are cords);
52
+ * - `CORD_printf(CORD format, ...)` and friends - cord's variant of
53
+ * `printf` (use "%r" for cords).
54
+ */
55
+
56
+ #ifndef CORD_H
57
+ #define CORD_H
58
+
59
+ #include <stddef.h>
60
+ #include <stdio.h>
61
+
62
+ #ifdef __cplusplus
63
+ extern "C" {
64
+ #endif
65
+
66
+ #if defined(GC_DLL) && !defined(CORD_NOT_DLL) && !defined(CORD_API)
67
+ /* Same as for `GC_API` in `gc_config_macros.h` file. */
68
+ # ifdef CORD_BUILD
69
+ # if defined(__MINGW32__) && !defined(__cplusplus) || defined(__CEGCC__)
70
+ # define CORD_API __declspec(dllexport)
71
+ # elif defined(_MSC_VER) || defined(__DMC__) || defined(__BORLANDC__) \
72
+ || defined(__CYGWIN__) || defined(__MINGW32__) \
73
+ || defined(__WATCOMC__)
74
+ # define CORD_API extern __declspec(dllexport)
75
+ # elif defined(__GNUC__) && !defined(GC_NO_VISIBILITY) \
76
+ && (__GNUC__ >= 4 || defined(GC_VISIBILITY_HIDDEN_SET))
77
+ /* Only matters if used in conjunction with `-fvisibility=hidden` option. */
78
+ # define CORD_API extern __attribute__((__visibility__("default")))
79
+ # endif
80
+ # else /* !CORD_BUILD */
81
+ # if defined(__BORLANDC__) || defined(__CEGCC__) || defined(__CYGWIN__) \
82
+ || defined(__DMC__) || defined(_MSC_VER)
83
+ # define CORD_API __declspec(dllimport)
84
+ # elif defined(__MINGW32__) || defined(__WATCOMC__)
85
+ # define CORD_API extern __declspec(dllimport)
86
+ # endif
87
+ # endif
88
+ #endif /* GC_DLL */
89
+
90
+ #ifndef CORD_API
91
+ # define CORD_API extern
92
+ #endif
93
+
94
+ /**
95
+ * Cords have type `const char *`. This is cheating quite a bit, and not
96
+ * 100% portable. But it means that nonempty character string constants
97
+ * may be used as cords directly, provided the string is never modified in
98
+ * place. The empty cord is represented by, and can be written as, `NULL`.
99
+ */
100
+ typedef const char *CORD;
101
+
102
+ /** An empty cord is always represented as nil. */
103
+ #define CORD_EMPTY 0
104
+
105
+ /** Is a nonempty cord represented as a C string? */
106
+ #define CORD_IS_STRING(s) (*(s) != '\0')
107
+
108
+ /**
109
+ * Concatenate two cords. If the arguments are C strings, they may not
110
+ * be subsequently altered.
111
+ */
112
+ CORD_API CORD CORD_cat(CORD, CORD);
113
+
114
+ /**
115
+ * Concatenate a cord and a C string with known length. Except for the
116
+ * empty string case, this is a special case of `CORD_cat`. Since the
117
+ * length is known, it can be faster. The string `y` is shared with
118
+ * the resulting cord. Hence it should not be altered by the caller.
119
+ */
120
+ CORD_API CORD CORD_cat_char_star(CORD /* `x` */, const char * /* `y` */,
121
+ size_t /* `y_len` */);
122
+
123
+ /** Compute the length of a cord. */
124
+ CORD_API size_t CORD_len(CORD);
125
+
126
+ /** Cords may be represented by functions defining the `i`-th character. */
127
+ typedef char (*CORD_fn)(size_t /* `i` */, void * /* `client_data` */);
128
+
129
+ /** Turn a functional description into a cord. */
130
+ CORD_API CORD CORD_from_fn(CORD_fn, void * /* `client_data` */,
131
+ size_t /* `len` */);
132
+
133
+ /**
134
+ * Return the substring (subcord really) of `x` with length at most `n`,
135
+ * starting at position `i`. (The initial character has position zero.)
136
+ */
137
+ CORD_API CORD CORD_substr(CORD, size_t /* `i` */, size_t /* `n` */);
138
+
139
+ /**
140
+ * Return the argument, but rebalanced to allow more efficient
141
+ * character retrieval, substring operations, and comparisons.
142
+ * This is useful only for cords that were built using repeated
143
+ * concatenation. Guarantees log time access to the result, unless
144
+ * the argument was obtained through a large number of repeated
145
+ * substring operations or the embedded functional descriptions take
146
+ * longer to evaluate. May reallocate significant parts of the cord.
147
+ * The argument is not modified; only the result is balanced.
148
+ */
149
+ CORD_API CORD CORD_balance(CORD);
150
+
151
+ /*
152
+ * The following traverse a cord by applying a function to each
153
+ * character. This is occasionally appropriate, especially where
154
+ * speed is crucial. But, since C does not have nested functions,
155
+ * clients of this sort of traversal are clumsy to write. Consider
156
+ * the functions that operate on cord positions instead.
157
+ */
158
+
159
+ /** Function to iteratively apply to individual characters in cord. */
160
+ typedef int (*CORD_iter_fn)(char, void * /* `client_data` */);
161
+
162
+ /**
163
+ * Function to apply to substrings of a cord. Each substring is
164
+ * a C character string, not a general cord.
165
+ */
166
+ typedef int (*CORD_batched_iter_fn)(const char *, void * /* `client_data` */);
167
+
168
+ #define CORD_NO_FN ((CORD_batched_iter_fn)0)
169
+
170
+ /**
171
+ * Apply `f1` to each character in the cord, in ascending order, starting at
172
+ * position `i`. If `f2` is not `CORD_NO_FN`, then multiple calls to `f1`
173
+ * may be replaced by a single call to `f2`. The latter is provided only
174
+ * to allow some optimization by the client. This terminates when the right
175
+ * end of this string is reached, or when `f1` or `f2` return a nonzero value.
176
+ * In the latter case this function returns a nonzero value; otherwise
177
+ * it returns 0. The specified value of `i` must be less than `CORD_len(x)`.
178
+ */
179
+ CORD_API int CORD_iter5(CORD, size_t /* `i` */, CORD_iter_fn /* `f1` */,
180
+ CORD_batched_iter_fn /* `f2` */,
181
+ void * /* `client_data` */);
182
+
183
+ /** A simpler variant of `CORD_iter5` that starts at 0, and without `f2`. */
184
+ CORD_API int CORD_iter(CORD, CORD_iter_fn /* `f1` */,
185
+ void * /* `client_data` */);
186
+ #define CORD_iter(x, f1, cd) CORD_iter5(x, 0, f1, CORD_NO_FN, cd)
187
+
188
+ /**
189
+ * Similar to `CORD_iter5`, but end-to-beginning. No provisions for
190
+ * `CORD_batched_iter_fn`.
191
+ */
192
+ CORD_API int CORD_riter4(CORD, size_t /* `i` */, CORD_iter_fn /* `f1` */,
193
+ void * /* `client_data` */);
194
+
195
+ /** A simpler variant of `CORD_riter4` that starts at the end. */
196
+ CORD_API int CORD_riter(CORD, CORD_iter_fn /* `f1` */,
197
+ void * /* `client_data` */);
198
+
199
+ #ifdef __cplusplus
200
+ } /* extern "C" */
201
+ #endif
202
+
203
+ /*
204
+ * Functions that operate on cord positions. The easy way to traverse cords.
205
+ * A cord position is logically a pair consisting of a cord and an index
206
+ * into that cord. But it is much faster to retrieve a character based on
207
+ * a position than on an index. Unfortunately, positions are big (order of
208
+ * a few 100 bytes), so allocate them with caution. Things in `cord_pos.h`
209
+ * file should be treated as opaque, except as described below. Also, note
210
+ * that `CORD_pos_fetch`, `CORD_next` and `CORD_prev` have both macro and
211
+ * function definitions. The former may evaluate their argument more than
212
+ * once.
213
+ */
214
+ #include "cord_pos.h"
215
+
216
+ /*
217
+ * Visible definitions from above:
218
+ * - `typedef <OPAQUE_but_fairly_big> CORD_pos[1]`;
219
+ * - `CORD CORD_pos_to_cord(CORD_pos p)` - extract the cord from
220
+ * a position;
221
+ * - `size_t CORD_pos_to_index(CORD_pos p)` - extract the current index
222
+ * from a position;
223
+ * - `char CORD_pos_fetch(CORD_pos p)` - fetch the character located at
224
+ * the given position;
225
+ * - `void CORD_set_pos(CORD_pos p, CORD x, size_t i)` - initialize the
226
+ * position to refer to the given cord and index;
227
+ * - `void CORD_next(CORD_pos p)` - advance the position to the next
228
+ * character;
229
+ * - `void CORD_prev(CORD_pos p)` - move the position to the preceding
230
+ * character;
231
+ * - `int CORD_pos_valid(CORD_pos p)` - check whether the position is
232
+ * valid, i.e. inside the cord.
233
+ */
234
+
235
+ #ifdef __cplusplus
236
+ extern "C" {
237
+ #endif
238
+
239
+ #define CORD_FOR(pos, cord) \
240
+ for (CORD_set_pos(pos, cord, 0); CORD_pos_valid(pos); CORD_next(pos))
241
+
242
+ /**
243
+ * An out-of-memory handler to call. Zero value means do nothing special,
244
+ * just abort. Use of the setter and getter is preferred over the direct
245
+ * access to the global variable.
246
+ */
247
+ typedef void (*CORD_oom_fn_t)(void);
248
+ CORD_API void CORD_set_oom_fn(CORD_oom_fn_t);
249
+ CORD_API CORD_oom_fn_t CORD_get_oom_fn(void);
250
+ #ifndef CORD_DONT_DECLARE_OOM_FN
251
+ CORD_API CORD_oom_fn_t CORD_oom_fn;
252
+ #endif
253
+ #ifdef CORD_BUILD
254
+ /* no export */ void CORD__call_oom_fn(void);
255
+ #endif
256
+
257
+ /**
258
+ * Dump the representation of `x` to `stdout` in an implementation
259
+ * defined manner. Intended for debugging only.
260
+ */
261
+ CORD_API void CORD_dump(CORD /* `x` */);
262
+
263
+ /*
264
+ * The following could easily be implemented by the client. They are
265
+ * provided by the cord library for convenience.
266
+ */
267
+
268
+ /** Concatenate a character to the end of a cord. */
269
+ CORD_API CORD CORD_cat_char(CORD, char);
270
+
271
+ /** Concatenate `n` cords. */
272
+ CORD_API CORD CORD_catn(int /* `n` */, /* `CORD` */...);
273
+
274
+ /** Return the character in `CORD_substr(x, i, 1)`. */
275
+ CORD_API char CORD_fetch(CORD /* `x` */, size_t /* `i` */);
276
+
277
+ /**
278
+ * Return a negative value, zero, or a positive value, depending on
279
+ * whether `x < y`, `x == y`, or `x > y`, respectively.
280
+ */
281
+ CORD_API int CORD_cmp(CORD /* `x` */, CORD /* `y` */);
282
+
283
+ /**
284
+ * A generalization that takes both starting positions for the comparison,
285
+ * and a limit on the number of characters to be compared.
286
+ */
287
+ CORD_API int CORD_ncmp(CORD /* `x` */, size_t /* `x_start` */, CORD /* `y` */,
288
+ size_t /* `y_start` */, size_t /* `len` */);
289
+
290
+ /**
291
+ * Find the first occurrence of `s` in `x` at position `start` or later.
292
+ * Return the position of the first character of `s` in `x`, or
293
+ * `CORD_NOT_FOUND` if there is none.
294
+ */
295
+ CORD_API size_t CORD_str(CORD /* `x` */, size_t /* `start` */, CORD /* `s` */);
296
+
297
+ /**
298
+ * Return a cord consisting of `i` copies of `c`. Dangerous in
299
+ * conjunction with `CORD_to_char_star`. `c` could be a NUL character.
300
+ * The resulting representation takes constant space, independent of `i`.
301
+ */
302
+ CORD_API CORD CORD_chars(char /* `c` */, size_t /* `i` */);
303
+
304
+ #define CORD_nul(i) CORD_chars('\0', i)
305
+
306
+ /**
307
+ * Turn a file `f` into cord. The file must be seekable. Its contents
308
+ * must remain constant. The file may be accessed as an immediate
309
+ * result of this call and/or as a result of subsequent accesses to
310
+ * the cord. Short files are likely to be immediately read, but
311
+ * long files are likely to be read on demand, possibly relying on
312
+ * `stdio` for buffering. We must have exclusive access to the
313
+ * descriptor `f`, i.e. we may read it at any time, and expect the file
314
+ * pointer to be where we left it. Normally this should be invoked as
315
+ * `CORD_from_file(fopen(...))`. The latter (`CORD_from_file`)
316
+ * arranges to close the file descriptor when it is no longer needed
317
+ * (e.g. when the result becomes inaccessible). The file `f` must be
318
+ * such that `ftell` reflects the actual character position in the
319
+ * file, i.e. the number of characters that can be or were read with
320
+ * `fread`. On UNIX systems this is always true. On Windows systems,
321
+ * `f` must be opened in binary mode.
322
+ */
323
+ CORD_API CORD CORD_from_file(FILE * /* `f` */);
324
+
325
+ /**
326
+ * Equivalent to `CORD_from_file`, except that the entire file will be
327
+ * read and the file pointer will be closed immediately. The binary mode
328
+ * restriction of `CORD_from_file` does not apply.
329
+ */
330
+ CORD_API CORD CORD_from_file_eager(FILE *);
331
+
332
+ /**
333
+ * Equivalent to `CORD_from_file`, except that the file will be read on
334
+ * demand. The binary mode restriction applies.
335
+ */
336
+ CORD_API CORD CORD_from_file_lazy(FILE *);
337
+
338
+ /**
339
+ * Turn a cord into a C string. The result shares no structure with `x`,
340
+ * and is thus modifiable.
341
+ */
342
+ CORD_API char *CORD_to_char_star(CORD /* `x` */);
343
+
344
+ /**
345
+ * Turn a C string into a `CORD`. The C string is copied, and so may
346
+ * subsequently be modified.
347
+ */
348
+ CORD_API CORD CORD_from_char_star(const char *);
349
+
350
+ /**
351
+ * Identical to `CORD_from_char_star`, but the result may share structure
352
+ * with the argument and is thus not modifiable.
353
+ */
354
+ CORD_API const char *CORD_to_const_char_star(CORD);
355
+
356
+ /**
357
+ * Write a cord to a file, starting at the current position.
358
+ * No trailing NUL characters and newlines are added. Returns `EOF`
359
+ * if a write error occurs, 1 otherwise.
360
+ */
361
+ CORD_API int CORD_put(CORD, FILE *);
362
+
363
+ /** "Not found" result for `CORD_chr()` and `CORD_rchr()`. */
364
+ #define CORD_NOT_FOUND ((size_t)(-1))
365
+
366
+ /**
367
+ * A vague analog of `strchr`. Returns the position (an integer, not
368
+ * a pointer) of the first occurrence of `char` `c` inside `x` at
369
+ * position `i` or later. The value `i` must be less than `CORD_len(x)`.
370
+ */
371
+ CORD_API size_t CORD_chr(CORD /* `x` */, size_t /* `i` */, int /* `c` */);
372
+
373
+ /**
374
+ * A vague analog of `strrchr`. Returns index of the last occurrence
375
+ * of `char` `c` inside `x` at position `i` or earlier. The value `i` must
376
+ * be less than `CORD_len(x)`.
377
+ */
378
+ CORD_API size_t CORD_rchr(CORD /* `x` */, size_t /* `i` */, int /* `c` */);
379
+
380
+ #ifdef __cplusplus
381
+ } /* extern "C" */
382
+ #endif
383
+
384
+ #ifndef CORD_NO_IO
385
+
386
+ # include <stdarg.h>
387
+
388
+ # ifdef __cplusplus
389
+ extern "C" {
390
+ # endif
391
+
392
+ /**
393
+ * The following ones provide functionality similar to the ANSI C functions
394
+ * with corresponding names, but with the following additions and changes:
395
+ * 1. A "%r" conversion specification specifies a `CORD` argument.
396
+ * Field width, precision, etc. have the same semantics as for "%s".
397
+ * (Note that "%c", "%C", and "%S" were already taken.)
398
+ * 2. The format string is represented as a `CORD`.
399
+ * 3. `CORD_sprintf` and `CORD_vsprintf` assign the result through
400
+ * the 1st argument. Unlike their ANSI C variants, there is no need
401
+ * to guess the correct buffer size.
402
+ * 4. Most of the conversions are implemented through the native
403
+ * `vsprintf`. Hence they are usually no faster, and idiosyncrasies
404
+ * of the native `printf` are preserved. However, `CORD` arguments
405
+ * to `CORD_sprintf`, `CORD_vsprintf` are *not* copied; the result
406
+ * shares the original structure. This may make them very efficient
407
+ * in some unusual applications. The format string is copied.
408
+ *
409
+ * The functions return the number of characters generated or -1 on error.
410
+ * This complies with the ANSI standard, but is inconsistent with some
411
+ * older implementations of `sprintf`.
412
+ */
413
+ CORD_API int CORD_sprintf(CORD * /* `out` */, CORD /* `format` */, ...);
414
+ CORD_API int CORD_vsprintf(CORD * /* `out` */, CORD /* `format` */, va_list);
415
+ CORD_API int CORD_fprintf(FILE *, CORD /* `format` */, ...);
416
+ CORD_API int CORD_vfprintf(FILE *, CORD /* `format` */, va_list);
417
+ CORD_API int CORD_printf(CORD /* `format` */, ...);
418
+ CORD_API int CORD_vprintf(CORD /* `format` */, va_list);
419
+
420
+ # ifdef __cplusplus
421
+ } /* extern "C" */
422
+ # endif
423
+
424
+ #endif /* CORD_NO_IO */
425
+
426
+ #endif /* CORD_H */