Skip to content

Commit e7d4dd5

Browse files
committed
Introduce occursin() predicate and use it
Also reorganize exports for clarity.
1 parent b2f5e4c commit e7d4dd5

12 files changed

+70
-48
lines changed

base/abstractarray.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,7 @@ _cs(d, a, b) = (a == b ? a : throw(DimensionMismatch(
12171217
"mismatch in dimension $d (expected $a got $b)")))
12181218

12191219
dims2cat(::Val{n}) where {n} = ntuple(i -> (i == n), Val(n))
1220-
dims2cat(dims) = ntuple(i -> (i in dims), maximum(dims))
1220+
dims2cat(dims) = ntuple(occursin(dims), maximum(dims))
12211221

12221222
cat(dims, X...) = cat_t(dims, promote_eltypeof(X...), X...)
12231223

base/deprecated.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -2130,16 +2130,16 @@ finalizer(f::Ptr{Void}, o::Function) = invoke(finalizer, Tuple{Ptr{Void}, Any},
21302130
@deprecate search(a::ByteArray, b::Char, i::Integer) findnext(equalto(UInt8(b)), a, i)
21312131
@deprecate search(a::ByteArray, b::Char) findfirst(equalto(UInt8(b)), a)
21322132

2133-
@deprecate search(s::AbstractString, c::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}, i::Integer) findnext(x -> x in c, s, i)
2134-
@deprecate search(s::AbstractString, c::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}) findfirst(x -> x in c, s)
2133+
@deprecate search(s::AbstractString, c::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}, i::Integer) findnext(occursin(c), s, i)
2134+
@deprecate search(s::AbstractString, c::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}) findfirst(occursin(c), s)
21352135
@deprecate search(s::AbstractString, t::AbstractString, i::Integer) findnext(t, s, i)
21362136
@deprecate search(s::AbstractString, t::AbstractString) findfirst(t, s)
21372137

21382138
@deprecate search(buf::IOBuffer, delim::UInt8) findfirst(equalto(delim), buf)
21392139
@deprecate search(buf::Base.GenericIOBuffer, delim::UInt8) findfirst(equalto(delim), buf)
21402140

2141-
@deprecate rsearch(s::AbstractString, c::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}, i::Integer) findprev(x -> x in c, s, i)
2142-
@deprecate rsearch(s::AbstractString, c::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}) findlast(x -> x in c, s)
2141+
@deprecate rsearch(s::AbstractString, c::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}, i::Integer) findprev(occursin(c), s, i)
2142+
@deprecate rsearch(s::AbstractString, c::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}) findlast(occursin(c), s)
21432143
@deprecate rsearch(s::AbstractString, t::AbstractString, i::Integer) findprev(t, s, i)
21442144
@deprecate rsearch(s::AbstractString, t::AbstractString) findlast(t, s)
21452145
@deprecate rsearch(s::ByteArray, t::ByteArray, i::Integer) findprev(t, s, i)

base/exports.jl

+28-25
Original file line numberDiff line numberDiff line change
@@ -436,18 +436,6 @@ export
436436
extrema,
437437
fill!,
438438
fill,
439-
find,
440-
findfirst,
441-
findlast,
442-
findin,
443-
findmax,
444-
findmin,
445-
findmin!,
446-
findmax!,
447-
findn,
448-
findnext,
449-
findprev,
450-
findnz,
451439
first,
452440
flipdim,
453441
hcat,
@@ -505,9 +493,6 @@ export
505493
rot180,
506494
rotl90,
507495
rotr90,
508-
searchsorted,
509-
searchsortedfirst,
510-
searchsortedlast,
511496
shuffle,
512497
shuffle!,
513498
size,
@@ -532,6 +517,34 @@ export
532517
view,
533518
zeros,
534519

520+
# search, find, match and related functions
521+
contains,
522+
eachmatch,
523+
endswith,
524+
equalto,
525+
find,
526+
findfirst,
527+
findlast,
528+
findin,
529+
findmax,
530+
findmin,
531+
findmin!,
532+
findmax!,
533+
findn,
534+
findnext,
535+
findprev,
536+
findnz,
537+
ismatch,
538+
occursin,
539+
match,
540+
matchall,
541+
rsearchindex,
542+
searchindex,
543+
searchsorted,
544+
searchsortedfirst,
545+
searchsortedlast,
546+
startswith,
547+
535548
# linear algebra
536549
bkfact!,
537550
bkfact,
@@ -644,7 +657,6 @@ export
644657
any!,
645658
any,
646659
collect,
647-
contains,
648660
count,
649661
delete!,
650662
deleteat!,
@@ -711,7 +723,6 @@ export
711723
# strings and text output
712724
ascii,
713725
base,
714-
startswith,
715726
bin,
716727
bitstring,
717728
bytes2hex,
@@ -723,8 +734,6 @@ export
723734
digits,
724735
digits!,
725736
dump,
726-
eachmatch,
727-
endswith,
728737
escape_string,
729738
graphemes,
730739
hex,
@@ -740,7 +749,6 @@ export
740749
isdigit,
741750
isgraph,
742751
islower,
743-
ismatch,
744752
isnumber,
745753
isprint,
746754
ispunct,
@@ -754,8 +762,6 @@ export
754762
lowercase,
755763
lpad,
756764
lstrip,
757-
match,
758-
matchall,
759765
ndigits,
760766
nextind,
761767
normalize_string,
@@ -771,10 +777,8 @@ export
771777
repr,
772778
reverseind,
773779
rpad,
774-
rsearchindex,
775780
rsplit,
776781
rstrip,
777-
searchindex,
778782
show,
779783
showcompact,
780784
showerror,
@@ -844,7 +848,6 @@ export
844848
identity,
845849
isbits,
846850
isequal,
847-
equalto,
848851
isimmutable,
849852
isless,
850853
ifelse,

