Skip to content

Commit a1626ad

Browse files
muhmuhtennicowilliams
authored andcommitted
Fix bizarre bsearch/1 behaviour with a stream argument
1 parent d1a07cb commit a1626ad

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/builtin.jq

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ def tostream:
224224
# the index of the target if the target is in the input array; and otherwise
225225
# (-1 - ix), where ix is the insertion point that would leave the array sorted.
226226
# If the input is not sorted, bsearch will terminate but with irrelevant results.
227-
def bsearch(target):
227+
def bsearch($target):
228228
if length == 0 then -1
229229
elif length == 1 then
230-
if target == .[0] then 0 elif target < .[0] then -1 else -2 end
230+
if $target == .[0] then 0 elif $target < .[0] then -1 else -2 end
231231
else . as $in
232232
# state variable: [start, end, answer]
233233
# where start and end are the upper and lower offsets to use.
@@ -237,14 +237,14 @@ def bsearch(target):
237237
else
238238
( ( (.[1] + .[0]) / 2 ) | floor ) as $mid
239239
| $in[$mid] as $monkey
240-
| if $monkey == target then (.[2] = $mid) # success
240+
| if $monkey == $target then (.[2] = $mid) # success
241241
elif .[0] == .[1] then (.[1] = -1) # failure
242-
elif $monkey < target then (.[0] = ($mid + 1))
242+
elif $monkey < $target then (.[0] = ($mid + 1))
243243
else (.[1] = ($mid - 1))
244244
end
245245
end )
246246
| if .[2] == null then # compute the insertion point
247-
if $in[ .[0] ] < target then (-2 -.[0])
247+
if $in[ .[0] ] < $target then (-2 -.[0])
248248
else (-1 -.[0])
249249
end
250250
else .[2]

tests/jq.test

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1427,8 +1427,10 @@ ascii_upcase
14271427
"useful but not for é"
14281428
"USEFUL BUT NOT FOR é"
14291429

1430-
bsearch(4)
1430+
bsearch(0,2,4)
14311431
[1,2,3]
1432+
-1
1433+
1
14321434
-4
14331435

14341436
# strptime tests are in optional.test

0 commit comments

Comments
 (0)