Skip to content

Commit

Permalink
fix findAll empty match fix issue #29
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed May 7, 2020
1 parent 92ff73c commit 499ead4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
5 changes: 2 additions & 3 deletions src/regex.nim
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,10 @@ iterator findAll*(

var i = start
var m: RegexMatch
while i < len(s):
while i <= len(s):
if not find(s, pattern, m, i):
break
if m.boundaries.b >= m.boundaries.a:
elif m.boundaries.b >= m.boundaries.a:
doAssert i < m.boundaries.b+1
i = m.boundaries.b+1
else: # empty match
Expand Down Expand Up @@ -496,7 +496,6 @@ iterator split*(s: string, sep: Regex): string {.inline, raises: [].} =
skipFirst = true
m: RegexMatch
# This is pretty much findAll
# but with an extra last iteration
while i <= len(s):
if not find(s, sep, m, i):
i = s.len+1
Expand Down
29 changes: 19 additions & 10 deletions tests/tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -945,14 +945,21 @@ test "tfindall":
check findAllBounds("a", re"a") == @[0 .. 0]
check findAllBounds("a", re"b") == newSeq[Slice[int]]()
check findAllBounds("", re"b") == newSeq[Slice[int]]()
check findAllBounds("a", re"") == @[0 .. -1]
check findAllBounds("ab", re"") == @[0 .. -1, 1 .. 0]
check findAllBounds("a", re"\b") == @[0 .. -1]
# This follows nre's empty match behaviour
check findAllBounds("a", re"") == @[0 .. -1, 1 .. 0]
check findAllBounds("ab", re"") == @[0 .. -1, 1 .. 0, 2 .. 1]
check findAllBounds("a", re"\b") == @[0 .. -1, 1 .. 0]
check findAllBounds("aΪⒶ弢", re"Ϊ") == @[1 .. 2]
check findAllBounds("aΪⒶ弢", re"") == @[3 .. 5]
check findAllBounds("aΪⒶ弢", re"弢") == @[6 .. 9]
check findAllBounds("aΪⒶ弢aΪⒶ弢", re"") == @[3 .. 5, 13 .. 15]
check findAllBounds("aaa", re"a*") == @[0 .. 2]
# This is nre and Python's re behaviour,
# they match aaa and then empty end
check findAllBounds("aaa", re"a*") == @[0 .. 2, 3 .. 2]
check findAllBounds("aaab", re"a*") == @[0 .. 2, 3 .. 2, 4 .. 3]
check findAllBounds("aaa", re"a+") == @[0 .. 2]
check findAllBounds("foo", re"") ==
@[0 .. -1, 1 .. 0, 2 .. 1, 3 .. 2]

test "tfindandcaptureall":
check findAndCaptureAll("abcabc", re"bc") == @["bc", "bc"]
Expand Down Expand Up @@ -1125,23 +1132,23 @@ test "treplace":
check "a".replace(re"(a)", "m($1) m($1)") ==
"m(a) m(a)"
check "aaa".replace(re"(a*)", "m($1)") ==
"m(aaa)"
"m(aaa)m()" # nre's behaviour
check "abc".replace(re"(a(b)c)", "m($1) m($2)") ==
"m(abc) m(b)"
check "abc".replace(re"(a(b))(c)", "m($1) m($2) m($3)") ==
"m(ab) m(b) m(c)"
check "abcabc".replace(re"(abc)*", "m($1)") ==
"m(abcabc)"
"m(abcabc)m()" # nre's behaviour
check "abcabc".replace(re"(abc)", "m($1)") ==
"m(abc)m(abc)"
check "abcabc".replace(re"(abc)", "m($1)") ==
"m(abc)m(abc)"
check "abcab".replace(re"(abc)", "m($1)") ==
"m(abc)ab"
check "abcabc".replace(re"((abc)*)", "m($1) m($2)") ==
"m(abcabc) m(abcabc)"
"m(abcabc) m(abcabc)m() m()" # nre's behaviour
check "abcabc".replace(re"((a)bc)*", "m($1) m($2)") ==
"m(abcabc) m(aa)"
"m(abcabc) m(aa)m() m()"
check "abc".replace(re"(b)", "m($1)") == "am(b)c"
check "abc".replace(re"d", "m($1)") == "abc"
check "abc".replace(re"(d)", "m($1)") == "abc"
Expand All @@ -1160,8 +1167,8 @@ test "treplace":
result.add(')')

check "abc".replace(re"(b)", by) == "am(b,)c"
check "aaa".replace(re"(a*)", by) == "m(aaa,)"
check "aaa".replace(re"(a)*", by) == "m(a,a,a,)"
check "aaa".replace(re"(a*)", by) == "m(aaa,)m(,)"
check "aaa".replace(re"(a)*", by) == "m(a,a,a,)m()"

block:
proc removeEvenWords(m: RegexMatch, s: string): string =
Expand Down Expand Up @@ -1646,6 +1653,8 @@ test "tmisc2":
# non-greedy
check find(a, re"__attribute__[ ]*\(\(.*\)\)([ ,;])", m) and
a[m.boundaries] == "__attribute__((__cdecl__)) *)(struct _exception *));"
# issue #29
check replace("foo", re"", "-") == "-f-o-o-"
block: # issue #13
const input = """foo
bar
Expand Down

0 comments on commit 499ead4

Please sign in to comment.