Skip to content

Commit

Permalink
Fix findfirst with decreasing step range (#33809)
Browse files Browse the repository at this point in the history
Fixes #33808
(cherry picked from commit 8333297)
  • Loading branch information
laborg authored and JeffBezanson committed Nov 13, 2019
1 parent cdbd38c commit 78b7402
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 2 additions & 1 deletion base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1755,7 +1755,8 @@ findfirst(testf::Function, A::Union{AbstractArray, AbstractString}) =
findnext(testf, A, first(keys(A)))

function findfirst(p::Union{Fix2{typeof(isequal),T},Fix2{typeof(==),T}}, r::StepRange{T,S}) where {T,S}
first(r) <= p.x <= last(r) || return nothing
isempty(r) && return nothing
minimum(r) <= p.x <= maximum(r) || return nothing
d = convert(S, p.x - first(r))
iszero(d % step(r)) || return nothing
return d ÷ step(r) + 1
Expand Down
2 changes: 2 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ end
@test findfirst(==(7), 1:2:10) == 4
@test findfirst(==(10), 1:2:10) == nothing
@test findfirst(==(11), 1:2:10) == nothing
@test findfirst(==(-7), 1:-1:-10) == 9
@test findfirst(==(2),1:-1:2) == nothing
end
@testset "reverse" begin
@test reverse(reverse(1:10)) == 1:10
Expand Down

0 comments on commit 78b7402

Please sign in to comment.