@@ -1087,15 +1087,10 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
1087
1087
public init < S: Sequence > ( _ elements: S ) where S. Iterator. Element == UInt8 {
1088
1088
let underestimatedCount = elements. underestimatedCount
1089
1089
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) }
1099
1094
}
1100
1095
1101
1096
public init ( _ bytes: Array < UInt8 > ) {
@@ -1658,11 +1653,24 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
1658
1653
}
1659
1654
}
1660
1655
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
+
1661
1669
/// An iterator over the contents of the data.
1662
1670
///
1663
1671
/// The iterator will increment byte-by-byte.
1664
1672
public func makeIterator( ) -> Data . Iterator {
1665
- return Iterator ( _data : self )
1673
+ return Iterator ( self )
1666
1674
}
1667
1675
1668
1676
public struct Iterator : IteratorProtocol {
@@ -1675,11 +1683,18 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
1675
1683
private var _idx : Data . Index
1676
1684
private let _endIdx : Data . Index
1677
1685
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
1680
1695
_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
1683
1698
}
1684
1699
1685
1700
public mutating func next( ) -> UInt8 ? {
0 commit comments