diff options
author | Muh Muhten <muh.muhten@gmail.com> | 2019-02-26 05:33:36 -0500 |
---|---|---|
committer | Nico Williams <nico@cryptonector.com> | 2019-02-26 11:05:25 -0600 |
commit | 8c1e7175ec75cc7933d9c2839a684250b4ff5ef4 (patch) | |
tree | e3a7d5512924aae2a1871b29031c627af297b73d | |
parent | fc6df0fdc1a130575e3f2074cae56e221cca7038 (diff) |
Reimplement fromstream/1 more compactly
-rw-r--r-- | src/builtin.jq | 28 |
1 files changed, 8 insertions, 20 deletions
diff --git a/src/builtin.jq b/src/builtin.jq index 6b092bc0..7916d7d8 100644 --- a/src/builtin.jq +++ b/src/builtin.jq @@ -206,26 +206,14 @@ def ascii_upcase: # Streaming utilities def truncate_stream(stream): . as $n | null | stream | . as $input | if (.[0]|length) > $n then setpath([0];$input[0][$n:]) else empty end; -def fromstream(i): - foreach i as $i ( - [null, null]; - - if ($i | length) == 2 then - if ($i[0] | length) == 0 then . - else [ ( .[0] | setpath($i[0]; $i[1]) ), .[1] ] - end - elif ($i[0] | length) == 1 then [ null, .[0] ] - else . - end; - - if ($i | length) == 1 then - if ($i[0] | length) == 1 then .[1] - else empty - end - elif ($i[0] | length) == 0 then $i[1] - else empty - end - ); +def fromstream(i): {x: null, e: false} as $init | + # .x = object being built; .e = emit and reset state + foreach i as $i ($init + ; if .e then $init else . end + | if $i|length == 2 + then setpath(["e"]; $i[0]|length==0) | setpath(["x"]+$i[0]; $i[1]) + else setpath(["e"]; $i[0]|length==1) end + ; if .e then .x else empty end); def tostream: {string:true,number:true,boolean:true,null:true} as $leaf_types | . as $dot | |