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

Function utf8CaseInsensitiveNeedleConstructor

javascript/lib.c:173–220  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

171}
172
173napi_value utf8CaseInsensitiveNeedleConstructor(napi_env env, napi_callback_info info) {
174 size_t argc = 2;
175 napi_value args[2];
176 napi_value js_this;
177 napi_get_cb_info(env, info, &argc, args, &js_this, NULL);
178
179 if (argc < 1) {
180 napi_throw_error(env, NULL, "Utf8CaseInsensitiveNeedle(needle, validate?) expects at least 1 argument");
181 return NULL;
182 }
183
184 void *needle_data;
185 size_t needle_length;
186 napi_status status = napi_get_buffer_info(env, args[0], &needle_data, &needle_length);
187 if (status != napi_ok) {
188 napi_throw_error(env, NULL, "First argument must be a Buffer");
189 return NULL;
190 }
191
192 bool validate = false;
193 if (argc > 1) { napi_get_value_bool(env, args[1], &validate); }
194 if (validate && sz_utf8_valid((sz_cptr_t)needle_data, needle_length) == sz_false_k) {
195 napi_throw_error(env, NULL, "Needle is not valid UTF-8");
196 return NULL;
197 }
198
199 utf8_case_insensitive_needle_t *needle = (utf8_case_insensitive_needle_t *)malloc(sizeof(*needle));
200 if (!needle) {
201 napi_throw_error(env, NULL, "Memory allocation failed");
202 return NULL;
203 }
204 needle->needle_length = needle_length;
205 needle->metadata = (sz_utf8_case_insensitive_needle_metadata_t) {0};
206 needle->needle_data = NULL;
207
208 if (needle_length) {
209 needle->needle_data = (sz_u8_t *)malloc(needle_length);
210 if (!needle->needle_data) {
211 free(needle);
212 napi_throw_error(env, NULL, "Memory allocation failed");
213 return NULL;
214 }
215 sz_copy((sz_ptr_t)needle->needle_data, (sz_cptr_t)needle_data, needle_length);
216 }
217
218 napi_wrap(env, js_this, needle, utf8_case_insensitive_needle_cleanup, NULL, NULL);
219 return js_this;
220}
221
222napi_value utf8CaseInsensitiveNeedleFindIn(napi_env env, napi_callback_info info) {
223 size_t argc = 2;

Callers

nothing calls this directly

Calls 2

sz_utf8_validFunction · 0.85
sz_copyFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…