File tree Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -401,8 +401,13 @@ extension String {
401
401
public init < C: Collection , Encoding: Unicode . Encoding > (
402
402
decoding codeUnits: C , as sourceEncoding: Encoding . Type
403
403
) where C. Iterator. Element == Encoding . CodeUnit {
404
+ guard _fastPath ( sourceEncoding == UTF8 . self) else {
405
+ self = String . _fromCodeUnits (
406
+ codeUnits, encoding: sourceEncoding, repair: true ) !. 0
407
+ return
408
+ }
409
+
404
410
if let contigBytes = codeUnits as? _HasContiguousBytes ,
405
- sourceEncoding == UTF8 . self,
406
411
contigBytes. _providesContiguousBytesNoCopy
407
412
{
408
413
self = contigBytes. withUnsafeBytes { rawBufPtr in
@@ -414,8 +419,13 @@ extension String {
414
419
return
415
420
}
416
421
417
- self = String . _fromCodeUnits (
418
- codeUnits, encoding: sourceEncoding, repair: true ) !. 0
422
+ // Just copying to an Array is significantly faster than performing
423
+ // generic operations
424
+ self = Array ( codeUnits) . withUnsafeBufferPointer {
425
+ let raw = UnsafeRawBufferPointer ( $0)
426
+ return String . _fromUTF8Repairing ( raw. bindMemory ( to: UInt8 . self) ) . 0
427
+ }
428
+ return
419
429
}
420
430
421
431
/// Calls the given closure with a pointer to the contents of the string,
You can’t perform that action at this time.
0 commit comments