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 7d8bef6 + 38590d6 commit c1fee3aCopy full SHA for c1fee3a
Sources/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) { buf, initedCount in
+ buf.baseAddress!.assign(from: cString, count: bytesToCopy)
+ initedCount = bytesToCopy
80
81
return result
82
0 commit comments