Skip to content

Commit b49bbc6

Browse files
authored
Ensure empty AttributedSubstrings produce no runs (#996)
1 parent 8d822ff commit b49bbc6

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Sources/FoundationEssentials/AttributedString/AttributedString+Runs.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ extension AttributedString {
4747
_runOffset: _guts.runs.count,
4848
runIndex: _guts.runs.endIndex.base,
4949
stringIndex: _strBounds.upperBound)
50-
} else if _strBounds.upperBound == _guts.string.startIndex {
51-
assert(stringLowerBound == stringUpperBound)
50+
} else if _strBounds.upperBound == _strBounds.lowerBound {
5251
end = start
5352
} else {
5453
let last = _guts.runs.index(atUTF8Offset: _strBounds.upperBound.utf8Offset - 1).index
@@ -62,7 +61,7 @@ extension AttributedString {
6261
assert(start._runIndex != nil && start._stringIndex != nil)
6362
assert(end._runIndex != nil && end._stringIndex != nil)
6463
assert(start._stringIndex!.utf8Offset <= _strBounds.lowerBound.utf8Offset)
65-
assert(end._stringIndex!.utf8Offset >= _strBounds.upperBound.utf8Offset)
64+
assert(end == start || end._stringIndex!.utf8Offset >= _strBounds.upperBound.utf8Offset)
6665
self._bounds = start ..< end
6766
}
6867
}

Tests/FoundationEssentialsTests/AttributedString/AttributedStringTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ final class TestAttributedString: XCTestCase {
4343
for _ in AttributedString().runs {
4444
XCTFail("Empty AttributedString should not enumerate any attributes")
4545
}
46+
47+
do {
48+
let str = AttributedString("Foo")
49+
for _ in str[str.startIndex ..< str.startIndex].runs {
50+
XCTFail("Empty AttributedSubstring should not enumerate any attributes")
51+
}
52+
}
53+
54+
do {
55+
let str = AttributedString("Foo", attributes: AttributeContainer.testInt(2))
56+
let i = str.index(afterCharacter: str.startIndex)
57+
for _ in str[i ..< i].runs {
58+
XCTFail("Empty AttributedSubstring should not enumerate any attributes")
59+
}
60+
}
4661
}
4762

4863
func verifyAttributes<T>(_ runs: AttributedString.Runs.AttributesSlice1<T>, string: AttributedString, expectation: [(String, T.Value?)]) where T.Value : Sendable {

0 commit comments

Comments
 (0)