Skip to content

Instantly share code, notes, and snippets.

@mytskine
Last active December 14, 2015 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mytskine/5028755 to your computer and use it in GitHub Desktop.
Save mytskine/5028755 to your computer and use it in GitHub Desktop.
Patch to SQLite'extension "ICU" to create collations that ignore case and accents. It adds to `icu_load_collation()` an optional third parameter that sets the collation strength (default=3).
diff --git a/icu.c b/icu.c
index ae28d70..63d26b9 100644
--- a/icu.c
+++ b/icu.c
@@ -418,10 +418,14 @@ static void icuLoadCollation(
UErrorCode status = U_ZERO_ERROR;
const char *zLocale; /* Locale identifier - (eg. "jp_JP") */
const char *zName; /* SQL Collation sequence name (eg. "japanese") */
+ int collStrength; /* Collation strength: 0..4 (default,primary..quaternary) */
UCollator *pUCollator; /* ICU library collation object */
int rc; /* Return code from sqlite3_create_collation_x() */
- assert(nArg==2);
+ if (nArg!=2 && nArg!=3) {
+ sqlite3_result_error(p,
+ "wrong number of arguments to function icu_load_collation()", -1);
+ }
zLocale = (const char *)sqlite3_value_text(apArg[0]);
zName = (const char *)sqlite3_value_text(apArg[1]);
@@ -436,6 +440,13 @@ static void icuLoadCollation(
}
assert(p);
+ if (nArg==3) {
+ collStrength = sqlite3_value_int(apArg[2]);
+ if (collStrength>0 && collStrength<=4) {
+ ucol_setStrength(pUCollator, collStrength-1);
+ }
+ }
+
rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator,
icuCollationColl, icuCollationDel
);
@@ -471,7 +482,7 @@ int sqlite3IcuInit(sqlite3 *db){
{"like", 2, SQLITE_UTF8, 0, icuLikeFunc},
{"like", 3, SQLITE_UTF8, 0, icuLikeFunc},
- {"icu_load_collation", 2, SQLITE_UTF8, (void*)db, icuLoadCollation},
+ {"icu_load_collation", -1, SQLITE_UTF8, (void*)db, icuLoadCollation},
};
int rc = SQLITE_OK;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment