Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(** Text encodings.
A SQLite database stores every TEXT value in one encoding, which its header names (fileformat2.html sec.1.3.13). The engine's text is UTF-8 throughout: a database in another encoding converts at the record boundary, as sqlite3 converts a value at its API, and its BINARY collation compares the stored bytes, which order UTF-16 text by code unit rather than by character.
The conversions are sqlite3's own (utf.c, sqlite3VdbeMemTranslate), byte for byte on malformed input too: a byte UTF-8 cannot start a character with, an overlong form and a UTF-16 surrogate read from UTF-8 become U+FFFD; a UTF-16 surrogate takes the unit after it whatever that unit is; an odd last byte of UTF-16 is dropped. sqlite3 converts a value only when it is read as text, so a stored text that does not convert back to its bytes is still copied as it is; {!to_utf8} remembers the bytes of such a text, and {!of_utf8} of that very string gives them back. *)
(** The type for text encodings. *)type t = | Utf8 (** UTF-8 *) | Utf16le (** UTF-16, least significant byte first *) | Utf16be (** UTF-16, most significant byte first *)
val pp : t Fmt.t(** [pp] formats an encoding by its SQL name ({!to_string}). *)
val equal : t -> t -> bool(** [equal a b] is [true] iff [a] and [b] are the same encoding. *)
val to_string : t -> string(** [to_string e] is [e]'s name as [PRAGMA encoding] answers it: ["UTF-8"], ["UTF-16le"] or ["UTF-16be"]. *)
val of_string : string -> t option(** [of_string name] is the encoding sqlite3 names [name], without regard to case (pragma.c, PragTyp_ENCODING): ["UTF8"] and ["UTF-8"]; ["UTF-16le"], ["UTF16le"], ["UTF-16be"] and ["UTF16be"]; ["UTF-16"] and ["UTF16"], which name the machine's own byte order. [None] for any other name. *)
val code : t -> int(** [code e] is the value the header field at offset 56 holds for [e]: [1], [2] or [3]. *)
val of_code : int -> t(** [of_code n] is the encoding of a header whose field at offset 56 is [n]: its low two bits, [2] UTF-16le, [3] UTF-16be and anything else UTF-8, as sqlite3 reads it (prepare.c, sqlite3InitOne). *)
val of_utf8 : t -> string -> string(** [of_utf8 e s] is the UTF-8 text [s] as [e] stores it. [of_utf8 Utf8 s] is [s] itself. Where [s] is, physically, a string {!to_utf8} [e] made of bytes it does not convert back to, it is those bytes: a copy of [s], or the same text spelt by anything else, converts. *)
val to_utf8 : t -> string -> int -> int -> string(** [to_utf8 e buf off len] is the text stored in [e] in the [len] bytes of [buf] at [off], as UTF-8, as sqlite3 reads it. {!of_utf8} [e] of the result is those bytes, whatever they are.
@raise Invalid_argument if [off] and [len] do not designate a slice of [buf]. *)
val compare_text : t -> string -> string -> int(** [compare_text e x y] orders the UTF-8 texts [x] and [y] as the BINARY collation orders them in a database stored in [e]: by the bytes {!of_utf8} [e] makes of them. It is [String.compare] for {!Utf8}, and differs from it in UTF-16 for any text holding a character past U+007F in little-endian order, and holding one past U+FFFF in big-endian order. *)