forked from ashvardanian/StringZilla
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.c
More file actions
289 lines (242 loc) · 10.5 KB
/
lib.c
File metadata and controls
289 lines (242 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
* @file lib.c
* @brief JavaScript bindings for StringZilla.
* @author Ash Vardanian
* @date September 18, 2023
*
* @copyright Copyright (c) 2023
* @see NodeJS docs: https://nodejs.org/api/n-api.html
*/
#include <stdio.h> // `printf` for debug builds
#include <stdlib.h> // `malloc` to export strings into UTF-8
#include <string.h> // strncmp
#include <node_api.h> // `napi_*` functions
#include <stringzilla/stringzilla.h> // `sz_*` functions
typedef struct {
napi_env env_;
napi_ref wrapper_;
sz_cptr_t start;
sz_size_t length;
} sz_wrapper_t;
napi_value indexOfAPI(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
napi_get_cb_info(env, info, &argc, args, NULL, NULL);
// Extract the C string from the JavaScript string for haystack and needle
sz_string_view_t haystack = {NULL, 0};
sz_string_view_t needle = {NULL, 0};
// For haystack
napi_get_value_string_utf8(env, args[0], NULL, 0, (size_t *)&haystack.length);
haystack.start = malloc(haystack.length + 1);
napi_get_value_string_utf8(env, args[0], (char *)haystack.start, haystack.length + 1, (size_t *)&haystack.length);
// For needle
napi_get_value_string_utf8(env, args[1], NULL, 0, (size_t *)&needle.length);
needle.start = malloc(needle.length + 1);
napi_get_value_string_utf8(env, args[1], (char *)needle.start, needle.length + 1, (size_t *)&needle.length);
// Convert the result to JavaScript BigInt and return
napi_value js_result;
if (needle.length == 0) { napi_create_bigint_int64(env, 0, &js_result); }
else {
sz_cptr_t result = sz_find(haystack.start, haystack.length, needle.start, needle.length);
// In JavaScript, if `indexOf` is unable to indexOf the specified value, then it should return -1
if (result == NULL) { napi_create_bigint_int64(env, -1, &js_result); }
else { napi_create_bigint_uint64(env, result - haystack.start, &js_result); }
}
// Cleanup
free((void *)haystack.start);
free((void *)needle.start);
return js_result;
}
napi_value countAPI(napi_env env, napi_callback_info info) {
size_t argc = 3;
napi_value args[3];
napi_get_cb_info(env, info, &argc, args, NULL, NULL);
// Extract the C string from the JavaScript string for haystack and needle
sz_string_view_t haystack = {NULL, 0};
sz_string_view_t needle = {NULL, 0};
// For haystack
napi_get_value_string_utf8(env, args[0], NULL, 0, (size_t *)&haystack.length);
haystack.start = malloc(haystack.length + 1);
napi_get_value_string_utf8(env, args[0], (char *)haystack.start, haystack.length + 1, (size_t *)&haystack.length);
// For needle
napi_get_value_string_utf8(env, args[1], NULL, 0, (size_t *)&needle.length);
needle.start = malloc(needle.length + 1);
napi_get_value_string_utf8(env, args[1], (char *)needle.start, needle.length + 1, (size_t *)&needle.length);
bool overlap = false;
if (argc > 2) { napi_get_value_bool(env, args[2], &overlap); }
void const *haystack_start = haystack.start, *needle_start = needle.start;
size_t count = 0;
if (needle.length == 0 || haystack.length == 0 || haystack.length < needle.length) { count = 0; }
else if (overlap) {
while (haystack.length) {
sz_cptr_t ptr = sz_find(haystack.start, haystack.length, needle.start, needle.length);
sz_bool_t found = ptr != NULL;
sz_size_t offset = found ? (sz_size_t)(ptr - haystack.start) : haystack.length;
count += found;
haystack.start += offset + found;
haystack.length -= offset + found;
}
}
else {
while (haystack.length) {
sz_cptr_t ptr = sz_find(haystack.start, haystack.length, needle.start, needle.length);
sz_bool_t found = ptr != NULL;
sz_size_t offset = found ? (sz_size_t)(ptr - haystack.start) : haystack.length;
count += found;
haystack.start += offset + needle.length;
haystack.length -= offset + needle.length * found;
}
}
// Cleanup
free((void *)haystack_start);
free((void *)needle_start);
// Convert the `count` to JavaScript `BigInt` and return
napi_value js_count;
napi_create_bigint_uint64(env, count, &js_count);
return js_count;
}
napi_value str_startswith(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
napi_value jsthis;
sz_wrapper_t *this;
sz_wrapper_t *arg;
napi_get_cb_info(env, info, &argc, args, &jsthis, NULL);
napi_unwrap(env, jsthis, (void **)&this);
napi_unwrap(env, args[0], (void **)&arg);
napi_value ret;
if (this->length < arg->length) { napi_get_boolean(env, false, &ret); }
else if (strncmp(this->start, arg->start, arg->length) == 0) { napi_get_boolean(env, true, &ret); }
else { napi_get_boolean(env, false, &ret); }
return ret;
}
napi_value str_endswith(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
napi_value jsthis;
sz_wrapper_t *this;
sz_wrapper_t *arg;
napi_get_cb_info(env, info, &argc, args, &jsthis, NULL);
napi_unwrap(env, jsthis, (void **)&this);
napi_unwrap(env, args[0], (void **)&arg);
napi_value ret;
if (this->length < arg->length) { napi_get_boolean(env, false, &ret); }
else if (strncmp(this->start + (this->length - arg->length), arg->start, arg->length) == 0) {
napi_get_boolean(env, true, &ret);
}
else { napi_get_boolean(env, false, &ret); }
return ret;
}
napi_value str_count(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
napi_value jsthis;
sz_wrapper_t *this;
sz_wrapper_t *arg;
napi_get_cb_info(env, info, &argc, args, &jsthis, NULL);
napi_unwrap(env, jsthis, (void **)&this);
napi_unwrap(env, args[0], (void **)&arg);
sz_string_view_t haystack = {this->start, this->length};
sz_string_view_t needle = {arg->start, arg->length};
bool overlap = false;
if (argc > 1) { napi_get_value_bool(env, args[1], &overlap); }
size_t count = 0;
if (needle.length == 0 || haystack.length == 0 || haystack.length < needle.length) { count = 0; }
else if (overlap) {
while (haystack.length) {
sz_cptr_t ptr = sz_find(haystack.start, haystack.length, needle.start, needle.length);
sz_bool_t found = ptr != NULL;
sz_size_t offset = found ? (sz_size_t)(ptr - haystack.start) : haystack.length;
count += found;
haystack.start += offset + found;
haystack.length -= offset + found;
}
}
else {
while (haystack.length) {
sz_cptr_t ptr = sz_find(haystack.start, haystack.length, needle.start, needle.length);
sz_bool_t found = ptr != NULL;
sz_size_t offset = found ? (sz_size_t)(ptr - haystack.start) : haystack.length;
count += found;
haystack.start += offset + needle.length;
haystack.length -= offset + needle.length * found;
}
}
napi_value js_count;
napi_create_bigint_uint64(env, count, &js_count);
return js_count;
}
napi_value _str_find(sz_find_t finder, napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
napi_value jsthis;
sz_wrapper_t *this;
sz_wrapper_t *needle;
napi_get_cb_info(env, info, &argc, args, &jsthis, NULL);
napi_unwrap(env, jsthis, (void **)&this);
napi_unwrap(env, args[0], (void **)&needle);
napi_value js_result;
if (needle->length == 0) { napi_create_bigint_int64(env, 0, &js_result); }
else {
sz_cptr_t result = finder(this->start, this->length, needle->start, needle->length);
// In JavaScript, if `indexOf` is unable to indexOf the specified value, then it should return -1
if (result == NULL) { napi_create_bigint_int64(env, -1, &js_result); }
else { napi_create_bigint_uint64(env, result - this->start, &js_result); }
}
return js_result;
}
napi_value str_find(napi_env env, napi_callback_info info) {
return _str_find(sz_find, env, info);
}
napi_value str_rfind(napi_env env, napi_callback_info info) {
return _str_find(sz_rfind, env, info);
}
static void destroy(napi_env _unused_env, void *obj, void *_unused_hint) {
sz_wrapper_t *sz = (sz_wrapper_t *)obj;
napi_delete_reference(sz->env_, sz->wrapper_);
free((void *)sz->start);
free(sz);
}
static napi_value create_instance(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
napi_value jsthis;
sz_wrapper_t *obj = malloc(sizeof(sz_wrapper_t));
napi_get_cb_info(env, info, &argc, args, &jsthis, NULL);
// if (type_check_data_buffer(env, args, argc) != ADDON_OK) {
// return NULL;
//}
// napi_get_buffer_info(env, args[0], &data, &buf_len);
// TODO type check
napi_get_value_string_utf8(env, args[0], NULL, 0, (size_t *)&obj->length);
obj->start = malloc(obj->length + 1);
napi_get_value_string_utf8(env, args[0], (char *)obj->start, obj->length + 1, (size_t *)&obj->length);
obj->env_ = env;
napi_wrap(env, jsthis, (void *)obj, destroy, NULL, &obj->wrapper_);
return jsthis;
}
napi_value Init(napi_env env, napi_value exports) {
// Define an array of property descriptors
napi_property_descriptor properties[] = {
{"indexOf", NULL, indexOfAPI, NULL, NULL, NULL, napi_default, NULL},
{"find", NULL, indexOfAPI, NULL, NULL, NULL, napi_default, NULL},
{"count", NULL, countAPI, NULL, NULL, NULL, napi_default, NULL}
};
// Define the properties on the `exports` object
size_t propertyCount = sizeof(properties) / sizeof(properties[0]);
napi_define_properties(env, exports, propertyCount, properties);
napi_property_descriptor obj_properties[] = {
{"indexOf", NULL, str_find, NULL, NULL, NULL, napi_default, NULL},
{"find", NULL, str_find, NULL, NULL, NULL, napi_default, NULL},
{"rfind", NULL, str_rfind, NULL, NULL, NULL, napi_default, NULL},
{"startswith", NULL, str_startswith, NULL, NULL, NULL, napi_default, NULL},
{"endswith", NULL, str_endswith, NULL, NULL, NULL, napi_default, NULL},
{"count", NULL, str_count, NULL, NULL, NULL, napi_default, NULL}
};
napi_value cons;
napi_define_class(env, "Str", NAPI_AUTO_LENGTH, create_instance, NULL,
sizeof(obj_properties) / sizeof(*obj_properties), obj_properties, &cons);
napi_set_named_property(env, exports, "Str", cons);
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)