You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: stdlib/public/SDK/Foundation/Data.swift
+65-26Lines changed: 65 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -66,7 +66,7 @@ internal final class _SwiftNSData : _SwiftNativeNSData, _SwiftNativeFoundationTy
66
66
67
67
`Data` can be initialized with an `UnsafePointer` and count, an array of `UInt8` (the primitive byte type), or an `UnsafeBufferPointer`. The buffer-oriented functions provide an extra measure of safety by automatically performing the size calculation, as the type is known at compile time.
@@ -163,29 +163,29 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
163
163
164
164
/// Initialize a `Data` with the specified size.
165
165
///
166
-
/// This initializer doesn't necessarily allocate the requested memory right away. Mutable data allocates additional memory as needed, so `capacity` simply establishes the initial capacity. When it does allocate the initial memory, though, it allocates the specified amount.
166
+
/// This initializer doesn't necessarily allocate the requested memory right away. `Data` allocates additional memory as needed, so `capacity` simply establishes the initial capacity. When it does allocate the initial memory, though, it allocates the specified amount.
167
167
///
168
168
/// This method sets the `count` of the data to 0.
169
169
///
170
170
/// If the capacity specified in `capacity` is greater than four memory pages in size, this may round the amount of requested memory up to the nearest full page.
171
171
///
172
172
/// - parameter capacity: The size of the data.
173
-
publicinit?(capacity:Int){
173
+
publicinit(capacity:Int){
174
174
iflet d =NSMutableData(capacity: capacity){
175
175
_wrapped =_SwiftNSData(mutableObject: d)
176
176
}else{
177
-
returnnil
177
+
fatalError("Unable to allocate data of the requested capacity")
178
178
}
179
179
}
180
180
181
181
/// Initialize a `Data` with the specified count of zeroed bytes.
182
182
///
183
183
/// - parameter count: The number of bytes the data initially contains.
184
-
publicinit?(count:Int){
184
+
publicinit(count:Int){
185
185
iflet d =NSMutableData(length: count){
186
186
_wrapped =_SwiftNSData(mutableObject: d)
187
187
}else{
188
-
returnnil
188
+
fatalError("Unable to allocate data of the requested count")
189
189
}
190
190
}
191
191
@@ -249,10 +249,10 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
249
249
/// If the resulting value is mutated, then `Data` will invoke the `mutableCopy()` function on the reference to copy the contents. You may customize the behavior of that function if you wish to return a specialized mutable subclass.
250
250
///
251
251
/// - parameter reference: The instance of `NSData` that you wish to wrap. This instance will be copied by `struct Data`.
@@ -461,11 +461,11 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
461
461
///
462
462
/// This will resize the data if required, to fit the entire contents of `data`.
463
463
///
464
-
/// - precondition: `range` must be within the range of the data.
465
-
/// - parameter range: The range in the data to replace.
464
+
/// - precondition: The bounds of `subrange` must be valid indicies of the collection.
465
+
/// - parameter subrange: The range in the data to replace. If `subrange.lowerBound == data.count && subrange.count == 0` then this operation is an append.
466
466
/// - parameter data: The replacement data.
467
-
publicmutatingfuncreplaceBytes(in range:Range<Index>, with data:Data){
// In the future, if we keep the malloced pointer and count inside this struct/ref instead of deferring to NSData, we may be able to do this more efficiently.
0 commit comments