MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

iterate over utf8 codepoints

+14 -6
+14 -6
src/ant.c
··· 10416 10416 10417 10417 static jsval_t for_of_iter_string(struct js *js, for_iter_ctx_t *ctx, jsval_t iterable) { 10418 10418 jshdl_t h_iterable = js_root(js, iterable); 10419 - jsoff_t slen; 10420 - (void) vstr(js, iterable, &slen); 10419 + jsoff_t byte_len; 10420 + jsoff_t soff = vstr(js, iterable, &byte_len); 10421 10421 10422 - for (jsoff_t i = 0; i < slen; i++) { 10422 + const char *str = (char *) &js->mem[soff]; 10423 + size_t utf16_len = utf16_strlen(str, byte_len); 10424 + 10425 + for (size_t i = 0; i < utf16_len; i++) { 10423 10426 jsval_t cur = js_deref(js, h_iterable); 10424 - jsoff_t soff = vstr(js, cur, NULL); 10425 - const char *str = (char *) &js->mem[soff]; 10426 - jsval_t char_str = js_mkstr(js, &str[i], 1); 10427 + jsoff_t cur_byte_len; 10428 + jsoff_t cur_soff = vstr(js, cur, &cur_byte_len); 10429 + const char *cur_str = (char *) &js->mem[cur_soff]; 10430 + 10431 + size_t char_bytes; 10432 + int byte_offset = utf16_index_to_byte_offset(cur_str, cur_byte_len, i, &char_bytes); 10433 + if (byte_offset < 0) break; 10434 + jsval_t char_str = js_mkstr(js, cur_str + byte_offset, char_bytes); 10427 10435 10428 10436 jsval_t err = for_iter_bind_var(js, ctx, char_str); 10429 10437 if (is_err(err)) { js_unroot(js, h_iterable); return err; }