Make fromstream/1 more efficient

This commit is contained in:
Eugen authored and Nico Williams committed 2018-12-18 14:20:35 -06:00
1 parent 6d3d2750ec
commit fb1c635af6
1 file changed
+17 -14
+17 -14
View File
@@ -217,22 +217,25 @@ def ascii_upcase:
def truncate_stream(stream): def truncate_stream(stream):
. as $n | null | stream | . as $input | if (.[0]|length) > $n then setpath([0];$input[0][$n:]) else empty end; . as $n | null | stream | . as $input | if (.[0]|length) > $n then setpath([0];$input[0][$n:]) else empty end;
def fromstream(i): def fromstream(i):
foreach i as $item ( foreach i as $i (
[null,false,null,false]; [null, null];
if ($item[0]|length) == 0 then [null,false,.[2],.[3]]
elif ($item|length) == 1 and ($item[0]|length) < 2 then [null,false,.[0],.[1]] if ($i | length) == 2 then
else . end | if ($i[0] | length) == 0 then .
. as $state | else [ ( .[0] | setpath($i[0]; $i[1]) ), .[1] ]
if ($item|length) > 1 and ($item[0]|length) > 0 then end
[.[0]|setpath(($item|.[0]); ($item|.[1])), elif ($i[0] | length) == 1 then [ null, .[0] ]
true,
$state[2],
$state[3]]
else . else .
end; end;
if ($item[0]|length) == 1 and ($item|length == 1) and .[3] then .[2] else empty end,
if ($item[0]|length) == 0 then $item[1] else empty 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 tostream: def tostream:
{string:true,number:true,boolean:true,null:true} as $leaf_types | {string:true,number:true,boolean:true,null:true} as $leaf_types |
. as $dot | . as $dot |