summary refs log tree commit diff stats
path: root/lib/impure/re.nim
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2015-04-09 16:37:47 +0200
committerAndreas Rumpf <rumpf_a@web.de>2015-04-09 16:37:47 +0200
commit15b5f52e8c180bd2df8d1fae5d29eaff6459a082 (patch)
treef01857483e7267185ba57f6f3ae7fe5d066f08ef /lib/impure/re.nim
parenta2d3dff6909eb1a0a7f4639b6f8d09e19342c4bf (diff)
parent54d945c5123c2d6856216c4dc03abdf289e096da (diff)
downloadNim-15b5f52e8c180bd2df8d1fae5d29eaff6459a082.tar.gz
Merge pull request #2494 from JosephTurner/fix-matching-error
Fixes matching error #2418
Diffstat (limited to 'lib/impure/re.nim')
-rw-r--r--lib/impure/re.nim20
1 files changed, 14 insertions, 6 deletions
diff --git a/lib/impure/re.nim b/lib/impure/re.nim
index c24734f89..ff2b70d2d 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] =
@@ -471,7 +474,12 @@ when isMainModule:
   var accum: seq[string] = @[]
   for word in split("00232this02939is39an22example111", re"\d+"):
     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):
     assert x == "d"