From 65c78b317d031cef50ccb2381cec7059250e5817 Mon Sep 17 00:00:00 2001 From: Fernando Chorney Date: Thu, 10 Dec 2020 15:22:21 -0600 Subject: [PATCH 1/3] `startswith` and `endswith` that work with `Regex` --- Project.toml | 2 +- README.md | 2 ++ src/Compat.jl | 29 +++++++++++++++++++++++++++++ test/runtests.jl | 15 +++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 5fcacf7b3..ccf3c5083 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Compat" uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" -version = "3.23.0" +version = "3.24.0" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" diff --git a/README.md b/README.md index 757f75c5a..4dc29bec2 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ without incrementing the major version of Compat.jl if necessary to match changes in `julia`. ## Supported features +* `startswith(s, r)`, and `endswith(s, r)` where `r` is a `regex` ([#29790]) (since Compat 3.24) * `sincospi(x)` for calculating the tuple `(sinpi(x), cospi(x))` ([#35816]) (since Compat 3.23) @@ -230,3 +231,4 @@ Note that you should specify the correct minimum version for `Compat` in the [#37396]: https://github.com/JuliaLang/julia/pull/37396 [#37391]: https://github.com/JuliaLang/julia/pull/37391 [#35816]: https://github.com/JuliaLang/julia/pull/35816 +[#29790]: https://github.com/JuliaLang/julia/pull/29790 diff --git a/src/Compat.jl b/src/Compat.jl index 685ed5160..b240dd047 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -820,6 +820,35 @@ if VERSION < v"1.6.0-DEV.292" # 6cd329c371c1db3d9876bc337e82e274e50420e8 sincospi(x) = (sinpi(x), cospi(x)) end +# https://github.com/JuliaLang/julia/pull/29790 +if VERSION < v"1.2.0-DEV.246" + using Base.PCRE + + function Base.startswith(s::AbstractString, r::Regex) + Base.compile(r) + return PCRE.exec( + r.regex, String(s), 0, r.match_options | PCRE.ANCHORED, r.match_data + ) + end + + function Base.startswith(s::SubString, r::Regex) + Base.compile(r) + return PCRE.exec(r.regex, s, 0, r.match_options | PCRE.ANCHORED, r.match_data) + end + + function Base.endswith(s::AbstractString, r::Regex) + Base.compile(r) + return PCRE.exec( + r.regex, String(s), 0, r.match_options | PCRE.ENDANCHORED, r.match_data + ) + end + + function Base.endswith(s::SubString, r::Regex) + Base.compile(r) + return PCRE.exec(r.regex, s, 0, r.match_options | PCRE.ENDANCHORED, r.match_data) + end +end + include("iterators.jl") include("deprecated.jl") diff --git a/test/runtests.jl b/test/runtests.jl index 0cb206122..72fd7cc1a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -802,3 +802,18 @@ import LinearAlgebra @test ImportRename.hm === LinearAlgebra.BLAS.hemm @test !isdefined(ImportRename, :hemm) end + +# https://github.com/JuliaLang/julia/pull/29790 +@testset "regex startswith and endswith" begin + @test startswith("abc", r"a") + @test endswith("abc", r"c") + @test !startswith("abc", r"b") + @test !startswith("abc", r"c") + @test !endswith("abc", r"a") + @test !endswith("abc", r"b") + + @test !startswith("abc", r"A") + @test startswith("abc", r"A"i) + @test !endswith("abc", r"C") + @test endswith("abc", r"C"i) +end From 1cc774f00182d76d88f24fd4f79a955ebf52271f Mon Sep 17 00:00:00 2001 From: Fernando Chorney Date: Thu, 10 Dec 2020 15:43:44 -0600 Subject: [PATCH 2/3] Update README.md Co-authored-by: Curtis Vogt --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4dc29bec2..f2cba19ac 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ without incrementing the major version of Compat.jl if necessary to match changes in `julia`. ## Supported features -* `startswith(s, r)`, and `endswith(s, r)` where `r` is a `regex` ([#29790]) (since Compat 3.24) +* `startswith(s, r::Regex)` and `endswith(s, r::Regex)` ([#29790]) (since Compat 3.24) * `sincospi(x)` for calculating the tuple `(sinpi(x), cospi(x))` ([#35816]) (since Compat 3.23) From 61a22e26906fb7fa045a39f5188a872ba75f7aba Mon Sep 17 00:00:00 2001 From: Fernando Chorney Date: Thu, 10 Dec 2020 15:49:27 -0600 Subject: [PATCH 3/3] Add a few more tests --- test/runtests.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 72fd7cc1a..b9329e610 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -806,14 +806,22 @@ end # https://github.com/JuliaLang/julia/pull/29790 @testset "regex startswith and endswith" begin @test startswith("abc", r"a") + @test startswith("abc", r"ab") @test endswith("abc", r"c") + @test endswith("abc", r"bc") @test !startswith("abc", r"b") @test !startswith("abc", r"c") + @test !startswith("abc", r"bc") @test !endswith("abc", r"a") @test !endswith("abc", r"b") + @test !endswith("abc", r"ab") @test !startswith("abc", r"A") + @test !startswith("abc", r"aB") @test startswith("abc", r"A"i) + @test startswith("abc", r"aB"i) @test !endswith("abc", r"C") + @test !endswith("abc", r"Bc") @test endswith("abc", r"C"i) + @test endswith("abc", r"Bc"i) end