base/operators.jl

+19
Original file line numberDiff line numberDiff line change
@@ -993,3 +993,22 @@ The returned function is of type `Base.EqualTo`. This allows dispatching to
993993
specialized methods by using e.g. `f::Base.EqualTo` in a method signature.
994994
"""
995995
const equalto = EqualTo
996+
997+
struct OccursIn{T} <: Function
998+
x::T
999+
1000+
OccursIn(x::T) where {T} = new{T}(x)
1001+
end
1002+
1003+
(f::OccursIn)(y) = y in f.x
1004+
1005+
"""
1006+
occursin(x)
1007+
1008+
Create a function that checks whether its argument is [`in`](@ref) `x`; i.e. returns
1009+
`y -> y in x`.
1010+
1011+
The returned function is of type `Base.OccursIn`. This allows dispatching to
1012+
specialized methods by using e.g. `f::Base.OccursIn` in a method signature.
1013+
"""
1014+
const occursin = OccursIn

base/repl/REPLCompletions.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function complete_symbol(sym, ffunc)
4040

4141
lookup_module = true
4242
t = Union{}
43-
if findlast(x -> x in non_identifier_chars, sym) < findlast(equalto('.'), sym)
43+
if findlast(occursin(non_identifier_chars), sym) < findlast(equalto('.'), sym)
4444
# Find module
4545
lookup_name, name = rsplit(sym, ".", limit=2)
4646

@@ -257,7 +257,7 @@ function find_start_brace(s::AbstractString; c_start='(', c_end=')')
257257
end
258258
braces != 1 && return 0:-1, -1
259259
method_name_end = reverseind(r, i)
260-
startind = nextind(s, findprev(x -> x in non_identifier_chars, s, method_name_end))
260+
startind = nextind(s, findprev(occursin(non_identifier_chars), s, method_name_end))
261261
return (startind:endof(s), method_name_end)
262262
end
263263

@@ -413,7 +413,7 @@ end
413413

414414
function bslash_completions(string, pos)
415415
slashpos = findprev(equalto('\\'), string, pos)
416-
if (findprev(x -> x in bslash_separators, string, pos) < slashpos &&
416+
if (findprev(occursin(bslash_separators), string, pos) < slashpos &&
417417
!(1 < slashpos && (string[prevind(string, slashpos)]=='\\')))
418418
# latex / emoji symbol substitution
419419
s = string[slashpos:pos]
@@ -537,7 +537,7 @@ function completions(string, pos)
537537
end
538538

539539
dotpos = findprev(equalto('.'), string, pos)
540-
startpos = nextind(string, findprev(x -> x in non_identifier_chars, string, pos))
540+
startpos = nextind(string, findprev(occursin(non_identifier_chars), string, pos))
541541

542542
ffunc = (mod,x)->true
543543
suggestions = String[]

base/strings/util.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ split(str::T, splitter; limit::Integer=0, keep::Bool=true) where {T<:SubString}
279279
_split(str, splitter, limit, keep, T[])
280280
split(str::T, splitter::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}};
281281
limit::Integer=0, keep::Bool=true) where {T<:SubString} =
282-
_split(str, x -> x in splitter, limit, keep, T[])
282+
_split(str, occursin(splitter), limit, keep, T[])
283283
split(str::T, splitter::Char; limit::Integer=0, keep::Bool=true) where {T<:SubString} =
284284
_split(str, equalto(splitter), limit, keep, T[])
285285

@@ -314,7 +314,7 @@ split(str::T, splitter;
314314
_split(str, splitter, limit, keep, SubString{T}[])
315315
split(str::T, splitter::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}};
316316
limit::Integer=0, keep::Bool=true) where {T<:AbstractString} =
317-
_split(str, x -> x in splitter, limit, keep, SubString{T}[])
317+
_split(str, occursin(splitter), limit, keep, SubString{T}[])
318318
split(str::T, splitter::Char;
319319
limit::Integer=0, keep::Bool=true) where {T<:AbstractString} =
320320
_split(str, equalto(splitter), limit, keep, SubString{T}[])
@@ -378,7 +378,7 @@ rsplit(str::T, splitter; limit::Integer=0, keep::Bool=true) where {T<:AbstractSt
378378
_rsplit(str, splitter, limit, keep, SubString{T}[])
379379
rsplit(str::T, splitter::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}};
380380
limit::Integer=0, keep::Bool=true) where {T<:AbstractString} =
381-
_rsplit(str, x -> x in splitter, limit, keep, SubString{T}[])
381+
_rsplit(str, occursin(splitter), limit, keep, SubString{T}[])
382382
rsplit(str::T, splitter::Char;
383383
limit::Integer=0, keep::Bool=true) where {T<:AbstractString} =
384384
_rsplit(str, equalto(splitter), limit, keep, SubString{T}[])
@@ -413,7 +413,7 @@ replace_new(str::String, pattern::Char, repl, count::Integer) =
413413
replace_new(str, equalto(pattern), repl, count)
414414
replace_new(str::String, pattern::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}},
415415
repl, count::Integer) =
416-
replace_new(str, x -> x in pattern, repl, count)
416+
replace_new(str, occursin(pattern), repl, count)
417417
function replace_new(str::String, pattern, repl, count::Integer)
418418
count == 0 && return str
419419
count < 0 && throw(DomainError(count, "`count` must be non-negative."))

test/arrayops.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ end
11841184

11851185
# logical indexing
11861186
a = [1:10;]; acopy = copy(a)
1187-
@test deleteat!(a, map(i -> i in idx, 1:length(a))) == [acopy[1:(first(idx)-1)]; acopy[(last(idx)+1):end]]
1187+
@test deleteat!(a, map(occursin(idx), 1:length(a))) == [acopy[1:(first(idx)-1)]; acopy[(last(idx)+1):end]]
11881188
end
11891189
a = [1:10;]
11901190
@test deleteat!(a, 11:10) == [1:10;]

test/bitset.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
data_in = (1,5,100)
77
s = BitSet(data_in)
88
data_out = collect(s)
9-
@test all(map(d->in(d,data_out), data_in))
9+
@test all(map(occursin(data_out), data_in))
1010
@test length(data_out) === length(data_in)
1111
end
1212

test/choosetests.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function choosetests(choices = [])
165165
end
166166

167167
if !net_on
168-
filter!(x -> !(x in net_required_for), tests)
168+
filter!(!occursin(net_required_for), tests)
169169
end
170170

171171
if "stdlib" in skip_tests
@@ -182,7 +182,7 @@ function choosetests(choices = [])
182182
filter!(x -> (x != "Profile"), tests)
183183
end
184184

185-
filter!(x -> !(x in skip_tests), tests)
185+
filter!(!occursin(skip_tests), tests)
186186

187187
tests, net_on, exit_on_error, seed
188188
end

test/distributed_exec.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ if DoFullTest
10901090
pids=addprocs_with_testenv(4);
10911091
@test_throws ErrorException rmprocs(pids; waitfor=0.001);
10921092
# wait for workers to be removed
1093-
while any(x -> (x in procs()), pids)
1093+
while any(occursin(procs()), pids)
10941094
sleep(0.1)
10951095
end
10961096
end
@@ -1555,7 +1555,7 @@ function reuseport_tests()
15551555
end
15561556

15571557
# Ensure that the code has indeed been successfully executed everywhere
1558-
@test all(p -> p in results, procs())
1558+
@test all(occursin(results), procs())
15591559
end
15601560

15611561
# Test that the client port is reused. SO_REUSEPORT may not be supported on

test/sets.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using Main.TestHelpers.OAs
1111
s = Set(data_in)
1212
data_out = collect(s)
1313
@test ===(typeof(data_out), Array{Any,1})
14-
@test all(map(d->in(d,data_out), data_in))
14+
@test all(map(occursin(data_out), data_in))
1515
@test length(data_out) == length(data_in)
1616
let f17741 = x -> x < 0 ? false : 1
1717
@test isa(Set(x for x = 1:3), Set{Int})

test/strings/search.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ end
1717
# sure that findnext/findprev/searchindex/rsearchindex are consistent
1818
# no matter what type of AbstractString the second argument is
1919
@test_throws BoundsError findnext(equalto('a'), "foo", 0)
20-
@test_throws BoundsError findnext(x -> x in Char[], "foo", 5)
21-
# @test_throws BoundsError findprev(x -> x in Char[], "foo", 0)
22-
@test_throws BoundsError findprev(x -> x in Char[], "foo", 5)
20+
@test_throws BoundsError findnext(occursin(Char[]), "foo", 5)
21+
# @test_throws BoundsError findprev(occursin(Char[]), "foo", 0)
22+
@test_throws BoundsError findprev(occursin(Char[]), "foo", 5)
2323

2424
# @test_throws BoundsError searchindex("foo", Char[], 0)
2525
# @test_throws BoundsError searchindex("foo", Char[], 5)

0 commit comments

Comments
 (0)