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

Function findLastAPI

javascript/lib.c:543–578  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

541}
542
543napi_value findLastAPI(napi_env env, napi_callback_info info) {
544 size_t argc = 2;
545 napi_value args[2];
546 napi_get_cb_info(env, info, &argc, args, NULL, NULL);
547
548 // Get buffer info for haystack (zero-copy)
549 void *haystack_data;
550 size_t haystack_length;
551 napi_status status = napi_get_buffer_info(env, args[0], &haystack_data, &haystack_length);
552 if (status != napi_ok) {
553 napi_throw_error(env, NULL, "First argument must be a Buffer");
554 return NULL;
555 }
556
557 // Get buffer info for needle (zero-copy)
558 void *needle_data;
559 size_t needle_length;
560 status = napi_get_buffer_info(env, args[1], &needle_data, &needle_length);
561 if (status != napi_ok) {
562 napi_throw_error(env, NULL, "Second argument must be a Buffer");
563 return NULL;
564 }
565
566 // Convert the result to JavaScript BigInt and return
567 napi_value js_result;
568 if (needle_length == 0) { napi_create_bigint_int64(env, haystack_length, &js_result); }
569 else {
570 sz_cptr_t result = sz_rfind((sz_cptr_t)haystack_data, haystack_length, (sz_cptr_t)needle_data, needle_length);
571
572 // In JavaScript, if `lastIndexOf` is unable to find the specified value, then it should return -1
573 if (result == NULL) { napi_create_bigint_int64(env, -1, &js_result); }
574 else { napi_create_bigint_uint64(env, result - (sz_cptr_t)haystack_data, &js_result); }
575 }
576
577 return js_result;
578}
579
580napi_value findByteAPI(napi_env env, napi_callback_info info) {
581 size_t argc = 2;

Callers

nothing calls this directly

Calls 1

sz_rfindFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…