SQLite – UTF–8 printf()

SQLite's printf function mimics a behavior of a standard C printf function. C library's printf operates on (single byte) characters and does not care about multi byte characters — this fact concerns width and precision specifications of format parameter. Those specifications determine number of bytes (or single byte characters), however, in case of SQLite, where all strings are in UTF–8 encoding form and characters like äöü occupy 2 bytes, it would be easier to have a function which treats width and precision as numbers of UTF–8 characters/codepoints rather then bytes.

Minor version

Due to a fact that the SQLite team implemented UTF–8 codepoints after version 3.22.0 (check–in [c883c4d3]), I will not continue a main version of the patch. Instead a minor version will be continued, which makes that printf() will not stringify supplied values (for c format specification), if they are INTEGER or FLOAT. Instead they will be treated as UTF–32 codepoints (and silently converted to U+FFFD if not in a Unicode range):

Main version

The following patch implements above–mentioned features and uses flags, which control parts of a code to be compiled. A flag will be unset (false/0) when it is undefined or defined to 0. A flag will be set (true/!0) when it is defined to an empty replacement list (-DCHN_SQLITE_ENABLE_UTF8PRINTF_S) or to something other then 0 (- DCHN_SQLITE_ENABLE_UTF8PRINTF_S=1). If all flags are unset then the patched files will behave like original ones:


Mail me