Skip to content
Myr

Fix bizarre bsearch/1 behaviour with a stream argument

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

authored by Muh Muhten · committed by Nico Williams · parent d1a07cb · Browse files · 2 files+8 added−6 removed
This commit

State

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

Diff stats

2 files+8 added−6 removed

  • tests:+3 added−1 removed
  • other:+5 added−5 removed

2 files+8 added−6 removed

modified:src/builtin.jq+5 added-5 removed
222 unmodified lines
223# Assuming the input array is sorted, bsearch/1 returns224# the index of the target if the target is in the input array; and otherwise225# (-1 - ix), where ix is the insertion point that would leave the array sorted.226# If the input is not sorted, bsearch will terminate but with irrelevant results.227def bsearch(target):227def bsearch($target):228 if length == 0 then -1229 elif length == 1 then230 if target == .[0] then 0 elif target < .[0] then -1 else -2 end230 if $target == .[0] then 0 elif $target < .[0] then -1 else -2 end231 else . as $in232 # state variable: [start, end, answer]233 # where start and end are the upper and lower offsets to use.234 | [0, length-1, null]
1 unmodified line
236 if .[2] != null then (.[1] = -1) # i.e. break237 else238 ( ( (.[1] + .[0]) / 2 ) | floor ) as $mid239 | $in[$mid] as $monkey240 | if $monkey == target then (.[2] = $mid) # success240 | if $monkey == $target then (.[2] = $mid) # success241 elif .[0] == .[1] then (.[1] = -1) # failure242 elif $monkey < target then (.[0] = ($mid + 1))242 elif $monkey < $target then (.[0] = ($mid + 1))243 else (.[1] = ($mid - 1))244 end245 end )246 | if .[2] == null then # compute the insertion point247 if $in[ .[0] ] < target then (-2 -.[0])247 if $in[ .[0] ] < $target then (-2 -.[0])248 else (-1 -.[0])249 end250 else .[2]251 end
28 unmodified lines
modified:tests/jq.test+3 added-1 removed
1425 unmodified lines
1426ascii_upcase1427"useful but not for é"1428"USEFUL BUT NOT FOR é"14291430bsearch(4)1430bsearch(0,2,4)1431[1,2,3]1432-1143311434-414351436# strptime tests are in optional.test1437
181 unmodified lines