Skip to content

Make startswith, endswith work with Regex #29790

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

Merged
merged 17 commits into from
Feb 1, 2019
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
1 change: 1 addition & 0 deletions base/pcre.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const COMPILE_MASK =
CASELESS |
DOLLAR_ENDONLY |
DOTALL |
ENDANCHORED |
EXTENDED |
FIRSTLINE |
MULTILINE |
Expand Down
62 changes: 62 additions & 0 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,68 @@ function occursin(r::Regex, s::SubString; offset::Integer=0)
r.match_data)
end

"""
startswith(s::AbstractString, prefix::Regex)

Return `true` if `s` starts with the regex pattern, `prefix`.

!!! note
`startswith` does not compile the anchoring into the regular
expression, but instead passes the anchoring as
`match_option` to PCRE. If compile time is amortized,
`occursin(r"^...", s)` is faster than `startswith(s, r"...")`.

See also [`occursin`](@ref) and [`endswith`](@ref).

# Examples
```jldoctest
julia> startswith("JuliaLang", r"Julia|Romeo")
true
```
"""
function startswith(s::AbstractString, r::Regex)
compile(r)
return PCRE.exec(r.regex, String(s), 0, r.match_options | PCRE.ANCHORED,
r.match_data)
end

function startswith(s::SubString, r::Regex)
compile(r)
return PCRE.exec(r.regex, s, 0, r.match_options | PCRE.ANCHORED,
r.match_data)
end

"""
endswith(s::AbstractString, suffix::Regex)

Return `true` if `s` ends with the regex pattern, `suffix`.

!!! note
`endswith` does not compile the anchoring into the regular
expression, but instead passes the anchoring as
`match_option` to PCRE. If compile time is amortized,
`occursin(r"...\$", s)` is faster than `endswith(s, r"...")`.

See also [`occursin`](@ref) and [`startswith`](@ref).

# Examples
```jldoctest
julia> endswith("JuliaLang", r"Lang|Roberts")
true
```
"""
function endswith(s::AbstractString, r::Regex)
compile(r)
return PCRE.exec(r.regex, String(s), 0, r.match_options | PCRE.ENDANCHORED,
r.match_data)
end

function endswith(s::SubString, r::Regex)
compile(r)
return PCRE.exec(r.regex, s, 0, r.match_options | PCRE.ENDANCHORED,
r.match_data)
end

"""
match(r::Regex, s::AbstractString[, idx::Integer[, addopts]])

Expand Down
12 changes: 12 additions & 0 deletions test/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@
# Regex behaves like a scalar in broadcasting
@test occursin.(r"Hello", ["Hello", "World"]) == [true, false]

@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)

# Test that PCRE throws the correct kind of error
# TODO: Uncomment this once the corresponding change has propagated to CI
#@test_throws ErrorException Base.PCRE.info(C_NULL, Base.PCRE.INFO_NAMECOUNT, UInt32)
Expand Down