mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Add basic CString <-> UTF8 API variants.
As proposed by SE-0107: UnsafeRawPointer: https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md#cstring-conversion Adds String.init(cString: UnsafePointer<UInt8>) Adds String.nulTerminatedUTF8CString: ContiguousArray<CChar> This is necessary for eliminating UnsafePointer conversion. Such conversion is extremely common for interoperability between Swift strings and C strings to bridge the difference between CChar and UTF8.CodeUnit. The standard library does not provide any convenient utilities for converting between the differently typed buffers. These APIs will handle the simplest cases involving C interoperability. More convenience can be added later.
This commit is contained in:
@@ -429,6 +429,10 @@ public struct UTF8 : UnicodeCodec {
|
||||
public static func _nullCodeUnitOffset(in input: UnsafePointer<CodeUnit>) -> Int {
|
||||
return Int(_swift_stdlib_strlen(UnsafePointer(input)))
|
||||
}
|
||||
// Support parsing C strings as-if they are UTF8 strings.
|
||||
public static func _nullCodeUnitOffset(in input: UnsafePointer<CChar>) -> Int {
|
||||
return Int(_swift_stdlib_strlen(input))
|
||||
}
|
||||
}
|
||||
|
||||
/// A codec for translating between Unicode scalar values and UTF-16 code
|
||||
|
||||
Reference in New Issue
Block a user