Skip to content

Commit 8333297

Browse files
laborgJeffBezanson
authored andcommitted
Fix findfirst with decreasing step range (#33809)
Fixes #33808
1 parent 7358173 commit 8333297

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

base/array.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1773,7 +1773,8 @@ findfirst(testf::Function, A::Union{AbstractArray, AbstractString}) =
17731773
findnext(testf, A, first(keys(A)))
17741774

17751775
function findfirst(p::Union{Fix2{typeof(isequal),T},Fix2{typeof(==),T}}, r::StepRange{T,S}) where {T,S}
1776-
first(r) <= p.x <= last(r) || return nothing
1776+
isempty(r) && return nothing
1777+
minimum(r) <= p.x <= maximum(r) || return nothing
17771778
d = convert(S, p.x - first(r))
17781779
iszero(d % step(r)) || return nothing
17791780
return d ÷ step(r) + 1

test/ranges.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ end
305305
@test findfirst(==(7), 1:2:10) == 4
306306
@test findfirst(==(10), 1:2:10) == nothing
307307
@test findfirst(==(11), 1:2:10) == nothing
308+
@test findfirst(==(-7), 1:-1:-10) == 9
309+
@test findfirst(==(2),1:-1:2) == nothing
308310
end
309311
@testset "reverse" begin
310312
@test reverse(reverse(1:10)) == 1:10

0 commit comments

Comments
 (0)