Skip to content

Commit 4e679a9

Browse files
authored
Ensure that subscripting AttributeSliceX behaves consistently regardless of index location (#1382)
1 parent 7ab3fb0 commit 4e679a9

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

Sources/FoundationEssentials/AttributedString/AttributedString+Runs.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,6 @@ extension AttributedString.Runs {
594594
// _strBounds.range(containing:) below validates that i._value is within the bounds of this slice
595595
precondition(!attributeNames.isEmpty)
596596
let r = _guts.findRun(at: i._value)
597-
if r.runIndex.offset == endIndex._runOffset {
598-
return (i, r.runIndex)
599-
}
600597
let currentRange = _strBounds.range(containing: i._value).range
601598

602599
guard constraints.count != 1 || constraints.contains(nil) else {

Tests/FoundationEssentialsTests/AttributedString/AttributedStringTests.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,34 @@ E {
19831983
}
19841984
}
19851985
}
1986+
1987+
@Test func runSliceSubscripting() {
1988+
var str = AttributedString("Foo", attributes: .init().testInt(1))
1989+
str += AttributedString("Bar", attributes: .init().testInt(2))
1990+
str += AttributedString("Baz", attributes: .init().testInt(3))
1991+
1992+
do {
1993+
let runsSlice = str.runs[\.testInt]
1994+
for (value, range) in runsSlice {
1995+
for idx in str.utf8[range].indices {
1996+
let subscriptResult = runsSlice[idx]
1997+
#expect(subscriptResult.0 == value, "Subscript index \(idx) did not produce same value as runs slice")
1998+
#expect(subscriptResult.1 == range, "Subscript index \(idx) did not produce same range as runs slice")
1999+
}
2000+
}
2001+
}
2002+
2003+
do {
2004+
let runsSlice = str[str.index(afterCharacter: str.startIndex) ..< str.index(beforeCharacter: str.endIndex)].runs[\.testInt]
2005+
for (value, range) in runsSlice {
2006+
for idx in str.utf8[range].indices {
2007+
let subscriptResult = runsSlice[idx]
2008+
#expect(subscriptResult.0 == value, "Subscript index \(idx) did not produce same value as runs slice")
2009+
#expect(subscriptResult.1 == range, "Subscript index \(idx) did not produce same range as runs slice")
2010+
}
2011+
}
2012+
}
2013+
}
19862014

19872015
// MARK: - Other Tests
19882016

0 commit comments

Comments
 (0)