Skip to content

Commit

Permalink
findAll fix #13
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed Apr 16, 2020
1 parent e6efb50 commit dea543c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/regex.nim
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,9 @@ when canUseMacro:
): bool {.inline, raises: [].} =
findImpl()

template findOneImpl: untyped {.dirty.} =
matchImpl(s, pattern, m, {mfFindMatch, mfFindAllMatch}, i)

iterator findAll*(
s: string,
pattern: Regex,
Expand All @@ -440,7 +443,7 @@ iterator findAll*(
var c: Rune
var m: RegexMatch
while i < len(s):
if not find(s, pattern, m, i):
if not findOneImpl():
break
if i < m.boundaries.b+1:
i = m.boundaries.b+1
Expand Down
11 changes: 11 additions & 0 deletions src/regex/nfamatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ template findMatch: untyped {.dirty.} =
m.boundaries = smA[0][2]
return true

func bwRuneAt(s: string, n: int): Rune =
## Take rune ending at ``n``
doAssert n > 0
var n = n
while n > 0 and s[n].ord shr 6 == 0b10:
dec n
fastRuneAt(s, n, result, false)

func matchImpl*(
text: string,
regex: Regex,
Expand All @@ -104,6 +112,9 @@ func matchImpl*(
smA = newSubmatches(regex.nfa.len)
smB = newSubmatches(regex.nfa.len)
smA.add((0'i16, -1'i32, start .. start-1))
when mfFindAllMatch in flags:
if start > 0:
cPrev = bwRuneAt(text, start).int32
while i < len(text):
fastRuneAt(text, i, c, true)
when mfShortestMatch in flags:
Expand Down
1 change: 1 addition & 0 deletions src/regex/nfatype.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type
mfShortestMatch
mfNoCaptures
mfFindMatch
mfFindAllMatch
MatchFlags* = set[MatchFlag]
RegexMatch* = object
## result from matching operations
Expand Down

0 comments on commit dea543c

Please sign in to comment.