Skip to content

Commit d543069

Browse files
committed
[Foundation] Data.append: Switch to using resetBytes(in:)
1 parent 88e1aba commit d543069

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

stdlib/public/Darwin/Foundation/Data.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,16 +2388,17 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
23882388
// Copy as much as we can in one shot.
23892389
let underestimatedCount = elements.underestimatedCount
23902390
let originalCount = _representation.count
2391-
_representation.count += underestimatedCount
2392-
var (iter, endIndex): (S.Iterator, Int) = _representation.withUnsafeMutableBytes { buffer in
2391+
resetBytes(in: self.endIndex ..< self.endIndex + underestimatedCount)
2392+
var (iter, copiedCount): (S.Iterator, Int) = _representation.withUnsafeMutableBytes { buffer in
2393+
assert(buffer.count == originalCount + underestimatedCount)
23932394
let start = buffer.baseAddress!.assumingMemoryBound(to: UInt8.self) + originalCount
23942395
let b = UnsafeMutableBufferPointer(start: start, count: buffer.count - originalCount)
23952396
return elements._copyContents(initializing: b)
23962397
}
2397-
guard endIndex == underestimatedCount else {
2398+
guard copiedCount == underestimatedCount else {
23982399
// We can't trap here. We have to allow an underfilled buffer
23992400
// to emulate the previous implementation.
2400-
_representation.replaceSubrange(originalCount + endIndex ..< _representation.endIndex, with: nil, count: 0)
2401+
_representation.replaceSubrange(startIndex + originalCount + copiedCount ..< endIndex, with: nil, count: 0)
24012402
return
24022403
}
24032404

0 commit comments

Comments
 (0)