Skip to content

Commit 0150e33

Browse files
committed
fix: resolve #14649 - add validation for multi-char separator
1 parent c0db072 commit 0150e33

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

strings/split.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,19 @@ def split(string: str, separator: str = " ") -> list:
1717
1818
>>> split(";abbb;;c;", separator=';')
1919
['', 'abbb', '', 'c', '']
20-
"""
21-
22-
split_words = []
2320
24-
last_index = 0
25-
for index, char in enumerate(string):
26-
if char == separator:
27-
split_words.append(string[last_index:index])
28-
last_index = index + 1
29-
if index + 1 == len(string):
30-
split_words.append(string[last_index : index + 1])
31-
return split_words
21+
>>> split("a--b--c", separator="--")
22+
Traceback (most recent call last):
23+
...
24+
ValueError: separator must be a single character
25+
"""
26+
if len(separator) != 1:
27+
raise ValueError("separator must be a single character")
28+
return string.split(separator)
3229

3330

3431
if __name__ == "__main__":
3532
from doctest import testmod
3633

3734
testmod()
35+

0 commit comments

Comments
 (0)