From eedd165e5ba57c87345f0f349703ec7cfe5a4e7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 25 Aug 2024 15:34:35 +0200 Subject: [PATCH] DatabaseCollation is Identifiable --- GRDB/Core/DatabaseCollation.swift | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/GRDB/Core/DatabaseCollation.swift b/GRDB/Core/DatabaseCollation.swift index 19ae4baed..792531da9 100644 --- a/GRDB/Core/DatabaseCollation.swift +++ b/GRDB/Core/DatabaseCollation.swift @@ -29,7 +29,33 @@ import Foundation /// - ``localizedCompare`` /// - ``localizedStandardCompare`` /// - ``unicodeCompare`` -public final class DatabaseCollation: Sendable { +public final class DatabaseCollation: Identifiable, Sendable { + /// The identifier of an SQLite collation. + /// + /// SQLite identifies collations by their name (case insensitive). + public struct ID: Hashable { + var name: String + + // Collation equality is based on the sqlite3_strnicmp SQLite function. + // (see https://www.sqlite.org/c3ref/create_collation.html). Computing + // a hash value that honors the Swift Hashable contract (value equality + // implies hash equality) is thus non trivial. But it's not that + // important, since this hashValue is only used when one adds + // or removes a collation from a database connection. + public func hash(into hasher: inout Hasher) { + hasher.combine(0) + } + + /// Two collations are equal if they share the same name (case insensitive) + public static func == (lhs: Self, rhs: Self) -> Bool { + // See + return sqlite3_stricmp(lhs.name, rhs.name) == 0 + } + } + + /// 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