diff --git a/README.md b/README.md index 6d5cebf4a..a1e49d167 100644 --- a/README.md +++ b/README.md @@ -338,7 +338,11 @@ Currently, the `@compat` macro supports the following syntaxes: * `copy!` and `unsafe_copy!` are now `copyto!` and `unsafe_copyto!` ([#24808]). -* `ismatch(r::Regex, str::AbstractString)` is now `contains(str, r)` ([#24673]). +* `contains(haystack, needle)` is now `occursin(needle, haystack)` ([#26283]). + `occursin` also has a new method for `Char` needles ([#22435]). + +* `ismatch(r::Regex, str::AbstractString, offset=0)` is now `occursin(r, str)` and + `occursin(r, str, offset = offset)` respectively ([#24673],[#26283]). * `ipermute!` is now `invpermute!` ([#25168]). @@ -514,6 +518,7 @@ includes this fix. Find the minimum version from there. [#22064]: https://github.com/JuliaLang/julia/issues/22064 [#22182]: https://github.com/JuliaLang/julia/issues/22182 [#22350]: https://github.com/JuliaLang/julia/issues/22350 +[#22435]: https://github.com/JuliaLang/julia/issues/22435 [#22475]: https://github.com/JuliaLang/julia/issues/22475 [#22512]: https://github.com/JuliaLang/julia/issues/22512 [#22629]: https://github.com/JuliaLang/julia/issues/22629 @@ -598,6 +603,7 @@ includes this fix. Find the minimum version from there. [#26089]: https://github.com/JuliaLang/julia/issues/26089 [#26149]: https://github.com/JuliaLang/julia/issues/26149 [#26156]: https://github.com/JuliaLang/julia/issues/26156 +[#26283]: https://github.com/JuliaLang/julia/issues/26283 [#26316]: https://github.com/JuliaLang/julia/issues/26316 [#26436]: https://github.com/JuliaLang/julia/issues/26436 [#26442]: https://github.com/JuliaLang/julia/issues/26442 diff --git a/src/Compat.jl b/src/Compat.jl index 17b33af25..333e02368 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -901,6 +901,25 @@ end occursin(x) = in(x) end +# PR #26283 +if VERSION < v"0.7.0-DEV.4639" + if isdefined(Base, :occursin) + import Base: occursin + else + export occursin + end + occursin(needle, haystack) = contains(haystack, needle) + if VERSION < v"0.7.0-DEV.3272" + occursin(r::Regex, s::AbstractString; offset::Integer = 0) = ismatch(r, s, offset) + else + occursin(r::Regex, s::AbstractString; offset::Integer = 0) = contains(s, r, offset) + end + # PR #22435 + if VERSION < v"0.7.0-DEV.702" + occursin(needle::Char, haystack::AbstractString) = searchindex(haystack,needle) != 0 + end +end + # 0.7.0-DEV.912 if VERSION < v"0.7.0-DEV.912" @@ -1171,7 +1190,7 @@ end export copyto!, unsafe_copyto! end -# 0.7.0-DEV.3272 +# 0.7.0-DEV.3272, keep this definition for 0.6 compatibility @static if VERSION < v"0.7.0-DEV.3272" Base.contains(str::AbstractString, r::Regex) = ismatch(r, str) end diff --git a/test/runtests.jl b/test/runtests.jl index 438e49bad..bb59c8ece 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -185,13 +185,13 @@ let filename = tempname() end end @test ret == [2] - @test contains(read(filename, String), "WARNING: hello") + @test occursin("WARNING: hello", read(filename, String)) ret = open(filename) do f redirect_stdin(f) do readline() end end - @test contains(ret, "WARNING: hello") + @test occursin("WARNING: hello", ret) rm(filename) end @@ -837,7 +837,7 @@ no_specialize_kw2(@nospecialize(x::Integer=0)) = sin(2) # 0.7 @test read(IOBuffer("aaaa"), String) == "aaaa" -@test contains(read(@__FILE__, String), "read(@__FILE__, String)") +@test occursin("read(@__FILE__, String)", read(@__FILE__, String)) @test read(`$(Base.julia_cmd()) --startup-file=no -e "println(:aaaa)"`, String) == "aaaa\n" # 0.7 @@ -1115,8 +1115,17 @@ end using Compat.Random @test rand(MersenneTwister(1234)) == 0.5908446386657102 -# 0.7 -@test contains("Hello, World!", r"World") +# 0.7, make sure this works on 0.6 +if VERSION < v"0.7.0-DEV.3272" + @test contains("Hello, World!", r"World") +end + +# 0.7.0-DEV.4639 +@test occursin(r"World", "Hello, World!") +@test occursin(r"World", "Hello, World!", offset = 4) +@test occursin("World", "Hello, World!") +# 0.7.0-DEV.912 +@test occursin('W', "Hello, World!") # 0.7.0-DEV.3449 let A = [2.0 1.0; 1.0 3.0], b = [1.0, 2.0], x = [0.2, 0.6]