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 5 commits
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ New language features
Language changes
----------------

* Empty ranges now compare equal, regardless of their startpoint and step ([#32348]).

Multi-threading changes
-----------------------
Expand Down
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
13 changes: 9 additions & 4 deletions stdlib/Dates/test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ using Dates

using InteractiveUtils: subtypes

# Compare ranges by comparing their `first` and `last` elements and their `length`. This
# returns `false` if empty ranges have different startpoints.
==ᵣ(r::AbstractRange, s::AbstractRange) =
(first(r) == first(s)) & (length(r) == length(s)) & (last(r) == last(s))

let
for T in (Dates.Date, Dates.DateTime)
f1 = T(2014); l1 = T(2013, 12, 31)
Expand Down Expand Up @@ -35,7 +40,7 @@ let
@test first(reverse(dr)) < f1
@test last(reverse(dr)) >= f1
@test issorted(dr)
@test sortperm(dr) == 1:1:0
@test sortperm(dr) == 1:1:0
@test !(f1 in dr)
@test !(l1 in dr)
@test !(f1 - pos_step in dr)
Expand Down Expand Up @@ -93,7 +98,7 @@ let
@test first(reverse(dr)) > l1
@test last(reverse(dr)) <= l1
@test issorted(dr)
@test sortperm(dr) == 1:1:0
@test sortperm(dr) == 1:1:0
@test !(l1 in dr)
@test !(l1 in dr)
@test !(l1 - neg_step in dr)
Expand Down Expand Up @@ -153,7 +158,7 @@ let
@test first(reverse(dr)) < f1
@test last(reverse(dr)) >= f1
@test issorted(dr)
@test sortperm(dr) == 1:1:0
@test sortperm(dr) == 1:1:0
@test !(f1 in dr)
@test !(l1 in dr)
@test !(f1 - pos_step in dr)
Expand Down Expand Up @@ -211,7 +216,7 @@ let
@test first(reverse(dr)) > l1
@test last(reverse(dr)) <= l1
@test issorted(dr)
@test sortperm(dr) == 1:1:0
@test sortperm(dr) == 1:1:0
@test !(l1 in dr)
@test !(l1 in dr)
@test !(l1 - neg_step in dr)
Expand Down
20 changes: 15 additions & 5 deletions stdlib/REPL/test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ test_scomplete(s) = map_completion_text(shell_completions(s,lastindex(s)))
test_bslashcomplete(s) = map_completion_text(bslash_completions(s,lastindex(s))[2])
test_complete_context(s) = map_completion_text(completions(s,lastindex(s),Main.CompletionFoo))

# Compare ranges by comparing their `first` and `last` elements and their `length`. This
# returns `false` if empty ranges have different startpoints.
==ᵣ(r::AbstractRange, s::AbstractRange) =
(first(r) == first(s)) & (length(r) == length(s)) & (last(r) == last(s))

let s = ""
c, r = test_complete(s)
@test "CompletionFoo" in c
Expand Down Expand Up @@ -126,7 +131,7 @@ end
let s = "Main.CompletionFoo."
c, r = test_complete(s)
@test "bar" in c
@test r == 20:19
@test r == 20:19
@test s[r] == ""
end

Expand Down Expand Up @@ -550,7 +555,12 @@ end

# The return type is of importance, before #8995 it would return nothing
# which would raise an error in the repl code.
@test (String[], 0:-1, false) == test_scomplete("\$a")
let c, r, res
c, r, res = test_scomplete("\$a")
@test c == String[]
@test r ==ᵣ 0:-1
@test res === false
end

if Sys.isunix()
let s, c, r
Expand Down Expand Up @@ -598,7 +608,7 @@ let s, c, r
s = "/tmp/"
c,r = test_scomplete(s)
@test !("tmp/" in c)
@test r == 6:5
@test r == 6:5
@test s[r] == ""
end

Expand All @@ -615,7 +625,7 @@ let s, c, r
file = joinpath(path, "repl completions")
s = "/tmp "
c,r = test_scomplete(s)
@test r == 6:5
@test r == 6:5
end

# Test completing paths with an escaped trailing space
Expand Down Expand Up @@ -929,7 +939,7 @@ end
let s = ""
c, r = test_complete_context(s)
@test "bar" in c
@test r == 1:0
@test r == 1:0
@test s[r] == ""
end

Expand Down
2 changes: 1 addition & 1 deletion test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6701,7 +6701,7 @@ function f27597(y)
return y
end
@test f27597([1]) == [1]
@test f27597([]) == 1:0
@test f27597([]) === 1:0

# issue #22291
wrap22291(ind) = (ind...,)
Expand Down
12 changes: 7 additions & 5 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,10 @@ end
end
end
@testset "indexing range with empty range (#4309)" begin
@test (3:6)[5:4] == 7:6
@test (3:6)[5:4] === 7:6
@test_throws BoundsError (3:6)[5:5]
@test_throws BoundsError (3:6)[5]
@test (0:2:10)[7:6] == 12:2:10
@test (0:2:10)[7:6] === 12:2:10
@test_throws BoundsError (0:2:10)[7:7]
end
# indexing with negative ranges (#8351)
Expand Down 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 Expand Up @@ -1477,11 +1479,11 @@ end
for stops in [-2, 0, 2, 100]
for lengths in [2, 10, 100]
if stops >= starts
@test range(starts, stops, length=lengths) == range(starts, stop=stops, length=lengths)
@test range(starts, stops, length=lengths) === range(starts, stop=stops, length=lengths)
end
end
for steps in [0.01, 1, 2]
@test range(starts, stops, step=steps) == range(starts, stop=stops, step=steps)
@test range(starts, stops, step=steps) === range(starts, stop=stops, step=steps)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
# findall:
@test findall(r"\w+", "foo bar") == [1:3, 5:7]
@test findall(r"\w+", "foo bar", overlap=true) == [1:3, 2:3, 3:3, 5:7, 6:7, 7:7]
@test findall(r"\w*", "foo bar") == [1:3, 4:3, 5:7, 8:7]
@test findall(r"\b", "foo bar") == [1:0, 4:3, 5:4, 8:7]
@test all(findall(r"\w*", "foo bar") .=== [1:3, 4:3, 5:7, 8:7]) # use === to compare empty ranges
@test all(findall(r"\b", "foo bar") .=== [1:0, 4:3, 5:4, 8:7]) # use === to compare empty ranges

# Named subpatterns
let m = match(r"(?<a>.)(.)(?<b>.)", "xyz")
Expand Down
34 changes: 20 additions & 14 deletions test/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ let a=[1:10;]
end
@test sum(randperm(6)) == 21

# Compare ranges by comparing their `first` and `last` elements and their `length`. This
# returns `false` if empty ranges have different startpoints, which is relevant for
# `searchsorted`
==ᵣ(r::AbstractRange, s::AbstractRange) =
(first(r) == first(s)) & (length(r) == length(s)) & (last(r) == last(s))

@testset "searchsorted" begin
numTypes = [ Int8, Int16, Int32, Int64, Int128,
UInt8, UInt16, UInt32, UInt64, UInt128,
Expand All @@ -46,16 +52,16 @@ end
@test searchsorted(fill(1, 15), 1, 6, 10, Forward) == 6:10

for R in numTypes, T in numTypes
@test searchsorted(R[1, 1, 2, 2, 3, 3], T(0)) == 1:0
@test searchsorted(R[1, 1, 2, 2, 3, 3], T(0)) === 1:0
@test searchsorted(R[1, 1, 2, 2, 3, 3], T(1)) == 1:2
@test searchsorted(R[1, 1, 2, 2, 3, 3], T(2)) == 3:4
@test searchsorted(R[1, 1, 2, 2, 3, 3], T(4)) == 7:6
@test searchsorted(R[1, 1, 2, 2, 3, 3], 2.5) == 5:4
@test searchsorted(R[1, 1, 2, 2, 3, 3], T(4)) === 7:6
@test searchsorted(R[1, 1, 2, 2, 3, 3], 2.5) === 5:4

@test searchsorted(1:3, T(0)) == 1:0
@test searchsorted(1:3, T(0)) == 1:0
@test searchsorted(1:3, T(1)) == 1:1
@test searchsorted(1:3, T(2)) == 2:2
@test searchsorted(1:3, T(4)) == 4:3
@test searchsorted(1:3, T(4)) == 4:3

@test searchsorted(R[1:10;], T(1), by=(x -> x >= 5)) == 1:4
@test searchsorted(R[1:10;], T(10), by=(x -> x >= 5)) == 5:10
Expand All @@ -67,31 +73,31 @@ end
rg_r = reverse(rg)
rgv, rgv_r = [rg;], [rg_r;]
for i = I
@test searchsorted(rg,i) == searchsorted(rgv,i)
@test searchsorted(rg_r,i,rev=true) == searchsorted(rgv_r,i,rev=true)
@test searchsorted(rg,i) === searchsorted(rgv,i)
@test searchsorted(rg_r,i,rev=true) === searchsorted(rgv_r,i,rev=true)
end
end

rg = 0.0:0.01:1.0
for i = 2:101
@test searchsorted(rg, rg[i]) == i:i
@test searchsorted(rg, prevfloat(rg[i])) == i:i-1
@test searchsorted(rg, nextfloat(rg[i])) == i+1:i
@test searchsorted(rg, prevfloat(rg[i])) === i:i-1
@test searchsorted(rg, nextfloat(rg[i])) === i+1:i
end

rg_r = reverse(rg)
for i = 1:100
@test searchsorted(rg_r, rg_r[i], rev=true) == i:i
@test searchsorted(rg_r, prevfloat(rg_r[i]), rev=true) == i+1:i
@test searchsorted(rg_r, nextfloat(rg_r[i]), rev=true) == i:i-1
@test searchsorted(rg_r, prevfloat(rg_r[i]), rev=true) === i+1:i
@test searchsorted(rg_r, nextfloat(rg_r[i]), rev=true) === i:i-1
end

@test searchsorted(1:10, 1, by=(x -> x >= 5)) == searchsorted([1:10;], 1, by=(x -> x >= 5))
@test searchsorted(1:10, 10, by=(x -> x >= 5)) == searchsorted([1:10;], 10, by=(x -> x >= 5))

@test searchsorted([], 0) == 1:0
@test searchsorted([1,2,3], 0) == 1:0
@test searchsorted([1,2,3], 4) == 4:3
@test searchsorted([], 0) === 1:0
@test searchsorted([1,2,3], 0) === 1:0
@test searchsorted([1,2,3], 4) === 4:3

@testset "issue 8866" begin
@test searchsortedfirst(500:1.0:600, -1.0e20) == 1
Expand Down
6 changes: 3 additions & 3 deletions test/strings/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ end
for i = 1:lastindex(u8str)
@test findnext("", u8str, i) == i:i-1
end
@test findfirst("", "") == 1:0
@test findfirst("", "") === 1:0

# string backward search with a zero-char string
for i = 1:lastindex(astr)
Expand All @@ -256,7 +256,7 @@ end
for i = 1:lastindex(u8str)
@test findprev("", u8str, i) == i:i-1
end
@test findlast("", "") == 1:0
@test findlast("", "") === 1:0

# string forward search with a zero-char regex
for i = 1:lastindex(astr)
Expand Down Expand Up @@ -377,7 +377,7 @@ s_18109 = "fooα🐨βcd3"
@testset "findall" begin
@test findall("fooo", "foo") == UnitRange{Int}[]
@test findall("ing", "Spinning laughing dancing") == [6:8, 15:17, 23:25]
@test findall("", "foo") == [1:0, 2:1, 3:2, 4:3]
@test all(findall("", "foo") .=== [1:0, 2:1, 3:2, 4:3]) # use === to compare empty ranges
@test findall("αβ", "blαh blαβ blαββy") == findall("αβ", "blαh blαβ blαββy", overlap=true) == [9:11, 16:18]
@test findall("aa", "aaaaaa") == [1:2, 3:4, 5:6]
@test findall("aa", "aaaaaa", overlap=true) == [1:2, 2:3, 3:4, 4:5, 5:6]
Expand Down