From 3dfe7e19b5d732eb5edc11b0dddfbfe447fd361c Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 26 Apr 2026 10:50:58 -0700 Subject: [PATCH] =?UTF-8?q?Add=20extension=20method=20`swiftString`=20to?= =?UTF-8?q?=20get=20a=20UTF-8=20Swift=20string=20=E2=80=94=20so=20we=20don?= =?UTF-8?q?=E2=80=99t=20have=20UTF-16-backed=20Strings,=20which=20take=20m?= =?UTF-8?q?ore=20memory=20(generally)=20and=20are=20slower=20to=20manipula?= =?UTF-8?q?te=20in=20our=20code=20which=20prepares=20Strings=20for=20UI=20?= =?UTF-8?q?display=20(stripping=20HTML=20etc.).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RSDatabase/FMResultSet+Extras.swift | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Modules/RSDatabase/Sources/RSDatabase/FMResultSet+Extras.swift b/Modules/RSDatabase/Sources/RSDatabase/FMResultSet+Extras.swift index 9d53ff5d7..47f7920c7 100644 --- a/Modules/RSDatabase/Sources/RSDatabase/FMResultSet+Extras.swift +++ b/Modules/RSDatabase/Sources/RSDatabase/FMResultSet+Extras.swift @@ -35,4 +35,26 @@ public extension FMResultSet { func mapToSet(_ completion: (_ row: FMResultSet) -> T?) -> Set { 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) + } }