Implement `range/3` in C for consistent error handling (#3267)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by itchyny · committed by GitHub · parent dd409de · Browse files · 4 files·+46 added−27 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+46 added−27 removed
- tests:+20 added−0 removed
- other:+26 added−27 removed
Provenance
- Commit
e371bfa- Tree
153b9b7- Author
itchyny <itchyny@cybozu.co.jp>- Committer
GitHub <noreply@github.com>- Parent
dd409de
4 files changed+46 added−27 removed
modified:src/builtin.c+12 added-14 removed
2115 unmodified lines2116 builtin_def_1arg[i].code));2117 }2118 }2119 {2120- // Note that we can now define `range` as a jq-coded function2120 block rangevar = gen_op_var_fresh(STOREV, "rangevar");2122- block rangestart = gen_op_var_fresh(STOREV, "rangestart");2123- block range = BLOCK(gen_op_simple(DUP),2124- gen_call("start", gen_noop()),2125- rangestart,2126- gen_call("end", gen_noop()),2127- gen_op_simple(DUP),2128- gen_op_bound(LOADV, rangestart),2129- // Reset rangevar for every value generated by "end"2130- rangevar,2131- gen_op_bound(RANGE, rangevar));2132- builtins = BLOCK(builtins, gen_function("range",2133- BLOCK(gen_param("start"), gen_param("end")),2134- range));2121+ builtins = BLOCK(builtins,2122+ gen_function("range",2123+ BLOCK(gen_param_regular("start"),2124+ gen_param_regular("end"),2125+ gen_param_regular("step")),2126+ BLOCK(gen_op_unbound(LOADV, "step"),2127+ gen_op_simple(DUP),2128+ gen_op_unbound(LOADV, "end"),2129+ gen_op_simple(DUP),2130+ gen_op_unbound(LOADV, "start"),2131+ rangevar,2132+ gen_op_bound(RANGE, rangevar))));2133 }2134 return BLOCK(builtins, b);2135 }2136 25 unmodified linesmodified:src/builtin.jq+2 added-6 removed
40 unmodified lines41 def to_entries: [keys_unsorted[] as $k | {key: $k, value: .[$k]}];42 def from_entries: map({ (.key // .Key // .name // .Name):43 if has("value") then .value else .Value end }) | add // {};44 def with_entries(f): to_entries | map(f) | from_entries;45-def reverse: [.[length - 1 - range(0;length)]];45+def reverse: [.[range(length-1;-1;-1)]];46 def indices($i): if type == "array" and ($i|type) == "array" then .[$i]47 elif type == "array" then .[[$i]]48 elif type == "string" and ($i|type) == "string" then _strindices($i)49 else .[$i] end;19 unmodified lines69 ) // "";70 def _flatten($x): reduce .[] as $i ([]; if $i | type == "array" and $x != 0 then . + ($i | _flatten($x-1)) else . + [$i] end);71 def flatten($x): if $x < 0 then error("flatten depth must not be negative") else _flatten($x) end;72 def flatten: _flatten(-1);73+def range($x;$y): range($x;$y;1);74 def range($x): range(0;$x);75 def fromdateiso8601: strptime("%Y-%m-%dT%H:%M:%SZ")|mktime;76 def todateiso8601: strftime("%Y-%m-%dT%H:%M:%SZ");77 def fromdate: fromdateiso8601;70 unmodified lines148 def skip($n; expr):149 if $n > 0 then foreach expr as $item ($n; . - 1; if . < 0 then $item else empty end)150 elif $n == 0 then expr151 else error("skip doesn't support negative count") end;151-# range/3, with a `by` expression argument152-def range($init; $upto; $by):153- if $by > 0 then $init|while(. < $upto; . + $by)154- elif $by < 0 then $init|while(. > $upto; . + $by)155- else empty end;152 def first(g): label $out | g | ., break $out;153 def isempty(g): first((g|false), true);154 def all(generator; condition): isempty(generator|condition and empty);155 def any(generator; condition): isempty(generator|condition or empty)|not;85 unmodified linesmodified:src/execute.c+12 added-7 removed
514 unmodified lines515 uint16_t level = *pc++;516 uint16_t v = *pc++;517 jv* var = frame_local_var(jq, v, level);518 jv max = stack_pop(jq);519+ jv step = stack_pop(jq);520 if (raising) {521 jv_free(max);522+ jv_free(step);523 goto do_backtrack;522- } 523- if (jv_get_kind(*var) != JV_KIND_NUMBER ||524- jv_get_kind(max) != JV_KIND_NUMBER) {525- set_error(jq, jv_invalid_with_msg(jv_string_fmt("Range bounds must be numeric")));524+ }525+526+ if (jv_get_kind(*var) != JV_KIND_NUMBER || jv_get_kind(max) != JV_KIND_NUMBER ||527+ jv_get_kind(step) != JV_KIND_NUMBER) {528+ set_error(jq, jv_invalid_with_msg(jv_string("Range bounds and step must be numeric")));529 jv_free(max);530+ jv_free(step);531 goto do_backtrack;528- } else if (jv_number_value(*var) >= jv_number_value(max)) {529- /* finished iterating */532+ } else if (jv_cmp(jv_copy(*var), jv_copy(max)) * jv_cmp(jv_copy(step), jv_number(0)) >= 0) {533 jv_free(max);534+ jv_free(step);535 goto do_backtrack;536 } else {537 jv curr = *var;534- *var = jv_number(jv_number_value(*var) + 1);538+ *var = jv_number(jv_number_value(curr) + jv_number_value(step));539 540 struct stack_pos spos = stack_get_pos(jq);541+ stack_push(jq, step);542 stack_push(jq, max);543 stack_save(jq, pc - 3, spos);544 545 stack_push(jq, curr);808 unmodified linesmodified:tests/jq.test+20 added
302 unmodified lines303 [range(0;-5;-1)]304 null305 [0,-1,-2,-3,-4]306 307+[range(0;-10;-3)]308+null309+[0,-3,-6,-9]310+311+[range(0;10;0)]312+null313+[]314+315 [range(0,1;4,5;1,2)]316 null317 [0,1,2,3,0,2, 0,1,2,3,4,0,2,4, 1,2,3,1,3, 1,2,3,4,1,3]318 319+try range(.) catch .320+null321+"Range bounds and step must be numeric"322+323+try range(0;"") catch .324+null325+"Range bounds and step must be numeric"326+327+try range(0;1;[]) catch .328+null329+"Range bounds and step must be numeric"330+331 [while(.<100; .*2)]332 1333 [1,2,4,8,16,32,64]334 2321 unmodified lines