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.
1 parent 55e3e82 commit 38590d6Copy full SHA for 38590d6
Sources/Foundation/NSStringAPI.swift
@@ -73,13 +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
- let result = [CChar](unsafeUninitializedCapacity: len + 1) { buf, initedCount in
78
- for i in 0..<len {
79
- buf[i] = cString[i]
80
- }
81
- buf[len] = 0
82
- initedCount = len + 1
+ 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
83
84
return result
85
0 commit comments