Skip to content

Commit

Permalink
Add occursin and find* methods replacing search and findin
Browse files Browse the repository at this point in the history
Add OccursIn predicate which replaces findin and all specific
findfirst/findnext/findlast/findprev methods for specific types.
These all call search or findin, but return nothing instead of 0
where appropriate. Note that there is an inconsistency with generic
find* methods which already existed in Julia 0.6 and which continue
to return 0: separate Compat.find* methods will have to be introduced
for them to avoid conflicts with Base.

Also fix in(::CartesianIndex, ::CartesianIndices) bug uncovered by importing in.
  • Loading branch information
nalimilan committed Feb 3, 2018
1 parent 19bad8c commit 3058197
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ Currently, the `@compat` macro supports the following syntaxes:

* `find` is now `findall` ([#25545]).

* `search` is now `findfirst`/`findnext` and `rsearch` is now `findlast`/`findprev`,
sometimes combined with `equalto` or `occursin` ([#24673]).

* `findin(a, b)` is now `findall(occursin(b), a)` ([#24673]).

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

* `isabstract` and `isleaftype` are now `isabstracttype` and `isconcretetype`, respectively
Expand Down Expand Up @@ -524,3 +529,4 @@ includes this fix. Find the minimum version from there.
[#25646]: https://github.com/JuliaLang/julia/issues/25646
[#25654]: https://github.com/JuliaLang/julia/issues/25654
[#24182]: https://github.com/JuliaLang/julia/issues/24182
[#24673]: https://github.com/JuliaLang/julia/issues/24673
56 changes: 55 additions & 1 deletion src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ end

@static if VERSION < v"0.7.0-DEV.3025"
import Base: convert, ndims, getindex, size, length, eltype,
start, next, done, first, last
start, next, done, first, last, in, tail
export CartesianIndices, LinearIndices

struct CartesianIndices{N,R<:NTuple{N,AbstractUnitRange{Int}}} <: AbstractArray{CartesianIndex{N},N}
Expand Down Expand Up @@ -1433,6 +1433,7 @@ else
@eval const $(Symbol("@error")) = Base.$(Symbol("@error"))
end

# 0.7.0-DEV.3415
if !isdefined(Base, :findall)
const findall = find
export findall
Expand Down Expand Up @@ -1509,6 +1510,59 @@ end
export lastindex
end

@static if VERSION < v"0.7.0-DEV.3272"
import Base: in, findfirst, findnext, findlast, findprev

struct OccursIn{T} <: Function
x::T

OccursIn(x::T) where {T} = new{T}(x)
end
(f::OccursIn)(y) = y in f.x
const occursin = OccursIn
export occursin

zero2nothing(x) = x == 0 ? nothing : x

findnext(r::Regex, s::AbstractString, idx::Integer) = search(s, r, idx)
findfirst(r::Regex, s::AbstractString) = search(s, r)
findnext(c::EqualTo{Char}, s::AbstractString, i::Integer) = zero2nothing(search(s, c.x, i))
findfirst(c::EqualTo{Char}, s::AbstractString) = zero2nothing(search(s, c.x))
findnext(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}, i::Integer) =
zero2nothing(search(a, b.x, i))
findfirst(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}) =
zero2nothing(search(a, b.x))

findnext(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
s::AbstractString, i::Integer) =
zero2nothing(search(s, c.x, i))
findfirst(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
s::AbstractString) =
zero2nothing(search(s, c.x))
findnext(t::AbstractString, s::AbstractString, i::Integer) = search(s, t, i)
findfirst(t::AbstractString, s::AbstractString) = search(s, t)

findfirst(delim::EqualTo{UInt8}, buf::IOBuffer) = zero2nothing(search(buf, delim.x))

findprev(c::EqualTo{Char}, s::AbstractString, i::Integer) = zero2nothing(rsearch(s, c.x, i))
findlast(c::EqualTo{Char}, s::AbstractString) = zero2nothing(rsearch(s, c.x))
findprev(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}, i::Integer) =
zero2nothing(rsearch(a, b.x, i))
findlast(b::EqualTo{<:Union{Int8,UInt8}}, a::Vector{<:Union{Int8,UInt8}}) =
zero2nothing(rsearch(a, b.x))

findprev(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
s::AbstractString, i::Integer) = zero2nothing(rsearch(s, c.x, i))
findlast(c::OccursIn{<:Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}},
s::AbstractString) = zero2nothing(rsearch(s, c.x))
findprev(t::AbstractString, s::AbstractString, i::Integer) = rsearch(s, t, i)
findlast(t::AbstractString, s::AbstractString) = rsearch(s, t)

findall(b::OccursIn, a) = findin(a, b.x)
# To fix ambiguity
findall(b::OccursIn, a::Number) = a in b.x ? [1] : Vector{Int}()
end

include("deprecated.jl")

end # module Compat
29 changes: 29 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,7 @@ let c = CartesianIndices(1:3, 1:2), l = LinearIndices(1:3, 1:2)
@test c[1:6] == vec(c)
@test l == l[c] == map(i -> l[i], c)
@test l[vec(c)] == collect(1:6)
@test CartesianIndex(1, 1) in CartesianIndices((3, 4))
end

if !isdefined(Base, Symbol("@info"))
Expand Down Expand Up @@ -1305,4 +1306,32 @@ end
@test lastindex(zeros(4)) == 4
@test lastindex(zeros(4,4)) == 16

# 0.7.0-DEV.3415
for (f1, f2, i) in ((findfirst, findnext, 1), (findlast, findprev, 2))
# Generic methods
@test f1(equalto(0), [1, 0]) == f2(equalto(0), [1, 0], i) == 2
@test f1(occursin([0, 2]), [1, 0]) == f2(occursin([0, 2]), [1, 0], i) == 2

# Specific methods
@test f2(equalto('a'), "ba", i) == f1(equalto('a'), "ba") == 2
for S in (Int8, UInt8), T in (Int8, UInt8)
# Bug in Julia 0.6
f1 === findlast && VERSION < v"0.7.0-DEV.3272" && continue
@test f2(equalto(S(1)), T[0, 1], i) == f1(equalto(S(1)), T[0, 1]) == 2
@test f2(equalto(S(9)), T[0, 1], i) == f1(equalto(S(9)), T[0, 1]) == nothing
end
for chars in (['a', 'z'], Set(['a', 'z']), ('a', 'z'))
@test f2(occursin(chars), "ba", i) == f1(occursin(chars), "ba") == 2
@test f2(occursin(chars), "bx", i) == f1(occursin(chars), "bx") == nothing
end
@test f2("a", "ba", i) == f1("a", "ba") == 2:2
@test f2("z", "ba", i) == f1("z", "ba") == 0:-1
end
@test findnext(r"a", "ba", 1) == findfirst(r"a", "ba") == 2:2
@test findnext(r"z", "ba", 1) == findfirst(r"z", "ba") == 0:-1
@test findfirst(equalto(UInt8(0)), IOBuffer(UInt8[1, 0])) == 2
@test findfirst(equalto(UInt8(9)), IOBuffer(UInt8[1, 0])) == nothing
@test findall([true, false, true]) == [1, 3]
@test findall(occursin([1, 2]), [1]) == [1]

nothing

0 comments on commit 3058197

Please sign in to comment.