From bc4f6354a9901266acaf63ddb64a438ac2ba2a32 Mon Sep 17 00:00:00 2001 From: Esteban C Borsani Date: Mon, 18 May 2020 10:50:23 -0300 Subject: [PATCH] Revert " treat start as text[start..^1], fixes #64 (#66)" This reverts commit 929fb32bf459f31f8deeaa8ffe3e6dbbf2b39667. --- src/regex.nim | 11 +++-------- src/regex/nfamacro.nim | 6 ++---- src/regex/nfamatch.nim | 6 ++---- tests/tests.nim | 23 ++++++++++++++--------- 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/regex.nim b/src/regex.nim index 3af49ce..b5991b1 100644 --- a/src/regex.nim +++ b/src/regex.nim @@ -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, @@ -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 @@ -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: @@ -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: diff --git a/src/regex/nfamacro.nim b/src/regex/nfamacro.nim index ed7d690..dc25990 100644 --- a/src/regex/nfamacro.nim +++ b/src/regex/nfamacro.nim @@ -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 @@ -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) diff --git a/src/regex/nfamatch.nim b/src/regex/nfamatch.nim index 808e1c3..23c5537 100644 --- a/src/regex/nfamatch.nim +++ b/src/regex/nfamatch.nim @@ -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 @@ -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) diff --git a/tests/tests.nim b/tests/tests.nim index 0b2b0ba..363f88e 100644 --- a/tests/tests.nim +++ b/tests/tests.nim @@ -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 @@ -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)