diff options
author | Nicolas Williams <nico@cryptonector.com> | 2014-06-08 02:01:44 -0500 |
---|---|---|
committer | Nicolas Williams <nico@cryptonector.com> | 2014-06-08 02:01:44 -0500 |
commit | 1dbe9317bce859e9beaa2d7e0ceaad5d561eb48e (patch) | |
tree | 8a4319b06b8ed63be88e6163238819385ea485c5 /jv.c | |
parent | ef4f3a54feaa9ebf967dd944bb8da5108a351d07 (diff) |
Add `indices(s)`, improve `index(s)`, `rindex(s)`
Now these deal with arrays as input and `s` being an array or a scalar.
Diffstat (limited to 'jv.c')
-rw-r--r-- | jv.c | 22 |
1 files changed, 22 insertions, 0 deletions
@@ -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) |