Skip to content

Commit

Permalink
Use codepoint index for indices/1, index/ 1 and rindex/1
Browse files Browse the repository at this point in the history
Previsouly byte index was used.

Fixes #1430 #1624 #3064
  • Loading branch information
wader committed Mar 12, 2024
1 parent 913b264 commit ca38058
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/jv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1274,15 +1274,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++;
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,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"]]
Expand Down

0 comments on commit ca38058

Please sign in to comment.