Skip to content

Commit 707400f

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 cf8343d commit 707400f

File tree

3 files changed

+67
-40
lines changed

3 files changed

+67
-40
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ Currently, the `@compat` macro supports the following syntaxes:
324324
* `search` is now `findfirst`/`findnext` and `rsearch` is now `findlast`/`findprev`,
325325
sometimes combined with `equalto` or `occursin` ([#24673]).
326326

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

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

src/Compat.jl

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,9 +1519,12 @@ end
15191519
export printstyled
15201520
end
15211521

1522-
@static if VERSION < v"0.7.0-DEV.3272"
1523-
import Base: in, findfirst, findnext, findlast, findprev
1524-
1522+
@static if VERSION >= v"0.7.0-DEV.3272"
1523+
findnext(xs...) = Base.findnext(xs...)
1524+
findfirst(xs...) = Base.findfirst(xs...)
1525+
findprev(xs...) = Base.findprev(xs...)
1526+
findlast(xs...) = Base.findlast(xs...)
1527+
else
15251528
struct OccursIn{T} <: Function
15261529
x::T
15271530

@@ -1531,41 +1534,47 @@ end
15311534
const occursin = OccursIn
15321535
export occursin
15331536

1534-
zero2nothing(x) = x == 0 ? nothing : x
1537+
zero2nothing(x::Integer) = x == 0 ? nothing : x
1538+
zero2nothing(x) = x
1539+
1540+
findnext(xs...) = zero2nothing(Base.findnext(xs...))
1541+
findfirst(xs...) = zero2nothing(Base.findfirst(xs...))
1542+
findprev(xs...) = zero2nothing(Base.findprev(xs...))
1543+
findlast(xs...) = zero2nothing(Base.findlast(xs...))
15351544

1536-
findnext(r::Regex, s::AbstractString, idx::Integer) = search(s, r, idx)
1537-
findfirst(r::Regex, s::AbstractString) = search(s, r)
1538-
findnext(c::EqualTo{Char}, s::AbstractString, i::Integer) = zero2nothing(search(s, c.x, i))
1539-
findfirst(c::EqualTo{Char}, s::AbstractString) = zero2nothing(search(s, c.x))
1540-
findnext(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}, i::Integer) =
1541-
zero2nothing(search(a, b.x, i))
1542-
findfirst(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}) =
1543-
zero2nothing(search(a, b.x))
1545+
Base.findnext(r::Regex, s::AbstractString, idx::Integer) = search(s, r, idx)
1546+
Base.findfirst(r::Regex, s::AbstractString) = search(s, r)
1547+
Base.findnext(c::EqualTo{Char}, s::AbstractString, i::Integer) = search(s, c.x, i)
1548+
Base.findfirst(c::EqualTo{Char}, s::AbstractString) = search(s, c.x)
1549+
Base.findnext(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}, i::Integer) =
1550+
search(a, b.x, i)
1551+
Base.findfirst(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}) =
1552+
search(a, b.x)
15441553

1545-
findnext(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
1554+
Base.findnext(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
15461555
s::AbstractString, i::Integer) =
1547-
zero2nothing(search(s, c.x, i))
1548-
findfirst(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
1556+
search(s, c.x, i)
1557+
Base.findfirst(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
15491558
s::AbstractString) =
1550-
zero2nothing(search(s, c.x))
1551-
findnext(t::AbstractString, s::AbstractString, i::Integer) = search(s, t, i)
1552-
findfirst(t::AbstractString, s::AbstractString) = search(s, t)
1553-
1554-
findfirst(delim::EqualTo{UInt8}, buf::IOBuffer) = zero2nothing(search(buf, delim.x))
1555-
1556-
findprev(c::EqualTo{Char}, s::AbstractString, i::Integer) = zero2nothing(rsearch(s, c.x, i))
1557-
findlast(c::EqualTo{Char}, s::AbstractString) = zero2nothing(rsearch(s, c.x))
1558-
findprev(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}, i::Integer) =
1559-
zero2nothing(rsearch(a, b.x, i))
1560-
findlast(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}) =
1561-
zero2nothing(rsearch(a, b.x))
1562-
1563-
findprev(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
1564-
s::AbstractString, i::Integer) = zero2nothing(rsearch(s, c.x, i))
1565-
findlast(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
1566-
s::AbstractString) = zero2nothing(rsearch(s, c.x))
1567-
findprev(t::AbstractString, s::AbstractString, i::Integer) = rsearch(s, t, i)
1568-
findlast(t::AbstractString, s::AbstractString) = rsearch(s, t)
1559+
search(s, c.x)
1560+
Base.findnext(t::AbstractString, s::AbstractString, i::Integer) = search(s, t, i)
1561+
Base.findfirst(t::AbstractString, s::AbstractString) = search(s, t)
1562+
1563+
Base.findfirst(delim::EqualTo{UInt8}, buf::IOBuffer) = search(buf, delim.x)
1564+
1565+
Base.findprev(c::EqualTo{Char}, s::AbstractString, i::Integer) = rsearch(s, c.x, i)
1566+
Base.findlast(c::EqualTo{Char}, s::AbstractString) = rsearch(s, c.x)
1567+
Base.findprev(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}, i::Integer) =
1568+
rsearch(a, b.x, i)
1569+
Base.findlast(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}) =
1570+
rsearch(a, b.x)
1571+
1572+
Base.findprev(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
1573+
s::AbstractString, i::Integer) = rsearch(s, c.x, i)
1574+
Base.findlast(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
1575+
s::AbstractString) = rsearch(s, c.x)
1576+
Base.findprev(t::AbstractString, s::AbstractString, i::Integer) = rsearch(s, t, i)
1577+
Base.findlast(t::AbstractString, s::AbstractString) = rsearch(s, t)
15691578

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

