Skip to content

Commit 9c7624c

Browse files
committed
Removes redundant buffer zeroing in _persistCString func
1 parent dc42259 commit 9c7624c

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

stdlib/public/Darwin/Foundation/NSStringAPI.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ internal func _persistCString(_ p: UnsafePointer<CChar>?) -> [CChar]? {
7373
guard let cString = p else {
7474
return nil
7575
}
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]
76+
let bytesToCopy = UTF8._nullCodeUnitOffset(in: cString) + 1 // +1 for the terminating NUL
77+
let result = [CChar](unsafeUninitializedCapacity: bytesToCopy) { buffer, initializedCount in
78+
buffer.baseAddress!.assign(from: cString, count: bytesToCopy)
79+
initializedCount = bytesToCopy
8080
}
8181
return result
8282
}

0 commit comments

Comments
 (0)