| 117 | } |
| 118 | |
| 119 | napi_value utf8CaseInsensitiveFindAPI(napi_env env, napi_callback_info info) { |
| 120 | size_t argc = 3; |
| 121 | napi_value args[3]; |
| 122 | napi_get_cb_info(env, info, &argc, args, NULL, NULL); |
| 123 | |
| 124 | if (argc < 2) { |
| 125 | napi_throw_error(env, NULL, |
| 126 | "utf8CaseInsensitiveFind(haystack, needle, validate?) expects at least 2 arguments"); |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | void *haystack_data, *needle_data; |
| 131 | size_t haystack_length, needle_length; |
| 132 | napi_status status = napi_get_buffer_info(env, args[0], &haystack_data, &haystack_length); |
| 133 | if (status != napi_ok) { |
| 134 | napi_throw_error(env, NULL, "First argument must be a Buffer"); |
| 135 | return NULL; |
| 136 | } |
| 137 | status = napi_get_buffer_info(env, args[1], &needle_data, &needle_length); |
| 138 | if (status != napi_ok) { |
| 139 | napi_throw_error(env, NULL, "Second argument must be a Buffer"); |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | bool validate = false; |
| 144 | if (argc > 2) { napi_get_value_bool(env, args[2], &validate); } |
| 145 | if (validate && (sz_utf8_valid((sz_cptr_t)haystack_data, haystack_length) == sz_false_k || |
| 146 | sz_utf8_valid((sz_cptr_t)needle_data, needle_length) == sz_false_k)) { |
| 147 | napi_throw_error(env, NULL, "Input is not valid UTF-8"); |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | sz_utf8_case_insensitive_needle_metadata_t metadata = {0}; |
| 152 | sz_size_t matched_length = 0; |
| 153 | sz_cptr_t match = sz_utf8_case_insensitive_find((sz_cptr_t)haystack_data, haystack_length, (sz_cptr_t)needle_data, |
| 154 | needle_length, &metadata, &matched_length); |
| 155 | |
| 156 | if (!match) return makeFindResultObject(env, -1, 0); |
| 157 | return makeFindResultObject(env, (int64_t)(match - (sz_cptr_t)haystack_data), (uint64_t)matched_length); |
| 158 | } |
| 159 | |
| 160 | typedef struct { |
| 161 | sz_u8_t *needle_data; |
nothing calls this directly
no test coverage detected
searching dependent graphs…