summary refs log tree commit diff stats
path: root/lib/impure/re.nim
diff options
context:
space:
mode:
authorJoseph Turner <turner.d.joseph@gmail.com>2015-04-08 16:35:55 +0100
committerJoseph Turner <turner.d.joseph@gmail.com>2015-04-08 17:00:28 +0100
commit13e5a9ea6ca4eee1d2ff0e7fc9b3c1ceff7a31b4 (patch)
treefc739bd83ff2b2e724ec565eab068b08652e21a8 /lib/impure/re.nim
parentb48f9c4e1440b28fedddec4833553e8f60648ec4 (diff)
downloadNim-13e5a9ea6ca4eee1d2ff0e7fc9b3c1ceff7a31b4.tar.gz
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.
Diffstat (limited to 'lib/impure/re.nim')
-rw-r--r--lib/impure/re.nim13
1 files changed, 8 insertions, 5 deletions
diff --git a/lib/impure/re.nim b/lib/impure/re.nim
index c24734f89..93dc4922d 100644
--- a/lib/impure/re.nim
+++ b/lib/impure/re.nim
@@ -373,23 +373,26 @@ iterator split*(s: string, sep: Regex): string =
   ## Results in:
   ##
   ## .. code-block:: nim
+  ##   ""
   ##   "this"
   ##   "is"
   ##   "an"
   ##   "example"
+  ##   ""
   ##
   var
-    first = 0
-    last = 0
+    first = -1
+    last = -1
   while last < len(s):
     var x = matchLen(s, sep, last)
     if x > 0: inc(last, x)
     first = last
+    if x == 0: inc(last)
     while last < len(s):
-      inc(last)
       x = matchLen(s, sep, last)
-      if x > 0: break
-    if first < last:
+      if x >= 0: break
+      inc(last)
+    if first <= last:
       yield substr(s, first, last-1)
 
 proc split*(s: string, sep: Regex): seq[string] =