Skip to content

Commit c93f3dc

Browse files
fchorneyomus
andauthored
startswith and endswith that work with Regex (#733)
* `startswith` and `endswith` that work with `Regex` * Update README.md Co-authored-by: Curtis Vogt <[email protected]> * Add a few more tests Co-authored-by: Curtis Vogt <[email protected]>
1 parent aca5c27 commit c93f3dc

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Compat"
22
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
3-
version = "3.23.0"
3+
version = "3.24.0"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ without incrementing the major version of Compat.jl if necessary to match
5454
changes in `julia`.
5555

5656
## Supported features
57+
* `startswith(s, r::Regex)` and `endswith(s, r::Regex)` ([#29790]) (since Compat 3.24)
5758

5859
* `sincospi(x)` for calculating the tuple `(sinpi(x), cospi(x))` ([#35816]) (since Compat 3.23)
5960

@@ -230,3 +231,4 @@ Note that you should specify the correct minimum version for `Compat` in the
230231
[#37396]: https://github.com/JuliaLang/julia/pull/37396
231232
[#37391]: https://github.com/JuliaLang/julia/pull/37391
232233
[#35816]: https://github.com/JuliaLang/julia/pull/35816
234+
[#29790]: https://github.com/JuliaLang/julia/pull/29790

src/Compat.jl

+29
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,35 @@ if VERSION < v"1.6.0-DEV.292" # 6cd329c371c1db3d9876bc337e82e274e50420e8
820820
sincospi(x) = (sinpi(x), cospi(x))
821821
end
822822

823+
# https://github.com/JuliaLang/julia/pull/29790
824+
if VERSION < v"1.2.0-DEV.246"
825+
using Base.PCRE
826+
827+
function Base.startswith(s::AbstractString, r::Regex)
828+
Base.compile(r)
829+
return PCRE.exec(
830+
r.regex, String(s), 0, r.match_options | PCRE.ANCHORED, r.match_data
831+
)
832+
end
833+
834+
function Base.startswith(s::SubString, r::Regex)
835+
Base.compile(r)
836+
return PCRE.exec(r.regex, s, 0, r.match_options | PCRE.ANCHORED, r.match_data)
837+
end
838+
839+
function Base.endswith(s::AbstractString, r::Regex)
840+
Base.compile(r)
841+
return PCRE.exec(
842+
r.regex, String(s), 0, r.match_options | PCRE.ENDANCHORED, r.match_data
843+
)
844+
end
845+
846+
function Base.endswith(s::SubString, r::Regex)
847+
Base.compile(r)
848+
return PCRE.exec(r.regex, s, 0, r.match_options | PCRE.ENDANCHORED, r.match_data)
849+
end
850+
end
851+
823852
include("iterators.jl")
824853
include("deprecated.jl")
825854

test/runtests.jl

+23
Original file line numberDiff line numberDiff line change
@@ -802,3 +802,26 @@ import LinearAlgebra
802802
@test ImportRename.hm === LinearAlgebra.BLAS.hemm
803803
@test !isdefined(ImportRename, :hemm)
804804
end
805+
806+
# https://github.com/JuliaLang/julia/pull/29790
807+
@testset "regex startswith and endswith" begin
808+
@test startswith("abc", r"a")
809+
@test startswith("abc", r"ab")
810+
@test endswith("abc", r"c")
811+
@test endswith("abc", r"bc")
812+
@test !startswith("abc", r"b")
813+
@test !startswith("abc", r"c")
814+
@test !startswith("abc", r"bc")
815+
@test !endswith("abc", r"a")
816+
@test !endswith("abc", r"b")
817+
@test !endswith("abc", r"ab")
818+
819+
@test !startswith("abc", r"A")
820+
@test !startswith("abc", r"aB")
821+
@test startswith("abc", r"A"i)
822+
@test startswith("abc", r"aB"i)
823+
@test !endswith("abc", r"C")
824+
@test !endswith("abc", r"Bc")
825+
@test endswith("abc", r"C"i)
826+
@test endswith("abc", r"Bc"i)
827+
end

0 commit comments

Comments
 (0)