mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 17:04:41 +00:00
Guard against calling split with an empty string as a separator. Fixes #5119
This commit is contained in:
@@ -808,6 +808,11 @@ proc split*(s: string, sep: string, maxsplit: int = -1): seq[string] {.noSideEff
|
||||
##
|
||||
## Substrings are separated by the string `sep`. This is a wrapper around the
|
||||
## `split iterator <#split.i,string,string>`_.
|
||||
##
|
||||
## If `sep` is an empty string, `ValueError` is raised.
|
||||
if sep.len == 0:
|
||||
raise newException(ValueError, "invalid separator: empty string not allowed")
|
||||
|
||||
accumulateResult(split(s, sep, maxsplit))
|
||||
|
||||
proc rsplit*(s: string, seps: set[char] = Whitespace,
|
||||
|
||||
19
tests/stdlib/tsplit2.nim
Normal file
19
tests/stdlib/tsplit2.nim
Normal file
@@ -0,0 +1,19 @@
|
||||
discard """
|
||||
file: "tsplit2.nim"
|
||||
output: "true"
|
||||
"""
|
||||
import strutils
|
||||
|
||||
var s = ""
|
||||
for w in split("|abc|xy|z", {'|'}):
|
||||
s.add("#")
|
||||
s.add(w)
|
||||
|
||||
try:
|
||||
discard "hello".split("")
|
||||
echo "false"
|
||||
except ValueError:
|
||||
echo "true"
|
||||
|
||||
#OUT true
|
||||
|
||||
Reference in New Issue
Block a user