@@ -1122,28 +1122,6 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
1122
1122
_sliceRange = 0 ..< count
1123
1123
}
1124
1124
1125
- /// Initialize a `Data` with the contents of an Array.
1126
- ///
1127
- /// - parameter bytes: An array of bytes to copy.
1128
- public init ( bytes: Array < UInt8 > ) {
1129
- let count = bytes. count
1130
- _backing = bytes. withUnsafeBufferPointer {
1131
- return _DataStorage ( bytes: $0. baseAddress, length: count)
1132
- }
1133
- _sliceRange = 0 ..< count
1134
- }
1135
-
1136
- /// Initialize a `Data` with the contents of an Array.
1137
- ///
1138
- /// - parameter bytes: An array of bytes to copy.
1139
- public init ( bytes: ArraySlice < UInt8 > ) {
1140
- let count = bytes. count
1141
- _backing = bytes. withUnsafeBufferPointer {
1142
- return _DataStorage ( bytes: $0. baseAddress, length: count)
1143
- }
1144
- _sliceRange = 0 ..< count
1145
- }
1146
-
1147
1125
/// Initialize a `Data` with a repeating byte pattern
1148
1126
///
1149
1127
/// - parameter repeatedValue: A byte to initialize the pattern
@@ -1260,30 +1238,20 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
1260
1238
}
1261
1239
1262
1240
// slightly faster paths for common sequences
1263
-
1241
+ @ inlinable
1264
1242
public init < S: Sequence > ( _ elements: S ) where S. Iterator. Element == UInt8 {
1265
- if elements is Array < UInt8 > {
1266
- self . init ( bytes: _identityCast ( elements, to: Array< UInt8> . self ) )
1267
- } else if elements is ArraySlice < UInt8 > {
1268
- self . init ( bytes: _identityCast ( elements, to: ArraySlice< UInt8> . self ) )
1269
- } else if elements is UnsafeBufferPointer < UInt8 > {
1270
- self . init ( buffer: _identityCast ( elements, to: UnsafeBufferPointer< UInt8> . self ) )
1271
- } else if let buffer = elements as? UnsafeMutableBufferPointer < UInt8 > {
1272
- self . init ( buffer: buffer)
1273
- } else if let data = elements as? Data {
1274
- let len = data. count
1275
- let backing = data. withUnsafeBytes { ( bytes: UnsafePointer < UInt8 > ) in
1276
- return _DataStorage ( bytes: bytes, length: len)
1277
- }
1278
- self . init ( backing: backing, range: 0 ..< len)
1279
- } else {
1280
- let underestimatedCount = elements. underestimatedCount
1281
- self . init ( count: underestimatedCount)
1282
-
1283
- let ( endIterator, _) = UnsafeMutableBufferPointer ( start: _backing. _bytes? . assumingMemoryBound ( to: UInt8 . self) , count: underestimatedCount) . initialize ( from: elements)
1284
- var iter = endIterator
1285
- while let byte = iter. next ( ) { self . append ( byte) }
1243
+ let backing = _DataStorage ( capacity: Swift . max ( elements. underestimatedCount, 1 ) )
1244
+ var ( iter, endIndex) = elements. _copyContents ( initializing: UnsafeMutableBufferPointer ( start: backing. _bytes? . bindMemory ( to: UInt8 . self, capacity: backing. _capacity) , count: backing. _capacity) )
1245
+ backing. _length = endIndex
1246
+ while var element = iter. next ( ) {
1247
+ backing. append ( & element, length: 1 )
1286
1248
}
1249
+ self . init ( backing: backing, range: 0 ..< backing. _length)
1250
+ }
1251
+
1252
+ @inlinable
1253
+ public init < S: Sequence > ( bytes elements: S ) where S. Iterator. Element == UInt8 {
1254
+ self . init ( elements)
1287
1255
}
1288
1256
1289
1257
@usableFromInline
@@ -1506,17 +1474,25 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
1506
1474
_append ( buffer)
1507
1475
}
1508
1476
1509
- public mutating func append< S : Sequence > ( contentsOf newElements: S ) where S. Iterator. Element == Iterator . Element {
1510
- for byte in newElements {
1511
- append ( byte)
1512
- }
1513
- }
1514
-
1515
1477
public mutating func append( contentsOf bytes: [ UInt8 ] ) {
1516
1478
bytes. withUnsafeBufferPointer { ( buffer: UnsafeBufferPointer < UInt8 > ) -> Void in
1517
1479
_append ( buffer)
1518
1480
}
1519
1481
}
1482
+
1483
+ @inlinable
1484
+ public mutating func append< S : Sequence > ( contentsOf newElements: S ) where S. Iterator. Element == Iterator . Element {
1485
+ let underestimatedCount = Swift . max ( newElements. underestimatedCount, 1 )
1486
+ _withStackOrHeapBuffer ( underestimatedCount) { ( buffer) in
1487
+ let capacity = buffer. pointee. capacity
1488
+ let base = buffer. pointee. memory. bindMemory ( to: UInt8 . self, capacity: capacity)
1489
+ var ( iter, endIndex) = newElements. _copyContents ( initializing: UnsafeMutableBufferPointer ( start: base, count: capacity) )
1490
+ _append ( UnsafeBufferPointer ( start: base, count: endIndex) )
1491
+ while var element = iter. next ( ) {
1492
+ append ( & element, count: 1 )
1493
+ }
1494
+ }
1495
+ }
1520
1496
1521
1497
// MARK: -
1522
1498
@@ -1897,13 +1873,7 @@ extension Data {
1897
1873
1898
1874
/// Provides bridging functionality for struct Data to class NSData and vice-versa.
1899
1875
1900
- #if DEPLOYMENT_RUNTIME_SWIFT
1901
- internal typealias DataBridgeType = _ObjectTypeBridgeable
1902
- #else
1903
- internal typealias DataBridgeType = _ObjectiveCBridgeable
1904
- #endif
1905
-
1906
- extension Data : DataBridgeType {
1876
+ extension Data : _ObjectiveCBridgeable {
1907
1877
@_semantics ( " convertToObjectiveC " )
1908
1878
public func _bridgeToObjectiveC( ) -> NSData {
1909
1879
return _backing. bridgedReference ( _sliceRange)
0 commit comments