test/runtests.jl

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,30 +1320,45 @@ let buf = IOBuffer()
13201320
end
13211321

13221322
# 0.7.0-DEV.3415
1323-
for (f1, f2, i) in ((findfirst, findnext, 1), (findlast, findprev, 2))
1323+
for (f1, f2, i) in ((Compat.findfirst, Compat.findnext, 1),
1324+
(Compat.findlast, Compat.findprev, 2))
13241325
# Generic methods
13251326
@test f1(equalto(0), [1, 0]) == f2(equalto(0), [1, 0], i) == 2
1327+
@test f1(equalto(9), [1, 0]) == f2(equalto(9), [1, 0], i) == nothing
13261328
@test f1(occursin([0, 2]), [1, 0]) == f2(occursin([0, 2]), [1, 0], i) == 2
1329+
@test f1(occursin([0, 2]), [1, 9]) == f2(occursin([0, 2]), [1, 9], i) == nothing
1330+
@test f1([true, false]) == f2([true, false], i) == 1
1331+
@test f1([false, false]) == f2([false, false], i) == nothing
13271332

13281333
# Specific methods
13291334
@test f2(equalto('a'), "ba", i) == f1(equalto('a'), "ba") == 2
13301335
for S in (Int8, UInt8), T in (Int8, UInt8)
13311336
# Bug in Julia 0.6
1332-
f1 === findlast && VERSION < v"0.7.0-DEV.3272" && continue
1337+
f1 === Compat.findlast && VERSION < v"0.7.0-DEV.3272" && continue
13331338
@test f2(equalto(S(1)), T[0, 1], i) == f1(equalto(S(1)), T[0, 1]) == 2
13341339
@test f2(equalto(S(9)), T[0, 1], i) == f1(equalto(S(9)), T[0, 1]) == nothing
13351340
end
13361341
for chars in (['a', 'z'], Set(['a', 'z']), ('a', 'z'))
13371342
@test f2(occursin(chars), "ba", i) == f1(occursin(chars), "ba") == 2
13381343
@test f2(occursin(chars), "bx", i) == f1(occursin(chars), "bx") == nothing
13391344
end
1345+
end
1346+
for (f1, f2, i) in ((findfirst, findnext, 1),
1347+
(findlast, findprev, 2),
1348+
(Compat.findfirst, Compat.findnext, 1),
1349+
(Compat.findlast, Compat.findprev, 2))
13401350
@test f2("a", "ba", i) == f1("a", "ba") == 2:2
13411351
@test f2("z", "ba", i) == f1("z", "ba") == 0:-1
13421352
end
1343-
@test findnext(r"a", "ba", 1) == findfirst(r"a", "ba") == 2:2
1344-
@test findnext(r"z", "ba", 1) == findfirst(r"z", "ba") == 0:-1
1345-
@test findfirst(equalto(UInt8(0)), IOBuffer(UInt8[1, 0])) == 2
1346-
@test findfirst(equalto(UInt8(9)), IOBuffer(UInt8[1, 0])) == nothing
1353+
for (f1, f2, i) in ((findfirst, findnext, 1),
1354+
(Compat.findfirst, Compat.findnext, 1))
1355+
@test f2(r"a", "ba", 1) == f1(r"a", "ba") == 2:2
1356+
@test f2(r"z", "ba", 1) == f1(r"z", "ba") == 0:-1
1357+
end
1358+
1359+
@test Compat.findfirst(equalto(UInt8(0)), IOBuffer(UInt8[1, 0])) == 2
1360+
@test Compat.findfirst(equalto(UInt8(9)), IOBuffer(UInt8[1, 0])) == nothing
1361+
13471362
@test findall([true, false, true]) == [1, 3]
13481363
@test findall(occursin([1, 2]), [1]) == [1]
13491364

0 commit comments

Comments
 (0)