Skip to content

Commit fbed646

Browse files
Merge pull request #5483 from airspeedswift/buffer-protocol
[stdlib] Make various free functions mutating an _ArrayBufferProtocol into extensions. Resolves ABI FIXME #14
2 parents bf2de4a + 77e13e0 commit fbed646

File tree

4 files changed

+237
-249
lines changed

4 files changed

+237
-249
lines changed

Diff for: stdlib/public/core/ArrayBuffer.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ extension _ArrayBuffer {
387387
}
388388
else {
389389
var refCopy = self
390-
refCopy.replace(
391-
subRange: i..<(i + 1),
390+
refCopy.replaceSubrange(
391+
i..<(i + 1),
392392
with: 1,
393393
elementsOf: CollectionOfOne(newValue))
394394
}

Diff for: stdlib/public/core/ArrayBufferProtocol.swift

+15-14
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ internal protocol _ArrayBufferProtocol
7272
///
7373
/// - Precondition: This buffer is backed by a uniquely-referenced
7474
/// `_ContiguousArrayBuffer`.
75-
mutating func replace<C>(
76-
subRange: Range<Int>,
75+
mutating func replaceSubrange<C>(
76+
_ subrange: Range<Int>,
7777
with newCount: Int,
7878
elementsOf newValues: C
7979
) where C : Collection, C.Iterator.Element == Element
@@ -124,41 +124,42 @@ internal protocol _ArrayBufferProtocol
124124
var identity: UnsafeRawPointer { get }
125125

126126
var startIndex: Int { get }
127+
var endIndex: Int { get }
127128
}
128129

129-
extension _ArrayBufferProtocol where Index == Int {
130+
extension _ArrayBufferProtocol {
130131

131132
internal var subscriptBaseAddress: UnsafeMutablePointer<Element> {
132133
return firstElementAddress
133134
}
134135

135-
internal mutating func replace<C>(
136-
subRange: Range<Int>,
136+
internal mutating func replaceSubrange<C>(
137+
_ subrange: Range<Int>,
137138
with newCount: Int,
138139
elementsOf newValues: C
139140
) where C : Collection, C.Iterator.Element == Element {
140141
_sanityCheck(startIndex == 0, "_SliceBuffer should override this function.")
141142
let oldCount = self.count
142-
let eraseCount = subRange.count
143+
let eraseCount = subrange.count
143144

144145
let growth = newCount - eraseCount
145146
self.count = oldCount + growth
146147

147148
let elements = self.subscriptBaseAddress
148-
let oldTailIndex = subRange.upperBound
149+
let oldTailIndex = subrange.upperBound
149150
let oldTailStart = elements + oldTailIndex
150151
let newTailIndex = oldTailIndex + growth
151152
let newTailStart = oldTailStart + growth
152-
let tailCount = oldCount - subRange.upperBound
153+
let tailCount = oldCount - subrange.upperBound
153154

154155
if growth > 0 {
155156
// Slide the tail part of the buffer forwards, in reverse order
156157
// so as not to self-clobber.
157158
newTailStart.moveInitialize(from: oldTailStart, count: tailCount)
158159

159-
// Assign over the original subRange
160+
// Assign over the original subrange
160161
var i = newValues.startIndex
161-
for j in CountableRange(subRange) {
162+
for j in CountableRange(subrange) {
162163
elements[j] = newValues[i]
163164
newValues.formIndex(after: &i)
164165
}
@@ -170,12 +171,12 @@ extension _ArrayBufferProtocol where Index == Int {
170171
_expectEnd(of: newValues, is: i)
171172
}
172173
else { // We're not growing the buffer
173-
// Assign all the new elements into the start of the subRange
174-
var i = subRange.lowerBound
174+
// Assign all the new elements into the start of the subrange
175+
var i = subrange.lowerBound
175176
var j = newValues.startIndex
176177
for _ in 0..<newCount {
177178
elements[i] = newValues[j]
178-
formIndex(after: &i)
179+
i += 1
179180
newValues.formIndex(after: &j)
180181
}
181182
_expectEnd(of: newValues, is: j)
@@ -201,7 +202,7 @@ extension _ArrayBufferProtocol where Index == Int {
201202
// Assign over the start of the replaced range with the tail
202203
newTailStart.moveAssign(from: oldTailStart, count: tailCount)
203204

204-
// Destroy elements remaining after the tail in subRange
205+
// Destroy elements remaining after the tail in subrange
205206
(newTailStart + tailCount).deinitialize(
206207
count: shrinkage - tailCount)
207208
}

0 commit comments

Comments
 (0)