Skip to content

Commit b562683

Browse files
committed
Add unexported find* functions returning nothing
Change methods added to Base functions to return 0 on 0.6, which matches what already happens without the optimized methods.
1 parent 3058197 commit b562683

File tree

3 files changed

+67
-40
lines changed

3 files changed

+67
-40
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@ Currently, the `@compat` macro supports the following syntaxes:
322322
* `search` is now `findfirst`/`findnext` and `rsearch` is now `findlast`/`findprev`,
323323
sometimes combined with `equalto` or `occursin` ([#24673]).
324324

325+
* `Compat.findfirst`, `Compat.findnext`, `Compat.findlast` and `Compat.findprev`,
326+
return `nothing` when no match is found (rather than `0`) as on Julia 0.7 ([#24673]).
327+
325328
* `findin(a, b)` is now `findall(occursin(b), a)` ([#24673]).
326329

327330
* `indmin` and `indmax` are now `argmin` and `argmax`, respectively ([#25654]).

src/Compat.jl

+43-34
Original file line numberDiff line numberDiff line change
@@ -1510,9 +1510,12 @@ end
15101510
export lastindex
15111511
end
15121512

1513-
@static if VERSION < v"0.7.0-DEV.3272"
1514-
import Base: in, findfirst, findnext, findlast, findprev
1515-
1513+
@static if VERSION >= v"0.7.0-DEV.3272"
1514+
findnext(xs...) = Base.findnext(xs...)
1515+
findfirst(xs...) = Base.findfirst(xs...)
1516+
findprev(xs...) = Base.findprev(xs...)
1517+
findlast(xs...) = Base.findlast(xs...)
1518+
else
15161519
struct OccursIn{T} <: Function
15171520
x::T
15181521

@@ -1522,41 +1525,47 @@ end
15221525
const occursin = OccursIn
15231526
export occursin
15241527

1525-
zero2nothing(x) = x == 0 ? nothing : x
1528+
zero2nothing(x::Integer) = x == 0 ? nothing : x
1529+
zero2nothing(x) = x
1530+
1531+
findnext(xs...) = zero2nothing(Base.findnext(xs...))
1532+
findfirst(xs...) = zero2nothing(Base.findfirst(xs...))
1533+
findprev(xs...) = zero2nothing(Base.findprev(xs...))
1534+
findlast(xs...) = zero2nothing(Base.findlast(xs...))
15261535

1527-
findnext(r::Regex, s::AbstractString, idx::Integer) = search(s, r, idx)
1528-
findfirst(r::Regex, s::AbstractString) = search(s, r)
1529-
findnext(c::EqualTo{Char}, s::AbstractString, i::Integer) = zero2nothing(search(s, c.x, i))
1530-
findfirst(c::EqualTo{Char}, s::AbstractString) = zero2nothing(search(s, c.x))
1531-
findnext(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}, i::Integer) =
1532-
zero2nothing(search(a, b.x, i))
1533-
findfirst(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}) =
1534-
zero2nothing(search(a, b.x))
1536+
Base.findnext(r::Regex, s::AbstractString, idx::Integer) = search(s, r, idx)
1537+
Base.findfirst(r::Regex, s::AbstractString) = search(s, r)
1538+
Base.findnext(c::EqualTo{Char}, s::AbstractString, i::Integer) = search(s, c.x, i)
1539+
Base.findfirst(c::EqualTo{Char}, s::AbstractString) = search(s, c.x)
1540+
Base.findnext(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}, i::Integer) =
1541+
search(a, b.x, i)
1542+
Base.findfirst(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}) =
1543+
search(a, b.x)
15351544

1536-
findnext(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
1545+
Base.findnext(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
15371546
s::AbstractString, i::Integer) =
1538-
zero2nothing(search(s, c.x, i))
1539-
findfirst(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
1547+
search(s, c.x, i)
1548+
Base.findfirst(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
15401549
s::AbstractString) =
1541-
zero2nothing(search(s, c.x))
1542-
findnext(t::AbstractString, s::AbstractString, i::Integer) = search(s, t, i)
1543-
findfirst(t::AbstractString, s::AbstractString) = search(s, t)
1544-
1545-
findfirst(delim::EqualTo{UInt8}, buf::IOBuffer) = zero2nothing(search(buf, delim.x))
1546-
1547-
findprev(c::EqualTo{Char}, s::AbstractString, i::Integer) = zero2nothing(rsearch(s, c.x, i))
1548-
findlast(c::EqualTo{Char}, s::AbstractString) = zero2nothing(rsearch(s, c.x))
1549-
findprev(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}, i::Integer) =
1550-
zero2nothing(rsearch(a, b.x, i))
1551-
findlast(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}) =
1552-
zero2nothing(rsearch(a, b.x))
1553-
1554-
findprev(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
1555-
s::AbstractString, i::Integer) = zero2nothing(rsearch(s, c.x, i))
1556-
findlast(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
1557-
s::AbstractString) = zero2nothing(rsearch(s, c.x))
1558-
findprev(t::AbstractString, s::AbstractString, i::Integer) = rsearch(s, t, i)
1559-
findlast(t::AbstractString, s::AbstractString) = rsearch(s, t)
1550+
search(s, c.x)
1551+
Base.findnext(t::AbstractString, s::AbstractString, i::Integer) = search(s, t, i)
1552+
Base.findfirst(t::AbstractString, s::AbstractString) = search(s, t)
1553+
1554+
Base.findfirst(delim::EqualTo{UInt8}, buf::IOBuffer) = search(buf, delim.x)
1555+
1556+
Base.findprev(c::EqualTo{Char}, s::AbstractString, i::Integer) = rsearch(s, c.x, i)
1557+
Base.findlast(c::EqualTo{Char}, s::AbstractString) = rsearch(s, c.x)
1558+
Base.findprev(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}, i::Integer) =
1559+
rsearch(a, b.x, i)
1560+
Base.findlast(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}) =
1561+
rsearch(a, b.x)
1562+
1563+
Base.findprev(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
1564+
s::AbstractString, i::Integer) = rsearch(s, c.x, i)
1565+
Base.findlast(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
1566+
s::AbstractString) = rsearch(s, c.x)
1567+
Base.findprev(t::AbstractString, s::AbstractString, i::Integer) = rsearch(s, t, i)
1568+
Base.findlast(t::AbstractString, s::AbstractString) = rsearch(s, t)
15601569

15611570
findall(b::OccursIn, a) = findin(a, b.x)
15621571
# To fix ambiguity

test/runtests.jl

+21-6
Original file line numberDiff line numberDiff line change
@@ -1307,30 +1307,45 @@ end
13071307
@test lastindex(zeros(4,4)) == 16
13081308

13091309
# 0.7.0-DEV.3415
1310-
for (f1, f2, i) in ((findfirst, findnext, 1), (findlast, findprev, 2))
1310+
for (f1, f2, i) in ((Compat.findfirst, Compat.findnext, 1),
1311+
(Compat.findlast, Compat.findprev, 2))
13111312
# Generic methods
13121313
@test f1(equalto(0), [1, 0]) == f2(equalto(0), [1, 0], i) == 2
1314+
@test f1(equalto(9), [1, 0]) == f2(equalto(9), [1, 0], i) == nothing
13131315
@test f1(occursin([0, 2]), [1, 0]) == f2(occursin([0, 2]), [1, 0], i) == 2
1316+
@test f1(occursin([0, 2]), [1, 9]) == f2(occursin([0, 2]), [1, 9], i) == nothing
1317+
@test f1([true, false]) == f2([true, false], i) == 1
1318+
@test f1([false, false]) == f2([false, false], i) == nothing
13141319

13151320
# Specific methods
13161321
@test f2(equalto('a'), "ba", i) == f1(equalto('a'), "ba") == 2
13171322
for S in (Int8, UInt8), T in (Int8, UInt8)
13181323
# Bug in Julia 0.6
1319-
f1 === findlast && VERSION < v"0.7.0-DEV.3272" && continue
1324+
f1 === Compat.findlast && VERSION < v"0.7.0-DEV.3272" && continue
13201325
@test f2(equalto(S(1)), T[0, 1], i) == f1(equalto(S(1)), T[0, 1]) == 2
13211326
@test f2(equalto(S(9)), T[0, 1], i) == f1(equalto(S(9)), T[0, 1]) == nothing
13221327
end
13231328
for chars in (['a', 'z'], Set(['a', 'z']), ('a', 'z'))
13241329
@test f2(occursin(chars), "ba", i) == f1(occursin(chars), "ba") == 2
13251330
@test f2(occursin(chars), "bx", i) == f1(occursin(chars), "bx") == nothing
13261331
end
1332+
end
1333+
for (f1, f2, i) in ((findfirst, findnext, 1),
1334+
(findlast, findprev, 2),
1335+
(Compat.findfirst, Compat.findnext, 1),
1336+
(Compat.findlast, Compat.findprev, 2))
13271337
@test f2("a", "ba", i) == f1("a", "ba") == 2:2
13281338
@test f2("z", "ba", i) == f1("z", "ba") == 0:-1
13291339
end
1330-
@test findnext(r"a", "ba", 1) == findfirst(r"a", "ba") == 2:2
1331-
@test findnext(r"z", "ba", 1) == findfirst(r"z", "ba") == 0:-1
1332-
@test findfirst(equalto(UInt8(0)), IOBuffer(UInt8[1, 0])) == 2
1333-
@test findfirst(equalto(UInt8(9)), IOBuffer(UInt8[1, 0])) == nothing
1340+
for (f1, f2, i) in ((findfirst, findnext, 1),
1341+
(Compat.findfirst, Compat.findnext, 1))
1342+
@test f2(r"a", "ba", 1) == f1(r"a", "ba") == 2:2
1343+
@test f2(r"z", "ba", 1) == f1(r"z", "ba") == 0:-1
1344+
end
1345+
1346+
@test Compat.findfirst(equalto(UInt8(0)), IOBuffer(UInt8[1, 0])) == 2
1347+
@test Compat.findfirst(equalto(UInt8(9)), IOBuffer(UInt8[1, 0])) == nothing
1348+
13341349
@test findall([true, false, true]) == [1, 3]
13351350
@test findall(occursin([1, 2]), [1]) == [1]
13361351

0 commit comments

Comments
 (0)