summaryrefslogtreecommitdiffstats
path: root/builtin.c
diff options
context:
space:
mode:
authorNicolas Williams <nico@cryptonector.com>2014-12-30 11:17:58 -0600
committerNicolas Williams <nico@cryptonector.com>2014-12-30 11:31:52 -0600
commitcbfc0d6130b784799256f29bb515727df9d894f4 (patch)
treefed8c9f451c06b15b1c5fb206a682e64685f4563 /builtin.c
parentc308b2881fce6ce53a66ae600a8b2803214f0b89 (diff)
Remove string indexing by string (fix #454)
This turns out to have been a bad idea: "foo"|.["o"] it interacts badly with `path()`. See #454 for the gory details.
Diffstat (limited to 'builtin.c')
-rw-r--r--builtin.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/builtin.c b/builtin.c
index 25879e3f..6399aff1 100644
--- a/builtin.c
+++ b/builtin.c
@@ -841,6 +841,10 @@ static jv f_string_explode(jq_state *jq, jv a) {
return jv_string_explode(a);
}
+static jv f_string_indexes(jq_state *jq, jv a, jv b) {
+ return jv_string_indexes(a, b);
+}
+
static jv f_string_implode(jq_state *jq, jv a) {
if (jv_get_kind(a) != JV_KIND_ARRAY) {
jv_free(a);
@@ -909,6 +913,7 @@ static const struct cfunction function_list[] = {
{(cfunction_ptr)f_string_split, "split", 2},
{(cfunction_ptr)f_string_explode, "explode", 1},
{(cfunction_ptr)f_string_implode, "implode", 1},
+ {(cfunction_ptr)f_string_indexes, "_strindices", 2},
{(cfunction_ptr)f_setpath, "setpath", 3}, // FIXME typechecking
{(cfunction_ptr)f_getpath, "getpath", 2},
{(cfunction_ptr)f_delpaths, "delpaths", 2},
@@ -1010,9 +1015,12 @@ static const char* const jq_builtins[] = {
"def from_entries: map({(.key // .Key): (.value // .Value)}) | add | .//={};",
"def with_entries(f): to_entries | map(f) | from_entries;",
"def reverse: [.[length - 1 - range(0;length)]];",
- "def indices($i): if type == \"array\" and ($i|type) == \"array\" then .[$i] elif type == \"array\" then .[[$i]] else .[$i] end;",
- "def index($i): if type == \"array\" and ($i|type) == \"array\" then .[$i] elif type == \"array\" then .[[$i]] else .[$i] end | .[0];",
- "def rindex($i): if type == \"array\" and ($i|type) == \"array\" then .[$i] elif type == \"array\" then .[[$i]] else .[$i] end | .[-1:][0];",
+ "def indices($i): if type == \"array\" and ($i|type) == \"array\" then .[$i]"
+ " elif type == \"array\" then .[[$i]]"
+ " elif type == \"string\" and ($i|type) == \"string\" then _strindices($i)"
+ " else .[$i] end;",
+ "def index($i): indices($i) | .[0];", // TODO: optimize
+ "def rindex($i): indices($i) | .[-1:][0];", // TODO: optimize
"def paths: path(recurse(if (type|. == \"array\" or . == \"object\") then .[] else empty end))|select(length > 0);",
"def paths(node_filter): . as $dot|paths|select(. as $p|$dot|getpath($p)|node_filter);",
"def any(generator; condition):"