From 5e529b3bfa7b3ca09cde7237134acd47e47ca48c Mon Sep 17 00:00:00 2001 From: Zoom Date: Wed, 21 Jun 2023 06:52:33 +0000 Subject: `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 --- tests/stdlib/tstrutils.nim | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'tests/stdlib/tstrutils.nim') 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 " -- cgit 1.4.1-2-gfad0 u/tree/040brace.cc?h=hlt&id=1193171f64f6b65d3ec714aa2d1d22b4e1d05832'>tree)
1
2
3
4
5
6
7
8
9
10
11
12
13