Add extension method swiftString to get a UTF-8 Swift string — so we don’t have UTF-16-backed Strings, which take more memory (generally) and are slower to manipulate in our code which prepares Strings for UI display (stripping HTML etc.).

This commit is contained in:
Brent Simmons committed 2026-04-26 10:50:58 -07:00
1 parent 200e5b19f3
commit 3dfe7e19b5
1 file changed
+22
@@ -35,4 +35,26 @@ public extension FMResultSet {
func mapToSet<T>(_ completion: (_ row: FMResultSet) -> T?) -> Set<T> {
return Set(compactMap(completion))
}
/// Returns a native UTF-8 Swift `String` for a TEXT column, bypassing
/// `NSString` bridging. Avoids the UTF-16 storage cost typical of
/// `NSString` for non-ASCII content. Returns `nil` for SQL `NULL`.
///
/// Reads through `dataNoCopy(forColumn:)`, which wraps SQLite-owned
/// bytes; the bytes are consumed synchronously by `String(decoding:as:)`
/// before this call returns, so the SQLite "valid until next column
/// access" lifetime is respected.
func swiftString(forColumn columnName: String) -> String? {
guard let data = dataNoCopy(forColumn: columnName) else {
return nil
}
return String(decoding: data, as: UTF8.self)
}
func swiftString(forColumnIndex columnIdx: Int32) -> String? {
guard let data = dataNoCopy(forColumnIndex: columnIdx) else {
return nil
}
return String(decoding: data, as: UTF8.self)
}
}