diff options
-rw-r--r-- | README.asciidoc | 5 | ||||
-rw-r--r-- | src/nre.nim | 2 | ||||
-rw-r--r-- | test/split.nim | 4 |
3 files changed, 6 insertions, 5 deletions
diff --git a/README.asciidoc b/README.asciidoc index dd7b78dc7..51f616b4e 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -63,8 +63,9 @@ Perl and Javascript use. `"123".split(r"") == @["1", "2", "3"]`. - If the pattern has a capture in it, it is added after the string split: `"12".split(re"(\d)") == @["", "1", "", "2", ""]`. - - If `maxsplit != -1`, then the string will only be split `maxsplit` times. - `"1.2.3".split(re"\.", maxsplit = 1) == @["1", "2.3"]` + - If `maxsplit != -1`, then the string will only be split `maxsplit - 1` + times. This means that there will be `maxsplit` strings in the output seq. + `"1.2.3".split(re"\.", maxsplit = 2) == @["1", "2.3"]` [[proc-replace]] ==== replace(string, Regex, sub): string diff --git a/src/nre.nim b/src/nre.nim index d907cae62..9e1a6a64e 100644 --- a/src/nre.nim +++ b/src/nre.nim @@ -417,7 +417,7 @@ proc split*(str: string, pattern: Regex, maxSplit = -1): seq[string] = # if there are captures, include them in the result result.add(cap) - if splits == maxSplit: + if splits == maxSplit - 1: break # last match: Each match takes the previous substring, diff --git a/test/split.nim b/test/split.nim index 9f9ebef36..6cd67df0a 100644 --- a/test/split.nim +++ b/test/split.nim @@ -13,8 +13,8 @@ suite "string splitting": check("12".split(re"(\d)") == @["", "1", "", "2", ""]) test "maxsplit": - check("123".split(re"", maxsplit = 1) == @["1", "23"]) - check("123".split(re"", maxsplit = 0) == @["123"]) + check("123".split(re"", maxsplit = 2) == @["1", "23"]) + check("123".split(re"", maxsplit = 1) == @["123"]) check("123".split(re"", maxsplit = -1) == @["1", "2", "3"]) test "perl split tests": |