summaryrefslogtreecommitdiffstats
path: root/jv.c
diff options
context:
space:
mode:
authorNicolas Williams <nico@cryptonector.com>2014-06-08 02:01:44 -0500
committerNicolas Williams <nico@cryptonector.com>2014-06-08 02:01:44 -0500
commit1dbe9317bce859e9beaa2d7e0ceaad5d561eb48e (patch)
tree8a4319b06b8ed63be88e6163238819385ea485c5 /jv.c
parentef4f3a54feaa9ebf967dd944bb8da5108a351d07 (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.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/jv.c b/jv.c
index 24971af6..2da8ecb0 100644
--- a/jv.c
+++ b/jv.c
@@ -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)