Skip to content

Commit a842c6d

Browse files
committed
[Foundation] Update Data sequence initializer to use initialize(from:) and add _copyContents
1 parent aeb61e3 commit a842c6d

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

stdlib/public/SDK/Foundation/Data.swift

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,15 +1087,10 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
10871087
public init<S: Sequence>(_ elements: S) where S.Iterator.Element == UInt8 {
10881088
let underestimatedCount = elements.underestimatedCount
10891089
self.init(count: underestimatedCount)
1090-
var idx = 0
1091-
for byte in elements {
1092-
if idx < underestimatedCount {
1093-
self[idx] = byte
1094-
} else {
1095-
self.append(byte)
1096-
}
1097-
idx += 1
1098-
}
1090+
1091+
let (endIterator, _) = UnsafeMutableBufferPointer(start: _backing._bytes?.assumingMemoryBound(to: UInt8.self), count: underestimatedCount).initialize(from: elements)
1092+
var iter = endIterator
1093+
while let byte = iter.next() { self.append(byte) }
10991094
}
11001095

11011096
public init(_ bytes: Array<UInt8>) {
@@ -1658,11 +1653,24 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
16581653
}
16591654
}
16601655

1656+
public func _copyContents(initializing buffer: UnsafeMutableBufferPointer<UInt8>) -> (Iterator, UnsafeMutableBufferPointer<UInt8>.Index) {
1657+
guard !isEmpty else { return (makeIterator(), buffer.startIndex) }
1658+
guard let p = buffer.baseAddress else {
1659+
preconditionFailure("Attempt to copy contents into nil buffer pointer")
1660+
}
1661+
let cnt = count
1662+
precondition(cnt <= buffer.count, "Insufficient space allocated to copy Data contents")
1663+
1664+
withUnsafeBytes { p.initialize(from: $0, count: cnt) }
1665+
1666+
return (Iterator(endOf: self), buffer.index(buffer.startIndex, offsetBy: cnt))
1667+
}
1668+
16611669
/// An iterator over the contents of the data.
16621670
///
16631671
/// The iterator will increment byte-by-byte.
16641672
public func makeIterator() -> Data.Iterator {
1665-
return Iterator(_data: self)
1673+
return Iterator(self)
16661674
}
16671675

16681676
public struct Iterator : IteratorProtocol {
@@ -1675,11 +1683,18 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
16751683
private var _idx: Data.Index
16761684
private let _endIdx: Data.Index
16771685

1678-
fileprivate init(_data: Data) {
1679-
self._data = _data
1686+
fileprivate init(_ data: Data) {
1687+
_data = data
1688+
_buffer = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
1689+
_idx = data.startIndex
1690+
_endIdx = data.endIndex
1691+
}
1692+
1693+
fileprivate init(endOf data: Data) {
1694+
self._data = data
16801695
_buffer = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
1681-
_idx = _data.startIndex
1682-
_endIdx = _data.endIndex
1696+
_idx = data.endIndex
1697+
_endIdx = data.endIndex
16831698
}
16841699

16851700
public mutating func next() -> UInt8? {

0 commit comments

Comments
 (0)