MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / countAPI

Function countAPI

javascript/lib.c:263–313  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

261}
262
263napi_value countAPI(napi_env env, napi_callback_info info) {
264 size_t argc = 3;
265 napi_value args[3];
266 napi_get_cb_info(env, info, &argc, args, NULL, NULL);
267
268 void *haystack_data, *needle_data;
269 size_t haystack_length, needle_length;
270 napi_status status = napi_get_buffer_info(env, args[0], &haystack_data, &haystack_length);
271 if (status != napi_ok) {
272 napi_throw_error(env, NULL, "First argument must be a Buffer");
273 return NULL;
274 }
275 status = napi_get_buffer_info(env, args[1], &needle_data, &needle_length);
276 if (status != napi_ok) {
277 napi_throw_error(env, NULL, "Second argument must be a Buffer");
278 return NULL;
279 }
280
281 bool overlap = false;
282 if (argc > 2) { napi_get_value_bool(env, args[2], &overlap); }
283
284 sz_string_view_t haystack = {(sz_cptr_t)haystack_data, haystack_length};
285 sz_string_view_t needle = {(sz_cptr_t)needle_data, needle_length};
286
287 size_t count = 0;
288 if (needle.length == 0 || haystack.length == 0 || haystack.length < needle.length) { count = 0; }
289 else if (overlap) {
290 while (haystack.length) {
291 sz_cptr_t ptr = sz_find(haystack.start, haystack.length, needle.start, needle.length);
292 sz_bool_t found = ptr != NULL;
293 sz_size_t offset = found ? (sz_size_t)(ptr - haystack.start) : haystack.length;
294 count += found;
295 haystack.start += offset + found;
296 haystack.length -= offset + found;
297 }
298 }
299 else {
300 while (haystack.length) {
301 sz_cptr_t ptr = sz_find(haystack.start, haystack.length, needle.start, needle.length);
302 sz_bool_t found = ptr != NULL;
303 sz_size_t offset = found ? (sz_size_t)(ptr - haystack.start) : haystack.length;
304 count += found;
305 haystack.start += offset + needle.length;
306 haystack.length -= offset + needle.length * found;
307 }
308 }
309
310 napi_value js_count;
311 napi_create_bigint_uint64(env, count, &js_count);
312 return js_count;
313}
314
315napi_value hashAPI(napi_env env, napi_callback_info info) {
316 size_t argc = 2;

Callers

nothing calls this directly

Calls 1

sz_findFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…