Skip to content

Commit b0524de

Browse files
committed
Bugfix: Allow regex captures of substrings of stringviews
A substring of `SubString{T}` is not `SubString{SubString{T}}`, which the previous code assumed, but just `SubString{T}`.
1 parent 292a9cc commit b0524de

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/regex.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ function Base.match(re::Regex, str::T, idx::Integer, add_opts::UInt32=UInt32(0))
2828
end
2929
n = div(PCRE.ovec_length(data), 2) - 1
3030
p = PCRE.ovec_ptr(data)
31-
mat = SubString(str, unsafe_load(p, 1)+1, prevind(str, unsafe_load(p, 2)+1))
32-
cap = Union{Nothing,SubString{T}}[unsafe_load(p,2i+1) == PCRE.UNSET ? nothing :
31+
SS = T <: SubString ? T : SubString{T}
32+
mat = SubString(str, unsafe_load(p, 1)+1, prevind(str, unsafe_load(p, 2)+1))::SS
33+
cap = Union{Nothing,SS}[unsafe_load(p,2i+1) == PCRE.UNSET ? nothing :
3334
SubString(str, unsafe_load(p,2i+1)+1,
3435
prevind(str, unsafe_load(p,2i+2)+1)) for i=1:n]
3536
off = Int[ unsafe_load(p,2i+1)+1 for i=1:n ]
@@ -192,4 +193,4 @@ end
192193

193194
Base.iterate(m::SVRegexMatch, args...) = iterate(m.captures, args...)
194195
Base.length(m::SVRegexMatch) = length(m.captures)
195-
Base.eltype(m::SVRegexMatch) = eltype(m.captures)
196+
Base.eltype(m::SVRegexMatch) = eltype(m.captures)

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ end
9696
sv = StringView(codeunits("foo 1234 bar"))
9797
@test match(r"[0-9]+", sv).match.string === sv
9898
@test eltype(eachmatch(r"[0-9]+", sv)) == SVRegexMatch{typeof(sv)}
99+
100+
# Regex match of substring of stringview
101+
s = only(match(r"^([a-z]+)$", SubString(StringView((b"abc")))))
102+
@test typeof(s) == SubString{StringView{typeof(b"abc")}}
99103
end
100104

101105
@testset "named subpatterns" begin

0 commit comments

Comments
 (0)