summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2018-11-11 16:39:49 +0000
committerGitHub <noreply@github.com>2018-11-11 16:39:49 +0000
commitd2f7ecb80e8672f404fa59c9eb2249ffe72f5645 (patch)
tree77da51613ce95f857e244544baf38c9840881e57
parent030fe373e4d43e33d88439da987da607e850796c (diff)
parent8b4a910c91908235e35e5fbc46f0a45df8eb459a (diff)
downloadNim-d2f7ecb80e8672f404fa59c9eb2249ffe72f5645.tar.gz
Merge pull request #9682 from cyberlis/reprocsplitfix
Fix split proc in re module ignored maxsplit argument.
-rw-r--r--lib/impure/re.nim9
1 files changed, 8 insertions, 1 deletions
diff --git a/lib/impure/re.nim b/lib/impure/re.nim
index b142f58cd..dc4ee326f 100644
--- a/lib/impure/re.nim
+++ b/lib/impure/re.nim
@@ -547,7 +547,7 @@ proc split*(s: string, sep: Regex, maxsplit = -1): seq[string] {.inline.} =
   ##
   ## The portion matched by ``sep`` is not returned.
   result = @[]
-  for x in split(s, sep): result.add x
+  for x in split(s, sep, maxsplit): result.add x
 
 proc escapeRe*(s: string): string =
   ## escapes ``s`` so that it is matched verbatim when used as a regular
@@ -636,6 +636,11 @@ when isMainModule:
   doAssert(accum == @["", "this", "is", "an", "example", ""])
 
   accum = @[]
+  for word in split("00232this02939is39an22example111", re"\d+", maxsplit=2):
+    accum.add(word)
+  doAssert(accum == @["", "this", "is39an22example111"])
+
+  accum = @[]
   for word in split("AAA :   : BBB", re"\s*:\s*"):
     accum.add(word)
   doAssert(accum == @["AAA", "", "BBB"])
@@ -647,6 +652,8 @@ when isMainModule:
   doAssert(split(";a;b;c", re";") == @["", "a", "b", "c"])
   doAssert(split(";a;b;c;", re";") == @["", "a", "b", "c", ""])
   doAssert(split("a;b;c;", re";") == @["a", "b", "c", ""])
+  doAssert(split("00232this02939is39an22example111", re"\d+", maxsplit=2) == @["", "this", "is39an22example111"])
+
 
   for x in findAll("abcdef", re"^{.}", 3):
     doAssert x == "d"