Skip to content

Commit 51e3290

Browse files
committed
Backport eachmatch
1 parent 948e8be commit 51e3290

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

src/strings/regex.jl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,46 @@ function match(re::Regex, str::AnnotatedString, idx::Integer, add_opts::UInt32=U
4242
_annotatedmatch(m, str)
4343
end
4444
end
45+
46+
struct AnnotatedRegexMatchIterator{S <: AnnotatedString}
47+
regex::Regex
48+
string::S
49+
overlap::Bool
50+
end
51+
52+
compile(itr::AnnotatedRegexMatchIterator) = (compile(itr.regex); itr)
53+
eltype(::Type{AnnotatedRegexMatchIterator{S}}) where {S} = AnnotatedRegexMatch{S}
54+
IteratorSize(::Type{<:AnnotatedRegexMatchIterator}) = SizeUnknown()
55+
56+
function iterate(itr::AnnotatedRegexMatchIterator, (offset,prevempty)=(1,false))
57+
opts_nonempty = UInt32(Base.PCRE.ANCHORED | Base.PCRE.NOTEMPTY_ATSTART)
58+
while true
59+
mat = match(itr.regex, itr.string, offset,
60+
prevempty ? opts_nonempty : UInt32(0))
61+
62+
if mat === nothing
63+
if prevempty && offset <= sizeof(itr.string)
64+
offset = nextind(itr.string, offset)
65+
prevempty = false
66+
continue
67+
else
68+
break
69+
end
70+
else
71+
if itr.overlap
72+
if !isempty(mat.match)
73+
offset = nextind(itr.string, mat.offset)
74+
else
75+
offset = mat.offset
76+
end
77+
else
78+
offset = mat.offset + ncodeunits(mat.match)
79+
end
80+
return (mat, (offset, isempty(mat.match)))
81+
end
82+
end
83+
nothing
84+
end
85+
86+
eachmatch(re::Regex, str::AnnotatedString; overlap = false) =
87+
AnnotatedRegexMatchIterator(re, str, overlap)

src/strings/strings.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
module AnnotatedStrings
44

5-
import Base: @propagate_inbounds, AbstractPipe, TTY, String, cmp, codepoint,
6-
codeunit, codeunits, convert, copy, eltype, empty, firstindex, get,
7-
getindex, haskey, in, isvalid, invoke, iterate, join, keys, keys, lastindex,
8-
length, ncodeunits, merge, parse, pipe_reader, pipe_writer, position, print,
9-
promote_rule, read, repeat, reverse, seek, seekend, setindex, show, skip,
10-
truncate, tryparse, write, *, ==
5+
import Base: @propagate_inbounds, AbstractPipe, IteratorSize, TTY, String, cmp,
6+
codepoint, codeunit, codeunits, compile, convert, copy, eachmatch, eltype,
7+
empty, firstindex, get, getindex, haskey, in, isvalid, invoke, iterate,
8+
join, keys, keys, lastindex, length, ncodeunits, match, merge, parse,
9+
pipe_reader, pipe_writer, position, print, promote_rule, read, repeat,
10+
reverse, seek, seekend, setindex, show, skip, truncate, tryparse, write,
11+
*, ==
1112

1213
using ..Compat
1314

0 commit comments

Comments
 (0)