Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/regex.nim
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,6 @@ template runeIncAt(s: string, n: var int) =
else:
n = s.len+1

template findNextImpl(
s, pattern, m, i, isNext: untyped
): untyped =
matchImpl(s, pattern, m, {mfFindMatch}, i, isNext)

iterator findAll*(
s: string,
pattern: Regex,
Expand All @@ -452,7 +447,7 @@ iterator findAll*(
var i = start
var m: RegexMatch
while i <= len(s):
if not findNextImpl(s, pattern, m, i, i > start):
if not find(s, pattern, m, i):
break
elif m.boundaries.b >= m.boundaries.a:
doAssert i < m.boundaries.b+1
Expand Down Expand Up @@ -502,7 +497,7 @@ iterator split*(s: string, sep: Regex): string {.inline, raises: [].} =
m: RegexMatch
# This is pretty much findAll
while i <= len(s):
if not findNextImpl(s, sep, m, i, true):
if not find(s, sep, m, i):
i = s.len+1
last = s.len+1
elif m.boundaries.b >= m.boundaries.a:
Expand Down Expand Up @@ -544,7 +539,7 @@ func splitIncl*(s: string, sep: Regex): seq[string] {.inline, raises: [].} =
skipFirst = true
m: RegexMatch
while i <= len(s):
if not findNextImpl(s, sep, m, i, true):
if not find(s, sep, m, i):
i = s.len+1
last = s.len+1
elif m.boundaries.b >= m.boundaries.a:
Expand Down
6 changes: 2 additions & 4 deletions src/regex/nfamacro.nim
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,7 @@ func matchImpl*(
regex: static Regex,
m: var RegexMatch,
flags: static MatchFlags,
start = 0,
isContinuation = false
start = 0
): bool {.inline.} =
const eoeNode {.used.} = regex.eoeNode()
# workaround Nim/issues/13252
Expand All @@ -469,8 +468,7 @@ func matchImpl*(
smB = newSubmatches(regex.nfa.len)
smA.add((0'i16, -1'i32, start .. start-1))
when mfFindMatch in flags:
if isContinuation and
0 <= start-1 and start-1 <= len(text)-1:
if 0 <= start-1 and start-1 <= len(text)-1:
cPrev = bwRuneAt(text, start-1).int32
while i < len(text):
fastRuneAt(text, i, c, true)
Expand Down
6 changes: 2 additions & 4 deletions src/regex/nfamatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ func matchImpl*(
regex: Regex,
m: var RegexMatch,
flags: static MatchFlags,
start = 0,
isContinuation = false
start = 0
): bool {.inline.} =
m.clear()
var
Expand All @@ -115,8 +114,7 @@ func matchImpl*(
smB = newSubmatches(regex.nfa.len)
smA.add((0'i16, -1'i32, start .. start-1))
when mfFindMatch in flags:
if isContinuation and
0 <= start-1 and start-1 <= len(text)-1:
if 0 <= start-1 and start-1 <= len(text)-1:
cPrev = bwRuneAt(text, start-1).int32
while i < len(text):
fastRuneAt(text, i, c, true)
Expand Down
23 changes: 14 additions & 9 deletions tests/tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1681,18 +1681,26 @@ test "tmisc2":
bazz//
//"""
check replace(input, re"(?m)$", "//") == expected
# We treat start as text[start..^1], see issue #64
check find("foobarbar", re"^bar", m, start=3)
check find("foobarbar", re"^bar", m, start=3) and
m.boundaries == 3 .. 5
check find("foobar\nbar", re"(?m)^bar", m, start=4) and
check(not find("foobarbar", re"^bar", m, start=3))
check find("foobar\nbar", re"(?m)^bar", m, start=3) and
m.boundaries == 7 .. 9
check find("foo\nbar\nbar", re"(?m)^bar", m, start=3) and
m.boundaries == 4 .. 6
check find("foo\nbar\nbar", re"(?m)^bar", m, start=4) and
m.boundaries == 4 .. 6
block:
# The bounds must contain the empty match index
check find("foo\nbar\nbar", re"(?m)^", m) and
m.boundaries == 0 .. -1
check find("foo\nbar\nbar", re"(?m)^", m, start=1) and
m.boundaries == 1 .. 0
m.boundaries == 4 .. 3
check find("foo\nbar\nbar", re"(?m)^", m, start=4) and
m.boundaries == 4 .. 3
check find("foo\nbar\nbar", re"(?m)^", m, start=5) and
m.boundaries == 8 .. 7
check find("foo\nbar\nbar", re"(?m)^", m, start=8) and
m.boundaries == 8 .. 7
check(not find("foo\nbar\nbar", re"(?m)^", m, start=9))
check find("foo\nbar\nbar", re"(?m)$", m) and
m.boundaries == 3 .. 2
check find("foo\nbar\nbar", re"(?m)$", m, start=3) and
Expand Down Expand Up @@ -1751,6 +1759,3 @@ test "tmisc2":
check split("aaa", re"a") == @["", "", "", ""]
check split("a\na\na", re"(?m)^") == @["a\n", "a\n", "a"]
check split("\n\n", re"(?m)^") == @["\n", "\n"]
# issue #64
check match("xabc", re"^abc$", m, start = 1)
check find("xabc", re"^abc$", m, start = 1)