Skip to content

Commit 7876eab

Browse files
authored
Merge pull request swiftlang#30131 from valeriyvan/RemoveRedundantZeroingStringGuts
Removes redundant buffer zeroing in StringGuts.swift by using `init(unsafeUninitializedCapacity:initializingWith:)
2 parents 686bbc7 + 47acd72 commit 7876eab

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

stdlib/public/core/StringGuts.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,12 @@ public // SPI(corelibs-foundation)
324324
func _persistCString(_ p: UnsafePointer<CChar>?) -> [CChar]? {
325325
guard let s = p else { return nil }
326326
let count = Int(_swift_stdlib_strlen(s))
327-
var result = [CChar](repeating: 0, count: count + 1)
328-
for i in 0..<count {
329-
result[i] = s[i]
327+
let result = [CChar](unsafeUninitializedCapacity: count + 1) { buf, initializedCount in
328+
for i in 0..<count {
329+
buf[i] = s[i]
330+
}
331+
buf[count] = 0
332+
initializedCount = count + 1
330333
}
331334
return result
332335
}

0 commit comments

Comments
 (0)