Skip to content

Commit f11f6b8

Browse files
committed
cleanup
1 parent 5bc521e commit f11f6b8

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

SQLite/Core/Connection.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -584,15 +584,20 @@ public final class Connection {
584584
/// - block: A collation function that takes two strings and returns the
585585
/// comparison result.
586586
public func createCollation(_ collation: String, _ block: @escaping (_ lhs: String, _ rhs: String) -> ComparisonResult) throws {
587-
// TODO correct capacity
588-
let box: Collation = { lhs, rhs in
589-
let lstr = String(cString: lhs.bindMemory(to: UInt8.self, capacity: 0))
590-
let rstr = String(cString: rhs.bindMemory(to: UInt8.self, capacity: 0))
591-
return Int32(Int(block(lstr, rstr).rawValue))
587+
let box: Collation = { (lhs:UnsafeRawPointer, rhs:UnsafeRawPointer) in
588+
let lstr = String(cString: lhs.assumingMemoryBound(to: UInt8.self))
589+
let rstr = String(cString: rhs.assumingMemoryBound(to: UInt8.self))
590+
return Int32(block(lstr, rstr).rawValue)
592591
}
593-
try check(sqlite3_create_collation_v2(handle, collation, SQLITE_UTF8, unsafeBitCast(box, to: UnsafeMutableRawPointer.self), { callback, _, lhs, _, rhs in
594-
unsafeBitCast(callback, to: Collation.self)(lhs!, rhs!)
595-
}, nil))
592+
try check(sqlite3_create_collation_v2(handle, collation, SQLITE_UTF8,
593+
unsafeBitCast(box, to: UnsafeMutableRawPointer.self),
594+
{ (callback:UnsafeMutableRawPointer?, _, lhs:UnsafeRawPointer?, _, rhs:UnsafeRawPointer?) in /* xCompare */
595+
if let lhs = lhs, let rhs = rhs {
596+
return unsafeBitCast(callback, to: Collation.self)(lhs, rhs)
597+
} else {
598+
fatalError("sqlite3_create_collation_v2 callback called with NULL pointer")
599+
}
600+
}, nil /* xDestroy */))
596601
collations[collation] = box
597602
}
598603
fileprivate typealias Collation = @convention(block) (UnsafeRawPointer, UnsafeRawPointer) -> Int32

0 commit comments

Comments
 (0)