Use withUnsafeTemporaryAllocation instead of a temporary Array

This commit is contained in:
David Smith
2022-03-17 14:54:05 -07:00
parent abd0582105
commit 9ad3c9a5db

View File

@@ -396,17 +396,15 @@ extension _StringGuts {
).0))
}
// TODO(String performance): Stack buffer if small enough
let cus = Array<UInt16>(unsafeUninitializedCapacity: count) {
buffer, initializedCapacity in
return withUnsafeTemporaryAllocation(
of: UInt16.self, capacity: count
) { buffer in
_cocoaStringCopyCharacters(
from: self._object.cocoaObject,
range: start..<end,
into: buffer.baseAddress._unsafelyUnwrappedUnchecked)
initializedCapacity = count
}
return cus.withUnsafeBufferPointer {
return Character(String._uncheckedFromUTF16($0))
into: buffer.baseAddress._unsafelyUnwrappedUnchecked
)
return Character(String._uncheckedFromUTF16(UnsafeBufferPointer(buffer)))
}
#else
fatalError("No foreign strings on Linux in this version of Swift")