Fixes matching error #2418

Fixes the split iterator, the main problem was with the incrementation
of 'last'. Last was first incremented to the index of the first
character after the match, but was then incremented again at the
beginning of the while loop. This caused a problem if that character
after the first match, also matched the regular expression.
This commit is contained in:
Joseph Turner 2015-04-08 16:35:55 +01:00
commit 13e5a9ea6c

View file

@ -373,23 +373,26 @@ iterator split*(s: string, sep: Regex): string =
## Results in: ## Results in:
## ##
## .. code-block:: nim ## .. code-block:: nim
## ""
## "this" ## "this"
## "is" ## "is"
## "an" ## "an"
## "example" ## "example"
## ""
## ##
var var
first = 0 first = -1
last = 0 last = -1
while last < len(s): while last < len(s):
var x = matchLen(s, sep, last) var x = matchLen(s, sep, last)
if x > 0: inc(last, x) if x > 0: inc(last, x)
first = last first = last
if x == 0: inc(last)
while last < len(s): while last < len(s):
inc(last)
x = matchLen(s, sep, last) x = matchLen(s, sep, last)
if x > 0: break if x >= 0: break
if first < last: inc(last)
if first <= last:
yield substr(s, first, last-1) yield substr(s, first, last-1)
proc split*(s: string, sep: Regex): seq[string] = proc split*(s: string, sep: Regex): seq[string] =