Fix maxsplit to conform to perl
This commit is contained in:
parent
04699a7587
commit
a938d42334
3 changed files with 6 additions and 5 deletions
|
|
@ -63,8 +63,9 @@ Perl and Javascript use.
|
||||||
`"123".split(r"") == @["1", "2", "3"]`.
|
`"123".split(r"") == @["1", "2", "3"]`.
|
||||||
- If the pattern has a capture in it, it is added after the string split:
|
- If the pattern has a capture in it, it is added after the string split:
|
||||||
`"12".split(re"(\d)") == @["", "1", "", "2", ""]`.
|
`"12".split(re"(\d)") == @["", "1", "", "2", ""]`.
|
||||||
- If `maxsplit != -1`, then the string will only be split `maxsplit` times.
|
- If `maxsplit != -1`, then the string will only be split `maxsplit - 1`
|
||||||
`"1.2.3".split(re"\.", maxsplit = 1) == @["1", "2.3"]`
|
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]]
|
[[proc-replace]]
|
||||||
==== replace(string, Regex, sub): string
|
==== 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
|
# if there are captures, include them in the result
|
||||||
result.add(cap)
|
result.add(cap)
|
||||||
|
|
||||||
if splits == maxSplit:
|
if splits == maxSplit - 1:
|
||||||
break
|
break
|
||||||
|
|
||||||
# last match: Each match takes the previous substring,
|
# last match: Each match takes the previous substring,
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,8 @@ suite "string splitting":
|
||||||
check("12".split(re"(\d)") == @["", "1", "", "2", ""])
|
check("12".split(re"(\d)") == @["", "1", "", "2", ""])
|
||||||
|
|
||||||
test "maxsplit":
|
test "maxsplit":
|
||||||
check("123".split(re"", maxsplit = 1) == @["1", "23"])
|
check("123".split(re"", maxsplit = 2) == @["1", "23"])
|
||||||
check("123".split(re"", maxsplit = 0) == @["123"])
|
check("123".split(re"", maxsplit = 1) == @["123"])
|
||||||
check("123".split(re"", maxsplit = -1) == @["1", "2", "3"])
|
check("123".split(re"", maxsplit = -1) == @["1", "2", "3"])
|
||||||
|
|
||||||
test "perl split tests":
|
test "perl split tests":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue