Skip to content

Commit

Permalink
Use CChar instead of Int8 when we deal with C strings
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Jul 29, 2023
1 parent 0b2b26f commit 9ff58c3
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion GRDB/Core/Database+Statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ extension SQLStatementCursor: Cursor {
let baseAddress = buffer.baseAddress! // never nil because the buffer contains the trailing \0.

// Compile next statement
var statementEnd: UnsafePointer<Int8>? = nil
var statementEnd: UnsafePointer<CChar>? = nil
let statement = try Statement(
database: database,
statementStart: baseAddress + offset,
Expand Down
4 changes: 2 additions & 2 deletions GRDB/Core/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
_ mask: CInt,
_ p: UnsafeMutableRawPointer?,
_ x: UnsafeMutableRawPointer?,
_ sqlite3_expanded_sql: @escaping @convention(c) (OpaquePointer?) -> UnsafeMutablePointer<Int8>?)
_ sqlite3_expanded_sql: @escaping @convention(c) (OpaquePointer?) -> UnsafeMutablePointer<CChar>?)
{
guard let trace else { return }

Expand Down Expand Up @@ -1896,7 +1896,7 @@ extension Database {
public struct Statement: CustomStringConvertible {
var sqliteStatement: SQLiteStatement
var unexpandedSQL: UnsafePointer<CChar>?
var sqlite3_expanded_sql: @convention(c) (OpaquePointer?) -> UnsafeMutablePointer<Int8>?
var sqlite3_expanded_sql: @convention(c) (OpaquePointer?) -> UnsafeMutablePointer<CChar>?
var publicStatementArguments: Bool // See Configuration.publicStatementArguments

/// The executed SQL, where bound parameters are not expanded.
Expand Down
4 changes: 2 additions & 2 deletions GRDB/Core/Statement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public final class Statement {
/// - throws: DatabaseError in case of compilation error.
required init?(
database: Database,
statementStart: UnsafePointer<Int8>,
statementEnd: UnsafeMutablePointer<UnsafePointer<Int8>?>,
statementStart: UnsafePointer<CChar>,
statementEnd: UnsafeMutablePointer<UnsafePointer<CChar>?>,
prepFlags: CUnsignedInt) throws
{
SchedulingWatchdog.preconditionValidQueue(database)
Expand Down
8 changes: 4 additions & 4 deletions GRDB/Core/StatementAuthorizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ final class StatementAuthorizer {

private func authorize(
_ actionCode: CInt,
_ cString1: UnsafePointer<Int8>?,
_ cString2: UnsafePointer<Int8>?,
_ cString3: UnsafePointer<Int8>?,
_ cString4: UnsafePointer<Int8>?)
_ cString1: UnsafePointer<CChar>?,
_ cString2: UnsafePointer<CChar>?,
_ cString3: UnsafePointer<CChar>?,
_ cString4: UnsafePointer<CChar>?)
-> CInt
{
// Uncomment when debugging
Expand Down
2 changes: 1 addition & 1 deletion GRDB/Core/Support/Foundation/DatabaseDateComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ extension DatabaseDateComponents: StatementColumnConvertible {
}
let length = Int(sqlite3_column_bytes(sqliteStatement, index)) // avoid an strlen
let components = cString.withMemoryRebound(
to: Int8.self,
to: CChar.self,
capacity: length + 1 /* trailing \0 */) { cString in
SQLiteDateParser().components(cString: cString, length: length)
}
Expand Down
14 changes: 7 additions & 7 deletions GRDB/Core/TransactionObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ public struct DatabaseEvent {
self.impl = impl
}

init(kind: Kind, rowID: Int64, databaseNameCString: UnsafePointer<Int8>?, tableNameCString: UnsafePointer<Int8>?) {
init(kind: Kind, rowID: Int64, databaseNameCString: UnsafePointer<CChar>?, tableNameCString: UnsafePointer<CChar>?) {
self.init(
kind: kind,
rowID: rowID,
Expand Down Expand Up @@ -1131,8 +1131,8 @@ private protocol DatabaseEventImpl {
/// Optimization: MetalDatabaseEventImpl does not create Swift strings from raw
/// SQLite char* until actually asked for databaseName or tableName.
private struct MetalDatabaseEventImpl: DatabaseEventImpl {
let databaseNameCString: UnsafePointer<Int8>?
let tableNameCString: UnsafePointer<Int8>?
let databaseNameCString: UnsafePointer<CChar>?
let tableNameCString: UnsafePointer<CChar>?

var databaseName: String { String(cString: databaseNameCString!) }
var tableName: String { String(cString: tableNameCString!) }
Expand Down Expand Up @@ -1267,8 +1267,8 @@ public struct DatabasePreUpdateEvent {
kind: Kind,
initialRowID: Int64,
finalRowID: Int64,
databaseNameCString: UnsafePointer<Int8>?,
tableNameCString: UnsafePointer<Int8>?)
databaseNameCString: UnsafePointer<CChar>?,
tableNameCString: UnsafePointer<CChar>?)
{
self.init(
kind: kind,
Expand Down Expand Up @@ -1324,8 +1324,8 @@ private struct MetalDatabasePreUpdateEventImpl: DatabasePreUpdateEventImpl {
let connection: SQLiteConnection
let kind: DatabasePreUpdateEvent.Kind

let databaseNameCString: UnsafePointer<Int8>?
let tableNameCString: UnsafePointer<Int8>?
let databaseNameCString: UnsafePointer<CChar>?
let tableNameCString: UnsafePointer<CChar>?

var databaseName: String { String(cString: databaseNameCString!) }
var tableName: String { String(cString: tableNameCString!) }
Expand Down
6 changes: 3 additions & 3 deletions GRDB/FTS/FTS5.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ public struct FTS5 {
private static func api_v2(
_ db: Database,
// swiftlint:disable:next line_length
_ sqlite3_prepare_v3: @convention(c) (OpaquePointer?, UnsafePointer<Int8>?, CInt, CUnsignedInt, UnsafeMutablePointer<OpaquePointer?>?, UnsafeMutablePointer<UnsafePointer<Int8>?>?) -> CInt,
_ sqlite3_prepare_v3: @convention(c) (OpaquePointer?, UnsafePointer<CChar>?, CInt, CUnsignedInt, UnsafeMutablePointer<OpaquePointer?>?, UnsafeMutablePointer<UnsafePointer<CChar>?>?) -> CInt,
// swiftlint:disable:next line_length
_ sqlite3_bind_pointer: @convention(c) (OpaquePointer?, CInt, UnsafeMutableRawPointer?, UnsafePointer<Int8>?, (@convention(c) (UnsafeMutableRawPointer?) -> Void)?) -> CInt)
_ sqlite3_bind_pointer: @convention(c) (OpaquePointer?, CInt, UnsafeMutableRawPointer?, UnsafePointer<CChar>?, (@convention(c) (UnsafeMutableRawPointer?) -> Void)?) -> CInt)
-> UnsafePointer<fts5_api>
{
var statement: SQLiteStatement? = nil
Expand All @@ -157,7 +157,7 @@ public struct FTS5 {
fatalError("FTS5 is not available")
}
defer { sqlite3_finalize(statement) }
type.utf8Start.withMemoryRebound(to: Int8.self, capacity: type.utf8CodeUnitCount) { typePointer in
type.utf8Start.withMemoryRebound(to: CChar.self, capacity: type.utf8CodeUnitCount) { typePointer in
_ = sqlite3_bind_pointer(statement, 1, &api, typePointer, nil)
}
sqlite3_step(statement)
Expand Down
4 changes: 2 additions & 2 deletions GRDB/FTS/FTS5CustomTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ extension Database {
tokenizerPointer: OpaquePointer?,
context: UnsafeMutableRawPointer?,
flags: CInt,
pText: UnsafePointer<Int8>?,
pText: UnsafePointer<CChar>?,
nText: CInt,
// swiftlint:disable:next line_length
tokenCallback: (@convention(c) (UnsafeMutableRawPointer?, CInt, UnsafePointer<Int8>?, CInt, CInt, CInt) -> CInt)?)
tokenCallback: (@convention(c) (UnsafeMutableRawPointer?, CInt, UnsafePointer<CChar>?, CInt, CInt, CInt) -> CInt)?)
-> CInt
{
guard let tokenizerPointer else {
Expand Down
14 changes: 7 additions & 7 deletions GRDB/FTS/FTS5Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Foundation
public typealias FTS5TokenCallback = @convention(c) (
_ context: UnsafeMutableRawPointer?,
_ flags: CInt,
_ pToken: UnsafePointer<Int8>?,
_ pToken: UnsafePointer<CChar>?,
_ nToken: CInt,
_ iStart: CInt,
_ iEnd: CInt)
Expand Down Expand Up @@ -77,7 +77,7 @@ public protocol FTS5Tokenizer: AnyObject {
func tokenize(
context: UnsafeMutableRawPointer?,
tokenization: FTS5Tokenization,
pText: UnsafePointer<Int8>?,
pText: UnsafePointer<CChar>?,
nText: CInt,
tokenCallback: @escaping FTS5TokenCallback)
-> CInt
Expand Down Expand Up @@ -143,7 +143,7 @@ extension FTS5Tokenizer {
guard let addr = buffer.baseAddress else {
return []
}
let pText = UnsafeMutableRawPointer(mutating: addr).assumingMemoryBound(to: Int8.self)
let pText = UnsafeMutableRawPointer(mutating: addr).assumingMemoryBound(to: CChar.self)
let nText = CInt(buffer.count)

var context = TokenizeContext()
Expand Down Expand Up @@ -206,8 +206,8 @@ extension Database {
} else {
func withArrayOfCStrings<Result>(
_ input: [String],
_ output: inout ContiguousArray<UnsafePointer<Int8>>,
_ accessor: (ContiguousArray<UnsafePointer<Int8>>) -> Result)
_ output: inout ContiguousArray<UnsafePointer<CChar>>,
_ accessor: (ContiguousArray<UnsafePointer<CChar>>) -> Result)
-> Result
{
if output.count == input.count {
Expand All @@ -219,7 +219,7 @@ extension Database {
}
}
}
var cStrings = ContiguousArray<UnsafePointer<Int8>>()
var cStrings = ContiguousArray<UnsafePointer<CChar>>()
cStrings.reserveCapacity(arguments.count)
code = withArrayOfCStrings(arguments, &cStrings) { (cStrings) in
cStrings.withUnsafeBufferPointer { azArg in
Expand Down Expand Up @@ -252,7 +252,7 @@ extension Database {
func tokenize(
context: UnsafeMutableRawPointer?,
tokenization: FTS5Tokenization,
pText: UnsafePointer<Int8>?,
pText: UnsafePointer<CChar>?,
nText: CInt,
tokenCallback: @escaping FTS5TokenCallback)
-> CInt
Expand Down
4 changes: 2 additions & 2 deletions GRDB/FTS/FTS5WrapperTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ extension FTS5WrapperTokenizer {
public func tokenize(
context: UnsafeMutableRawPointer?,
tokenization: FTS5Tokenization,
pText: UnsafePointer<Int8>?,
pText: UnsafePointer<CChar>?,
nText: CInt,
tokenCallback: @escaping FTS5TokenCallback)
-> CInt
Expand Down Expand Up @@ -138,7 +138,7 @@ extension FTS5WrapperTokenizer {
return
}
let pToken = UnsafeMutableRawPointer(mutating: addr)
.assumingMemoryBound(to: Int8.self)
.assumingMemoryBound(to: CChar.self)
let nToken = CInt(buffer.count)

// Inject token bytes into SQLite
Expand Down
4 changes: 2 additions & 2 deletions Tests/GRDBTests/CompilationProtocolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private class UserDatabaseValueConvertible2 : DatabaseValueConvertible {

#if SQLITE_ENABLE_FTS5
private class UserFTS5Tokenizer : FTS5Tokenizer {
func tokenize(context: UnsafeMutableRawPointer?, tokenization: FTS5Tokenization, pText: UnsafePointer<Int8>?, nText: CInt, tokenCallback: @escaping FTS5TokenCallback) -> CInt { preconditionFailure() }
func tokenize(context: UnsafeMutableRawPointer?, tokenization: FTS5Tokenization, pText: UnsafePointer<CChar>?, nText: CInt, tokenCallback: @escaping FTS5TokenCallback) -> CInt { preconditionFailure() }
}
#endif

Expand All @@ -62,7 +62,7 @@ private class UserFTS5Tokenizer : FTS5Tokenizer {
private class UserFTS5CustomTokenizer : FTS5CustomTokenizer {
static let name: String = "UserFTS5CustomTokenizer"
required init(db: Database, arguments: [String]) throws { preconditionFailure() }
func tokenize(context: UnsafeMutableRawPointer?, tokenization: FTS5Tokenization, pText: UnsafePointer<Int8>?, nText: CInt, tokenCallback: @escaping FTS5TokenCallback) -> CInt { preconditionFailure() }
func tokenize(context: UnsafeMutableRawPointer?, tokenization: FTS5Tokenization, pText: UnsafePointer<CChar>?, nText: CInt, tokenCallback: @escaping FTS5TokenCallback) -> CInt { preconditionFailure() }
}
#endif

Expand Down
10 changes: 5 additions & 5 deletions Tests/GRDBTests/FTS5CustomTokenizerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private final class StopWordsTokenizer : FTS5CustomTokenizer {
// TODO: test that deinit is called
}

func tokenize(context: UnsafeMutableRawPointer?, tokenization: FTS5Tokenization, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: @escaping FTS5TokenCallback) -> Int32 {
func tokenize(context: UnsafeMutableRawPointer?, tokenization: FTS5Tokenization, pText: UnsafePointer<CChar>?, nText: Int32, tokenCallback: @escaping FTS5TokenCallback) -> Int32 {

// The way we implement stop words is by letting wrappedTokenizer do its
// job but intercepting its tokens before they feed SQLite.
Expand Down Expand Up @@ -76,7 +76,7 @@ private final class NFKCTokenizer : FTS5CustomTokenizer {
// TODO: test that deinit is called
}

func tokenize(context: UnsafeMutableRawPointer?, tokenization: FTS5Tokenization, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: @escaping FTS5TokenCallback) -> Int32 {
func tokenize(context: UnsafeMutableRawPointer?, tokenization: FTS5Tokenization, pText: UnsafePointer<CChar>?, nText: Int32, tokenCallback: @escaping FTS5TokenCallback) -> Int32 {

// The way we implement NFKC conversion is by letting wrappedTokenizer
// do its job, but intercepting its tokens before they feed SQLite.
Expand Down Expand Up @@ -107,7 +107,7 @@ private final class NFKCTokenizer : FTS5CustomTokenizer {
guard let addr = buffer.baseAddress else {
return 0 // SQLITE_OK
}
let pToken = UnsafeMutableRawPointer(mutating: addr).assumingMemoryBound(to: Int8.self)
let pToken = UnsafeMutableRawPointer(mutating: addr).assumingMemoryBound(to: CChar.self)
let nToken = Int32(buffer.count)
return customContext.tokenCallback(customContext.context, flags, pToken, nToken, iStart, iEnd)
}
Expand Down Expand Up @@ -135,7 +135,7 @@ private final class SynonymsTokenizer : FTS5CustomTokenizer {
// TODO: test that deinit is called
}

func tokenize(context: UnsafeMutableRawPointer?, tokenization: FTS5Tokenization, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: @escaping FTS5TokenCallback) -> Int32 {
func tokenize(context: UnsafeMutableRawPointer?, tokenization: FTS5Tokenization, pText: UnsafePointer<CChar>?, nText: Int32, tokenCallback: @escaping FTS5TokenCallback) -> Int32 {
// Don't look for synonyms when tokenizing queries, as advised by
// https://www.sqlite.org/fts5.html#synonym_support
if tokenization.contains(.query) {
Expand Down Expand Up @@ -176,7 +176,7 @@ private final class SynonymsTokenizer : FTS5CustomTokenizer {
guard let addr = buffer.baseAddress else {
return 0 // SQLITE_OK
}
let pToken = UnsafeMutableRawPointer(mutating: addr).assumingMemoryBound(to: Int8.self)
let pToken = UnsafeMutableRawPointer(mutating: addr).assumingMemoryBound(to: CChar.self)
let nToken = Int32(buffer.count)
// Set FTS5_TOKEN_COLOCATED for all but first token
let synonymFlags = (index == 0) ? flags : flags | 1 // 1: FTS5_TOKEN_COLOCATED
Expand Down

0 comments on commit 9ff58c3

Please sign in to comment.