Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug #14468 zero-width split #19248

Merged
merged 1 commit into from
Dec 13, 2021
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: 7 additions & 4 deletions lib/impure/re.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions tests/stdlib/tre.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()