@@ -974,18 +974,35 @@ public struct AttributedString : AttributedStringProtocol {
974974 public static func == ( lhs: Runs , rhs: Runs ) -> Bool {
975975 let lhsSlice = lhs. _guts. runs [ lhs. _startingRunIndex ..< lhs. _endingRunIndex]
976976 let rhsSlice = rhs. _guts. runs [ rhs. _startingRunIndex ..< rhs. _endingRunIndex]
977- guard lhsSlice. count == rhs. count else {
977+
978+ // If there are different numbers of runs, they aren't equal
979+ guard lhsSlice. count == rhsSlice. count else {
978980 return false
979981 }
980- // Compare all inner runs (their lengths will not be limited by _range)
981- if lhsSlice. count > 2 && !lhsSlice[ lhsSlice. startIndex + 1 ..< lhsSlice. endIndex - 1 ] . elementsEqual ( rhsSlice [ rhsSlice. startIndex + 1 ..< rhsSlice. endIndex - 1 ] ) {
982+
983+ let runCount = lhsSlice. count
984+
985+ // Empty slices are always equal
986+ guard runCount > 0 else {
987+ return true
988+ }
989+
990+ // Compare the first run (clamping their ranges) since we know each has at least one run
991+ if lhs. _guts. run ( at: lhs. startIndex, clampedBy: lhs. _range) != rhs. _guts. run ( at: rhs. startIndex, clampedBy: rhs. _range) {
982992 return false
983993 }
984- // If the inner runs are equivalent, check the first and last runs with clamped ranges
985- 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) {
994+
995+ // Compare all inner runs if they exist without needing to clamp ranges
996+ if runCount > 2 && !lhsSlice[ lhsSlice. startIndex + 1 ..< lhsSlice. endIndex - 1 ] . elementsEqual ( rhsSlice [ rhsSlice. startIndex + 1 ..< rhsSlice. endIndex - 1 ] ) {
986997 return false
987998 }
988- return lhs. _guts. run ( at: lhs. startIndex, clampedBy: lhs. _range) == rhs. _guts. run ( at: rhs. startIndex, clampedBy: rhs. _range)
999+
1000+ // 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)
1001+ 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) {
1002+ return false
1003+ }
1004+
1005+ return true
9891006 }
9901007
9911008 public var description : String {
0 commit comments