diff --git a/Sources/FoundationEssentials/AttributedString/AttributedString+Runs.swift b/Sources/FoundationEssentials/AttributedString/AttributedString+Runs.swift index 58fbc07ec..17fc34ab4 100644 --- a/Sources/FoundationEssentials/AttributedString/AttributedString+Runs.swift +++ b/Sources/FoundationEssentials/AttributedString/AttributedString+Runs.swift @@ -594,9 +594,6 @@ extension AttributedString.Runs { // _strBounds.range(containing:) below validates that i._value is within the bounds of this slice precondition(!attributeNames.isEmpty) let r = _guts.findRun(at: i._value) - if r.runIndex.offset == endIndex._runOffset { - return (i, r.runIndex) - } let currentRange = _strBounds.range(containing: i._value).range guard constraints.count != 1 || constraints.contains(nil) else { diff --git a/Tests/FoundationEssentialsTests/AttributedString/AttributedStringTests.swift b/Tests/FoundationEssentialsTests/AttributedString/AttributedStringTests.swift index 670aabf97..36befc4e9 100644 --- a/Tests/FoundationEssentialsTests/AttributedString/AttributedStringTests.swift +++ b/Tests/FoundationEssentialsTests/AttributedString/AttributedStringTests.swift @@ -1983,6 +1983,34 @@ E { } } } + + @Test func runSliceSubscripting() { + var str = AttributedString("Foo", attributes: .init().testInt(1)) + str += AttributedString("Bar", attributes: .init().testInt(2)) + str += AttributedString("Baz", attributes: .init().testInt(3)) + + do { + let runsSlice = str.runs[\.testInt] + for (value, range) in runsSlice { + for idx in str.utf8[range].indices { + let subscriptResult = runsSlice[idx] + #expect(subscriptResult.0 == value, "Subscript index \(idx) did not produce same value as runs slice") + #expect(subscriptResult.1 == range, "Subscript index \(idx) did not produce same range as runs slice") + } + } + } + + do { + let runsSlice = str[str.index(afterCharacter: str.startIndex) ..< str.index(beforeCharacter: str.endIndex)].runs[\.testInt] + for (value, range) in runsSlice { + for idx in str.utf8[range].indices { + let subscriptResult = runsSlice[idx] + #expect(subscriptResult.0 == value, "Subscript index \(idx) did not produce same value as runs slice") + #expect(subscriptResult.1 == range, "Subscript index \(idx) did not produce same range as runs slice") + } + } + } + } // MARK: - Other Tests