Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

startswith and endswith that work with Regex #733

Merged
merged 3 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ without incrementing the major version of Compat.jl if necessary to match
changes in `julia`.

## Supported features
* `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)

Expand Down Expand Up @@ -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
29 changes: 29 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
23 changes: 23 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -802,3 +802,26 @@ 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
fchorney marked this conversation as resolved.
Show resolved Hide resolved
@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