Skip to content

Commit c1fee3a

Browse files
authored
Merge pull request #2701 from valeriyvan/RedundantZeroingNSStringAPI
Removes redundant buffer zeroing in NSStringAPI
2 parents 7d8bef6 + 38590d6 commit c1fee3a

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Sources/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) { buf, initedCount in
78+
buf.baseAddress!.assign(from: cString, count: bytesToCopy)
79+
initedCount = bytesToCopy
8080
}
8181
return result
8282
}

0 commit comments

Comments
 (0)