Skip to content

Commit

Permalink
define occursin(needle, haystack), add method for Char needle (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre authored Mar 21, 2018
1 parent 0c7525e commit 76a456b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]).

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
21 changes: 20 additions & 1 deletion src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
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 76a456b

Please sign in to comment.