Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Make empty ranges compare equal #32348

Merged
merged 9 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -714,18 +714,20 @@ show(io::IO, r::UnitRange) = print(io, repr(first(r)), ':', repr(last(r)))
show(io::IO, r::OneTo) = print(io, "Base.OneTo(", r.stop, ")")

==(r::T, s::T) where {T<:AbstractRange} =
(first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s))
(isempty(r) & isempty(s)) | ((first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s)))
==(r::OrdinalRange, s::OrdinalRange) =
(first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s))
(isempty(r) & isempty(s)) | ((first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s)))
==(r::T, s::T) where {T<:Union{StepRangeLen,LinRange}} =
(first(r) == first(s)) & (length(r) == length(s)) & (last(r) == last(s))
(isempty(r) & isempty(s)) | ((first(r) == first(s)) & (length(r) == length(s)) & (last(r) == last(s)))
==(r::Union{StepRange{T},StepRangeLen{T,T}}, s::Union{StepRange{T},StepRangeLen{T,T}}) where {T} =
(first(r) == first(s)) & (last(r) == last(s)) & (step(r) == step(s))
(isempty(r) & isempty(s)) | ((first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s)))

function ==(r::AbstractRange, s::AbstractRange)
lr = length(r)
if lr != length(s)
return false
elseif iszero(lr)
return true
end
yr, ys = iterate(r), iterate(s)
while yr !== nothing
Expand Down
8 changes: 4 additions & 4 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ end
function _split(str::AbstractString, splitter, limit::Integer, keepempty::Bool, strs::Array)
i = 1 # firstindex(str)
n = lastindex(str)
r = something(findfirst(splitter,str), 0)
if r != 0:-1
r = findfirst(splitter,str)
if !isnothing(r)
sostock marked this conversation as resolved.
Show resolved Hide resolved
j, k = first(r), nextind(str,last(r))
while 0 < j <= n && length(strs) != limit-1
if i < k
Expand All @@ -335,8 +335,8 @@ function _split(str::AbstractString, splitter, limit::Integer, keepempty::Bool,
i = k
end
(k <= j) && (k = nextind(str,j))
r = something(findnext(splitter,str,k), 0)
r == 0:-1 && break
r = findnext(splitter,str,k)
isnothing(r) && break
j, k = first(r), nextind(str,last(r))
end
end
Expand Down
4 changes: 3 additions & 1 deletion test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,8 @@ end
map(Int32,1:3:17), map(Int64,1:3:17), 1:0, 1:-1:0, 17:-3:0,
0.0:0.1:1.0, map(Float32,0.0:0.1:1.0),map(Float32,LinRange(0.0, 1.0, 11)),
1.0:eps():1.0 .+ 10eps(), 9007199254740990.:1.0:9007199254740994,
range(0, stop=1, length=20), map(Float32, range(0, stop=1, length=20))]
range(0, stop=1, length=20), map(Float32, range(0, stop=1, length=20)),
3:2, 5:-2:7, range(0.0, step=2.0, length=0), 3//2:3//2:0//1, LinRange(2,3,0)]
for r in Rs
local r
ar = Vector(r)
Expand All @@ -785,6 +786,7 @@ end
@test 1:1:10 == 1:10 == 1:10 == Base.OneTo(10) == Base.OneTo(10)
@test 1:10 != 2:10 != 2:11 != Base.OneTo(11)
@test Base.OneTo(10) != Base.OneTo(11) != 1:10
@test Base.OneTo(0) == 5:4
end
# issue #2959
@test 1.0:1.5 == 1.0:1.0:1.5 == 1.0:1.0
Expand Down