summary refs log tree commit diff stats
path: root/src/nre.nim
diff options
context:
space:
mode:
authorFlaviu Tamas <tamasflaviu@gmail.com>2015-01-18 12:28:19 -0500
committerFlaviu Tamas <tamasflaviu@gmail.com>2015-01-18 12:28:19 -0500
commit1bcaa2172967f78513cb638bfc958e2e39d9a049 (patch)
tree577c50c8c3a1284ef4ea3e9d8026d48499fae5bf /src/nre.nim
parentff2e3de580e798c4061b8b8a359392974c9091a0 (diff)
downloadNim-1bcaa2172967f78513cb638bfc958e2e39d9a049.tar.gz
Add maxsplit
Diffstat (limited to 'src/nre.nim')
-rw-r--r--src/nre.nim7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/nre.nim b/src/nre.nim
index b66a1f6da..6ba5f560f 100644
--- a/src/nre.nim
+++ b/src/nre.nim
@@ -384,9 +384,10 @@ proc renderBounds(str: string, bounds: Slice[int]): string =
   for i in bounds.a .. bounds.b:
     result.add("^")
 
-proc split*(str: string, pattern: Regex): seq[string] =
+proc split*(str: string, pattern: Regex, maxSplit = -1): seq[string] =
   result = @[]
   var lastIdx = 0
+  var splits = 0
 
   for match in str.findIter(pattern):
     # upper bound is exclusive, lower is inclusive:
@@ -405,6 +406,7 @@ proc split*(str: string, pattern: Regex): seq[string] =
       discard
     else:
       result.add(str.substr(lastIdx, bounds.a - 1))
+      splits += 1
 
     lastIdx = bounds.b
 
@@ -412,6 +414,9 @@ proc split*(str: string, pattern: Regex): seq[string] =
       # if there are captures, include them in the result
       result.add(cap)
 
+    if splits == maxSplit:
+      break
+
   # last match: Each match takes the previous substring,
   # but "1 2".split(/ /) needs to return @["1", "2"].
   # This handles "2"