Skip to content

Commit 190b8a7

Browse files
committed
Changes implementation of _persistCString from _StringGuts to be in sync with implementation from stdlib
1 parent 34353b8 commit 190b8a7

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
@@ -323,13 +323,10 @@ extension _StringGuts {
323323
public // SPI(corelibs-foundation)
324324
func _persistCString(_ p: UnsafePointer<CChar>?) -> [CChar]? {
325325
guard let s = p else { return nil }
326-
let count = Int(_swift_stdlib_strlen(s))
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
326+
let bytesToCopy = UTF8._nullCodeUnitOffset(in: s) + 1 // +1 for the terminating NUL
327+
let result = [CChar](unsafeUninitializedCapacity: bytesToCopy) { buf, initedCount in
328+
buf.baseAddress!.assign(from: cString, count: bytesToCopy)
329+
initedCount = bytesToCopy
333330
}
334331
return result
335332
}

0 commit comments

Comments
 (0)