@@ -206,8 +206,10 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
206
206
207
207
/// Initializes a data object with the given Base64 encoded string.
208
208
public init ? ( base64Encoded base64String: String , options: Base64DecodingOptions = [ ] ) {
209
- let encodedBytes = Array ( base64String. utf8)
210
- guard var decodedBytes = NSData . base64DecodeBytes ( encodedBytes, options: options) else {
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 {
211
213
return nil
212
214
}
213
215
super. init ( )
@@ -216,9 +218,8 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
216
218
217
219
/// Initializes a data object with the given Base64 encoded data.
218
220
public init ? ( base64Encoded base64Data: Data , options: Base64DecodingOptions = [ ] ) {
219
- var encodedBytes = [ UInt8] ( repeating: 0 , count: base64Data. count)
220
- base64Data. _nsObject. getBytes ( & encodedBytes, length: encodedBytes. count)
221
- guard var decodedBytes = NSData . base64DecodeBytes ( encodedBytes, options: options) else {
221
+ guard var decodedBytes = base64Data
222
+ . withUnsafeBytes ( { NSData . base64DecodeBytes ( $0, options: options) } ) else {
222
223
return nil
223
224
}
224
225
super. init ( )
@@ -670,7 +671,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
670
671
- parameter options: Options for handling invalid input
671
672
- returns: The decoded bytes.
672
673
*/
673
- private static func base64DecodeBytes( _ bytes: [ UInt8 ] , options: Base64DecodingOptions = [ ] ) -> [ UInt8 ] ? {
674
+ private static func base64DecodeBytes( _ bytes: UnsafeRawBufferPointer , options: Base64DecodingOptions = [ ] ) -> [ UInt8 ] ? {
674
675
var decodedBytes = [ UInt8] ( )
675
676
decodedBytes. reserveCapacity ( ( bytes. count/ 3 ) * 2 )
676
677
0 commit comments