Merge pull request #2494 from JosephTurner/fix-matching-error

Fixes matching error #2418
This commit is contained in:
Andreas Rumpf 2015-04-09 16:37:47 +02:00
commit 15b5f52e8c
2 changed files with 19 additions and 7 deletions

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] =
@ -471,7 +474,12 @@ when isMainModule:
var accum: seq[string] = @[] var accum: seq[string] = @[]
for word in split("00232this02939is39an22example111", re"\d+"): for word in split("00232this02939is39an22example111", re"\d+"):
accum.add(word) accum.add(word)
assert(accum == @["this", "is", "an", "example"]) assert(accum == @["", "this", "is", "an", "example", ""])
accum = @[]
for word in split("AAA : : BBB", re"\s*:\s*"):
accum.add(word)
assert(accum == @["AAA", "", "BBB"])
for x in findAll("abcdef", re"^{.}", 3): for x in findAll("abcdef", re"^{.}", 3):
assert x == "d" assert x == "d"

View file

@ -73,7 +73,11 @@ News
for ``expr`` and ``stmt``. The new names capture the semantics much better for ``expr`` and ``stmt``. The new names capture the semantics much better
and most likely ``expr`` and ``stmt`` will be deprecated in favor of the and most likely ``expr`` and ``stmt`` will be deprecated in favor of the
new names. new names.
- The ``split`` method in module ``re`` has changed. It now handles the case
of matches having a length of 0, and empty strings being yielded from the
iterator. A notable change might be that a pattern being matched at the
beginning and end of a string, will result in an empty string being produced
at the start and the end of the iterator.
Language Additions Language Additions
------------------ ------------------