diff options
author | Mattias Wadman <mattias.wadman@gmail.com> | 2024-11-17 10:22:05 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-17 10:22:05 +0100 |
commit | 8619f8a8ac746887cf43abd8e2116abba253cdcc (patch) | |
tree | 9db8a8ff702a37759988a3a95245832687cd3cc9 | |
parent | a7b22539a479400fe8b3fc40840ca587044ecbc2 (diff) |
Previsouly byte index was used.
Fixes #1430 #1624 #3064
-rw-r--r-- | src/jv.c | 12 | ||||
-rw-r--r-- | tests/jq.test | 16 |
2 files changed, 25 insertions, 3 deletions
@@ -1273,15 +1273,21 @@ jv jv_string_indexes(jv j, jv k) { assert(JVP_HAS_KIND(k, JV_KIND_STRING)); const char *jstr = jv_string_value(j); const char *idxstr = jv_string_value(k); - const char *p; + const char *p, *lp; int jlen = jv_string_length_bytes(jv_copy(j)); int idxlen = jv_string_length_bytes(jv_copy(k)); jv a = jv_array(); if (idxlen != 0) { - p = jstr; + int n = 0; + p = lp = jstr; while ((p = _jq_memmem(p, (jstr + jlen) - p, idxstr, idxlen)) != NULL) { - a = jv_array_append(a, jv_number(p - jstr)); + while (lp < p) { + lp += jvp_utf8_decode_length(*lp); + n++; + } + + a = jv_array_append(a, jv_number(n)); p++; } } diff --git a/tests/jq.test b/tests/jq.test index 5c1b81d3..2d8b47e7 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -1416,6 +1416,22 @@ indices(", ") "a,b, cd,e, fgh, ijkl" [3,9,14] +index("!") +"здравствуй мир!" +14 + +.[:rindex("x")] +"正xyz" +"正" + +indices("o") +"🇬🇧oo" +[2,3] + +indices("o") +"ƒoo" +[1,2] + [.[]|split(",")] ["a, bc, def, ghij, jklmn, a,b, c,d, e,f", "a,b,c,d, e,f,g,h"] [["a"," bc"," def"," ghij"," jklmn"," a","b"," c","d"," e","f"],["a","b","c","d"," e","f","g","h"]] |