From f3acfecc160ad710ba3526d0b2089de7ed23245e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 13 Sep 2025 12:21:36 +0200 Subject: [PATCH] DatabaseCollation no longer uses a deprecated String initializer --- GRDB/Core/Database.swift | 2 +- GRDB/Core/DatabaseCollation.swift | 36 ++++++++------- Tests/GRDBTests/DatabaseCollationTests.swift | 46 ++++++++++---------- 3 files changed, 44 insertions(+), 40 deletions(-) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 48eaa905c..e1bc96ee1 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -806,7 +806,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib collationPointer, { (collationPointer, length1, buffer1, length2, buffer2) in let collation = Unmanaged.fromOpaque(collationPointer!).takeUnretainedValue() - return CInt(collation.function(length1, buffer1, length2, buffer2).rawValue) + return collation.xCompare(length1, buffer1.unsafelyUnwrapped, length2, buffer2.unsafelyUnwrapped) }, nil) guard code == SQLITE_OK else { // Assume a GRDB bug: there is no point throwing any error. diff --git a/GRDB/Core/DatabaseCollation.swift b/GRDB/Core/DatabaseCollation.swift index 6748874c8..ea4ad3392 100644 --- a/GRDB/Core/DatabaseCollation.swift +++ b/GRDB/Core/DatabaseCollation.swift @@ -53,12 +53,21 @@ public final class DatabaseCollation: Identifiable, Sendable { } } + /// Feeds the `xCompare` parameter of sqlite3_create_collation_v2 + /// + typealias XCompare = @Sendable ( + _ length1: CInt, + _ buffer1: UnsafeRawPointer, + _ length2: CInt, + _ buffer2: UnsafeRawPointer + ) -> CInt + /// The identifier of the collation. public var id: ID { ID(name: name) } /// The name of the collation. public let name: String - let function: @Sendable (CInt, UnsafeRawPointer?, CInt, UnsafeRawPointer?) -> ComparisonResult + let xCompare: XCompare /// Creates a collation. /// @@ -75,21 +84,16 @@ public final class DatabaseCollation: Identifiable, Sendable { /// - parameters: /// - name: The collation name. /// - function: A function that compares two strings. - public init(_ name: String, function: @escaping @Sendable (String, String) -> ComparisonResult) { + public convenience init(_ name: String, function: @escaping @Sendable (String, String) -> ComparisonResult) { + self.init(name, xCompare: { (length1, buffer1, length2, buffer2) in + let string1 = String(decoding: UnsafeRawBufferPointer(start: buffer1, count: Int(length1)), as: UTF8.self) + let string2 = String(decoding: UnsafeRawBufferPointer(start: buffer2, count: Int(length2)), as: UTF8.self) + return CInt(function(string1, string2).rawValue) + }) + } + + init(_ name: String, xCompare: @escaping XCompare) { self.name = name - self.function = { (length1, buffer1, length2, buffer2) in - // Buffers are not C strings: they do not end with \0. - let string1 = String( - bytesNoCopy: UnsafeMutableRawPointer(mutating: buffer1.unsafelyUnwrapped), - length: Int(length1), - encoding: .utf8, - freeWhenDone: false)! - let string2 = String( - bytesNoCopy: UnsafeMutableRawPointer(mutating: buffer2.unsafelyUnwrapped), - length: Int(length2), - encoding: .utf8, - freeWhenDone: false)! - return function(string1, string2) - } + self.xCompare = xCompare } } diff --git a/Tests/GRDBTests/DatabaseCollationTests.swift b/Tests/GRDBTests/DatabaseCollationTests.swift index 366659683..0602595fb 100644 --- a/Tests/GRDBTests/DatabaseCollationTests.swift +++ b/Tests/GRDBTests/DatabaseCollationTests.swift @@ -6,38 +6,38 @@ class DatabaseCollationTests: GRDBTestCase { func testDefaultCollations() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in - try db.execute(sql: "CREATE TABLE strings (id INTEGER PRIMARY KEY, name TEXT)") - try db.execute(sql: "INSERT INTO strings VALUES (1, '1')") - try db.execute(sql: "INSERT INTO strings VALUES (2, '2')") - try db.execute(sql: "INSERT INTO strings VALUES (3, '10')") - try db.execute(sql: "INSERT INTO strings VALUES (4, 'a')") - try db.execute(sql: "INSERT INTO strings VALUES (5, 'à')") - try db.execute(sql: "INSERT INTO strings VALUES (6, 'A')") - try db.execute(sql: "INSERT INTO strings VALUES (7, 'Z')") - try db.execute(sql: "INSERT INTO strings VALUES (8, 'z')") + try db.execute(sql: """ + CREATE TABLE strings (id INTEGER PRIMARY KEY, name TEXT); + INSERT INTO strings VALUES (1, '1'); + INSERT INTO strings VALUES (2, '2'); + INSERT INTO strings VALUES (3, '10'); + INSERT INTO strings VALUES (4, 'a'); + INSERT INTO strings VALUES (5, 'à'); + INSERT INTO strings VALUES (6, 'A'); + INSERT INTO strings VALUES (7, 'Z'); + INSERT INTO strings VALUES (8, 'z'); + INSERT INTO strings VALUES (9, ''); + INSERT INTO strings VALUES (10, NULL); + INSERT INTO strings VALUES (11, x'42FF'); -- Invalid UTF8 "B�" + """) - // Swift 4.2 and Swift 4.1 don't sort strings in the same way - if "z" < "à" { - XCTAssertEqual( - try Int.fetchAll(db, sql: "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.unicodeCompare.name), id"), - [1,3,2,6,7,4,8,5]) - } else { - XCTAssertEqual( - try Int.fetchAll(db, sql: "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.unicodeCompare.name), id"), - [1,3,2,6,7,4,5,8]) - } + // Note that "B�" is always last. We can observe that SQLite + // does not invoke the collation for invalid UTF8 strings. + XCTAssertEqual( + try Int.fetchAll(db, sql: "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.unicodeCompare.name), id"), + [10,9,1,3,2,6,7,4,8,5,11]) XCTAssertEqual( try Int.fetchAll(db, sql: "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.caseInsensitiveCompare.name), id"), - [1,3,2,4,6,5,7,8]) + [10,9,1,3,2,4,6,5,7,8,11]) XCTAssertEqual( try Int.fetchAll(db, sql: "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.localizedCaseInsensitiveCompare.name), id"), - [1,3,2,4,6,5,7,8]) + [10,9,1,3,2,4,6,5,7,8,11]) XCTAssertEqual( try Int.fetchAll(db, sql: "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.localizedCompare.name), id"), - [1,3,2,4,6,5,8,7]) + [10,9,1,3,2,4,6,5,8,7,11]) XCTAssertEqual( try Int.fetchAll(db, sql: "SELECT id FROM strings ORDER BY name COLLATE \(DatabaseCollation.localizedStandardCompare.name), id"), - [1,2,3,4,6,5,8,7]) + [10,9,1,2,3,4,6,5,8,7,11]) } }