Skip to content

Commit 4715d68

Browse files
authored
Merge pull request #30237 from valeriyvan/RemoveRedundantZeroingStringGuts
Changes implementation of _persistCString from _StringGuts
2 parents be61002 + f49f6a9 commit 4715d68

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

stdlib/public/core/StringGuts.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,10 @@ extension _StringGuts {
325325
public // SPI(corelibs-foundation)
326326
func _persistCString(_ p: UnsafePointer<CChar>?) -> [CChar]? {
327327
guard let s = p else { return nil }
328-
let count = Int(_swift_stdlib_strlen(s))
329-
let result = [CChar](unsafeUninitializedCapacity: count + 1) { buf, initializedCount in
330-
for i in 0..<count {
331-
buf[i] = s[i]
332-
}
333-
buf[count] = 0
334-
initializedCount = count + 1
328+
let bytesToCopy = UTF8._nullCodeUnitOffset(in: s) + 1 // +1 for the terminating NUL
329+
let result = [CChar](unsafeUninitializedCapacity: bytesToCopy) { buf, initedCount in
330+
buf.baseAddress!.assign(from: s, count: bytesToCopy)
331+
initedCount = bytesToCopy
335332
}
336333
return result
337334
}

0 commit comments

Comments
 (0)