diff options
author | Carlo Capocasa <carlo@capocasa.net> | 2021-12-13 07:29:22 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-13 07:29:22 +0100 |
commit | 0ff4b2ba7ee74ed2758b0eb5992e4ccab7433952 (patch) | |
tree | ead0619e45e2af6d33dbfb9b70b20a590b29aba1 | |
parent | 4b5cecd902cc4126ff9d6cda9edb78a13a421239 (diff) | |
download | Nim-0ff4b2ba7ee74ed2758b0eb5992e4ccab7433952.tar.gz |
fix bug #14468 zero-width split (#19248)
-rw-r--r-- | lib/impure/re.nim | 11 | ||||
-rw-r--r-- | tests/stdlib/tre.nim | 6 |
2 files changed, 13 insertions, 4 deletions
diff --git a/lib/impure/re.nim b/lib/impure/re.nim index 8e5efbb28..4eacf0091 100644 --- a/lib/impure/re.nim +++ b/lib/impure/re.nim @@ -556,19 +556,22 @@ iterator split*(s: string, sep: Regex; maxsplit = -1): string = @["", "this", "is", "an", "example", ""] var last = 0 var splits = maxsplit - var x: int + var x = -1 + if len(s) == 0: + last = 1 + if matchLen(s, sep, 0) == 0: + x = 0 while last <= len(s): var first = last var sepLen = 1 + if x == 0: + inc(last) while last < len(s): x = matchLen(s, sep, last) if x >= 0: sepLen = x break inc(last) - if x == 0: - if last >= len(s): break - inc last if splits == 0: last = len(s) yield substr(s, first, last-1) if splits == 0: break diff --git a/tests/stdlib/tre.nim b/tests/stdlib/tre.nim index 2857c6c9e..9f27f7db2 100644 --- a/tests/stdlib/tre.nim +++ b/tests/stdlib/tre.nim @@ -108,4 +108,10 @@ proc testAll() = doAssert replace("foo", re"", "-") == "-f-o-o-" doAssert replace("ooo", re"o", "-") == "---" + block: # bug #14468 + accum = @[] + for word in split("this is an example", re"\b"): + accum.add(word) + doAssert(accum == @["this", " ", "is", " ", "an", " ", "example"]) + testAll() |