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:
Andrew Trick
2016-07-20 17:12:48 -07:00
parent 6e75e165e9
commit c7aa8284c9
5 changed files with 128 additions and 14 deletions

View File

@@ -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