The old `String.fromCString` function was convenient for wrapping inessential C function calls, if the `nil` coalescing operator can provide a fallback:
let message =
String.fromCString(sqlite3_errmsg(handle)) ??
String.fromCString(sqlite3_errstr(result)) ?? ""
I also think the `validatingUTF8` argument label is misleading, because neither initializer allows ill-formed UTF-8 data to be copied.
If the precondition is removed, it should be possible to combine the initializers:
/// Create a new `String` by copying the nul-terminated UTF-8 data
/// referenced by a `cString`.
///
/// Fails if the `cString` is `NULL`; or if it contains ill-formed code
/// units and no repairing has been requested. Otherwise replaces
/// ill-formed code units with replacement characters (U+FFFD).
public init?(
cString: UnsafePointer<CChar>,
repairingInvalidCodeUnits isRepairing: Bool = true
) {
guard let (result, _) = String.decodeCString(
UnsafePointer(cString),
as: UTF8.self,
repairingInvalidCodeUnits: isRepairing) else {
return nil
}
self = result
}
The old `String.fromCString` function was convenient for wrapping inessential C function calls, if the `nil` coalescing operator can provide a fallback:
let message =
String.fromCString(sqlite3_errmsg(handle)) ??
String.fromCString(sqlite3_errstr(result)) ?? ""
It is a part of a long-term strategy to make UnsafePointer
non-nullable. Nullable pointers will be modeled as optional pointers.
So this code would look like this, assuming that sqlite3 is returning
optional (nullable) pointers: