@@ -69,11 +69,6 @@ extension RangeReplaceableCollection {
69
69
/// number of changes contained by the parameter.
70
70
@available ( macOS 10 . 15 , iOS 13 , tvOS 13 , watchOS 6 , * )
71
71
public func applying( _ difference: CollectionDifference < Element > ) -> Self ? {
72
- var result = Self ( )
73
- var enumeratedRemoves = 0
74
- var enumeratedInserts = 0
75
- var enumeratedOriginals = 0
76
- var currentIndex = self . startIndex
77
72
78
73
func append(
79
74
into target: inout Self ,
@@ -87,7 +82,12 @@ extension RangeReplaceableCollection {
87
82
target. append ( contentsOf: source [ start..< index] )
88
83
}
89
84
85
+ var result = Self ( )
90
86
do {
87
+ var enumeratedRemoves = 0
88
+ var enumeratedInserts = 0
89
+ var enumeratedOriginals = 0
90
+ var currentIndex = self . startIndex
91
91
try difference. _fastEnumeratedApply { change in
92
92
switch change {
93
93
case . remove( offset: let offset, element: _, associatedWith: _) :
@@ -109,15 +109,14 @@ extension RangeReplaceableCollection {
109
109
}
110
110
_internalInvariant ( enumeratedOriginals <= self . count)
111
111
}
112
- let origCount = self . count - enumeratedOriginals
113
- try append ( into: & result, contentsOf: self , from: & currentIndex, count: origCount)
114
- _internalInvariant ( enumeratedOriginals + origCount == self . count)
112
+ if currentIndex < self . endIndex {
113
+ result. append ( contentsOf: self [ currentIndex... ] )
114
+ }
115
+ _internalInvariant ( result. count == self . count + enumeratedInserts - enumeratedRemoves)
115
116
} catch {
116
117
return nil
117
118
}
118
119
119
- _internalInvariant ( currentIndex == self . endIndex)
120
- _internalInvariant ( result. count == self . count + enumeratedInserts - enumeratedRemoves)
121
120
return result
122
121
}
123
122
}
0 commit comments