mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-16 16:14:20 +00:00
Add maxsplit
This commit is contained in:
@@ -54,7 +54,7 @@ Variants:
|
||||
- `proc findAllStr(...)` returns a `seq[string]`
|
||||
|
||||
[[proc-split]]
|
||||
==== split(string, Regex): seq[string]
|
||||
==== split(string, Regex, maxsplit = -1): seq[string]
|
||||
|
||||
Splits the string with the given regex. This works according to the rules that
|
||||
Perl and Javascript use.
|
||||
@@ -63,6 +63,8 @@ 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.
|
||||
`"123".split(re"", maxsplit = 1) == @["1", "23"]`
|
||||
|
||||
[[proc-replace]]
|
||||
==== replace(string, Regex, sub): string
|
||||
|
||||
@@ -384,9 +384,10 @@ proc renderBounds(str: string, bounds: Slice[int]): string =
|
||||
for i in bounds.a .. bounds.b:
|
||||
result.add("^")
|
||||
|
||||
proc split*(str: string, pattern: Regex): seq[string] =
|
||||
proc split*(str: string, pattern: Regex, maxSplit = -1): seq[string] =
|
||||
result = @[]
|
||||
var lastIdx = 0
|
||||
var splits = 0
|
||||
|
||||
for match in str.findIter(pattern):
|
||||
# upper bound is exclusive, lower is inclusive:
|
||||
@@ -405,6 +406,7 @@ proc split*(str: string, pattern: Regex): seq[string] =
|
||||
discard
|
||||
else:
|
||||
result.add(str.substr(lastIdx, bounds.a - 1))
|
||||
splits += 1
|
||||
|
||||
lastIdx = bounds.b
|
||||
|
||||
@@ -412,6 +414,9 @@ proc split*(str: string, pattern: Regex): seq[string] =
|
||||
# if there are captures, include them in the result
|
||||
result.add(cap)
|
||||
|
||||
if splits == maxSplit:
|
||||
break
|
||||
|
||||
# last match: Each match takes the previous substring,
|
||||
# but "1 2".split(/ /) needs to return @["1", "2"].
|
||||
# This handles "2"
|
||||
|
||||
@@ -11,3 +11,8 @@ suite "string splitting":
|
||||
|
||||
test "captured patterns":
|
||||
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 = -1) == @["1", "2", "3"])
|
||||
|
||||
Reference in New Issue
Block a user