Skip to content

Commit f997650

Browse files
authored
Bugfix: Allow regex captures of substrings of stringviews (#27)
A substring of `SubString{T}` is not `SubString{SubString{T}}`, which the previous code assumed, but just `SubString{T}`.
1 parent 6f9020c commit f997650

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "StringViews"
22
uuid = "354b36f9-a18e-4713-926e-db85100087ba"
33
authors = ["Steven G. Johnson <[email protected]>"]
4-
version = "1.3.3"
4+
version = "1.3.4"
55

66
[compat]
77
julia = "1.6"

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+
strv = only(match(r"^([a-z]+)$", SubString(StringView((b"abc")))))
102+
@test typeof(strv) == SubString{StringView{typeof(b"abc")}}
99103
end
100104

101105
@testset "named subpatterns" begin

0 commit comments

Comments
 (0)