summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorCarlo Capocasa <carlo@capocasa.net>2021-12-13 07:29:22 +0100
committerGitHub <noreply@github.com>2021-12-13 07:29:22 +0100
commit0ff4b2ba7ee74ed2758b0eb5992e4ccab7433952 (patch)
treeead0619e45e2af6d33dbfb9b70b20a590b29aba1 /lib
parent4b5cecd902cc4126ff9d6cda9edb78a13a421239 (diff)
downloadNim-0ff4b2ba7ee74ed2758b0eb5992e4ccab7433952.tar.gz
fix bug #14468 zero-width split (#19248)
Diffstat (limited to 'lib')
-rw-r--r--lib/impure/re.nim11
1 files changed, 7 insertions, 4 deletions
diff --git a/lib/impure/re.nim b/lib/impure/re.nim
index 8e5efbb28..4eacf0091 100644
--- a/lib/impure/re.nim
+++ b/lib/impure/re.nim
@@ -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