From 6ca43fc9cfb399143a74fd6c24fd5bae1a323954 Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Sat, 16 Dec 2017 15:08:21 +0100 Subject: [PATCH] Deprecate ismatch(r, s) in favor of contains(s, r) --- NEWS.md | 2 ++ base/deprecated.jl | 2 ++ base/docs/utils.jl | 2 +- base/exports.jl | 1 - base/interactiveutil.jl | 6 ++-- base/libc.jl | 2 +- base/linalg/blas.jl | 2 +- base/linalg/uniformscaling.jl | 2 +- base/markdown/Common/block.jl | 8 ++--- base/markdown/Common/inline.jl | 2 +- base/markdown/render/rst.jl | 2 +- base/markdown/render/terminal/formatting.jl | 2 +- base/methodshow.jl | 2 +- base/path.jl | 10 +++---- base/pkg/read.jl | 6 ++-- base/pkg/reqs.jl | 6 ++-- base/random/misc.jl | 2 +- base/regex.jl | 26 ++-------------- base/repl/REPLCompletions.jl | 4 +-- base/repl/latex_symbols.jl | 2 +- base/strings/search.jl | 16 ++++++++-- base/version.jl | 6 ++-- doc/src/manual/strings.md | 8 ++--- doc/src/stdlib/strings.md | 3 +- stdlib/Test/src/Test.jl | 10 +++---- stdlib/Test/src/logging.jl | 22 +++++++------- test/asmvariant.jl | 8 ++--- test/compile.jl | 4 +-- test/goto.jl | 2 +- test/libdl.jl | 2 +- test/logging.jl | 6 ++-- test/pkg.jl | 8 ++--- test/reflection.jl | 10 +++---- test/regex.jl | 2 +- test/repl.jl | 33 +++++++++++---------- test/replutil.jl | 2 +- test/show.jl | 4 +-- test/strings/types.jl | 4 +-- 38 files changed, 118 insertions(+), 123 deletions(-) diff --git a/NEWS.md b/NEWS.md index 5e0fcfebb5f51..bfbab7b7c2d19 100644 --- a/NEWS.md +++ b/NEWS.md @@ -904,6 +904,8 @@ Deprecated or removed `findlast`/`findprev` respectively, in combination with the new `equalto` and `occursin` predicates for some methods ([#24673] + * `ismatch(regex, str)` has been deprecated in favor of `contains(str, regex)` ([#24673]). + Command-line option changes --------------------------- diff --git a/base/deprecated.jl b/base/deprecated.jl index 3630a321c0517..2afc55f9e1dba 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -3860,6 +3860,8 @@ end @deprecate rsearchindex(s::AbstractString, c::Char) first(findlast(equalto(c), s)) @deprecate rsearchindex(s::AbstractString, c::Char, i::Integer) first(findprev(equalto(c), s, i)) +@deprecate ismatch(r::Regex, s::AbstractString) contains(s, r) + # END 0.7 deprecations # BEGIN 1.0 deprecations diff --git a/base/docs/utils.jl b/base/docs/utils.jl index 8d4cccd1e081d..a2d9211e40009 100644 --- a/base/docs/utils.jl +++ b/base/docs/utils.jl @@ -359,7 +359,7 @@ const builtins = ["abstract type", "baremodule", "begin", "break", moduleusings(mod) = ccall(:jl_module_usings, Any, (Any,), mod) -filtervalid(names) = filter(x->!ismatch(r"#", x), map(string, names)) +filtervalid(names) = filter(x->!contains(x, r"#"), map(string, names)) accessible(mod::Module) = [filter!(s -> !Base.isdeprecated(mod, s), names(mod, true, true)); diff --git a/base/exports.jl b/base/exports.jl index 79479e5090503..847a662c6d54d 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -503,7 +503,6 @@ export findnext, findprev, findnz, - ismatch, occursin, match, matchall, diff --git a/base/interactiveutil.jl b/base/interactiveutil.jl index 10fc71e7cc706..ca7e397f49ccf 100644 --- a/base/interactiveutil.jl +++ b/base/interactiveutil.jl @@ -334,13 +334,13 @@ function versioninfo(io::IO=STDOUT; verbose::Bool=false, packages::Bool=false) println(io, "Environment:") for (k,v) in ENV - if ismatch(r"JULIA", String(k)) + if contains(String(k), r"JULIA") println(io, " $(k) = $(v)") end end if verbose for (k,v) in ENV - if ismatch(r"PATH|FLAG|^TERM$|HOME", String(k)) + if contains(String(k), r"PATH|FLAG|^TERM$|HOME") println(io, " $(k) = $(v)") end end @@ -737,7 +737,7 @@ function varinfo(m::Module=Main, pattern::Regex=r"") (value ∈ (Base, Main, Core) ? "" : format_bytes(summarysize(value))), summary(value)] end - for v in sort!(names(m)) if isdefined(m, v) && ismatch(pattern, string(v)) ] + for v in sort!(names(m)) if isdefined(m, v) && contains(string(v), pattern) ] pushfirst!(rows, Any["name", "size", "summary"]) diff --git a/base/libc.jl b/base/libc.jl index cbe3323c2ef23..fe9fdfb180787 100644 --- a/base/libc.jl +++ b/base/libc.jl @@ -204,7 +204,7 @@ function strptime(fmt::AbstractString, timestr::AbstractString) @static if Sys.isapple() # if we didn't explicitly parse the weekday or year day, use mktime # to fill them in automatically. - if !ismatch(r"([^%]|^)%(a|A|j|w|Ow)", fmt) + if !contains(fmt, r"([^%]|^)%(a|A|j|w|Ow)") ccall(:mktime, Int, (Ref{TmStruct},), tm) end end diff --git a/base/linalg/blas.jl b/base/linalg/blas.jl index f35f871da5248..a31f084c8078e 100644 --- a/base/linalg/blas.jl +++ b/base/linalg/blas.jl @@ -122,7 +122,7 @@ function check() blas = vendor() if blas == :openblas || blas == :openblas64 openblas_config = openblas_get_config() - openblas64 = ismatch(r".*USE64BITINT.*", openblas_config) + openblas64 = contains(openblas_config, r".*USE64BITINT.*") if Base.USE_BLAS64 != openblas64 if !openblas64 @error """ diff --git a/base/linalg/uniformscaling.jl b/base/linalg/uniformscaling.jl index 97773b9322c61..29bc95fd1403b 100644 --- a/base/linalg/uniformscaling.jl +++ b/base/linalg/uniformscaling.jl @@ -55,7 +55,7 @@ getindex(J::UniformScaling, i::Integer,j::Integer) = ifelse(i==j,J.λ,zero(J.λ) function show(io::IO, J::UniformScaling) s = "$(J.λ)" - if ismatch(r"\w+\s*[\+\-]\s*\w+", s) + if contains(s, r"\w+\s*[\+\-]\s*\w+") s = "($s)" end print(io, "$(typeof(J))\n$s*I") diff --git a/base/markdown/Common/block.jl b/base/markdown/Common/block.jl index a0c4a095d3bae..12b59bf7fc6fc 100644 --- a/base/markdown/Common/block.jl +++ b/base/markdown/Common/block.jl @@ -211,11 +211,11 @@ function admonition(stream::IO, block::MD) let untitled = r"^([a-z]+)$", # !!! titled = r"^([a-z]+) \"(.*)\"$", # !!! "" line = strip(readline(stream)) - if ismatch(untitled, line) + if contains(line, untitled) m = match(untitled, line) # When no title is provided we use CATEGORY_NAME, capitalising it. m.captures[1], ucfirst(m.captures[1]) - elseif ismatch(titled, line) + elseif contains(line, titled) m = match(titled, line) # To have a blank TITLE provide an explicit empty string as TITLE. m.captures[1], m.captures[2] @@ -270,10 +270,10 @@ function list(stream::IO, block::MD) indent = isempty(bullet) ? (return false) : length(bullet) # Calculate the starting number and regex to use for bullet matching. initial, regex = - if ismatch(BULLETS, bullet) + if contains(bullet, BULLETS) # An unordered list. Use `-1` to flag the list as unordered. -1, BULLETS - elseif ismatch(r"^ {0,3}\d+(\.|\))( |$)", bullet) + elseif contains(bullet, r"^ {0,3}\d+(\.|\))( |$)") # An ordered list. Either with `1. ` or `1) ` style numbering. r = contains(bullet, ".") ? r"^ {0,3}(\d+)\.( |$)" : r"^ {0,3}(\d+)\)( |$)" Base.parse(Int, match(r, bullet).captures[1]), r diff --git a/base/markdown/Common/inline.jl b/base/markdown/Common/inline.jl index 276504cc5a13e..0eab3eb0051d6 100644 --- a/base/markdown/Common/inline.jl +++ b/base/markdown/Common/inline.jl @@ -140,7 +140,7 @@ function _is_mailto(s::AbstractString) length(s) < 6 && return false # slicing strings is a bit risky, but this equality check is safe lowercase(s[1:6]) == "mailto:" || return false - return ismatch(_email_regex, s[6:end]) + return contains(s[6:end], _email_regex) end # ––––––––––– diff --git a/base/markdown/render/rst.jl b/base/markdown/render/rst.jl index d4bbaf199a9b5..737cf316c8851 100644 --- a/base/markdown/render/rst.jl +++ b/base/markdown/render/rst.jl @@ -115,7 +115,7 @@ rstinline(io::IO, md::Vector) = !isempty(md) && rstinline(io, md...) # rstinline(io::IO, md::Image) = rstinline(io, ".. image:: ", md.url) function rstinline(io::IO, md::Link) - if ismatch(r":(func|obj|ref|exc|class|const|data):`\.*", md.url) + if contains(md.url, r":(func|obj|ref|exc|class|const|data):`\.*") rstinline(io, md.url) else rstinline(io, "`", md.text, " <", md.url, ">`_") diff --git a/base/markdown/render/terminal/formatting.jl b/base/markdown/render/terminal/formatting.jl index c3516babdc785..4326bd8e6a737 100644 --- a/base/markdown/render/terminal/formatting.jl +++ b/base/markdown/render/terminal/formatting.jl @@ -59,7 +59,7 @@ lines(s) = split(s, "\n") # This could really be more efficient function wrapped_lines(io::IO, s::AbstractString; width = 80, i = 0) - if ismatch(r"\n", s) + if contains(s, r"\n") return vcat(map(s->wrapped_lines(io, s, width = width, i = i), split(s, "\n"))...) end ws = words(s) diff --git a/base/methodshow.jl b/base/methodshow.jl index a1c14eb7b0057..7afa482d69f8a 100644 --- a/base/methodshow.jl +++ b/base/methodshow.jl @@ -202,7 +202,7 @@ function url(m::Method) (m.file == :null || m.file == :string) && return "" file = string(m.file) line = m.line - line <= 0 || ismatch(r"In\[[0-9]+\]", file) && return "" + line <= 0 || contains(file, r"In\[[0-9]+\]") && return "" Sys.iswindows() && (file = replace(file, '\\' => '/')) if inbase(M) if isempty(Base.GIT_VERSION_INFO.commit) diff --git a/base/path.jl b/base/path.jl index 2c6c6f10505e7..c602e409d7009 100644 --- a/base/path.jl +++ b/base/path.jl @@ -78,7 +78,7 @@ end if Sys.iswindows() - isabspath(path::String) = ismatch(path_absolute_re, path) + isabspath(path::String) = contains(path, path_absolute_re) else isabspath(path::String) = startswith(path, '/') end @@ -113,7 +113,7 @@ julia> isdirpath("/home/") true ``` """ -isdirpath(path::String) = ismatch(path_directory_re, splitdrive(path)[2]) +isdirpath(path::String) = contains(splitdrive(path)[2], path_directory_re) """ splitdir(path::AbstractString) -> (AbstractString, AbstractString) @@ -218,9 +218,9 @@ function joinpath(a::String, b::String) B, b = splitdrive(b) !isempty(B) && A != B && return string(B,b) C = isempty(B) ? A : B - isempty(a) ? string(C,b) : - ismatch(path_separator_re, a[end:end]) ? string(C,a,b) : - string(C,a,pathsep(a,b),b) + isempty(a) ? string(C,b) : + contains(a[end:end], path_separator_re) ? string(C,a,b) : + string(C,a,pathsep(a,b),b) end joinpath(a::AbstractString, b::AbstractString) = joinpath(String(a), String(b)) diff --git a/base/pkg/read.jl b/base/pkg/read.jl index 721ac55b907df..e68e8457d25e7 100644 --- a/base/pkg/read.jl +++ b/base/pkg/read.jl @@ -18,7 +18,7 @@ function available(names=readdir("METADATA")) versdir = joinpath("METADATA", pkg, "versions") isdir(versdir) || continue for ver in readdir(versdir) - ismatch(Base.VERSION_REGEX, ver) || continue + contains(ver, Base.VERSION_REGEX) || continue isfile(versdir, ver, "sha1") || continue haskey(pkgs,pkg) || (pkgs[pkg] = Dict{VersionNumber,Available}()) pkgs[pkg][VersionNumber(ver)] = Available( @@ -39,7 +39,7 @@ function latest(names=readdir("METADATA")) isdir(versdir) || continue pkgversions = VersionNumber[] for ver in readdir(versdir) - ismatch(Base.VERSION_REGEX, ver) || continue + contains(ver, Base.VERSION_REGEX) || continue isfile(versdir, ver, "sha1") || continue push!(pkgversions, VersionNumber(ver)) end @@ -114,7 +114,7 @@ function ispinned(prepo::LibGit2.GitRepo) LibGit2.isattached(prepo) || return false br = LibGit2.branch(prepo) # note: regex is based on the naming scheme used in Entry.pin() - return ismatch(r"^pinned\.[0-9a-f]{8}\.tmp$", br) + return contains(br, r"^pinned\.[0-9a-f]{8}\.tmp$") end function installed_version(pkg::AbstractString, prepo::LibGit2.GitRepo, avail::Dict=available(pkg)) diff --git a/base/pkg/reqs.jl b/base/pkg/reqs.jl index 57a539e1aab48..7bc9db969bbef 100644 --- a/base/pkg/reqs.jl +++ b/base/pkg/reqs.jl @@ -26,7 +26,7 @@ struct Requirement <: Line end isempty(fields) && throw(PkgError("invalid requires entry: $content")) package = popfirst!(fields) - all(field->ismatch(Base.VERSION_REGEX, field), fields) || + all(field->contains(field, Base.VERSION_REGEX), fields) || throw(PkgError("invalid requires entry for $package: $content")) versions = map(VersionNumber, fields) issorted(versions) || throw(PkgError("invalid requires entry for $package: $content")) @@ -58,7 +58,7 @@ function read(readable::Vector{<:AbstractString}) lines = Line[] for line in readable line = chomp(line) - push!(lines, ismatch(r"^\s*(?:#|$)", line) ? Comment(line) : Requirement(line)) + push!(lines, contains(line, r"^\s*(?:#|$)") ? Comment(line) : Requirement(line)) end return lines end @@ -66,7 +66,7 @@ end function read(readable::Union{IO,Base.AbstractCmd}) lines = Line[] for line in eachline(readable) - push!(lines, ismatch(r"^\s*(?:#|$)", line) ? Comment(line) : Requirement(line)) + push!(lines, contains(line, r"^\s*(?:#|$)") ? Comment(line) : Requirement(line)) end return lines end diff --git a/base/random/misc.jl b/base/random/misc.jl index 6c20d1a943d35..68abece65b197 100644 --- a/base/random/misc.jl +++ b/base/random/misc.jl @@ -432,7 +432,7 @@ let groupings = [1:8; 10:13; 15:18; 20:23; 25:36] function UUID(s::AbstractString) s = Base.Unicode.lowercase(s) - if !ismatch(r"^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$", s) + if !contains(s, r"^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$") throw(ArgumentError("Malformed UUID string")) end diff --git a/base/regex.jl b/base/regex.jl index 0c28617852cca..565e93844c999 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -141,39 +141,19 @@ function getindex(m::RegexMatch, name::Symbol) end getindex(m::RegexMatch, name::AbstractString) = m[Symbol(name)] -""" - ismatch(r::Regex, s::AbstractString) -> Bool - -Test whether a string contains a match of the given regular expression. - -# Examples -```jldoctest -julia> rx = r"a.a" -r"a.a" - -julia> ismatch(rx, "aba") -true - -julia> ismatch(rx, "abba") -false - -julia> rx("aba") -true -``` -""" -function ismatch(r::Regex, s::AbstractString, offset::Integer=0) +function contains(s::AbstractString, r::Regex, offset::Integer=0) compile(r) return PCRE.exec(r.regex, String(s), offset, r.match_options, r.match_data) end -function ismatch(r::Regex, s::SubString, offset::Integer=0) +function contains(s::SubString, r::Regex, offset::Integer=0) compile(r) return PCRE.exec(r.regex, s, offset, r.match_options, r.match_data) end -(r::Regex)(s) = ismatch(r, s) +(r::Regex)(s) = contains(s, r) """ match(r::Regex, s::AbstractString[, idx::Integer[, addopts]]) diff --git a/base/repl/REPLCompletions.jl b/base/repl/REPLCompletions.jl index ae7241aa5b941..4c88d236fee6f 100644 --- a/base/repl/REPLCompletions.jl +++ b/base/repl/REPLCompletions.jl @@ -116,7 +116,7 @@ function complete_keyword(s::Union{String,SubString{String}}) end function complete_path(path::AbstractString, pos; use_envpath=false) - if Base.Sys.isunix() && ismatch(r"^~(?:/|$)", path) + if Base.Sys.isunix() && contains(path, r"^~(?:/|$)") # if the path is just "~", don't consider the expanded username as a prefix if path == "~" dir, prefix = homedir(), "" @@ -409,7 +409,7 @@ function afterusing(string::String, startpos::Int) r = findfirst(r"\s(gnisu|tropmi)\b", rstr) isempty(r) && return false fr = reverseind(str, last(r)) - return ismatch(r"^\b(using|import)\s*((\w+[.])*\w+\s*,\s*)*$", str[fr:end]) + return contains(str[fr:end], r"^\b(using|import)\s*((\w+[.])*\w+\s*,\s*)*$") end function bslash_completions(string, pos) diff --git a/base/repl/latex_symbols.jl b/base/repl/latex_symbols.jl index a4792522a8e7a..c3719e49c7149 100644 --- a/base/repl/latex_symbols.jl +++ b/base/repl/latex_symbols.jl @@ -24,7 +24,7 @@ for c in child_nodes(root(xdoc)) id = attribute(ce, "id") U = string(map(s -> Char(parse(Int, s, 16)), split(id[2:end], "-"))...) - if ismatch(r"^\\[A-Za-z]+$",L) && !isa(U,String) + if contains(L, r"^\\[A-Za-z]+$") && !isa(U,String) if L in Ls println("# duplicated symbol $L ($id)") else diff --git a/base/strings/search.jl b/base/strings/search.jl index d8e011c4ccc79..8002e9fdd59e1 100644 --- a/base/strings/search.jl +++ b/base/strings/search.jl @@ -448,16 +448,28 @@ julia> findprev("Julia", "JuliaLang", 6) findprev(t::AbstractString, s::AbstractString, i::Integer) = _rsearch(s, t, i) """ - contains(haystack::AbstractString, needle::Union{AbstractString,Char}) + contains(haystack::AbstractString, needle::Union{AbstractString,Regex,Char}) -Determine whether the second argument is a substring of the first. +Determine whether the second argument is a substring of the first. If `needle` +is a regular expression, checks whether `haystack` contains a match. # Examples ```jldoctest julia> contains("JuliaLang is pretty cool!", "Julia") true + +julia> contains("JuliaLang is pretty cool!", 'a') +true + +julia> contains("aba", r"a.a") +true + +julia> contains("abba", r"a.a") +false ``` """ +function contains end + contains(haystack::AbstractString, needle::Union{AbstractString,Char}) = _searchindex(haystack, needle, start(haystack)) != 0 diff --git a/base/version.jl b/base/version.jl index 66cc198466d9f..7dc2fe27c3818 100644 --- a/base/version.jl +++ b/base/version.jl @@ -21,7 +21,7 @@ struct VersionNumber if ident isa Integer ident >= 0 || throw(ArgumentError("invalid negative pre-release identifier: $ident")) else - if !ismatch(r"^(?:|[0-9a-z-]*[a-z-][0-9a-z-]*)$"i, ident) || + if !contains(ident, r"^(?:|[0-9a-z-]*[a-z-][0-9a-z-]*)$"i) || isempty(ident) && !(length(pre)==1 && isempty(bld)) throw(ArgumentError("invalid pre-release identifier: $(repr(ident))")) end @@ -31,7 +31,7 @@ struct VersionNumber if ident isa Integer ident >= 0 || throw(ArgumentError("invalid negative build identifier: $ident")) else - if !ismatch(r"^(?:|[0-9a-z-]*[a-z-][0-9a-z-]*)$"i, ident) || + if !contains(ident, r"^(?:|[0-9a-z-]*[a-z-][0-9a-z-]*)$"i) || isempty(ident) && length(bld)!=1 throw(ArgumentError("invalid build identifier: $(repr(ident))")) end @@ -83,7 +83,7 @@ function split_idents(s::AbstractString) idents = split(s, '.') ntuple(length(idents)) do i ident = idents[i] - ismatch(r"^\d+$", ident) ? parse(UInt64, ident) : String(ident) + contains(ident, r"^\d+$") ? parse(UInt64, ident) : String(ident) end end diff --git a/doc/src/manual/strings.md b/doc/src/manual/strings.md index 017ce693cdc32..97991a0681258 100644 --- a/doc/src/manual/strings.md +++ b/doc/src/manual/strings.md @@ -604,17 +604,17 @@ julia> typeof(ans) Regex ``` -To check if a regex matches a string, use [`ismatch`](@ref): +To check if a regex matches a string, use [`contains`](@ref): ```jldoctest -julia> ismatch(r"^\s*(?:#|$)", "not a comment") +julia> contains("not a comment", r"^\s*(?:#|$)") false -julia> ismatch(r"^\s*(?:#|$)", "# a comment") +julia> contains("# a comment", r"^\s*(?:#|$)") true ``` -As one can see here, [`ismatch`](@ref) simply returns true or false, indicating whether the +As one can see here, [`contains`](@ref) simply returns true or false, indicating whether the given regex matches the string or not. Commonly, however, one wants to know not just whether a string matched, but also *how* it matched. To capture this information about a match, use the [`match`](@ref) function instead: diff --git a/doc/src/stdlib/strings.md b/doc/src/stdlib/strings.md index 04657aa131530..1175c11e3268d 100644 --- a/doc/src/stdlib/strings.md +++ b/doc/src/stdlib/strings.md @@ -24,7 +24,6 @@ Base.Docs.@text_str Base.isvalid(::Any) Base.isvalid(::Any, ::Any) Base.isvalid(::AbstractString, ::Integer) -Base.ismatch Base.match Base.eachmatch Base.matchall @@ -37,7 +36,7 @@ Base.findfirst(::AbstractString, ::AbstractString) Base.findnext(::AbstractString, ::AbstractString, ::Integer) Base.findlast(::AbstractString, ::AbstractString) Base.findprev(::AbstractString, ::AbstractString, ::Integer) -Base.contains(::AbstractString, ::AbstractString) +Base.contains Base.reverse(::Union{String,SubString{String}}) Base.replace(s::AbstractString, ::Pair) Base.split diff --git a/stdlib/Test/src/Test.jl b/stdlib/Test/src/Test.jl index a7ddfd4cc4daf..3e0fe0d7c5a5d 100644 --- a/stdlib/Test/src/Test.jl +++ b/stdlib/Test/src/Test.jl @@ -511,10 +511,10 @@ end # Test for warning messages (deprecated) -ismatch_warn(s::AbstractString, output) = contains(output, s) -ismatch_warn(s::Regex, output) = ismatch(s, output) -ismatch_warn(s::Function, output) = s(output) -ismatch_warn(S::Union{AbstractArray,Tuple}, output) = all(s -> ismatch_warn(s, output), S) +contains_warn(output, s::AbstractString) = contains(output, s) +contains_warn(output, s::Regex) = contains(output, s) +contains_warn(output, s::Function) = s(output) +contains_warn(output, S::Union{AbstractArray,Tuple}) = all(s -> contains_warn(output, s), S) """ @test_warn msg expr @@ -536,7 +536,7 @@ macro test_warn(msg, expr) $(esc(expr)) end end - @test ismatch_warn($(esc(msg)), read(fname, String)) + @test contains_warn(read(fname, String), $(esc(msg))) ret finally rm(fname, force=true) diff --git a/stdlib/Test/src/logging.jl b/stdlib/Test/src/logging.jl index 7cc40d1fc8a75..e31b07c448d75 100644 --- a/stdlib/Test/src/logging.jl +++ b/stdlib/Test/src/logging.jl @@ -3,7 +3,7 @@ using Logging import Logging: Info, shouldlog, handle_message, min_enabled_level, catch_exceptions -import Base: ismatch +import Base: contains #------------------------------------------------------------------------------- # Log records @@ -113,7 +113,7 @@ corresponding to the arguments to passed to `AbstractLogger` via the Elements which are present will be matched pairwise with the log record fields using `==` by default, with the special cases that `Symbol`s may be used for the standard log levels, and `Regex`s in the pattern will match string or -Symbol fields using `ismatch`. +Symbol fields using `contains`. # Examples @@ -181,9 +181,9 @@ function match_logs(f, patterns...; match_mode::Symbol=:all, kwargs...) logs,value = collect_test_logs(f; kwargs...) if match_mode == :all didmatch = length(logs) == length(patterns) && - all(ismatch(p,l) for (p,l) in zip(patterns, logs)) + all(contains(l,p) for (p,l) in zip(patterns, logs)) elseif match_mode == :any - didmatch = all(any(ismatch(p,l) for l in logs) for p in patterns) + didmatch = all(any(contains(l,p) for l in logs) for p in patterns) end didmatch,logs,value end @@ -201,15 +201,15 @@ function parse_level(level::Symbol) end end -logfield_ismatch(a, b) = a == b -logfield_ismatch(r::Regex, b) = ismatch(r, b) -logfield_ismatch(r::Regex, b::Symbol) = ismatch(r, String(b)) -logfield_ismatch(a::Symbol, b::LogLevel) = parse_level(a) == b -logfield_ismatch(a::Ignored, b) = true +logfield_contains(a, b) = a == b +logfield_contains(a, r::Regex) = contains(a, r) +logfield_contains(a::Symbol, r::Regex) = contains(String(a), r) +logfield_contains(a::LogLevel, b::Symbol) = a == parse_level(b) +logfield_contains(a, b::Ignored) = true -function ismatch(pattern::Tuple, r::LogRecord) +function contains(r::LogRecord, pattern::Tuple) stdfields = (r.level, r.message, r._module, r.group, r.id, r.file, r.line) - all(logfield_ismatch(p,f) for (p,f) in zip(pattern, stdfields[1:length(pattern)])) + all(logfield_contains(f, p) for (f, p) in zip(stdfields[1:length(pattern)], pattern)) end """ diff --git a/test/asmvariant.jl b/test/asmvariant.jl index 5c75776bbb6e2..28b0fbd6f124b 100644 --- a/test/asmvariant.jl +++ b/test/asmvariant.jl @@ -4,7 +4,7 @@ using Test ix86 = r"i[356]86" -if Sys.ARCH === :x86_64 || ismatch(ix86, string(Sys.ARCH)) +if Sys.ARCH === :x86_64 || contains(string(Sys.ARCH), ix86) function linear_foo() x = 4 y = 5 @@ -17,16 +17,16 @@ if Sys.ARCH === :x86_64 || ismatch(ix86, string(Sys.ARCH)) code_native(buf,linear_foo,(),:att) output=String(take!(buf)) - @test ismatch(rgx,output) + @test contains(output,rgx) #test that the code output is intel syntax by checking it has no occurrences of '%' code_native(buf,linear_foo,(),:intel) output=String(take!(buf)) - @test !(ismatch(rgx,output)) + @test !contains(output,rgx) code_native(buf,linear_foo,()) output=String(take!(buf)) - @test ismatch(rgx, output) + @test contains(output,rgx) end diff --git a/test/compile.jl b/test/compile.jl index d5c2562c185da..2fc3702c2885e 100644 --- a/test/compile.jl +++ b/test/compile.jl @@ -458,7 +458,7 @@ let dir = mktempdir() let fname = tempname() try @test readchomp(pipeline(`$exename -E $(testcode)`, stderr=fname)) == "nothing" - @test ismatch(Regex("Replacing module `$Test_module`"), read(fname, String)) + @test contains(read(fname, String), Regex("Replacing module `$Test_module`")) finally rm(fname, force=true) end @@ -470,7 +470,7 @@ let dir = mktempdir() try @test readchomp(pipeline(`$exename -E $(testcode)`, stderr=fname)) == "nothing" # e.g `@test_nowarn` - @test Test.ismatch_warn(r"^(?!.)"s, read(fname, String)) + @test Test.contains_warn(read(fname, String), r"^(?!.)"s) finally rm(fname, force=true) end diff --git a/test/goto.jl b/test/goto.jl index 11f1a9f024958..4e8063ffb0990 100644 --- a/test/goto.jl +++ b/test/goto.jl @@ -75,7 +75,7 @@ let e = Meta.lower(@__MODULE__, quote end end) @test (e::Expr).head === :error - @test ismatch(r"label \"#\d+#a\" referenced but not defined", e.args[1]) + @test contains(e.args[1], r"label \"#\d+#a\" referenced but not defined") end function goto_test5_3() diff --git a/test/libdl.jl b/test/libdl.jl index 1cc1b19dd351b..af2b396b92421 100644 --- a/test/libdl.jl +++ b/test/libdl.jl @@ -13,7 +13,7 @@ if !Sys.iswindows() || Sys.windows_version() >= Sys.WINDOWS_VISTA_VER end end @test length(filter(dlls) do dl - return ismatch(Regex("^libjulia(?:.*)\\.$(Libdl.dlext)(?:\\..+)?\$"), basename(dl)) + return contains(basename(dl), Regex("^libjulia(?:.*)\\.$(Libdl.dlext)(?:\\..+)?\$")) end) == 1 # look for something libjulia-like (but only one) # library handle pointer must not be NULL diff --git a/test/logging.jl b/test/logging.jl index 39bcc9ec73b07..eb73c3180ddee 100644 --- a/test/logging.jl +++ b/test/logging.jl @@ -59,7 +59,7 @@ end @test record.file == Base.source_path() @test record.line == kwargs[:real_line] @test record.id isa Symbol - @test ismatch(r"^.*logging_[[:xdigit:]]{8}$", String(record.id)) + @test contains(String(record.id), r"^.*logging_[[:xdigit:]]{8}$") # User-defined metadata @test kwargs[:bar_val] === bar_val @@ -69,13 +69,13 @@ end # Keyword values accessible from message block record2 = logs[2] - @test ismatch((Info,"test2"), record2) + @test contains(record2, (Info,"test2")) kwargs = record2.kwargs @test kwargs[:value_in_msg_block] === 1000.0 # Splatting of keywords record3 = logs[3] - @test ismatch((Info,"test3"), record3) + @test contains(record3, (Info,"test3")) kwargs = record3.kwargs @test sort(collect(keys(kwargs))) == [:a, :b] @test kwargs[:a] === 1 diff --git a/test/pkg.jl b/test/pkg.jl index b2aa9ae5e801b..3f2dee5d118b2 100644 --- a/test/pkg.jl +++ b/test/pkg.jl @@ -516,9 +516,9 @@ temp_pkg_dir() do logs,_ = Test.collect_test_logs() do Pkg.update("ColorTypes") end - @test any(ismatch((:info,r"Upgrading ColorTypes: v0\.2\.2 => v\d+\.\d+\.\d+"), l) for l in logs) - @test any(ismatch((:info,r"Upgrading Compat: v0\.7\.18 => v\d+\.\d+\.\d+"), l) for l in logs) - @test !any(ismatch((:info,r"Upgrading Colors"),l) for l in logs) + @test any(contains(l, (:info,r"Upgrading ColorTypes: v0\.2\.2 => v\d+\.\d+\.\d+")) for l in logs) + @test any(contains(l, (:info,r"Upgrading Compat: v0\.7\.18 => v\d+\.\d+\.\d+")) for l in logs) + @test !any(contains(l, (:info,r"Upgrading Colors")) for l in logs) @test Pkg.installed("Colors") == v"0.6.4" @@ -541,7 +541,7 @@ temp_pkg_dir() do Pkg.add(package) msg = read(ignorestatus(`$(Base.julia_cmd()) --startup-file=no -e "redirect_stderr(STDOUT); using Logging; global_logger(SimpleLogger(STDOUT)); using Example; Pkg.update(\"$package\")"`), String) - @test ismatch(Regex("- $package.*Restart Julia to use the updated versions","s"), msg) + @test contains(msg, Regex("- $package.*Restart Julia to use the updated versions","s")) end # Verify that the --startup-file flag is respected by Pkg.build / Pkg.test diff --git a/test/reflection.jl b/test/reflection.jl index b80b6e1191356..23cce3b530cf1 100644 --- a/test/reflection.jl +++ b/test/reflection.jl @@ -27,8 +27,8 @@ function test_code_reflection(freflect, f, types, tester) end function test_code_reflections(tester, freflect) - test_code_reflection(freflect, ismatch, - Tuple{Regex, AbstractString}, tester) # abstract type + test_code_reflection(freflect, contains, + Tuple{AbstractString, Regex}, tester) # abstract type test_code_reflection(freflect, +, Tuple{Int, Int}, tester) # leaftype signature test_code_reflection(freflect, +, Tuple{Array{Float32}, Array{Float32}}, tester) # incomplete types @@ -466,12 +466,12 @@ function test_typed_ast_printing(Base.@nospecialize(f), Base.@nospecialize(types for i in 1:length(src.slotnames) name = src.slotnames[i] if name in dupnames - if name in must_used_vars && ismatch(Regex("_$i\\b"), str) + if name in must_used_vars && contains(str, Regex("_$i\\b")) must_used_checked[name] = true global used_dup_var_tested15714 = true end else - @test !ismatch(Regex("_$i\\b"), str) + @test !contains(str, Regex("_$i\\b")) if name in must_used_vars global used_unique_var_tested15714 = true end @@ -490,7 +490,7 @@ function test_typed_ast_printing(Base.@nospecialize(f), Base.@nospecialize(types # Use the variable names that we know should be present in the optimized AST for i in 2:length(src.slotnames) name = src.slotnames[i] - if name in must_used_vars && ismatch(Regex("_$i\\b"), str) + if name in must_used_vars && contains(str, Regex("_$i\\b")) must_used_checked[name] = true end end diff --git a/test/regex.jl b/test/regex.jl index 050428a8f959d..4acd9c9269482 100644 --- a/test/regex.jl +++ b/test/regex.jl @@ -32,7 +32,7 @@ show(buf, r"") @test read(buf, String) == "r\"\"" # see #10994, #11447: PCRE2 allows NUL chars in the pattern -@test ismatch(Regex("^a\0b\$"), "a\0b") +@test contains("a\0b", Regex("^a\0b\$")) # regex match / search string must be a String @test_throws ArgumentError match(r"test", GenericString("this is a test")) diff --git a/test/repl.jl b/test/repl.jl index ec3748d9bca08..e0aa274cf46c1 100644 --- a/test/repl.jl +++ b/test/repl.jl @@ -823,22 +823,23 @@ for keys = [altkeys, merge(altkeys...)], # Check the history file history = read(histfile, String) - @test ismatch(r""" - ^\#\ time:\ .*\n - \#\ mode:\ julia\n - \t1\ \+\ 1;\n - \#\ time:\ .*\n - \#\ mode:\ julia\n - \tmulti=2;\n - \tline=2;\n - \#\ time:\ .*\n - \#\ mode:\ julia\n - \tmulti=3;\n - \tline=1;\n - \#\ time:\ .*\n - \#\ mode:\ julia\n - \t1\ \*\ 1;\n$ - """xm, history) + @test contains(history, + r""" + ^\#\ time:\ .*\n + \#\ mode:\ julia\n + \t1\ \+\ 1;\n + \#\ time:\ .*\n + \#\ mode:\ julia\n + \tmulti=2;\n + \tline=2;\n + \#\ time:\ .*\n + \#\ mode:\ julia\n + \tmulti=3;\n + \tline=1;\n + \#\ time:\ .*\n + \#\ mode:\ julia\n + \t1\ \*\ 1;\n$ + """xm) end finally rm(histfile, force=true) diff --git a/test/replutil.jl b/test/replutil.jl index 9d5b15f3df69d..38d2f76d0dffd 100644 --- a/test/replutil.jl +++ b/test/replutil.jl @@ -310,7 +310,7 @@ let err_str, @test contains(err_str, "MethodError: objects of type Array{Float64,1} are not callable") end @test stringmime("text/plain", FunctionLike()) == "(::$(curmod_prefix)FunctionLike) (generic function with 0 methods)" -@test ismatch(r"^@doc \(macro with \d+ method[s]?\)$", stringmime("text/plain", getfield(Base, Symbol("@doc")))) +@test contains(stringmime("text/plain", getfield(Base, Symbol("@doc"))), r"^@doc \(macro with \d+ method[s]?\)$") method_defs_lineno = @__LINE__() + 1 Base.Symbol() = throw(ErrorException("1")) diff --git a/test/show.jl b/test/show.jl index 0992c40f291ca..e80a759370a02 100644 --- a/test/show.jl +++ b/test/show.jl @@ -408,7 +408,7 @@ let a = Expr(:quote,Expr(:$,:x8d003)) end # issue #9865 -@test ismatch(r"^Set\(\[.+….+\]\)$", replstr(Set(1:100))) +@test contains(replstr(Set(1:100)), r"^Set\(\[.+….+\]\)$") # issue #11413 @test string(:(*{1, 2})) == "*{1, 2}" @@ -752,7 +752,7 @@ let repr = sprint(dump, Int64) end let repr = sprint(dump, Any) @test length(repr) == 4 - @test ismatch(r"^Any\n", repr) + @test contains(repr, r"^Any\n") @test endswith(repr, '\n') end let repr = sprint(dump, Integer) diff --git a/test/strings/types.jl b/test/strings/types.jl index 22125b52c7463..0eefac0549c3c 100644 --- a/test/strings/types.jl +++ b/test/strings/types.jl @@ -154,8 +154,8 @@ end @test parse(Float32, SubString("10",1,1)) === 1.0f0 # issue #5870 -@test !ismatch(Regex("aa"), SubString("",1,0)) -@test ismatch(Regex(""), SubString("",1,0)) +@test !contains(SubString("",1,0), Regex("aa")) +@test contains(SubString("",1,0), Regex("")) # isvalid, length, prevind, nextind for SubString{String} let s = "lorem ipsum", sdict = Dict(