Skip to content

Commit 9ff87db

Browse files
authored
Fix empty.split w/ empty separator (#353)
1 parent bef2092 commit 9ff87db

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Sources/_StringProcessing/Algorithms/Algorithms/Split.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ extension SplitCollection: Sequence {
7575
: base[index...]
7676
}
7777

78+
if index == base.endIndex {
79+
return finish()
80+
}
81+
7882
if splitCounter >= maxSplits {
7983
return finish()
8084
}

Tests/RegexTests/AlgorithmsTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,17 @@ class AlgorithmTests: XCTestCase {
128128
XCTAssertEqual(actual, expected, file: file, line: line)
129129
}
130130

131-
expectSplit("", "", ["", ""])
131+
expectSplit("", "", [""])
132132
expectSplit("", "x", [""])
133133
expectSplit("a", "", ["", "a", ""])
134134
expectSplit("a", "x", ["a"])
135135
expectSplit("a", "a", ["", ""])
136136
expectSplit("a____a____a", "_+", ["a", "a", "a"])
137137
expectSplit("____a____a____a____", "_+", ["", "a", "a", "a", ""])
138-
138+
139+
XCTAssertEqual("".split(separator: ""), [])
140+
XCTAssertEqual("".split(separator: "", omittingEmptySubsequences: false), [""])
141+
139142
// Test that original `split` functions are still accessible
140143
let splitRef = "abcd".split
141144
XCTAssert(type(of: splitRef) == ((Character, Int, Bool) -> [Substring]).self)

0 commit comments

Comments
 (0)