@@ -228,26 +228,28 @@ extension String {
228
228
return _slowFromCodeUnits ( input, encoding: encoding, repair: repair)
229
229
}
230
230
231
- var result : String ? = nil
232
-
233
- if let contigBytes = input as? _HasContiguousBytes ,
234
- contigBytes . _providesContiguousBytesNoCopy {
235
- result = contigBytes . withUnsafeBytes { rawBufPtr in
236
- let buffer = UnsafeBufferPointer (
237
- start : rawBufPtr . baseAddress ? . assumingMemoryBound ( to : UInt8 . self ) ,
238
- count : rawBufPtr . count )
239
- return String . _fromASCIIValidating ( buffer )
240
- }
231
+ // Needed for double-optional due to returning optional string in
232
+ // _fromASCIIValidating in withContiguousStorageIfAvailable
233
+ let resultOpt : String ?
234
+
235
+ if let strOpt = input . withContiguousStorageIfAvailable ( {
236
+ ( buffer : UnsafeBufferPointer < Input . Element > ) -> String ? in
237
+ return String . _fromASCIIValidating (
238
+ UnsafeRawBufferPointer ( buffer ) . bindMemory ( to : UInt8 . self ) )
239
+ } ) {
240
+ resultOpt = strOpt
241
241
} else {
242
- result = Array ( input) . withUnsafeBufferPointer {
242
+ resultOpt = Array ( input) . withUnsafeBufferPointer {
243
243
let buffer = UnsafeRawBufferPointer ( $0) . bindMemory ( to: UInt8 . self)
244
244
return String . _fromASCIIValidating ( buffer)
245
245
}
246
246
}
247
247
248
- return result != nil ?
249
- ( result!, repairsMade: false ) :
250
- _slowFromCodeUnits ( input, encoding: encoding, repair: repair)
248
+ guard let result = resultOpt else {
249
+ return _slowFromCodeUnits ( input, encoding: encoding, repair: repair)
250
+ }
251
+
252
+ return ( result, repairsMade: false )
251
253
}
252
254
253
255
public // @testable
0 commit comments