We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 68a5343 + 3428368 commit db0c39fCopy full SHA for db0c39f
stdlib/public/Darwin/Foundation/NSStringAPI.swift
@@ -73,10 +73,10 @@ internal func _persistCString(_ p: UnsafePointer<CChar>?) -> [CChar]? {
73
guard let cString = p else {
74
return nil
75
}
76
- let len = UTF8._nullCodeUnitOffset(in: cString)
77
- var result = [CChar](repeating: 0, count: len + 1)
78
- for i in 0..<len {
79
- result[i] = cString[i]
+ let bytesToCopy = UTF8._nullCodeUnitOffset(in: cString) + 1 // +1 for the terminating NUL
+ let result = [CChar](unsafeUninitializedCapacity: bytesToCopy) { buffer, initializedCount in
+ buffer.baseAddress!.initialize(from: cString, count: bytesToCopy)
+ initializedCount = bytesToCopy
80
81
return result
82
0 commit comments