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
Provenance
- Commit
a7b2253- Tree
3261ad5- Author
itchyny <itchyny@cybozu.co.jp>- Committer
GitHub <noreply@github.com>- Parent
3c5707f
6 files changed+46 added−4 removed
modified:docs/content/manual/dev/manual.yml+4 added-1 removed
3067 unmodified lines3068 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`.3070 3071 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: ['[]']3078 3079 - title: "`first`, `last`, `nth(n)`"3080 body: |3081 746 unmodified linesmodified:src/builtin.c+28 added
1877 unmodified lines1878 #undef LIBM_DDD1879 #undef LIBM_DD1880 #undef LIBM_DA1881 1882+// This is a hack to make last(g) yield no output values,1883+// if g yields no output values, without using boxing.1884+static 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+}1908+1909 struct bytecoded_builtin { const char* name; block code; };1910 static block bind_bytecoded_builtins(block b) {1911 block builtins = gen_noop();1912 {11 unmodified lines1924 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 linesmodified:src/builtin.jq-1 removed
168 unmodified lines169 def all(condition): all(.[]; condition);170 def any(condition): any(.[]; condition);171 def all: all(.[]; .);172 def any: any(.[]; .);173-def last(g): reduce g as $item (null; $item);173 def nth($n; g):174 if $n < 0 then error("nth doesn't support negative indices")175 else first(skip($n; g)) end;176 def first: .[0];108 unmodified linesmodified:tests/jq.test+4 added
360 unmodified lines361 [first(range(.)), last(range(.))]362 10363 [0,9]364 365+[first(range(.)), last(range(.))]366+0367+[]368+369 [nth(0,5,9,10,15; range(.)), try nth(-1; range(.)) catch .]370 10371 [0,5,9,"nth doesn't support negative indices"]372 1859 unmodified linesmodified:tests/man.test+5 added-1 removed
879 unmodified lines880 [skip(3; .[])]881 [0,1,2,3,4,5,6,7,8,9]882 [3,4,5,6,7,8,9]883 884-[first(range(.)), last(range(.)), nth(./2; range(.))]884+[first(range(.)), last(range(.)), nth(5; range(.))]885 10886 [0,9,5]887 888+[first(empty), last(empty), nth(5; empty)]889+null890+[]891+892 [range(.)]|[first, last, nth(5)]893 10894 [0,9,5]895 84 unmodified linesmodified:jq.1.prebuilt+5 added-1 removed
3473 unmodified lines3474 .IP "" 43475 .3476 .nf3477 3478-jq \'[first(range(\.)), last(range(\.)), nth(\./2; range(\.))]\'3478+jq \'[first(range(\.)), last(range(\.)), nth(5; range(\.))]\'3479 103480 => [0,9,5]3481+3482+jq \'[first(empty), last(empty), nth(5; empty)]\'3483+ null3484+=> []3485 .3486 .fi3487 .3488 .IP "" 0779 unmodified lines