Skip to content
Myr

fix: make last(empty) yield no output values (#3179)

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

authored by itchyny · committed by GitHub · parent 3c5707f · Browse files · 6 files+46 added−4 removed
This commit

State

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

Diff stats

6 files+46 added−4 removed

  • tests:+9 added−1 removed
  • other:+37 added−3 removed

6 files+46 added−4 removed

modified:docs/content/manual/dev/manual.yml+4 added-1 removed
3067 unmodified lines
3068 The `nth(n; expr)` function extracts the nth value output by `expr`.3069 Note that `nth(n; expr)` doesn't support negative values of `n`.30703071 examples:3072 - program: '[first(range(.)), last(range(.)), nth(./2; range(.))]'3072 - program: '[first(range(.)), last(range(.)), nth(5; range(.))]'3073 input: '10'3074 output: ['[0,9,5]']3075 - program: '[first(empty), last(empty), nth(5; empty)]'3076 input: 'null'3077 output: ['[]']30783079 - title: "`first`, `last`, `nth(n)`"3080 body: |3081
746 unmodified lines
modified:src/builtin.c+28 added
1877 unmodified lines
1878#undef LIBM_DDD1879#undef LIBM_DD1880#undef LIBM_DA18811882// This is a hack to make last(g) yield no output values,1883// if g yields no output values, without using boxing.1884static block gen_last_1() {1885 block last_var = gen_op_var_fresh(STOREV, "last");1886 block is_empty_var = gen_op_var_fresh(STOREV, "is_empty");1887 block init = BLOCK(gen_op_simple(DUP),1888 gen_const(jv_null()),1889 last_var,1890 gen_op_simple(DUP),1891 gen_const(jv_true()),1892 is_empty_var);1893 block call_arg = BLOCK(gen_call("arg", gen_noop()),1894 gen_op_simple(DUP),1895 gen_op_bound(STOREV, last_var),1896 gen_const(jv_false()),1897 gen_op_bound(STOREV, is_empty_var),1898 gen_op_simple(BACKTRACK));1899 block if_empty = gen_op_simple(BACKTRACK);1900 return BLOCK(init,1901 gen_op_target(FORK, call_arg),1902 call_arg,1903 BLOCK(gen_op_bound(LOADVN, is_empty_var),1904 gen_op_target(JUMP_F, if_empty),1905 if_empty,1906 gen_op_bound(LOADVN, last_var)));1907}19081909struct bytecoded_builtin { const char* name; block code; };1910static block bind_bytecoded_builtins(block b) {1911 block builtins = gen_noop();1912 {
11 unmodified lines
1924 struct bytecoded_builtin builtin_def_1arg[] = {1925 {"path", BLOCK(gen_op_simple(PATH_BEGIN),1926 gen_call("arg", gen_noop()),1927 gen_op_simple(PATH_END))},1928 {"last", gen_last_1()},1929 };1930 for (unsigned i=0; i<sizeof(builtin_def_1arg)/sizeof(builtin_def_1arg[0]); i++) {1931 builtins = BLOCK(builtins, gen_function(builtin_def_1arg[i].name,1932 gen_param("arg"),
48 unmodified lines
modified:src/builtin.jq-1 removed
168 unmodified lines
169def all(condition): all(.[]; condition);170def any(condition): any(.[]; condition);171def all: all(.[]; .);172def any: any(.[]; .);173def last(g): reduce g as $item (null; $item);173def nth($n; g):174 if $n < 0 then error("nth doesn't support negative indices")175 else first(skip($n; g)) end;176def first: .[0];
108 unmodified lines
modified:tests/jq.test+4 added
360 unmodified lines
361[first(range(.)), last(range(.))]36210363[0,9]364365[first(range(.)), last(range(.))]3660367[]368369[nth(0,5,9,10,15; range(.)), try nth(-1; range(.)) catch .]37010371[0,5,9,"nth doesn't support negative indices"]372
1859 unmodified lines
modified:tests/man.test+5 added-1 removed
879 unmodified lines
880[skip(3; .[])]881[0,1,2,3,4,5,6,7,8,9]882[3,4,5,6,7,8,9]883884[first(range(.)), last(range(.)), nth(./2; range(.))]884[first(range(.)), last(range(.)), nth(5; range(.))]88510886[0,9,5]887888[first(empty), last(empty), nth(5; empty)]889null890[]891892[range(.)]|[first, last, nth(5)]89310894[0,9,5]895
84 unmodified lines
modified:jq.1.prebuilt+5 added-1 removed
3473 unmodified lines
3474.IP "" 43475.3476.nf34773478jq \'[first(range(\.)), last(range(\.)), nth(\./2; range(\.))]\'3478jq \'[first(range(\.)), last(range(\.)), nth(5; range(\.))]\'3479 103480=> [0,9,5]34813482jq \'[first(empty), last(empty), nth(5; empty)]\'3483 null3484=> []3485.3486.fi3487.3488.IP "" 0
779 unmodified lines