mirror of
https://github.com/nim-lang/Nim.git
synced 2026-05-23 13:19:55 +00:00
Fix maxsplit to conform to perl
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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":
|
||||
|
||||
Reference in New Issue
Block a user