Add support for negative indices for .[]; fix #462
Signed-off-by: Nicolas Williams <nico@cryptonector.com>
This commit is contained in:
1 parent
7d3a44a1e2
commit
d0ca11d665
3 files changed
+17
-1
No files matched your search
@@ -256,6 +256,11 @@ sections:
|
|||||||
the array), or omitted (in which case it refers to the start
|
the array), or omitted (in which case it refers to the start
|
||||||
or end of the array).
|
or end of the array).
|
||||||
|
|
||||||
|
The `.[2]` syntax can be used to return the element at the
|
||||||
|
given index. Negative indices are allowed, with -1 referring
|
||||||
|
to the last element, -2 referring to the next to last element,
|
||||||
|
and so on.
|
||||||
|
|
||||||
The `?` "operator" can also be used with the slice operator,
|
The `?` "operator" can also be used with the slice operator,
|
||||||
as in `.[10:15]?`, which outputs values where the inputs are
|
as in `.[10:15]?`, which outputs values where the inputs are
|
||||||
slice-able.
|
slice-able.
|
||||||
@@ -285,6 +290,10 @@ sections:
|
|||||||
input: '["a","b","c","d","e"]'
|
input: '["a","b","c","d","e"]'
|
||||||
output: ['["d", "e"]']
|
output: ['["d", "e"]']
|
||||||
|
|
||||||
|
- program: '.[-2]'
|
||||||
|
input: '[1,2,3]'
|
||||||
|
output: ['2']
|
||||||
|
|
||||||
- title: "`.[]`"
|
- title: "`.[]`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
|
|||||||
@@ -57,7 +57,10 @@ jv jv_get(jv t, jv k) {
|
|||||||
}
|
}
|
||||||
} else if (jv_get_kind(t) == JV_KIND_ARRAY && jv_get_kind(k) == JV_KIND_NUMBER) {
|
} else if (jv_get_kind(t) == JV_KIND_ARRAY && jv_get_kind(k) == JV_KIND_NUMBER) {
|
||||||
if(jv_is_integer(k)){
|
if(jv_is_integer(k)){
|
||||||
v = jv_array_get(t, (int)jv_number_value(k));
|
int idx = (int)jv_number_value(k);
|
||||||
|
if (idx < 0)
|
||||||
|
idx += jv_array_length(jv_copy(t));
|
||||||
|
v = jv_array_get(t, idx);
|
||||||
if (!jv_is_valid(v)) {
|
if (!jv_is_valid(v)) {
|
||||||
jv_free(v);
|
jv_free(v);
|
||||||
v = jv_null();
|
v = jv_null();
|
||||||
|
|||||||
@@ -202,6 +202,10 @@ null
|
|||||||
2
|
2
|
||||||
3
|
3
|
||||||
|
|
||||||
|
.[-2]
|
||||||
|
[1,2,3]
|
||||||
|
2
|
||||||
|
|
||||||
[range(0;10)]
|
[range(0;10)]
|
||||||
null
|
null
|
||||||
[0,1,2,3,4,5,6,7,8,9]
|
[0,1,2,3,4,5,6,7,8,9]
|
||||||
|
|||||||
Reference in new issue
Block a user