Skip to content

Commit

Permalink
define occursin(needle, haystack), add method for Char needle
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Mar 20, 2018
1 parent 0c7525e commit 7d779f9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ Currently, the `@compat` macro supports the following syntaxes:

* `Compat.mv` and `Compat.cp` with `force` keyword argument ([#26069]).

* `occursin(needle, haystack::AbstractString)` now has a method for `Char` needles ([#22435], [#26283]).

## Renaming

* `Display` is now `AbstractDisplay` ([#24831]).
Expand Down Expand Up @@ -338,7 +340,10 @@ 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]).

* `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]).

Expand Down Expand Up @@ -514,6 +519,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
Expand Down Expand Up @@ -598,6 +604,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
18 changes: 17 additions & 1 deletion src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,22 @@ end
occursin(x) = in(x)
end

# PR#26283
@static if VERSION < v"0.7.0-DEV.4639"
occursin(needle, haystack) = contains(haystack, needle)
end
@static if VERSION < v"0.7.0-DEV.4639"
@static 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
end

# PR#22435
@static if VERSION < v"0.7.0-DEV.702"
Base.contains(haystack::AbstractString, needle::Char) = searchindex(haystack,needle) != 0
end

# 0.7.0-DEV.912
if VERSION < v"0.7.0-DEV.912"
Expand Down Expand Up @@ -1171,7 +1187,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
Expand Down
19 changes: 14 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 7d779f9

Please sign in to comment.