Skip to content

Commit c9c99f3

Browse files
committed
Deprecate contains to a more general isfound function
While `contains` was generally limited to strings and regular expressions (after the move from `ismatch` to `contains`), `isfound` works more generally, as its fallback method is defined in terms of `findfirst`.
1 parent ce26ed0 commit c9c99f3

Some content is hidden

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

86 files changed

+514
-519
lines changed

DISTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ for result in eachrow(results)
364364
b = result[:backport]
365365
if (isna(a) && !isna(b)) || (isna(b) && !isna(a))
366366
color = :yellow
367-
elseif a != b && contains(b, "pass")
367+
elseif a != b && isfound("pass", b)
368368
color = :green
369369
elseif a != b
370370
color = :red

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,8 @@ Deprecated or removed
10671067
* The `remove_destination` keyword argument to `cp`, `mv`, and the unexported `cptree`
10681068
has been renamed to `force` ([#25979]).
10691069

1070+
* `contains` has been deprecated in favor of a more general `isfound` function ([#26283]).
1071+
10701072
* The methods of `range` based on positional arguments have been deprecated in favor of
10711073
keyword arguments ([#25896]).
10721074

base/array.jl

+22
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,28 @@ end
16961696
findfirst(testf::Function, A::Union{AbstractArray, AbstractString}) =
16971697
findnext(testf, A, first(keys(A)))
16981698

1699+
"""
1700+
isfound(needle, haystack)
1701+
1702+
Determine whether `needle` is found within `haystack`. This function is effectively
1703+
equivalent to `findfirst(needle, haystack) !== nothing`.
1704+
1705+
See also [`findfirst`](@ref) and [`any`](@ref).
1706+
1707+
# Examples
1708+
```jldoctest
1709+
julia> isfound('x', "hexonxonx")
1710+
true
1711+
1712+
julia> isfound("hi", "hi mom")
1713+
true
1714+
1715+
julia> isfound(equalto(3), 5:10)
1716+
false
1717+
```
1718+
"""
1719+
isfound(needle, haystack) = findfirst(needle, haystack) !== nothing
1720+
16991721
"""
17001722
findprev(A, i)
17011723

base/client.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,11 @@ incomplete_tag(ex) = :none
244244
function incomplete_tag(ex::Expr)
245245
Meta.isexpr(ex, :incomplete) || return :none
246246
msg = ex.args[1]
247-
contains(msg, "string") && return :string
248-
contains(msg, "comment") && return :comment
249-
contains(msg, "requires end") && return :block
250-
contains(msg, "\"`\"") && return :cmd
251-
contains(msg, "character") && return :char
247+
isfound("string", msg) && return :string
248+
isfound("comment", msg) && return :comment
249+
isfound("requires end", msg) && return :block
250+
isfound("\"`\"", msg) && return :cmd
251+
isfound("character", msg) && return :char
252252
return :other
253253
end
254254

base/deprecated.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ end
12291229
@deprecate rsearchindex(s::AbstractString, c::Char) findlast(equalto(c), s)
12301230
@deprecate rsearchindex(s::AbstractString, c::Char, i::Integer) findprev(equalto(c), s, i)
12311231

1232-
@deprecate ismatch(r::Regex, s::AbstractString) contains(s, r)
1232+
@deprecate ismatch(r::Regex, s::AbstractString) isfound(r, s)
12331233

12341234
@deprecate findin(a, b) findall(occursin(b), a)
12351235

@@ -1454,6 +1454,10 @@ function slicedim(A::AbstractVector, d::Integer, i::Number)
14541454
end
14551455
end
14561456

1457+
# PR #26283
1458+
@deprecate contains(haystack, needle) isfound(needle, haystack)
1459+
@deprecate contains(s::AbstractString, r::Regex, offset::Integer) isfound(r, s, offset=offset)
1460+
14571461
# Issue #25786
14581462
@deprecate_binding DevNull devnull
14591463
# TODO: When these are removed, also remove the uppercase variants in libuv.jl and stream.jl

base/libc.jl

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

base/loading.jl

+9-9
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ function project_file_name_uuid_path(project_file::String,
388388
uuid = dummy_uuid(project_file)
389389
path = joinpath("src", "$name.jl")
390390
for line in eachline(io)
391-
contains(line, re_section) && break
391+
isfound(re_section, line) && break
392392
if (m = match(re_name_to_string, line)) != nothing
393393
name = String(m.captures[1])
394394
elseif (m = match(re_uuid_to_string, line)) != nothing
@@ -407,7 +407,7 @@ function project_file_manifest_path(project_file::String)::Union{Nothing,String}
407407
open(project_file) do io
408408
dir = abspath(dirname(project_file))
409409
for line in eachline(io)
410-
contains(line, re_section) && break
410+
isfound(re_section, line) && break
411411
if (m = match(re_manifest_to_string, line)) != nothing
412412
return normpath(joinpath(dir, m.captures[1]))
413413
end
@@ -494,9 +494,9 @@ function explicit_project_deps_get(project_file::String, name::String)::Union{Bo
494494
state = :top
495495
for line in eachline(io)
496496
if state == :top
497-
if contains(line, re_section)
497+
if isfound(re_section, line)
498498
root_name == name && return root_uuid
499-
state = contains(line, re_section_deps) ? :deps : :other
499+
state = isfound(re_section_deps, line) ? :deps : :other
500500
elseif (m = match(re_name_to_string, line)) != nothing
501501
root_name = String(m.captures[1])
502502
elseif (m = match(re_uuid_to_string, line)) != nothing
@@ -506,7 +506,7 @@ function explicit_project_deps_get(project_file::String, name::String)::Union{Bo
506506
if (m = match(re_key_to_string, line)) != nothing
507507
m.captures[1] == name && return UUID(m.captures[2])
508508
end
509-
elseif contains(line, re_section)
509+
elseif isfound(re_section, line)
510510
state = :deps
511511
end
512512
end
@@ -524,7 +524,7 @@ function explicit_manifest_deps_get(manifest_file::String, where::UUID, name::St
524524
uuid = deps = nothing
525525
state = :other
526526
for line in eachline(io)
527-
if contains(line, re_array_of_tables)
527+
if isfound(re_array_of_tables, line)
528528
uuid == where && break
529529
uuid = deps = nothing
530530
state = :stanza
@@ -533,9 +533,9 @@ function explicit_manifest_deps_get(manifest_file::String, where::UUID, name::St
533533
uuid = UUID(m.captures[1])
534534
elseif (m = match(re_deps_to_any, line)) != nothing
535535
deps = String(m.captures[1])
536-
elseif contains(line, re_subsection_deps)
536+
elseif isfound(re_subsection_deps, line)
537537
state = :deps
538-
elseif contains(line, re_section)
538+
elseif isfound(re_section, line)
539539
state = :other
540540
end
541541
elseif state == :deps && uuid == where
@@ -551,7 +551,7 @@ function explicit_manifest_deps_get(manifest_file::String, where::UUID, name::St
551551
@warn "Unexpected TOML deps format:\n$deps"
552552
return nothing
553553
end
554-
contains(deps, repr(name)) || return true
554+
isfound(repr(name), deps) || return true
555555
seekstart(io) # rewind IO handle
556556
manifest_file_name_uuid(manifest_file, name, io)
557557
end

base/methodshow.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ function url(m::Method)
202202
(m.file == :null || m.file == :string) && return ""
203203
file = string(m.file)
204204
line = m.line
205-
line <= 0 || contains(file, r"In\[[0-9]+\]") && return ""
205+
line <= 0 || isfound(r"In\[[0-9]+\]", file) && return ""
206206
Sys.iswindows() && (file = replace(file, '\\' => '/'))
207207
libgit2_id = PkgId(UUID((0x76f85450_5226_5b5a,0x8eaa_529ad045b433)), "LibGit2")
208208
if inbase(M)

base/mpfr.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ end
936936

937937
function _prettify_bigfloat(s::String)::String
938938
mantissa, exponent = split(s, 'e')
939-
if !contains(mantissa, '.')
939+
if !isfound('.', mantissa)
940940
mantissa = string(mantissa, '.')
941941
end
942942
mantissa = rstrip(mantissa, '0')

base/path.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ end
7878

7979

8080
if Sys.iswindows()
81-
isabspath(path::String) = contains(path, path_absolute_re)
81+
isabspath(path::String) = isfound(path_absolute_re, path)
8282
else
8383
isabspath(path::String) = startswith(path, '/')
8484
end
@@ -113,7 +113,7 @@ julia> isdirpath("/home/")
113113
true
114114
```
115115
"""
116-
isdirpath(path::String) = contains(splitdrive(path)[2], path_directory_re)
116+
isdirpath(path::String) = isfound(path_directory_re, splitdrive(path)[2])
117117

118118
"""
119119
splitdir(path::AbstractString) -> (AbstractString, AbstractString)
@@ -218,9 +218,9 @@ function joinpath(a::String, b::String)
218218
B, b = splitdrive(b)
219219
!isempty(B) && A != B && return string(B,b)
220220
C = isempty(B) ? A : B
221-
isempty(a) ? string(C,b) :
222-
contains(a[end:end], path_separator_re) ? string(C,a,b) :
223-
string(C,a,pathsep(a,b),b)
221+
isempty(a) ? string(C,b) :
222+
isfound(path_separator_re, a[end:end]) ? string(C,a,b) :
223+
string(C,a,pathsep(a,b),b)
224224
end
225225
joinpath(a::AbstractString, b::AbstractString) = joinpath(String(a), String(b))
226226

base/regex.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,19 @@ function getindex(m::RegexMatch, name::Symbol)
141141
end
142142
getindex(m::RegexMatch, name::AbstractString) = m[Symbol(name)]
143143

144-
function contains(s::AbstractString, r::Regex, offset::Integer=0)
144+
function isfound(r::Regex, s::AbstractString; offset::Integer=0)
145145
compile(r)
146146
return PCRE.exec(r.regex, String(s), offset, r.match_options,
147147
r.match_data)
148148
end
149149

150-
function contains(s::SubString, r::Regex, offset::Integer=0)
150+
function isfound(r::Regex, s::SubString; offset::Integer=0)
151151
compile(r)
152152
return PCRE.exec(r.regex, s, offset, r.match_options,
153153
r.match_data)
154154
end
155155

156-
(r::Regex)(s) = contains(s, r)
156+
(r::Regex)(s) = isfound(r, s)
157157

158158
"""
159159
match(r::Regex, s::AbstractString[, idx::Integer[, addopts]])

base/strings/search.jl

+2-25
Original file line numberDiff line numberDiff line change
@@ -425,30 +425,7 @@ julia> findprev("Julia", "JuliaLang", 6)
425425
"""
426426
findprev(t::AbstractString, s::AbstractString, i::Integer) = _rsearch(s, t, i)
427427

428-
"""
429-
contains(haystack::AbstractString, needle::Union{AbstractString,Regex,Char})
430-
431-
Determine whether the second argument is a substring of the first. If `needle`
432-
is a regular expression, checks whether `haystack` contains a match.
433-
434-
# Examples
435-
```jldoctest
436-
julia> contains("JuliaLang is pretty cool!", "Julia")
437-
true
438-
439-
julia> contains("JuliaLang is pretty cool!", 'a')
440-
true
441-
442-
julia> contains("aba", r"a.a")
443-
true
444-
445-
julia> contains("abba", r"a.a")
446-
false
447-
```
448-
"""
449-
function contains end
450-
451-
contains(haystack::AbstractString, needle::Union{AbstractString,Char}) =
428+
isfound(needle::Union{AbstractString,Char}, haystack::AbstractString) =
452429
_searchindex(haystack, needle, firstindex(haystack)) != 0
453430

454-
in(::AbstractString, ::AbstractString) = error("use contains(x,y) for string containment")
431+
in(::AbstractString, ::AbstractString) = error("use isfound(x, y) for string containment")

base/uuid.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let groupings = [1:8; 10:13; 15:18; 20:23; 25:36]
3030
function UUID(s::AbstractString)
3131
s = lowercase(s)
3232

33-
if !contains(s, r"^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$")
33+
if !isfound(r"^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$", s)
3434
throw(ArgumentError("Malformed UUID string: $(repr(s))"))
3535
end
3636

base/version.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct VersionNumber
2121
if ident isa Integer
2222
ident >= 0 || throw(ArgumentError("invalid negative pre-release identifier: $ident"))
2323
else
24-
if !contains(ident, r"^(?:|[0-9a-z-]*[a-z-][0-9a-z-]*)$"i) ||
24+
if !isfound(r"^(?:|[0-9a-z-]*[a-z-][0-9a-z-]*)$"i, ident) ||
2525
isempty(ident) && !(length(pre)==1 && isempty(bld))
2626
throw(ArgumentError("invalid pre-release identifier: $(repr(ident))"))
2727
end
@@ -31,7 +31,7 @@ struct VersionNumber
3131
if ident isa Integer
3232
ident >= 0 || throw(ArgumentError("invalid negative build identifier: $ident"))
3333
else
34-
if !contains(ident, r"^(?:|[0-9a-z-]*[a-z-][0-9a-z-]*)$"i) ||
34+
if !isfound(r"^(?:|[0-9a-z-]*[a-z-][0-9a-z-]*)$"i, ident) ||
3535
isempty(ident) && length(bld)!=1
3636
throw(ArgumentError("invalid build identifier: $(repr(ident))"))
3737
end
@@ -83,7 +83,7 @@ function split_idents(s::AbstractString)
8383
idents = split(s, '.')
8484
ntuple(length(idents)) do i
8585
ident = idents[i]
86-
contains(ident, r"^\d+$") ? parse(UInt64, ident) : String(ident)
86+
isfound(r"^\d+$", ident) ? parse(UInt64, ident) : String(ident)
8787
end
8888
end
8989

contrib/add_license_to_files.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function check_lines!(
8484
remove = []
8585
for i in 1:length(lines)
8686
line = lines[i]
87-
if contains(line, checktxt)
87+
if isfound(checktxt, line)
8888
if strip(line) == strip(prefix * checktxt) || strip(line) == strip(checktxt)
8989
push!(remove, i)
9090
else

contrib/fixup_precompile.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function needs_USE_GPL_LIBS(s::String)
2-
contains(s, "CHOLMOD") && return true
2+
isfound("CHOLMOD", s) && return true
33
return false
44
end
55

@@ -24,7 +24,7 @@ function fixup_precompile(new_precompile_file; merge=false)
2424
for line in eachline(file)
2525
line = strip(line)
2626
# filter out closures, which might have different generated names in different environments
27-
contains(line, r"#[0-9]") && continue
27+
isfound(r"#[0-9]", line) && continue
2828
# Other stuff than precompile statements might have been written to STDERR
2929
startswith(line, "precompile(Tuple{") || continue
3030
# Ok, add the line
@@ -65,4 +65,4 @@ elseif length(ARGS) == 2
6565
fixup_precompile(joinpath(pwd(), ARGS[2]); merge = true)
6666
else
6767
error("invalid arguments")
68-
end
68+
end

doc/src/base/strings.md

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ Base.findfirst(::AbstractString, ::AbstractString)
3535
Base.findnext(::AbstractString, ::AbstractString, ::Integer)
3636
Base.findlast(::AbstractString, ::AbstractString)
3737
Base.findprev(::AbstractString, ::AbstractString, ::Integer)
38-
Base.contains
3938
Base.reverse(::Union{String,SubString{String}})
4039
Base.replace(s::AbstractString, ::Pair)
4140
Base.split

0 commit comments

Comments
 (0)