Skip to content

Commit 0d55e8d

Browse files
committed
Changes signature of private func base64DecodeBytes to accept collection of bytes which significantly simplifies callers
1 parent b0dd7d9 commit 0d55e8d

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

Sources/Foundation/NSData.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
206206

207207
/// Initializes a data object with the given Base64 encoded string.
208208
public init?(base64Encoded base64String: String, options: Base64DecodingOptions = []) {
209-
guard var decodedBytes = base64String.utf8CString.withUnsafeBytes( { buffer -> [UInt8]? in
210-
let bufferDroppingTerminatingNul = UnsafeRawBufferPointer(rebasing: buffer.dropLast())
211-
return NSData.base64DecodeBytes(bufferDroppingTerminatingNul, options: options)
212-
}) else {
209+
guard var decodedBytes = NSData.base64DecodeBytes(base64String.utf8, options: options) else {
213210
return nil
214211
}
215212
super.init()
@@ -218,8 +215,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
218215

219216
/// Initializes a data object with the given Base64 encoded data.
220217
public init?(base64Encoded base64Data: Data, options: Base64DecodingOptions = []) {
221-
guard var decodedBytes = base64Data
222-
.withUnsafeBytes({ NSData.base64DecodeBytes($0, options: options) }) else {
218+
guard var decodedBytes = NSData.base64DecodeBytes(base64Data, options: options) else {
223219
return nil
224220
}
225221
super.init()
@@ -671,7 +667,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
671667
- parameter options: Options for handling invalid input
672668
- returns: The decoded bytes.
673669
*/
674-
private static func base64DecodeBytes(_ bytes: UnsafeRawBufferPointer, options: Base64DecodingOptions = []) -> [UInt8]? {
670+
private static func base64DecodeBytes<T: Collection>(_ bytes: T, options: Base64DecodingOptions = []) -> [UInt8]? where T.Element == UInt8 {
675671
var decodedBytes = [UInt8]()
676672
decodedBytes.reserveCapacity((bytes.count/3)*2)
677673

0 commit comments

Comments
 (0)