Skip to content

Commit 4111b21

Browse files
committed
[String] Bug fix for empty-range getCharacters
1 parent b635bba commit 4111b21

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

stdlib/public/core/StringUTF16View.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,8 @@ extension String {
537537
) {
538538
_internalInvariant(_guts.isFastUTF8)
539539

540+
if _slowPath(range.isEmpty) { return }
541+
540542
return _guts.withFastUTF8 { utf8 in
541543
var writeIdx = 0
542544
let writeEnd = buffer.count

validation-test/stdlib/StringBreadcrumbs.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,17 @@ let StringBreadcrumbsTests = TestSuite("StringBreadcrumbsTests")
3838

3939
func validateBreadcrumbs(_ str: String) {
4040
var utf16CodeUnits = Array(str.utf16)
41-
var utf16Indices = Array(str.utf16.indices)
4241
var outputBuffer = Array<UInt16>(repeating: 0, count: utf16CodeUnits.count)
4342

44-
for i in 0..<(utf16CodeUnits.count-1) {
45-
for j in (i+1)..<utf16CodeUnits.count {
43+
// Include the endIndex, so we can test end conversions
44+
var utf16Indices = Array(str.utf16.indices) + [str.utf16.endIndex]
45+
46+
for i in 0...utf16CodeUnits.count {
47+
for j in i...utf16CodeUnits.count {
4648
let range = Range(uncheckedBounds: (i, j))
4749

4850
let indexRange = str._toUTF16Indices(range)
51+
4952
// Range<String.Index> <=> Range<Int>
5053
expectEqual(utf16Indices[i], indexRange.lowerBound)
5154
expectEqual(utf16Indices[j], indexRange.upperBound)
@@ -69,6 +72,13 @@ func validateBreadcrumbs(_ str: String) {
6972
}
7073
}
7174

75+
StringBreadcrumbsTests.test("uniform strings") {
76+
validateBreadcrumbs(smallASCII)
77+
validateBreadcrumbs(largeASCII)
78+
validateBreadcrumbs(smallUnicode)
79+
validateBreadcrumbs(largeUnicode)
80+
}
81+
7282
StringBreadcrumbsTests.test("largeString") {
7383
validateBreadcrumbs(largeString)
7484
}

0 commit comments

Comments
 (0)