Skip to content

Fix logic error in AttributedString.Runs.== for empty collections #4635

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
Sep 22, 2022
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
29 changes: 23 additions & 6 deletions Sources/Foundation/AttributedString/AttributedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -974,18 +974,35 @@ public struct AttributedString : AttributedStringProtocol {
public static func == (lhs: Runs, rhs: Runs) -> Bool {
let lhsSlice = lhs._guts.runs[lhs._startingRunIndex ..< lhs._endingRunIndex]
let rhsSlice = rhs._guts.runs[rhs._startingRunIndex ..< rhs._endingRunIndex]
guard lhsSlice.count == rhs.count else {

// If there are different numbers of runs, they aren't equal
guard lhsSlice.count == rhsSlice.count else {
return false
}
// Compare all inner runs (their lengths will not be limited by _range)
if lhsSlice.count > 2 && !lhsSlice[lhsSlice.startIndex + 1 ..< lhsSlice.endIndex - 1].elementsEqual(rhsSlice[rhsSlice.startIndex + 1 ..< rhsSlice.endIndex - 1]) {

let runCount = lhsSlice.count

// Empty slices are always equal
guard runCount > 0 else {
return true
}

// Compare the first run (clamping their ranges) since we know each has at least one run
if lhs._guts.run(at: lhs.startIndex, clampedBy: lhs._range) != rhs._guts.run(at: rhs.startIndex, clampedBy: rhs._range) {
return false
}
// If the inner runs are equivalent, check the first and last runs with clamped ranges
if lhsSlice.count > 1 && lhs._guts.run(at: Index(rangeIndex: lhs._endingRunIndex - 1), clampedBy: lhs._range) != rhs._guts.run(at: Index(rangeIndex: rhs._endingRunIndex - 1), clampedBy: rhs._range) {

// Compare all inner runs if they exist without needing to clamp ranges
if runCount > 2 && !lhsSlice[lhsSlice.startIndex + 1 ..< lhsSlice.endIndex - 1].elementsEqual(rhsSlice[rhsSlice.startIndex + 1 ..< rhsSlice.endIndex - 1]) {
return false
}
return lhs._guts.run(at: lhs.startIndex, clampedBy: lhs._range) == rhs._guts.run(at: rhs.startIndex, clampedBy: rhs._range)

// If there are more than one run (so we didn't already check this as the first run), check the last run (clamping its range)
if runCount > 1 && lhs._guts.run(at: Index(rangeIndex: lhs._endingRunIndex - 1), clampedBy: lhs._range) != rhs._guts.run(at: Index(rangeIndex: rhs._endingRunIndex - 1), clampedBy: rhs._range) {
return false
}

return true
}

public var description: String {
Expand Down
14 changes: 14 additions & 0 deletions Tests/Foundation/Tests/TestAttributedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,19 @@ E {
XCTAssertFalse(desc.isEmpty)
}
}

func testSubstringEquality() {
let str = AttributedString("")
let range = str.startIndex ..< str.endIndex
XCTAssertEqual(str[range], str[range])

let str2 = "A" + AttributedString("A", attributes: .init().testInt(2))
let substringA = str2[str2.startIndex ..< str2.index(afterCharacter: str2.startIndex)]
let substringB = str2[str2.index(afterCharacter: str2.startIndex) ..< str2.endIndex]
XCTAssertNotEqual(substringA, substringB)
XCTAssertEqual(substringA, substringA)
XCTAssertEqual(substringB, substringB)
}

// MARK: - Coding Tests

Expand Down Expand Up @@ -2196,6 +2209,7 @@ E {
("testSubstringBase", testSubstringBase),
("testSubstringGetAttribute", testSubstringGetAttribute),
("testSubstringDescription", testSubstringDescription),
("testSubstringEquality", testSubstringEquality),
("testJSONEncoding", testJSONEncoding),
("testDecodingThenConvertingToNSAttributedString", testDecodingThenConvertingToNSAttributedString),
("testCustomAttributeCoding", testCustomAttributeCoding),
Expand Down