diff options
author | Flaviu Tamas <tamasflaviu@gmail.com> | 2015-01-19 07:38:09 -0500 |
---|---|---|
committer | Flaviu Tamas <tamasflaviu@gmail.com> | 2015-01-19 07:38:09 -0500 |
commit | a938d42334822b78d4f84417d066f0f059dcf8ec (patch) | |
tree | 4a52fcf3c4d82751c040bc8099d8ecf82ae04669 | |
parent | 04699a7587dc83d37bd5e1618409cac11dc04f75 (diff) | |
download | Nim-a938d42334822b78d4f84417d066f0f059dcf8ec.tar.gz |
Fix maxsplit to conform to perl
-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": |