From e23df488c629f60c36a20368df5e49c4697bd262 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Tue, 20 Mar 2018 09:04:46 +0100 Subject: [PATCH] contains(haystack, needle) -> occursin(needle, haystack) --- REQUIRE | 2 +- src/CrossReferences.jl | 4 ++-- src/Deps.jl | 2 +- src/DocChecks.jl | 16 ++++++++-------- src/Documenter.jl | 8 ++++---- src/Expanders.jl | 6 +++--- src/Utilities/DOM.jl | 2 +- src/Utilities/Utilities.jl | 12 ++++++------ src/Writers/HTMLWriter.jl | 6 +++--- src/Writers/LaTeXWriter.jl | 4 ++-- test/utilities.jl | 5 +++-- 11 files changed, 34 insertions(+), 33 deletions(-) diff --git a/REQUIRE b/REQUIRE index d87b91ff37..2aa1c97d2d 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,3 +1,3 @@ julia 0.6 -Compat 0.58.0 +Compat 0.62.0 DocStringExtensions 0.2 diff --git a/src/CrossReferences.jl b/src/CrossReferences.jl index 0f4642bbed..73de719937 100644 --- a/src/CrossReferences.jl +++ b/src/CrossReferences.jl @@ -44,7 +44,7 @@ const NAMED_XREF = r"^@ref (.+)$" function xref(link::Markdown.Link, meta, page, doc) link.url == "@ref" ? basicxref(link, meta, page, doc) : - contains(link.url, NAMED_XREF) ? namedxref(link, meta, page, doc) : nothing + occursin(NAMED_XREF, link.url) ? namedxref(link, meta, page, doc) : nothing return false # Stop `walk`ing down this `link` element. end xref(other, meta, page, doc) = true # Continue to `walk` through element `other`. @@ -55,7 +55,7 @@ function basicxref(link::Markdown.Link, meta, page, doc) elseif isa(link.text, Vector) # No `name` was provided, since given a `@ref`, so slugify the `.text` instead. text = strip(sprint(Markdown.plain, Markdown.Paragraph(link.text))) - if contains(text, r"#[0-9]+") + if occursin(r"#[0-9]+", text) issue_xref(link, lstrip(text, '#'), meta, page, doc) else name = Utilities.slugify(text) diff --git a/src/Deps.jl b/src/Deps.jl index b712fdab75..a05994a7df 100644 --- a/src/Deps.jl +++ b/src/Deps.jl @@ -42,7 +42,7 @@ function localbin() end function updatepath!(p = localbin()) - if contains(ENV["PATH"], p) + if occursin(p, ENV["PATH"]) ENV["PATH"] else ENV["PATH"] = "$p:$(ENV["PATH"])" diff --git a/src/DocChecks.jl b/src/DocChecks.jl index 42be21794b..8da3350a21 100644 --- a/src/DocChecks.jl +++ b/src/DocChecks.jl @@ -177,10 +177,10 @@ function doctest(block::Markdown.Code, meta::Dict, doc::Documents.Document, page Meta.isexpr(expr, :block) && (expr.head = :toplevel) eval(sandbox, expr) end - if contains(block.code, r"^julia> "m) + if occursin(r"^julia> "m, block.code) eval_repl(block, sandbox, meta, doc, page) block.language = "julia-repl" - elseif contains(block.code, r"^# output$"m) + elseif occursin(r"^# output$"m, block.code) eval_script(block, sandbox, meta, doc, page) block.language = "julia" else @@ -280,7 +280,7 @@ function filter_doctests(strings::NTuple{2, AbstractString}, meta_block_filters == nothing && meta_block_filters == [] doctest_local_filters = get(meta[:LocalDocTestArguments], :filter, []) for r in [doc.user.doctestfilters; meta_block_filters; doctest_local_filters] - if all(contains.(strings, r)) + if all(occursin.(r, strings)) strings = replace.(strings, r => "") end end @@ -456,7 +456,7 @@ function repl_splitter(code) # TODO: handle multiline comments? # ANON_FUNC_DECLARATION deals with `x->x` -> `#1 (generic function ....)` on 0.7 # TODO: Remove this special case and just disallow lines with comments? - startswith(line, '#') && !contains(line, ANON_FUNC_DECLARATION) && continue + startswith(line, '#') && !occursin(ANON_FUNC_DECLARATION, line) && continue prompt = match(PROMPT_REGEX, line) if prompt === nothing source = match(SOURCE_REGEX, line) @@ -484,7 +484,7 @@ end function takeuntil!(r, buf, lines) while !isempty(lines) line = lines[1] - if !contains(line, r) + if !occursin(r, line) println(buf, popfirst!(lines)) else break @@ -595,13 +595,13 @@ function linkcheck(link::Markdown.Link, doc::Documents.Document) return false end local STATUS_REGEX = r"^HTTP/1.1 (\d+) (.+)$"m - if contains(result, STATUS_REGEX) + if occursin(STATUS_REGEX, result) status = parse(Int, match(STATUS_REGEX, result).captures[1]) if status < 300 printstyled(INDENT, "$(status) ", link.url, "\n", color=:green) elseif status < 400 LOCATION_REGEX = r"^Location: (.+)$"m - if contains(result, LOCATION_REGEX) + if occursin(LOCATION_REGEX, result) location = strip(match(LOCATION_REGEX, result).captures[1]) printstyled(INDENT, "$(status) ", link.url, "\n", color=:yellow) printstyled(INDENT, " -> ", location, "\n\n", color=:yellow) @@ -622,7 +622,7 @@ end linkcheck(other, doc::Documents.Document) = true linkcheck_ismatch(r::String, url) = (url == r) -linkcheck_ismatch(r::Regex, url) = contains(url, r) +linkcheck_ismatch(r::Regex, url) = occursin(r, url) function disable_color(func) orig = setcolor!(false) diff --git a/src/Documenter.jl b/src/Documenter.jl index d8e7abfdc3..65d4fc1207 100644 --- a/src/Documenter.jl +++ b/src/Documenter.jl @@ -376,13 +376,13 @@ function deploydocs(; if !isa(julia, AbstractString) error("julia must be a string, got $julia ($(typeof(julia)))") end - if !isempty(travis_repo_slug) && !contains(repo, travis_repo_slug) + if !isempty(travis_repo_slug) && !occursin(travis_repo_slug, repo) warn("repo $repo does not match $travis_repo_slug") end # When should a deploy be attempted? should_deploy = - contains(repo, travis_repo_slug) && + occursin(travis_repo_slug, repo) && travis_pull_request == "false" && travis_osname == osname && travis_julia == julia && @@ -518,7 +518,7 @@ function git_push( Writers.HTMLWriter.generate_siteinfo_file(tagged_dir, tag) # Build a `release-*.*` folder as well when the travis tag is # valid, which it *should* always be anyway. - if contains(tag, Base.VERSION_REGEX) + if occursin(Base.VERSION_REGEX, tag) version = VersionNumber(tag) release = "release-$(version.major).$(version.minor)" gitrm_copy(target_dir, joinpath(dirname, release)) @@ -584,7 +584,7 @@ end function getenv(regex::Regex) for (key, value) in ENV - contains(key, regex) && return value + occursin(regex, key) && return value end error("could not find key/iv pair.") end diff --git a/src/Expanders.jl b/src/Expanders.jl index caaab5937a..b71c4175e5 100644 --- a/src/Expanders.jl +++ b/src/Expanders.jl @@ -562,7 +562,7 @@ end # Utilities. # ---------- -iscode(x::Markdown.Code, r::Regex) = contains(x.language, r) +iscode(x::Markdown.Code, r::Regex) = occursin(r, x.language) iscode(x::Markdown.Code, lang) = x.language == lang iscode(x, lang) = false @@ -571,7 +571,7 @@ const NAMEDHEADER_REGEX = r"^@id (.+)$" function namedheader(h::Markdown.Header) if isa(h.text, Vector) && length(h.text) === 1 && isa(h.text[1], Markdown.Link) url = h.text[1].url - contains(url, NAMEDHEADER_REGEX) + occursin(NAMEDHEADER_REGEX, url) else false end @@ -581,7 +581,7 @@ end function droplines(code; skip = 0) buffer = IOBuffer() for line in split(code, '\n')[(skip + 1):end] - contains(line, r"^(.*)#\s*hide$") && continue + occursin(r"^(.*)#\s*hide$", line) && continue println(buffer, rstrip(line)) end strip(String(take!(buffer)), '\n') diff --git a/src/Utilities/DOM.jl b/src/Utilities/DOM.jl index 570dbeaca8..ec011922ff 100644 --- a/src/Utilities/DOM.jl +++ b/src/Utilities/DOM.jl @@ -284,7 +284,7 @@ string is constructed with the characters escaped. The returned object should always be treated as an immutable copy and compared using `==` rather than `===`. """ function escapehtml(text::AbstractString) - if contains(text, r"[<>&'\"]") + if occursin(r"[<>&'\"]", text) buffer = IOBuffer() for char in text char === '<' ? write(buffer, "<") : diff --git a/src/Utilities/Utilities.jl b/src/Utilities/Utilities.jl index 4331c71bf9..e870f1d4c6 100644 --- a/src/Utilities/Utilities.jl +++ b/src/Utilities/Utilities.jl @@ -118,7 +118,7 @@ function parseblock(code::AbstractString, doc, page; skip = 0, keywords = true) code = string(code, '\n') code = last(split(code, '\n', limit = skip + 1)) # Check whether we have windows-style line endings. - offset = contains(code, "\n\r") ? 2 : 1 + offset = occursin("\n\r", code) ? 2 : 1 endofstr = lastindex(code) results = [] cursor = 1 @@ -332,7 +332,7 @@ filterdocs(other, modules::Set{Module}) = other """ Does the given docstring represent actual documentation or a no docs error message? """ -nodocs(x) = contains(stringmime("text/plain", x), "No documentation found.") +nodocs(x) = occursin("No documentation found.", stringmime("text/plain", x)) nodocs(::Nothing) = false header_level(::Markdown.Header{N}) where {N} = N @@ -464,11 +464,11 @@ end # "https://bitbucket.org/xxx" => RepoBitbucket # If no match, returns RepoUnknown function repo_host_from_url(repoURL::String) - if contains(repoURL, "bitbucket") + if occursin("bitbucket", repoURL) return RepoBitbucket - elseif contains(repoURL, "github") + elseif occursin("github", repoURL) return RepoGithub - elseif contains(repoURL, "gitlab") + elseif occursin("gitlab", repoURL) return RepoGitlab else return RepoUnknown @@ -592,7 +592,7 @@ end Checks whether `url` is an absolute URL (as opposed to a relative one). """ -isabsurl(url) = contains(url, ABSURL_REGEX) +isabsurl(url) = occursin(ABSURL_REGEX, url) const ABSURL_REGEX = r"^[[:alpha:]+-.]+://" include("DOM.jl") diff --git a/src/Writers/HTMLWriter.jl b/src/Writers/HTMLWriter.jl index 5f97fca0bd..3202d1d9b8 100644 --- a/src/Writers/HTMLWriter.jl +++ b/src/Writers/HTMLWriter.jl @@ -474,8 +474,8 @@ function generate_version_file(dir::AbstractString) tag_folders = [] for each in readdir(dir) each in ("stable", "latest") ? push!(named_folders, each) : - contains(each, r"release\-\d+\.\d+") ? push!(release_folders, each) : - contains(each, Base.VERSION_REGEX) ? push!(tag_folders, each) : nothing + occursin(r"release\-\d+\.\d+", each) ? push!(release_folders, each) : + occursin(Base.VERSION_REGEX, each) ? push!(tag_folders, each) : nothing end open(joinpath(dir, "versions.js"), "w") do buf println(buf, "var DOC_VERSIONS = [") @@ -895,7 +895,7 @@ function mdconvert(c::Markdown.Code, parent::MDBlockContext; kwargs...) # script-type doctest should match the corresponding one in DocChecks.jl. This makes # sure that doctests get highlighted the same way independent of whether they're # being run or not. - contains(c.code, r"^julia> "m) ? "julia-repl" : "julia" + occursin(r"^julia> "m, c.code) ? "julia-repl" : "julia" else c.language end diff --git a/src/Writers/LaTeXWriter.jl b/src/Writers/LaTeXWriter.jl index 899af0814f..4537df6903 100644 --- a/src/Writers/LaTeXWriter.jl +++ b/src/Writers/LaTeXWriter.jl @@ -269,7 +269,7 @@ function latex(io::IO, code::Markdown.Code) # script-type doctest should match the corresponding one in DocChecks.jl. This makes # sure that doctests get highlighted the same way independent of whether they're # being run or not. - contains(code.code, r"^julia> "m) ? "julia-repl" : "julia" + occursin(r"^julia> "m, code.code) ? "julia-repl" : "julia" else code.language end @@ -440,7 +440,7 @@ function latexinline(io::IO, md::Markdown.Link) if io.in_header latexinline(io, md.text) else - if contains(md.url, ".md#") + if occursin(".md#", md.url) file, target = split(md.url, ".md#"; limit = 2) id = string(hash(target)) wrapinline(io, "hyperlink") do diff --git a/test/utilities.jl b/test/utilities.jl index edef754194..25c6958b33 100644 --- a/test/utilities.jl +++ b/test/utilities.jl @@ -2,6 +2,7 @@ module UtilitiesTests import Compat using Compat.Test +import Compat: occursin import Compat.Base64: stringmime import Documenter @@ -47,9 +48,9 @@ end @test a !== nothing @test a === doc @test b !== nothing - @test contains(stringmime("text/plain", b), "Documenter unit tests.") + @test occursin("Documenter unit tests.", stringmime("text/plain", b)) @test c !== nothing - @test !contains(stringmime("text/plain", c), "Documenter unit tests.") + @test !occursin("Documenter unit tests.", stringmime("text/plain", c)) @test d === nothing end