@@ -212,14 +212,18 @@ julia> hr
212212"11"
213213```
214214"""
215- struct RegexMatch <: AbstractMatch
216- match:: SubString{String }
217- captures:: Vector{Union{Nothing,SubString{String }}}
215+ struct RegexMatch{S <: AbstractString } <: AbstractMatch
216+ match:: SubString{S }
217+ captures:: Vector{Union{Nothing,SubString{S }}}
218218 offset:: Int
219219 offsets:: Vector{Int}
220220 regex:: Regex
221221end
222222
223+ RegexMatch (match:: SubString{S} , captures:: Vector{Union{Nothing,SubString{S}}} ,
224+ offset:: Union{Int, UInt} , offsets:: Vector{Int} , regex:: Regex ) where {S<: AbstractString } =
225+ RegexMatch {S} (match, captures, offset, offsets, regex)
226+
223227"""
224228 keys(m::RegexMatch) -> Vector
225229
@@ -423,9 +427,35 @@ function match(re::Regex, str::Union{SubString{String}, String}, idx::Integer,
423427 return result
424428end
425429
430+ function _annotatedmatch (m:: RegexMatch{S} , str:: AnnotatedString{S} ) where {S<: AbstractString }
431+ RegexMatch {AnnotatedString{S}} (
432+ (@inbounds SubString {AnnotatedString{S}} (
433+ str, m. match. offset, m. match. ncodeunits, Val (:noshift ))),
434+ Union{Nothing,SubString{AnnotatedString{S}}}[
435+ if ! isnothing (cap)
436+ (@inbounds SubString {AnnotatedString{S}} (
437+ str, cap. offset, cap. ncodeunits, Val (:noshift )))
438+ end for cap in m. captures],
439+ m. offset, m. offsets, m. regex)
440+ end
441+
442+ function match (re:: Regex , str:: AnnotatedString )
443+ m = match (re, str. string)
444+ if ! isnothing (m)
445+ _annotatedmatch (m, str)
446+ end
447+ end
448+
449+ function match (re:: Regex , str:: AnnotatedString , idx:: Integer , add_opts:: UInt32 = UInt32 (0 ))
450+ m = match (re, str. string, idx, add_opts)
451+ if ! isnothing (m)
452+ _annotatedmatch (m, str)
453+ end
454+ end
455+
426456match (r:: Regex , s:: AbstractString ) = match (r, s, firstindex (s))
427457match (r:: Regex , s:: AbstractString , i:: Integer ) = throw (ArgumentError (
428- " regex matching is only available for the String type ; use String(s) to convert"
458+ " regex matching is only available for the String and AnnotatedString types ; use String(s) to convert"
429459))
430460
431461findnext (re:: Regex , str:: Union{String,SubString} , idx:: Integer ) = _findnext_re (re, str, idx, C_NULL )
@@ -671,18 +701,19 @@ function _replace(io, repl_s::SubstitutionString, str, r, re)
671701 end
672702end
673703
674- struct RegexMatchIterator
704+ struct RegexMatchIterator{S <: AbstractString }
675705 regex:: Regex
676- string:: String
706+ string:: S
677707 overlap:: Bool
678708
679- function RegexMatchIterator (regex:: Regex , string:: AbstractString , ovr:: Bool = false )
680- new (regex, string, ovr)
681- end
709+ RegexMatchIterator (regex:: Regex , string:: AbstractString , ovr:: Bool = false ) =
710+ new {String} (regex, String (string), ovr)
711+ RegexMatchIterator (regex:: Regex , string:: AnnotatedString , ovr:: Bool = false ) =
712+ new {AnnotatedString{String}} (regex, AnnotatedString (String (string. string), string. annotations), ovr)
682713end
683714compile (itr:: RegexMatchIterator ) = (compile (itr. regex); itr)
684- eltype (:: Type{RegexMatchIterator} ) = RegexMatch
685- IteratorSize (:: Type{RegexMatchIterator} ) = SizeUnknown ()
715+ eltype (:: Type{<: RegexMatchIterator} ) = RegexMatch
716+ IteratorSize (:: Type{<: RegexMatchIterator} ) = SizeUnknown ()
686717
687718function iterate (itr:: RegexMatchIterator , (offset,prevempty)= (1 ,false ))
688719 opts_nonempty = UInt32 (PCRE. ANCHORED | PCRE. NOTEMPTY_ATSTART)
@@ -727,7 +758,7 @@ julia> rx = r"a.a"
727758r"a.a"
728759
729760julia> m = eachmatch(rx, "a1a2a3a")
730- Base.RegexMatchIterator(r"a.a", "a1a2a3a", false)
761+ Base.RegexMatchIterator{String} (r"a.a", "a1a2a3a", false)
731762
732763julia> collect(m)
7337642-element Vector{RegexMatch}:
0 commit comments