| 220 | } |
| 221 | |
| 222 | napi_value utf8CaseInsensitiveNeedleFindIn(napi_env env, napi_callback_info info) { |
| 223 | size_t argc = 2; |
| 224 | napi_value args[2]; |
| 225 | napi_value js_this; |
| 226 | napi_get_cb_info(env, info, &argc, args, &js_this, NULL); |
| 227 | |
| 228 | if (argc < 1) { |
| 229 | napi_throw_error(env, NULL, "findIn(haystack, validate?) expects at least 1 argument"); |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | utf8_case_insensitive_needle_t *needle; |
| 234 | napi_unwrap(env, js_this, (void **)&needle); |
| 235 | if (!needle) { |
| 236 | napi_throw_error(env, NULL, "Internal error: missing needle"); |
| 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | void *haystack_data; |
| 241 | size_t haystack_length; |
| 242 | napi_status status = napi_get_buffer_info(env, args[0], &haystack_data, &haystack_length); |
| 243 | if (status != napi_ok) { |
| 244 | napi_throw_error(env, NULL, "First argument must be a Buffer"); |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | bool validate = false; |
| 249 | if (argc > 1) { napi_get_value_bool(env, args[1], &validate); } |
| 250 | if (validate && sz_utf8_valid((sz_cptr_t)haystack_data, haystack_length) == sz_false_k) { |
| 251 | napi_throw_error(env, NULL, "Haystack is not valid UTF-8"); |
| 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | sz_size_t matched_length = 0; |
| 256 | sz_cptr_t match = |
| 257 | sz_utf8_case_insensitive_find((sz_cptr_t)haystack_data, haystack_length, (sz_cptr_t)needle->needle_data, |
| 258 | needle->needle_length, &needle->metadata, &matched_length); |
| 259 | if (!match) return makeFindResultObject(env, -1, 0); |
| 260 | return makeFindResultObject(env, (int64_t)(match - (sz_cptr_t)haystack_data), (uint64_t)matched_length); |
| 261 | } |
| 262 | |
| 263 | napi_value countAPI(napi_env env, napi_callback_info info) { |
| 264 | size_t argc = 3; |
nothing calls this directly
no test coverage detected
searching dependent graphs…