Skip to content

Ensure that subscripting AttributeSliceX behaves consistently regardless of index location #1382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading