Fix maxsplit to conform to perl

This commit is contained in:
Flaviu Tamas
2015-01-19 07:38:09 -05:00
parent 04699a7587
commit a938d42334
3 changed files with 6 additions and 5 deletions

View File

@@ -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

View File

@@ -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,

View File

@@ -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":