File tree Expand file tree Collapse file tree 1 file changed +4
-7
lines changed Expand file tree Collapse file tree 1 file changed +4
-7
lines changed Original file line number Diff line number Diff line change @@ -323,13 +323,10 @@ extension _StringGuts {
323
323
public // SPI(corelibs-foundation)
324
324
func _persistCString( _ p: UnsafePointer < CChar > ? ) -> [ CChar ] ? {
325
325
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
333
330
}
334
331
return result
335
332
}
You can’t perform that action at this time.
0 commit comments