Add indices(s), improve index(s), rindex(s)

Now these deal with arrays as input and `s` being an array or a scalar.
This commit is contained in:
Nicolas Williams committed 2014-06-08 02:01:44 -05:00
1 parent ef4f3a54fe
commit 1dbe9317bc
6 files changed
+61 -4

No files matched your search

+22
View File
@@ -364,6 +364,28 @@ int jv_array_contains(jv a, jv b) {
return r;
}
jv jv_array_indexes(jv a, jv b) {
jv res = jv_array();
int idx = -1;
jv_array_foreach(a, ai, aelem) {
jv_array_foreach(b, bi, belem) {
// quieten compiler warnings about aelem not being used... by
// using it
if ((bi == 0 && !jv_equal(jv_copy(aelem), jv_copy(belem))) ||
(bi > 0 && !jv_equal(jv_array_get(jv_copy(a), ai + bi), jv_copy(belem))))
idx = -1;
else if (bi == 0 && idx == -1)
idx = ai;
}
if (idx > -1)
res = jv_array_append(res, jv_number(idx));
idx = -1;
}
jv_free(a);
jv_free(b);
return res;
}
/*
* Strings (internal helpers)