Skip to content

Commit c5a8488

Browse files
committed
define occursin(needle, haystack), add method for Char needle
1 parent 0c7525e commit c5a8488

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ Currently, the `@compat` macro supports the following syntaxes:
285285

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

288+
* `occursin(needle, haystack::AbstractString)` now has a method for `Char` needles ([#22435], [#26283]).
289+
288290
## Renaming
289291

290292
* `Display` is now `AbstractDisplay` ([#24831]).
@@ -338,7 +340,10 @@ Currently, the `@compat` macro supports the following syntaxes:
338340

339341
* `copy!` and `unsafe_copy!` are now `copyto!` and `unsafe_copyto!` ([#24808]).
340342

341-
* `ismatch(r::Regex, str::AbstractString)` is now `contains(str, r)` ([#24673]).
343+
* `contains(haystack, needle)` is now `occursin(needle, haystack)` ([#26283]).
344+
345+
* `ismatch(r::Regex, str::AbstractString, offset=0)` is now `occursin(r, str)` and
346+
`occursin(r, str, offset = offset)` respectively ([#24673],[#26283]).
342347

343348
* `ipermute!` is now `invpermute!` ([#25168]).
344349

@@ -514,6 +519,7 @@ includes this fix. Find the minimum version from there.
514519
[#22064]: https://github.com/JuliaLang/julia/issues/22064
515520
[#22182]: https://github.com/JuliaLang/julia/issues/22182
516521
[#22350]: https://github.com/JuliaLang/julia/issues/22350
522+
[#22435]: https://github.com/JuliaLang/julia/issues/22435
517523
[#22475]: https://github.com/JuliaLang/julia/issues/22475
518524
[#22512]: https://github.com/JuliaLang/julia/issues/22512
519525
[#22629]: https://github.com/JuliaLang/julia/issues/22629
@@ -598,6 +604,7 @@ includes this fix. Find the minimum version from there.
598604
[#26089]: https://github.com/JuliaLang/julia/issues/26089
599605
[#26149]: https://github.com/JuliaLang/julia/issues/26149
600606
[#26156]: https://github.com/JuliaLang/julia/issues/26156
607+
[#26283]: https://github.com/JuliaLang/julia/issues/26283
601608
[#26316]: https://github.com/JuliaLang/julia/issues/26316
602609
[#26436]: https://github.com/JuliaLang/julia/issues/26436
603610
[#26442]: https://github.com/JuliaLang/julia/issues/26442

src/Compat.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,22 @@ end
901901
occursin(x) = in(x)
902902
end
903903

904+
# PR#26283
905+
@static if VERSION < v"0.7.0-DEV.4639"
906+
Base.occursin(needle, haystack) = contains(haystack, needle)
907+
end
908+
@static if VERSION < v"0.7.0-DEV.4639"
909+
@static if VERSION < v"0.7.0-DEV.3272"
910+
Base.occursin(r::Regex, s::AbstractString; offset::Integer = 0) = ismatch(r, s, offset)
911+
else
912+
Base.occursin(r::Regex, s::AbstractString; offset::Integer = 0) = contains(s, r, offset)
913+
end
914+
end
915+
916+
# PR#22435
917+
@static if VERSION < v"0.7.0-DEV.702"
918+
Base.contains(haystack::AbstractString, needle::Char) = searchindex(haystack,needle) != 0
919+
end
904920

905921
# 0.7.0-DEV.912
906922
if VERSION < v"0.7.0-DEV.912"
@@ -1171,7 +1187,7 @@ end
11711187
export copyto!, unsafe_copyto!
11721188
end
11731189

1174-
# 0.7.0-DEV.3272
1190+
# 0.7.0-DEV.3272, keep this definition for 0.6 compatibility
11751191
@static if VERSION < v"0.7.0-DEV.3272"
11761192
Base.contains(str::AbstractString, r::Regex) = ismatch(r, str)
11771193
end

test/runtests.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ let filename = tempname()
185185
end
186186
end
187187
@test ret == [2]
188-
@test contains(read(filename, String), "WARNING: hello")
188+
@test occursin("WARNING: hello", read(filename, String))
189189
ret = open(filename) do f
190190
redirect_stdin(f) do
191191
readline()
192192
end
193193
end
194-
@test contains(ret, "WARNING: hello")
194+
@test occursin("WARNING: hello", ret)
195195
rm(filename)
196196
end
197197

@@ -837,7 +837,7 @@ no_specialize_kw2(@nospecialize(x::Integer=0)) = sin(2)
837837

838838
# 0.7
839839
@test read(IOBuffer("aaaa"), String) == "aaaa"
840-
@test contains(read(@__FILE__, String), "read(@__FILE__, String)")
840+
@test occursin("read(@__FILE__, String)", read(@__FILE__, String))
841841
@test read(`$(Base.julia_cmd()) --startup-file=no -e "println(:aaaa)"`, String) == "aaaa\n"
842842

843843
# 0.7
@@ -1115,8 +1115,17 @@ end
11151115
using Compat.Random
11161116
@test rand(MersenneTwister(1234)) == 0.5908446386657102
11171117

1118-
# 0.7
1119-
@test contains("Hello, World!", r"World")
1118+
# 0.7, make sure this works on 0.6
1119+
if VERSION < v"0.7.0-DEV.3272"
1120+
@test contains("Hello, World!", r"World")
1121+
end
1122+
1123+
# 0.7.0-DEV.4639
1124+
@test occursin(r"World", "Hello, World!")
1125+
@test occursin(r"World", "Hello, World!", offset = 4)
1126+
@test occursin("World", "Hello, World!")
1127+
# 0.7.0-DEV.912
1128+
@test occursin('W', "Hello, World!")
11201129

11211130
# 0.7.0-DEV.3449
11221131
let A = [2.0 1.0; 1.0 3.0], b = [1.0, 2.0], x = [0.2, 0.6]

0 commit comments

Comments
 (0)