Skip to content

Commit 1950086

Browse files
authored
Merge pull request #24673 from JuliaLang/nl/search
Clean up search and find API
2 parents 236d190 + ff8b17d commit 1950086

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+808
-803
lines changed

NEWS.md

+8
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,14 @@ Deprecated or removed
900900
in favor of dot overloading (`getproperty`) so factors should now be accessed as e.g.
901901
`F.Q` instead of `F[:Q]` ([#25184]).
902902

903+
* `search` and `rsearch` have been deprecated in favor of `findfirst`/`findnext` and
904+
`findlast`/`findprev` respectively, in combination with the new `equalto` and `occursin`
905+
predicates for some methods ([#24673]
906+
907+
* `ismatch(regex, str)` has been deprecated in favor of `contains(str, regex)` ([#24673]).
908+
909+
* `findin(a, b)` has been deprecated in favor of `find(occursin(b), a)` ([#24673]).
910+
903911
Command-line option changes
904912
---------------------------
905913

base/abstractarray.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ get(A::AbstractArray, I::Dims, default) = checkbounds(Bool, A, I...) ? A[I...] :
10551055

10561056
function get!(X::AbstractVector{T}, A::AbstractVector, I::Union{AbstractRange,AbstractVector{Int}}, default::T) where T
10571057
# 1d is not linear indexing
1058-
ind = findin(I, indices1(A))
1058+
ind = find(occursin(indices1(A)), I)
10591059
X[ind] = A[I[ind]]
10601060
Xind = indices1(X)
10611061
X[first(Xind):first(ind)-1] = default
@@ -1064,7 +1064,7 @@ function get!(X::AbstractVector{T}, A::AbstractVector, I::Union{AbstractRange,Ab
10641064
end
10651065
function get!(X::AbstractArray{T}, A::AbstractArray, I::Union{AbstractRange,AbstractVector{Int}}, default::T) where T
10661066
# Linear indexing
1067-
ind = findin(I, 1:length(A))
1067+
ind = find(occursin(1:length(A)), I)
10681068
X[ind] = A[I[ind]]
10691069
X[1:first(ind)-1] = default
10701070
X[last(ind)+1:length(X)] = default
@@ -1237,7 +1237,7 @@ _cs(d, a, b) = (a == b ? a : throw(DimensionMismatch(
12371237
"mismatch in dimension $d (expected $a got $b)")))
12381238

12391239
dims2cat(::Val{n}) where {n} = ntuple(i -> (i == n), Val(n))
1240-
dims2cat(dims) = ntuple(i -> (i in dims), maximum(dims))
1240+
dims2cat(dims) = ntuple(occursin(dims), maximum(dims))
12411241

12421242
cat(dims, X...) = cat_t(dims, promote_eltypeof(X...), X...)
12431243

base/array.jl

+11-36
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,7 @@ end
17991799

18001800
find(x::Bool) = x ? [1] : Vector{Int}()
18011801
find(testf::Function, x::Number) = !testf(x) ? Vector{Int}() : [1]
1802+
find(p::OccursIn, x::Number) = x in p.x ? Vector{Int}() : [1]
18021803

18031804
"""
18041805
findnz(A)
@@ -2008,7 +2009,7 @@ function _findin(a, b)
20082009
ind
20092010
end
20102011

2011-
# If two collections are already sorted, findin can be computed with
2012+
# If two collections are already sorted, _findin can be computed with
20122013
# a single traversal of the two collections. This is much faster than
20132014
# using a hash table (although it has the same complexity).
20142015
function _sortedfindin(v, w)
@@ -2050,42 +2051,16 @@ function _sortedfindin(v, w)
20502051
return out
20512052
end
20522053

2053-
"""
2054-
findin(a, b)
2055-
2056-
Return the indices of elements in collection `a` that appear in collection `b`.
2057-
2058-
# Examples
2059-
```jldoctest
2060-
julia> a = collect(1:3:15)
2061-
5-element Array{Int64,1}:
2062-
1
2063-
4
2064-
7
2065-
10
2066-
13
2067-
2068-
julia> b = collect(2:4:10)
2069-
3-element Array{Int64,1}:
2070-
2
2071-
6
2072-
10
2073-
2074-
julia> findin(a,b) # 10 is the only common element
2075-
1-element Array{Int64,1}:
2076-
4
2077-
```
2078-
"""
2079-
function findin(a::Array{<:Real}, b::Union{Array{<:Real},Real})
2080-
if issorted(a, Sort.Forward) && issorted(b, Sort.Forward)
2081-
return _sortedfindin(a, b)
2054+
function find(pred::OccursIn{<:Union{Array{<:Real},Real}}, x::Array{<:Real})
2055+
if issorted(x, Sort.Forward) && issorted(pred.x, Sort.Forward)
2056+
return _sortedfindin(x, pred.x)
20822057
else
2083-
return _findin(a, b)
2058+
return _findin(x, pred.x)
20842059
end
20852060
end
20862061
# issorted fails for some element types so the method above has to be restricted
20872062
# to element with isless/< defined.
2088-
findin(a, b) = _findin(a, b)
2063+
find(pred::OccursIn, x::Union{AbstractArray, Tuple}) = _findin(x, pred.x)
20892064

20902065
# Copying subregions
20912066
function indcopy(sz::Dims, I::Vector)
@@ -2094,8 +2069,8 @@ function indcopy(sz::Dims, I::Vector)
20942069
for i = n+1:length(sz)
20952070
s *= sz[i]
20962071
end
2097-
dst = eltype(I)[findin(I[i], i < n ? (1:sz[i]) : (1:s)) for i = 1:n]
2098-
src = eltype(I)[I[i][findin(I[i], i < n ? (1:sz[i]) : (1:s))] for i = 1:n]
2072+
dst = eltype(I)[_findin(I[i], i < n ? (1:sz[i]) : (1:s)) for i = 1:n]
2073+
src = eltype(I)[I[i][_findin(I[i], i < n ? (1:sz[i]) : (1:s))] for i = 1:n]
20992074
dst, src
21002075
end
21012076

@@ -2105,8 +2080,8 @@ function indcopy(sz::Dims, I::Tuple{Vararg{RangeIndex}})
21052080
for i = n+1:length(sz)
21062081
s *= sz[i]
21072082
end
2108-
dst::typeof(I) = ntuple(i-> findin(I[i], i < n ? (1:sz[i]) : (1:s)), n)::typeof(I)
2109-
src::typeof(I) = ntuple(i-> I[i][findin(I[i], i < n ? (1:sz[i]) : (1:s))], n)::typeof(I)
2083+
dst::typeof(I) = ntuple(i-> _findin(I[i], i < n ? (1:sz[i]) : (1:s)), n)::typeof(I)
2084+
src::typeof(I) = ntuple(i-> I[i][_findin(I[i], i < n ? (1:sz[i]) : (1:s))], n)::typeof(I)
21102085
dst, src
21112086
end
21122087

base/deprecated.jl

+51-2
Original file line numberDiff line numberDiff line change
@@ -3813,16 +3813,65 @@ end
38133813
@deprecate getq(F::Factorization) F.Q
38143814
end
38153815

3816-
# issue #5290
38173816
@deprecate lexcmp(x::AbstractArray, y::AbstractArray) cmp(x, y)
38183817
@deprecate lexcmp(x::Real, y::Real) cmp(isless, x, y)
38193818
@deprecate lexcmp(x::Complex, y::Complex) cmp((real(x),imag(x)), (real(y),imag(y)))
38203819
@deprecate lexcmp(x, y) cmp(x, y)
38213820

38223821
@deprecate lexless isless
38233822

3824-
# END 0.7 deprecations
3823+
@deprecate search(str::Union{String,SubString}, re::Regex, idx::Integer) findnext(re, str, idx)
3824+
@deprecate search(s::AbstractString, r::Regex, idx::Integer) findnext(r, s, idx)
3825+
@deprecate search(s::AbstractString, r::Regex) findfirst(r, s)
3826+
@deprecate search(s::AbstractString, c::Char, i::Integer) findnext(equalto(c), s, i)
3827+
@deprecate search(s::AbstractString, c::Char) findfirst(equalto(c), s)
3828+
@deprecate search(a::ByteArray, b::Union{Int8,UInt8}, i::Integer) findnext(equalto(b), a, i)
3829+
@deprecate search(a::ByteArray, b::Union{Int8,UInt8}) findfirst(equalto(b), a)
3830+
@deprecate search(a::String, b::Union{Int8,UInt8}, i::Integer) findnext(equalto(b), unsafe_wrap(Vector{UInt8}, a), i)
3831+
@deprecate search(a::String, b::Union{Int8,UInt8}) findfirst(equalto(b), unsafe_wrap(Vector{UInt8}, a))
3832+
@deprecate search(a::ByteArray, b::Char, i::Integer) findnext(equalto(UInt8(b)), a, i)
3833+
@deprecate search(a::ByteArray, b::Char) findfirst(equalto(UInt8(b)), a)
3834+
3835+
@deprecate search(s::AbstractString, c::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}, i::Integer) findnext(occursin(c), s, i)
3836+
@deprecate search(s::AbstractString, c::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}) findfirst(occursin(c), s)
3837+
@deprecate search(s::AbstractString, t::AbstractString, i::Integer) findnext(t, s, i)
3838+
@deprecate search(s::AbstractString, t::AbstractString) findfirst(t, s)
3839+
3840+
@deprecate search(buf::IOBuffer, delim::UInt8) findfirst(equalto(delim), buf)
3841+
@deprecate search(buf::Base.GenericIOBuffer, delim::UInt8) findfirst(equalto(delim), buf)
3842+
3843+
@deprecate rsearch(s::AbstractString, c::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}, i::Integer) findprev(occursin(c), s, i)
3844+
@deprecate rsearch(s::AbstractString, c::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}) findlast(occursin(c), s)
3845+
@deprecate rsearch(s::AbstractString, t::AbstractString, i::Integer) findprev(t, s, i)
3846+
@deprecate rsearch(s::AbstractString, t::AbstractString) findlast(t, s)
3847+
@deprecate rsearch(s::ByteArray, t::ByteArray, i::Integer) findprev(t, s, i)
3848+
@deprecate rsearch(s::ByteArray, t::ByteArray) findlast(t, s)
3849+
3850+
@deprecate rsearch(str::Union{String,SubString}, re::Regex, idx::Integer) findprev(re, str, idx)
3851+
@deprecate rsearch(str::Union{String,SubString}, re::Regex) findlast(re, str)
3852+
@deprecate rsearch(s::AbstractString, r::Regex, idx::Integer) findprev(r, s, idx)
3853+
@deprecate rsearch(s::AbstractString, r::Regex) findlast(r, s)
3854+
@deprecate rsearch(s::AbstractString, c::Char, i::Integer) findprev(equalto(c), s, i)
3855+
@deprecate rsearch(s::AbstractString, c::Char) findlast(equalto(c), s)
3856+
@deprecate rsearch(a::Union{String,ByteArray}, b::Union{Int8,UInt8}, i::Integer = endof(a)) findprev(equalto(b), a, i)
3857+
@deprecate rsearch(a::String, b::Union{Int8,UInt8}, i::Integer = endof(a)) findprev(equalto(Char(b)), a, i)
3858+
@deprecate rsearch(a::ByteArray, b::Char, i::Integer = endof(a)) findprev(equalto(UInt8(b)), a, i)
3859+
3860+
@deprecate searchindex(s::AbstractString, t::AbstractString) first(findfirst(t, s))
3861+
@deprecate searchindex(s::AbstractString, t::AbstractString, i::Integer) first(findnext(t, s, i))
3862+
@deprecate rsearchindex(s::AbstractString, t::AbstractString) first(findlast(t, s))
3863+
@deprecate rsearchindex(s::AbstractString, t::AbstractString, i::Integer) first(findprev(t, s, i))
3864+
3865+
@deprecate searchindex(s::AbstractString, c::Char) first(findfirst(equalto(c), s))
3866+
@deprecate searchindex(s::AbstractString, c::Char, i::Integer) first(findnext(equalto(c), s, i))
3867+
@deprecate rsearchindex(s::AbstractString, c::Char) first(findlast(equalto(c), s))
3868+
@deprecate rsearchindex(s::AbstractString, c::Char, i::Integer) first(findprev(equalto(c), s, i))
3869+
3870+
@deprecate ismatch(r::Regex, s::AbstractString) contains(s, r)
3871+
3872+
@deprecate findin(a, b) find(occursin(b), a)
38253873

3874+
# END 0.7 deprecations
38263875
# BEGIN 1.0 deprecations
38273876

38283877
# END 1.0 deprecations

base/docs/utils.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ const builtins = ["abstract type", "baremodule", "begin", "break",
349349

350350
moduleusings(mod) = ccall(:jl_module_usings, Any, (Any,), mod)
351351

352-
filtervalid(names) = filter(x->!ismatch(r"#", x), map(string, names))
352+
filtervalid(names) = filter(x->!contains(x, r"#"), map(string, names))
353353

354354
accessible(mod::Module) =
355355
[filter!(s -> !Base.isdeprecated(mod, s), names(mod, true, true));
@@ -363,7 +363,7 @@ completions(name::Symbol) = completions(string(name))
363363
# Searching and apropos
364364

365365
# Docsearch simply returns true or false if an object contains the given needle
366-
docsearch(haystack::AbstractString, needle) = !isempty(search(haystack, needle))
366+
docsearch(haystack::AbstractString, needle) = !isempty(findfirst(needle, haystack))
367367
docsearch(haystack::Symbol, needle) = docsearch(string(haystack), needle)
368368
docsearch(::Nothing, needle) = false
369369
function docsearch(haystack::Array, needle)

base/exports.jl

+24-27
Original file line numberDiff line numberDiff line change
@@ -409,18 +409,6 @@ export
409409
extrema,
410410
fill!,
411411
fill,
412-
find,
413-
findfirst,
414-
findlast,
415-
findin,
416-
findmax,
417-
findmin,
418-
findmin!,
419-
findmax!,
420-
findn,
421-
findnext,
422-
findprev,
423-
findnz,
424412
first,
425413
flipdim,
426414
hcat,
@@ -476,9 +464,6 @@ export
476464
rot180,
477465
rotl90,
478466
rotr90,
479-
searchsorted,
480-
searchsortedfirst,
481-
searchsortedlast,
482467
shuffle,
483468
shuffle!,
484469
size,
@@ -501,6 +486,30 @@ export
501486
view,
502487
zeros,
503488

489+
# search, find, match and related functions
490+
contains,
491+
eachmatch,
492+
endswith,
493+
equalto,
494+
find,
495+
findfirst,
496+
findlast,
497+
findmax,
498+
findmin,
499+
findmin!,
500+
findmax!,
501+
findn,
502+
findnext,
503+
findprev,
504+
findnz,
505+
occursin,
506+
match,
507+
matchall,
508+
searchsorted,
509+
searchsortedfirst,
510+
searchsortedlast,
511+
startswith,
512+
504513
# linear algebra
505514
bkfact!,
506515
bkfact,
@@ -611,7 +620,6 @@ export
611620
any!,
612621
any,
613622
collect,
614-
contains,
615623
count,
616624
delete!,
617625
deleteat!,
@@ -679,7 +687,6 @@ export
679687
# strings and text output
680688
ascii,
681689
base,
682-
startswith,
683690
bin,
684691
bitstring,
685692
bytes2hex,
@@ -691,22 +698,17 @@ export
691698
digits,
692699
digits!,
693700
dump,
694-
eachmatch,
695-
endswith,
696701
escape_string,
697702
hex,
698703
hex2bytes,
699704
hex2bytes!,
700705
info,
701706
isascii,
702-
ismatch,
703707
isvalid,
704708
join,
705709
logging,
706710
lpad,
707711
lstrip,
708-
match,
709-
matchall,
710712
ncodeunits,
711713
ndigits,
712714
nextind,
@@ -723,12 +725,8 @@ export
723725
repr,
724726
reverseind,
725727
rpad,
726-
rsearch,
727-
rsearchindex,
728728
rsplit,
729729
rstrip,
730-
search,
731-
searchindex,
732730
show,
733731
showcompact,
734732
showerror,
@@ -800,7 +798,6 @@ export
800798
identity,
801799
isbits,
802800
isequal,
803-
equalto,
804801
isimmutable,
805802
isless,
806803
ifelse,

base/interactiveutil.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,13 @@ function versioninfo(io::IO=STDOUT; verbose::Bool=false, packages::Bool=false)
334334

335335
println(io, "Environment:")
336336
for (k,v) in ENV
337-
if ismatch(r"JULIA", String(k))
337+
if contains(String(k), r"JULIA")
338338
println(io, " $(k) = $(v)")
339339
end
340340
end
341341
if verbose
342342
for (k,v) in ENV
343-
if ismatch(r"PATH|FLAG|^TERM$|HOME", String(k))
343+
if contains(String(k), r"PATH|FLAG|^TERM$|HOME")
344344
println(io, " $(k) = $(v)")
345345
end
346346
end
@@ -743,7 +743,7 @@ function varinfo(m::Module=Main, pattern::Regex=r"")
743743
(value (Base, Main, Core) ? "" : format_bytes(summarysize(value))),
744744
summary(value)]
745745
end
746-
for v in sort!(names(m)) if isdefined(m, v) && ismatch(pattern, string(v)) ]
746+
for v in sort!(names(m)) if isdefined(m, v) && contains(string(v), pattern) ]
747747

748748
pushfirst!(rows, Any["name", "size", "summary"])
749749

base/iobuffer.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -426,18 +426,18 @@ read(io::GenericIOBuffer) = read!(io,StringVector(nb_available(io)))
426426
readavailable(io::GenericIOBuffer) = read(io)
427427
read(io::GenericIOBuffer, nb::Integer) = read!(io,StringVector(min(nb, nb_available(io))))
428428

429-
function search(buf::IOBuffer, delim::UInt8)
429+
function findfirst(delim::EqualTo{UInt8}, buf::IOBuffer)
430430
p = pointer(buf.data, buf.ptr)
431-
q = @gc_preserve buf ccall(:memchr,Ptr{UInt8},(Ptr{UInt8},Int32,Csize_t),p,delim,nb_available(buf))
431+
q = @gc_preserve buf ccall(:memchr,Ptr{UInt8},(Ptr{UInt8},Int32,Csize_t),p,delim.x,nb_available(buf))
432432
nb::Int = (q == C_NULL ? 0 : q-p+1)
433433
return nb
434434
end
435435

436-
function search(buf::GenericIOBuffer, delim::UInt8)
436+
function findfirst(delim::EqualTo{UInt8}, buf::GenericIOBuffer)
437437
data = buf.data
438438
for i = buf.ptr : buf.size
439439
@inbounds b = data[i]
440-
if b == delim
440+
if b == delim.x
441441
return i - buf.ptr + 1
442442
end
443443
end

base/libc.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ function strptime(fmt::AbstractString, timestr::AbstractString)
204204
@static if Sys.isapple()
205205
# if we didn't explicitly parse the weekday or year day, use mktime
206206
# to fill them in automatically.
207-
if !ismatch(r"([^%]|^)%(a|A|j|w|Ow)", fmt)
207+
if !contains(fmt, r"([^%]|^)%(a|A|j|w|Ow)")
208208
ccall(:mktime, Int, (Ref{TmStruct},), tm)
209209
end
210210
end

0 commit comments

Comments
 (0)