Skip to content
Myr

Remove string indexing by string (fix #454)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This turns out to have been a bad idea: "foo"|.["o"] it interacts badly with `path()`. See #454 for the gory details.

authored by Nicolas Williams · committed by Nicolas Williams · parent c308b28 · Browse files · 3 files+11 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+11 added−17 removed

  • tests:+0 added−12 removed
  • other:+11 added−5 removed

3 files+11 added−17 removed

modified:tests/all.test-12 removed
900 unmodified lines
901[.[]|rtrimstr("foo")]902["fo", "foo", "barfoo", "foobar", "foob"]903["fo","","bar","foobar","foob"]904905.[","]906"a,bc,def,ghij,klmno"907[1,4,8,13]908909.[", "]910"a,bc,def,ghij,klmno"911[]912913.[", "]914"a,b,, c, d,ef, , ghi, jklmn, o"915[4,7,13,15,20,27]916905[(index(","), rindex(",")), indices(",")]906"a,bc,def,ghij,klmno"907[1,13,[1,4,8,13]]908
198 unmodified lines
modified:builtin.c+11 added-3 removed
839 unmodified lines
840 }841 return jv_string_explode(a);842}843844static jv f_string_indexes(jq_state *jq, jv a, jv b) {845 return jv_string_indexes(a, b);846}847848static jv f_string_implode(jq_state *jq, jv a) {849 if (jv_get_kind(a) != JV_KIND_ARRAY) {850 jv_free(a);851 return jv_invalid_with_msg(jv_string("implode input must be an array"));
60 unmodified lines
912 {(cfunction_ptr)f_rtrimstr, "rtrimstr", 2},913 {(cfunction_ptr)f_string_split, "split", 2},914 {(cfunction_ptr)f_string_explode, "explode", 1},915 {(cfunction_ptr)f_string_implode, "implode", 1},916 {(cfunction_ptr)f_string_indexes, "_strindices", 2},917 {(cfunction_ptr)f_setpath, "setpath", 3}, // FIXME typechecking918 {(cfunction_ptr)f_getpath, "getpath", 2},919 {(cfunction_ptr)f_delpaths, "delpaths", 2},920 {(cfunction_ptr)f_has, "has", 2},
93 unmodified lines
1014 "def to_entries: [keys_unsorted[] as $k | {key: $k, value: .[$k]}];",1015 "def from_entries: map({(.key // .Key): (.value // .Value)}) | add | .//={};",1016 "def with_entries(f): to_entries | map(f) | from_entries;",1017 "def reverse: [.[length - 1 - range(0;length)]];",1013 "def indices($i): if type == \"array\" and ($i|type) == \"array\" then .[$i] elif type == \"array\" then .[[$i]] else .[$i] end;",1014 "def index($i): if type == \"array\" and ($i|type) == \"array\" then .[$i] elif type == \"array\" then .[[$i]] else .[$i] end | .[0];",1015 "def rindex($i): if type == \"array\" and ($i|type) == \"array\" then .[$i] elif type == \"array\" then .[[$i]] else .[$i] end | .[-1:][0];",1018 "def indices($i): if type == \"array\" and ($i|type) == \"array\" then .[$i]"1019 " elif type == \"array\" then .[[$i]]"1020 " elif type == \"string\" and ($i|type) == \"string\" then _strindices($i)"1021 " else .[$i] end;",1022 "def index($i): indices($i) | .[0];", // TODO: optimize1023 "def rindex($i): indices($i) | .[-1:][0];", // TODO: optimize1024 "def paths: path(recurse(if (type|. == \"array\" or . == \"object\") then .[] else empty end))|select(length > 0);",1025 "def paths(node_filter): . as $dot|paths|select(. as $p|$dot|getpath($p)|node_filter);",1026 "def any(generator; condition):"1027 " [foreach generator as $i"
220 unmodified lines
modified:jv_aux.c-2 removed
84 unmodified lines
85 } else {86 v = jv_invalid_with_msg(jv_string_fmt("Start and end indices of an string slice must be numbers"));87 jv_free(t);88 }89 } else if (jv_get_kind(t) == JV_KIND_STRING && jv_get_kind(k) == JV_KIND_STRING) {90 v = jv_string_indexes(t, k);89 } else if (jv_get_kind(t) == JV_KIND_ARRAY && jv_get_kind(k) == JV_KIND_ARRAY) {90 v = jv_array_indexes(t, k);91 } else if (jv_get_kind(t) == JV_KIND_NULL && 92 (jv_get_kind(k) == JV_KIND_STRING ||
525 unmodified lines