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 @@ -325,13 +325,10 @@ extension _StringGuts {
325
325
public // SPI(corelibs-foundation)
326
326
func _persistCString( _ p: UnsafePointer < CChar > ? ) -> [ CChar ] ? {
327
327
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
335
332
}
336
333
return result
337
334
}
You can’t perform that action at this time.
0 commit comments