strutils.split/rsplit now return src on an empty sep (#22136)

This is a rebase of an earlier rejected PR. Following the discussion
around it, this commit provides a valid output for and edge case
of an empty separator for `split` and `rsplit` routines. The empty
separator is interpreted as "split by no separators" and the initial
string is returned. This is consistent with the behaviour of the
`set[char]` version of `split`/`rsplit` routines and unifies them all.

Compared to a commit merged earlier, this one has a benefit of
not using assertions that will be removed in release builds
and thus still not preventing possible infinite loops (which was the
earlier behaviour for this edge case for separator of type `string`).

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
(cherry picked from commit 5e529b3bfa)
This commit is contained in:
Zoom
2023-06-21 06:52:33 +00:00
committed by narimiran
parent 9d573f4a1d
commit 6863b5d289
4 changed files with 59 additions and 22 deletions

View File

@@ -26,7 +26,6 @@ Hi Andreas! How do you feel, Rumpf?
[2, 3, 4, 5]
[2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
true
<h1><a href="http://force7.de/nim">Nim</a></h1>
'''
"""
@@ -206,11 +205,7 @@ block tsplit2:
s.add("#")
s.add(w)
try:
discard "hello".split("")
echo "false"
except AssertionDefect:
echo "true"
doAssert "true".split("") == @["true"]

View File

@@ -51,6 +51,15 @@ template main() =
doAssert s.split(maxsplit = 4) == @["", "this", "is", "an", "example "]
doAssert s.split(' ', maxsplit = 1) == @["", "this is an example "]
doAssert s.split(" ", maxsplit = 4) == @["", "this", "is", "an", "example "]
# Empty string:
doAssert "".split() == @[""]
doAssert "".split(" ") == @[""]
doAssert "".split({' '}) == @[""]
# Empty separators:
doAssert "".split({}) == @[""]
doAssert "".split("") == @[""]
doAssert s.split({}) == @[s]
doAssert s.split("") == @[s]
block: # splitLines
let fixture = "a\nb\rc\r\nd"
@@ -61,12 +70,21 @@ template main() =
block: # rsplit
doAssert rsplit("foo bar", seps = Whitespace) == @["foo", "bar"]
doAssert rsplit(" foo bar", seps = Whitespace, maxsplit = 1) == @[" foo", "bar"]
doAssert rsplit(" foo bar ", seps = Whitespace, maxsplit = 1) == @[
" foo bar", ""]
doAssert rsplit(" foo bar ", seps = Whitespace, maxsplit = 1) == @[" foo bar", ""]
doAssert rsplit(":foo:bar", sep = ':') == @["", "foo", "bar"]
doAssert rsplit(":foo:bar", sep = ':', maxsplit = 2) == @["", "foo", "bar"]
doAssert rsplit(":foo:bar", sep = ':', maxsplit = 3) == @["", "foo", "bar"]
doAssert rsplit("foothebar", sep = "the") == @["foo", "bar"]
# Empty string:
doAssert "".rsplit() == @[""]
doAssert "".rsplit(" ") == @[""]
doAssert "".rsplit({' '}) == @[""]
# Empty separators:
let s = " this is an example "
doAssert "".rsplit({}) == @[""]
doAssert "".rsplit("") == @[""]
doAssert s.rsplit({}) == @[s]
doAssert s.rsplit("") == @[s]
block: # splitWhitespace
let s = " this is an example "