Merge pull request #9682 from cyberlis/reprocsplitfix
Fix split proc in re module ignored maxsplit argument.
This commit is contained in:
commit
d2f7ecb80e
1 changed files with 8 additions and 1 deletions
|
|
@ -547,7 +547,7 @@ proc split*(s: string, sep: Regex, maxsplit = -1): seq[string] {.inline.} =
|
||||||
##
|
##
|
||||||
## The portion matched by ``sep`` is not returned.
|
## The portion matched by ``sep`` is not returned.
|
||||||
result = @[]
|
result = @[]
|
||||||
for x in split(s, sep): result.add x
|
for x in split(s, sep, maxsplit): result.add x
|
||||||
|
|
||||||
proc escapeRe*(s: string): string =
|
proc escapeRe*(s: string): string =
|
||||||
## escapes ``s`` so that it is matched verbatim when used as a regular
|
## escapes ``s`` so that it is matched verbatim when used as a regular
|
||||||
|
|
@ -635,6 +635,11 @@ when isMainModule:
|
||||||
accum.add(word)
|
accum.add(word)
|
||||||
doAssert(accum == @["", "this", "is", "an", "example", ""])
|
doAssert(accum == @["", "this", "is", "an", "example", ""])
|
||||||
|
|
||||||
|
accum = @[]
|
||||||
|
for word in split("00232this02939is39an22example111", re"\d+", maxsplit=2):
|
||||||
|
accum.add(word)
|
||||||
|
doAssert(accum == @["", "this", "is39an22example111"])
|
||||||
|
|
||||||
accum = @[]
|
accum = @[]
|
||||||
for word in split("AAA : : BBB", re"\s*:\s*"):
|
for word in split("AAA : : BBB", re"\s*:\s*"):
|
||||||
accum.add(word)
|
accum.add(word)
|
||||||
|
|
@ -647,6 +652,8 @@ when isMainModule:
|
||||||
doAssert(split(";a;b;c", re";") == @["", "a", "b", "c"])
|
doAssert(split(";a;b;c", re";") == @["", "a", "b", "c"])
|
||||||
doAssert(split(";a;b;c;", re";") == @["", "a", "b", "c", ""])
|
doAssert(split(";a;b;c;", re";") == @["", "a", "b", "c", ""])
|
||||||
doAssert(split("a;b;c;", re";") == @["a", "b", "c", ""])
|
doAssert(split("a;b;c;", re";") == @["a", "b", "c", ""])
|
||||||
|
doAssert(split("00232this02939is39an22example111", re"\d+", maxsplit=2) == @["", "this", "is39an22example111"])
|
||||||
|
|
||||||
|
|
||||||
for x in findAll("abcdef", re"^{.}", 3):
|
for x in findAll("abcdef", re"^{.}", 3):
|
||||||
doAssert x == "d"
|
doAssert x == "d"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue