summary refs log tree commit diff stats
path: root/tests/stdlib/tstrutils.nim
diff options
context:
space:
mode:
authorZoom <ZoomRmc@users.noreply.github.com>2023-06-21 06:52:33 +0000
committerGitHub <noreply@github.com>2023-06-21 08:52:33 +0200
commit5e529b3bfa7b3ca09cde7237134acd47e47ca48c (patch)
treef85af533ea8c251a48bddac06a600069be0bcc4a /tests/stdlib/tstrutils.nim
parenta345cde26e15ef8e1c905c48507195739fab4515 (diff)
downloadNim-5e529b3bfa7b3ca09cde7237134acd47e47ca48c.tar.gz
`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>
Diffstat (limited to 'tests/stdlib/tstrutils.nim')
-rw-r--r--tests/stdlib/tstrutils.nim16
1 files changed, 10 insertions, 6 deletions
diff --git a/tests/stdlib/tstrutils.nim b/tests/stdlib/tstrutils.nim
index d53e9d8b4..847e22656 100644
--- a/tests/stdlib/tstrutils.nim
+++ b/tests/stdlib/tstrutils.nim
@@ -58,8 +58,10 @@ template main() =
     doAssert "".split(" ") == @[""]
     doAssert "".split({' '}) == @[""]
     # Empty separators:
-    doAssertRaises(AssertionDefect): discard s.split({})
-    doAssertRaises(AssertionDefect): discard s.split("")
+    doAssert "".split({}) == @[""]
+    doAssert "".split("") == @[""]
+    doAssert s.split({}) == @[s]
+    doAssert s.split("") == @[s]
 
   block: # splitLines
     let fixture = "a\nb\rc\r\nd"
@@ -70,8 +72,7 @@ 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"]
@@ -81,8 +82,11 @@ template main() =
     doAssert "".rsplit(" ") == @[""]
     doAssert "".rsplit({' '}) == @[""]
     # Empty separators:
-    doAssertRaises(AssertionDefect): discard "".rsplit({})
-    doAssertRaises(AssertionDefect): discard "".rsplit("")
+    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  "