dict @ 59f03c5663d07f87993d682e7fd07b453fc19a11

feat: Add spellfix extension
   1diff --git a/CMakeLists.txt b/CMakeLists.txt
   2index c34734d2daf984075b1bed87f236c2ac33e5ded2..fd48cee9086754409f9cec0ac69cc280720c3188 100644
   3--- a/CMakeLists.txt
   4+++ b/CMakeLists.txt
   5@@ -7,5 +7,7 @@ add_executable(dict ${src})
   6 
   7 target_compile_options(dict PRIVATE -Wall -Wextra -Wpedantic -Werror)
   8 target_include_directories(dict PUBLIC "${PROJECT_BINARY_DIR}")
   9-target_link_libraries(dict sqlite3 ncursesw)
  10+target_link_libraries(dict sqlite3 ncursesw m)
  11 
  12+
  13+add_subdirectory(ext)
  14diff --git a/data.c b/data.c
  15index 81ee9f31356c495fe4754f9105a3211f863e9956..2c3f3bb0bbc811c9a8c3552e84d60f8c50714892 100644
  16--- a/data.c
  17+++ b/data.c
  18@@ -11,14 +11,20 @@ Data* new_data(const char* con) {
  19     Data* data = (Data*)malloc(sizeof(Data));
  20 
  21     int v = sqlite3_open(con, &(data->db));
  22-    if (v == SQLITE_OK) {
  23-        return data;
  24+    if (v != SQLITE_OK) {
  25+        print_result_code(v);
  26+        return NULL;
  27     }
  28 
  29-    print_result_code(v);
  30-    return NULL;
  31-}
  32+    sqlite3_enable_load_extension(data->db, 1);
  33+    v = sqlite3_load_extension(data->db, "ext/libsqlite3ext", "sqlite3_spellfix_init",0);
  34+    if (v != SQLITE_OK) {
  35+        print_result_code(v);
  36+        return NULL;
  37+    }
  38 
  39+    return data;
  40+}
  41 
  42 void free_data(Data* data) {
  43     sqlite3_close(data->db);
  44@@ -103,85 +109,5 @@     return list;
  45 }
  46 
  47 void print_result_code(int code) {
  48-    switch(code) {
  49-        // Primary result code
  50-        case SQLITE_ABORT:
  51-            printf("SQLITE_ABORT");
  52-            break;
  53-        case SQLITE_AUTH:
  54-            printf("SQLITE_AUTH");
  55-            break;
  56-        case SQLITE_BUSY:
  57-            printf("SQLITE_BUSY");
  58-            break;
  59-        case SQLITE_CANTOPEN:
  60-            printf("SQLITE_CANTOPEN");
  61-            break;
  62-        case SQLITE_CONSTRAINT:
  63-            printf("SQLITE_CONSTRAINT");
  64-            break;
  65-        case SQLITE_CORRUPT:
  66-            printf("SQLITE_CORRUPT");
  67-            break;
  68-        case SQLITE_DONE:
  69-            printf("SQLITE_DONE");
  70-            break;
  71-        case SQLITE_EMPTY:
  72-            printf("SQLITE_EMPTY");
  73-            break;
  74-        case SQLITE_ERROR:
  75-            printf("SQLITE_ERROR");
  76-            break;
  77-        case SQLITE_FORMAT:
  78-            printf("SQLITE_FORMAT");
  79-            break;
  80-        case SQLITE_INTERNAL:
  81-            printf("SQLITE_INTERNAL");
  82-            break;
  83-        case SQLITE_INTERRUPT:
  84-            printf("SQLITE_INTERRUPT");
  85-            break;
  86-        case SQLITE_IOERR:
  87-            printf("SQLITE_IOERR");
  88-            break;
  89-        case SQLITE_LOCKED:
  90-            printf("SQLITE_LOCKED");
  91-            break;
  92-        case SQLITE_MISMATCH:
  93-            printf("SQLITE_MISMATCH");
  94-            break;
  95-        case SQLITE_MISUSE:
  96-            printf("SQLITE_MISUSE");
  97-            break;
  98-        case SQLITE_NOLFS:
  99-            printf("SQLITE_NOLFS");
 100-            break;
 101-        case SQLITE_NOMEM:
 102-            printf("SQLITE_NOMEM");
 103-            break;
 104-        case SQLITE_NOTADB:
 105-            printf("SQLITE_NOTADB");
 106-            break;
 107-        case SQLITE_NOTFOUND:
 108-            printf("SQLITE_NOTFOUND");
 109-            break;
 110-        case SQLITE_NOTICE:
 111-            printf("SQLITE_NOTICE");
 112-            break;
 113-        case SQLITE_OK:
 114-            printf("SQLITE_OK");
 115-            break;
 116-        case SQLITE_PERM:
 117-            printf("SQLITE_PERM");
 118-            break;
 119-        case SQLITE_SCHEMA:
 120-            printf("SQLITE_SCHEMA");
 121-            break;
 122-        case SQLITE_TOOBIG:
 123-            printf("SQLITE_TOOBIG");
 124-            break;
 125-        case SQLITE_WARNING:
 126-            printf("SQLITE_WARNING");
 127-            break;
 128-    }
 129+    printf(sqlite3_errstr(code));
 130 }
 131diff --git a/ext/CMakeLists.txt b/ext/CMakeLists.txt
 132new file mode 100644
 133index 0000000000000000000000000000000000000000..d107d7f52608d512dd8a4cf800e55a6a0a166c32
 134--- /dev/null
 135+++ b/ext/CMakeLists.txt
 136@@ -0,0 +1,4 @@
 137+project(sqlite3ext VERSION 0.1 DESCRIPTION "sqlite3 ext library")
 138+
 139+file(GLOB src CONFIGURE_DEPENDS "*.c")
 140+add_library(sqlite3ext SHARED ${src})
 141diff --git a/ext/spellfix.c b/ext/spellfix.c
 142new file mode 100644
 143index 0000000000000000000000000000000000000000..a0c5aafd1009b6cfe79f5d46c98572f77a335706
 144--- /dev/null
 145+++ b/ext/spellfix.c
 146@@ -0,0 +1,3076 @@
 147+/*
 148+** 2012 April 10
 149+**
 150+** The author disclaims copyright to this source code.  In place of
 151+** a legal notice, here is a blessing:
 152+**
 153+**    May you do good and not evil.
 154+**    May you find forgiveness for yourself and forgive others.
 155+**    May you share freely, never taking more than you give.
 156+**
 157+*************************************************************************
 158+**
 159+** This module implements the spellfix1 VIRTUAL TABLE that can be used
 160+** to search a large vocabulary for close matches.  See separate
 161+** documentation (http://www.sqlite.org/spellfix1.html) for details.
 162+*/
 163+#include "sqlite3ext.h"
 164+SQLITE_EXTENSION_INIT1
 165+
 166+#ifndef SQLITE_AMALGAMATION
 167+# if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
 168+#  define NDEBUG 1
 169+# endif
 170+# if defined(NDEBUG) && defined(SQLITE_DEBUG)
 171+#  undef NDEBUG
 172+# endif
 173+# include <string.h>
 174+# include <stdio.h>
 175+# include <stdlib.h>
 176+# include <assert.h>
 177+# define ALWAYS(X)  1
 178+# define NEVER(X)   0
 179+  typedef unsigned char u8;
 180+  typedef unsigned short u16;
 181+#endif
 182+#include <ctype.h>
 183+
 184+#ifndef SQLITE_OMIT_VIRTUALTABLE
 185+
 186+/*
 187+** Character classes for ASCII characters:
 188+**
 189+**   0   ''        Silent letters:   H W
 190+**   1   'A'       Any vowel:   A E I O U (Y)
 191+**   2   'B'       A bilabeal stop or fricative:  B F P V W
 192+**   3   'C'       Other fricatives or back stops:  C G J K Q S X Z
 193+**   4   'D'       Alveolar stops:  D T
 194+**   5   'H'       Letter H at the beginning of a word
 195+**   6   'L'       Glide:  L
 196+**   7   'R'       Semivowel:  R
 197+**   8   'M'       Nasals:  M N
 198+**   9   'Y'       Letter Y at the beginning of a word.
 199+**   10  '9'       Digits: 0 1 2 3 4 5 6 7 8 9
 200+**   11  ' '       White space
 201+**   12  '?'       Other.
 202+*/
 203+#define CCLASS_SILENT         0
 204+#define CCLASS_VOWEL          1
 205+#define CCLASS_B              2
 206+#define CCLASS_C              3
 207+#define CCLASS_D              4
 208+#define CCLASS_H              5
 209+#define CCLASS_L              6
 210+#define CCLASS_R              7
 211+#define CCLASS_M              8
 212+#define CCLASS_Y              9
 213+#define CCLASS_DIGIT         10
 214+#define CCLASS_SPACE         11
 215+#define CCLASS_OTHER         12
 216+
 217+/*
 218+** The following table gives the character class for non-initial ASCII
 219+** characters.
 220+*/
 221+static const unsigned char midClass[] = {
 222+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 223+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 224+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 225+ /*   */ CCLASS_SPACE,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 226+ /*   */ CCLASS_SPACE,    /*   */ CCLASS_SPACE,   /*   */ CCLASS_OTHER,
 227+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 228+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 229+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 230+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 231+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 232+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_SPACE,
 233+ /* ! */ CCLASS_OTHER,    /* " */ CCLASS_OTHER,   /* # */ CCLASS_OTHER,
 234+ /* $ */ CCLASS_OTHER,    /* % */ CCLASS_OTHER,   /* & */ CCLASS_OTHER,
 235+ /* ' */ CCLASS_SILENT,   /* ( */ CCLASS_OTHER,   /* ) */ CCLASS_OTHER,
 236+ /* * */ CCLASS_OTHER,    /* + */ CCLASS_OTHER,   /* , */ CCLASS_OTHER,
 237+ /* - */ CCLASS_OTHER,    /* . */ CCLASS_OTHER,   /* / */ CCLASS_OTHER,
 238+ /* 0 */ CCLASS_DIGIT,    /* 1 */ CCLASS_DIGIT,   /* 2 */ CCLASS_DIGIT,
 239+ /* 3 */ CCLASS_DIGIT,    /* 4 */ CCLASS_DIGIT,   /* 5 */ CCLASS_DIGIT,
 240+ /* 6 */ CCLASS_DIGIT,    /* 7 */ CCLASS_DIGIT,   /* 8 */ CCLASS_DIGIT,
 241+ /* 9 */ CCLASS_DIGIT,    /* : */ CCLASS_OTHER,   /* ; */ CCLASS_OTHER,
 242+ /* < */ CCLASS_OTHER,    /* = */ CCLASS_OTHER,   /* > */ CCLASS_OTHER,
 243+ /* ? */ CCLASS_OTHER,    /* @ */ CCLASS_OTHER,   /* A */ CCLASS_VOWEL,
 244+ /* B */ CCLASS_B,        /* C */ CCLASS_C,       /* D */ CCLASS_D,
 245+ /* E */ CCLASS_VOWEL,    /* F */ CCLASS_B,       /* G */ CCLASS_C,
 246+ /* H */ CCLASS_SILENT,   /* I */ CCLASS_VOWEL,   /* J */ CCLASS_C,
 247+ /* K */ CCLASS_C,        /* L */ CCLASS_L,       /* M */ CCLASS_M,
 248+ /* N */ CCLASS_M,        /* O */ CCLASS_VOWEL,   /* P */ CCLASS_B,
 249+ /* Q */ CCLASS_C,        /* R */ CCLASS_R,       /* S */ CCLASS_C,
 250+ /* T */ CCLASS_D,        /* U */ CCLASS_VOWEL,   /* V */ CCLASS_B,
 251+ /* W */ CCLASS_B,        /* X */ CCLASS_C,       /* Y */ CCLASS_VOWEL,
 252+ /* Z */ CCLASS_C,        /* [ */ CCLASS_OTHER,   /* \ */ CCLASS_OTHER,
 253+ /* ] */ CCLASS_OTHER,    /* ^ */ CCLASS_OTHER,   /* _ */ CCLASS_OTHER,
 254+ /* ` */ CCLASS_OTHER,    /* a */ CCLASS_VOWEL,   /* b */ CCLASS_B,
 255+ /* c */ CCLASS_C,        /* d */ CCLASS_D,       /* e */ CCLASS_VOWEL,
 256+ /* f */ CCLASS_B,        /* g */ CCLASS_C,       /* h */ CCLASS_SILENT,
 257+ /* i */ CCLASS_VOWEL,    /* j */ CCLASS_C,       /* k */ CCLASS_C,
 258+ /* l */ CCLASS_L,        /* m */ CCLASS_M,       /* n */ CCLASS_M,
 259+ /* o */ CCLASS_VOWEL,    /* p */ CCLASS_B,       /* q */ CCLASS_C,
 260+ /* r */ CCLASS_R,        /* s */ CCLASS_C,       /* t */ CCLASS_D,
 261+ /* u */ CCLASS_VOWEL,    /* v */ CCLASS_B,       /* w */ CCLASS_B,
 262+ /* x */ CCLASS_C,        /* y */ CCLASS_VOWEL,   /* z */ CCLASS_C,
 263+ /* { */ CCLASS_OTHER,    /* | */ CCLASS_OTHER,   /* } */ CCLASS_OTHER,
 264+ /* ~ */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   
 265+};
 266+/* 
 267+** This tables gives the character class for ASCII characters that form the
 268+** initial character of a word.  The only difference from midClass is with
 269+** the letters H, W, and Y.
 270+*/
 271+static const unsigned char initClass[] = {
 272+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 273+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 274+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 275+ /*   */ CCLASS_SPACE,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 276+ /*   */ CCLASS_SPACE,    /*   */ CCLASS_SPACE,   /*   */ CCLASS_OTHER,
 277+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 278+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 279+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 280+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 281+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
 282+ /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_SPACE,
 283+ /* ! */ CCLASS_OTHER,    /* " */ CCLASS_OTHER,   /* # */ CCLASS_OTHER,
 284+ /* $ */ CCLASS_OTHER,    /* % */ CCLASS_OTHER,   /* & */ CCLASS_OTHER,
 285+ /* ' */ CCLASS_OTHER,    /* ( */ CCLASS_OTHER,   /* ) */ CCLASS_OTHER,
 286+ /* * */ CCLASS_OTHER,    /* + */ CCLASS_OTHER,   /* , */ CCLASS_OTHER,
 287+ /* - */ CCLASS_OTHER,    /* . */ CCLASS_OTHER,   /* / */ CCLASS_OTHER,
 288+ /* 0 */ CCLASS_DIGIT,    /* 1 */ CCLASS_DIGIT,   /* 2 */ CCLASS_DIGIT,
 289+ /* 3 */ CCLASS_DIGIT,    /* 4 */ CCLASS_DIGIT,   /* 5 */ CCLASS_DIGIT,
 290+ /* 6 */ CCLASS_DIGIT,    /* 7 */ CCLASS_DIGIT,   /* 8 */ CCLASS_DIGIT,
 291+ /* 9 */ CCLASS_DIGIT,    /* : */ CCLASS_OTHER,   /* ; */ CCLASS_OTHER,
 292+ /* < */ CCLASS_OTHER,    /* = */ CCLASS_OTHER,   /* > */ CCLASS_OTHER,
 293+ /* ? */ CCLASS_OTHER,    /* @ */ CCLASS_OTHER,   /* A */ CCLASS_VOWEL,
 294+ /* B */ CCLASS_B,        /* C */ CCLASS_C,       /* D */ CCLASS_D,
 295+ /* E */ CCLASS_VOWEL,    /* F */ CCLASS_B,       /* G */ CCLASS_C,
 296+ /* H */ CCLASS_SILENT,   /* I */ CCLASS_VOWEL,   /* J */ CCLASS_C,
 297+ /* K */ CCLASS_C,        /* L */ CCLASS_L,       /* M */ CCLASS_M,
 298+ /* N */ CCLASS_M,        /* O */ CCLASS_VOWEL,   /* P */ CCLASS_B,
 299+ /* Q */ CCLASS_C,        /* R */ CCLASS_R,       /* S */ CCLASS_C,
 300+ /* T */ CCLASS_D,        /* U */ CCLASS_VOWEL,   /* V */ CCLASS_B,
 301+ /* W */ CCLASS_B,        /* X */ CCLASS_C,       /* Y */ CCLASS_Y,
 302+ /* Z */ CCLASS_C,        /* [ */ CCLASS_OTHER,   /* \ */ CCLASS_OTHER,
 303+ /* ] */ CCLASS_OTHER,    /* ^ */ CCLASS_OTHER,   /* _ */ CCLASS_OTHER,
 304+ /* ` */ CCLASS_OTHER,    /* a */ CCLASS_VOWEL,   /* b */ CCLASS_B,
 305+ /* c */ CCLASS_C,        /* d */ CCLASS_D,       /* e */ CCLASS_VOWEL,
 306+ /* f */ CCLASS_B,        /* g */ CCLASS_C,       /* h */ CCLASS_SILENT,
 307+ /* i */ CCLASS_VOWEL,    /* j */ CCLASS_C,       /* k */ CCLASS_C,
 308+ /* l */ CCLASS_L,        /* m */ CCLASS_M,       /* n */ CCLASS_M,
 309+ /* o */ CCLASS_VOWEL,    /* p */ CCLASS_B,       /* q */ CCLASS_C,
 310+ /* r */ CCLASS_R,        /* s */ CCLASS_C,       /* t */ CCLASS_D,
 311+ /* u */ CCLASS_VOWEL,    /* v */ CCLASS_B,       /* w */ CCLASS_B,
 312+ /* x */ CCLASS_C,        /* y */ CCLASS_Y,       /* z */ CCLASS_C,
 313+ /* { */ CCLASS_OTHER,    /* | */ CCLASS_OTHER,   /* } */ CCLASS_OTHER,
 314+ /* ~ */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   
 315+};
 316+
 317+/*
 318+** Mapping from the character class number (0-13) to a symbol for each
 319+** character class.  Note that initClass[] can be used to map the class
 320+** symbol back into the class number.
 321+*/
 322+static const unsigned char className[] = ".ABCDHLRMY9 ?";
 323+
 324+/*
 325+** Generate a "phonetic hash" from a string of ASCII characters
 326+** in zIn[0..nIn-1].
 327+**
 328+**   * Map characters by character class as defined above.
 329+**   * Omit double-letters
 330+**   * Omit vowels beside R and L
 331+**   * Omit T when followed by CH
 332+**   * Omit W when followed by R
 333+**   * Omit D when followed by J or G
 334+**   * Omit K in KN or G in GN at the beginning of a word
 335+**
 336+** Space to hold the result is obtained from sqlite3_malloc()
 337+**
 338+** Return NULL if memory allocation fails.  
 339+*/
 340+static unsigned char *phoneticHash(const unsigned char *zIn, int nIn){
 341+  unsigned char *zOut = sqlite3_malloc64( nIn + 1 );
 342+  int i;
 343+  int nOut = 0;
 344+  char cPrev = 0x77;
 345+  char cPrevX = 0x77;
 346+  const unsigned char *aClass = initClass;
 347+
 348+  if( zOut==0 ) return 0;
 349+  if( nIn>2 ){
 350+    switch( zIn[0] ){
 351+      case 'g': 
 352+      case 'k': {
 353+        if( zIn[1]=='n' ){ zIn++; nIn--; }
 354+        break;
 355+      }
 356+    }
 357+  }
 358+  for(i=0; i<nIn; i++){
 359+    unsigned char c = zIn[i];
 360+    if( i+1<nIn ){
 361+      if( c=='w' && zIn[i+1]=='r' ) continue;
 362+      if( c=='d' && (zIn[i+1]=='j' || zIn[i+1]=='g') ) continue;
 363+      if( i+2<nIn ){
 364+        if( c=='t' && zIn[i+1]=='c' && zIn[i+2]=='h' ) continue;
 365+      }
 366+    }
 367+    c = aClass[c&0x7f];
 368+    if( c==CCLASS_SPACE ) continue;
 369+    if( c==CCLASS_OTHER && cPrev!=CCLASS_DIGIT ) continue;
 370+    aClass = midClass;
 371+    if( c==CCLASS_VOWEL && (cPrevX==CCLASS_R || cPrevX==CCLASS_L) ){
 372+       continue; /* No vowels beside L or R */ 
 373+    }
 374+    if( (c==CCLASS_R || c==CCLASS_L) && cPrevX==CCLASS_VOWEL ){
 375+       nOut--;   /* No vowels beside L or R */
 376+    }
 377+    cPrev = c;
 378+    if( c==CCLASS_SILENT ) continue;
 379+    cPrevX = c;
 380+    c = className[c];
 381+    assert( nOut>=0 );
 382+    if( nOut==0 || c!=zOut[nOut-1] ) zOut[nOut++] = c;
 383+  }
 384+  zOut[nOut] = 0;
 385+  return zOut;
 386+}
 387+
 388+/*
 389+** This is an SQL function wrapper around phoneticHash().  See
 390+** the description of phoneticHash() for additional information.
 391+*/
 392+static void phoneticHashSqlFunc(
 393+  sqlite3_context *context,
 394+  int argc,
 395+  sqlite3_value **argv
 396+){
 397+  const unsigned char *zIn;
 398+  unsigned char *zOut;
 399+
 400+  zIn = sqlite3_value_text(argv[0]);
 401+  if( zIn==0 ) return;
 402+  zOut = phoneticHash(zIn, sqlite3_value_bytes(argv[0]));
 403+  if( zOut==0 ){
 404+    sqlite3_result_error_nomem(context);
 405+  }else{
 406+    sqlite3_result_text(context, (char*)zOut, -1, sqlite3_free);
 407+  }
 408+}
 409+
 410+/*
 411+** Return the character class number for a character given its
 412+** context.
 413+*/
 414+static char characterClass(char cPrev, char c){
 415+  return cPrev==0 ? initClass[c&0x7f] : midClass[c&0x7f];
 416+}
 417+
 418+/*
 419+** Return the cost of inserting or deleting character c immediately
 420+** following character cPrev.  If cPrev==0, that means c is the first
 421+** character of the word.
 422+*/
 423+static int insertOrDeleteCost(char cPrev, char c, char cNext){
 424+  char classC = characterClass(cPrev, c);
 425+  char classCprev;
 426+
 427+  if( classC==CCLASS_SILENT ){
 428+    /* Insert or delete "silent" characters such as H or W */
 429+    return 1;
 430+  }
 431+  if( cPrev==c ){
 432+    /* Repeated characters, or miss a repeat */
 433+    return 10;
 434+  }
 435+  if( classC==CCLASS_VOWEL && (cPrev=='r' || cNext=='r') ){
 436+    return 20;  /* Insert a vowel before or after 'r' */
 437+  }
 438+  classCprev = characterClass(cPrev, cPrev);
 439+  if( classC==classCprev ){
 440+    if( classC==CCLASS_VOWEL ){
 441+      /* Remove or add a new vowel to a vowel cluster */
 442+      return 15;
 443+    }else{
 444+      /* Remove or add a consonant not in the same class */
 445+      return 50;
 446+    }
 447+  }
 448+
 449+  /* any other character insertion or deletion */
 450+  return 100;
 451+}
 452+
 453+/*
 454+** Divide the insertion cost by this factor when appending to the
 455+** end of the word.
 456+*/
 457+#define FINAL_INS_COST_DIV  4
 458+
 459+/*
 460+** Return the cost of substituting cTo in place of cFrom assuming
 461+** the previous character is cPrev.  If cPrev==0 then cTo is the first
 462+** character of the word.
 463+*/
 464+static int substituteCost(char cPrev, char cFrom, char cTo){
 465+  char classFrom, classTo;
 466+  if( cFrom==cTo ){
 467+    /* Exact match */
 468+    return 0;
 469+  }
 470+  if( cFrom==(cTo^0x20) && ((cTo>='A' && cTo<='Z') || (cTo>='a' && cTo<='z')) ){
 471+    /* differ only in case */
 472+    return 0;
 473+  }
 474+  classFrom = characterClass(cPrev, cFrom);
 475+  classTo = characterClass(cPrev, cTo);
 476+  if( classFrom==classTo ){
 477+    /* Same character class */
 478+    return 40;
 479+  }
 480+  if( classFrom>=CCLASS_B && classFrom<=CCLASS_Y
 481+      && classTo>=CCLASS_B && classTo<=CCLASS_Y ){
 482+    /* Convert from one consonant to another, but in a different class */
 483+    return 75;
 484+  }
 485+  /* Any other subsitution */
 486+  return 100;
 487+}
 488+
 489+/*
 490+** Given two strings zA and zB which are pure ASCII, return the cost
 491+** of transforming zA into zB.  If zA ends with '*' assume that it is
 492+** a prefix of zB and give only minimal penalty for extra characters
 493+** on the end of zB.
 494+**
 495+** Smaller numbers mean a closer match.
 496+**
 497+** Negative values indicate an error:
 498+**    -1  One of the inputs is NULL
 499+**    -2  Non-ASCII characters on input
 500+**    -3  Unable to allocate memory 
 501+**
 502+** If pnMatch is not NULL, then *pnMatch is set to the number of bytes
 503+** of zB that matched the pattern in zA. If zA does not end with a '*',
 504+** then this value is always the number of bytes in zB (i.e. strlen(zB)).
 505+** If zA does end in a '*', then it is the number of bytes in the prefix
 506+** of zB that was deemed to match zA.
 507+*/
 508+static int editdist1(const char *zA, const char *zB, int *pnMatch){
 509+  int nA, nB;            /* Number of characters in zA[] and zB[] */
 510+  int xA, xB;            /* Loop counters for zA[] and zB[] */
 511+  char cA = 0, cB;       /* Current character of zA and zB */
 512+  char cAprev, cBprev;   /* Previous character of zA and zB */
 513+  char cAnext, cBnext;   /* Next character in zA and zB */
 514+  int d;                 /* North-west cost value */
 515+  int dc = 0;            /* North-west character value */
 516+  int res;               /* Final result */
 517+  int *m;                /* The cost matrix */
 518+  char *cx;              /* Corresponding character values */
 519+  int *toFree = 0;       /* Malloced space */
 520+  int nMatch = 0;
 521+  int mStack[60+15];     /* Stack space to use if not too much is needed */
 522+
 523+  /* Early out if either input is NULL */
 524+  if( zA==0 || zB==0 ) return -1;
 525+
 526+  /* Skip any common prefix */
 527+  while( zA[0] && zA[0]==zB[0] ){ dc = zA[0]; zA++; zB++; nMatch++; }
 528+  if( pnMatch ) *pnMatch = nMatch;
 529+  if( zA[0]==0 && zB[0]==0 ) return 0;
 530+
 531+#if 0
 532+  printf("A=\"%s\" B=\"%s\" dc=%c\n", zA, zB, dc?dc:' ');
 533+#endif
 534+
 535+  /* Verify input strings and measure their lengths */
 536+  for(nA=0; zA[nA]; nA++){
 537+    if( zA[nA]&0x80 ) return -2;
 538+  }
 539+  for(nB=0; zB[nB]; nB++){
 540+    if( zB[nB]&0x80 ) return -2;
 541+  }
 542+
 543+  /* Special processing if either string is empty */
 544+  if( nA==0 ){
 545+    cBprev = (char)dc;
 546+    for(xB=res=0; (cB = zB[xB])!=0; xB++){
 547+      res += insertOrDeleteCost(cBprev, cB, zB[xB+1])/FINAL_INS_COST_DIV;
 548+      cBprev = cB;
 549+    }
 550+    return res;
 551+  }
 552+  if( nB==0 ){
 553+    cAprev = (char)dc;
 554+    for(xA=res=0; (cA = zA[xA])!=0; xA++){
 555+      res += insertOrDeleteCost(cAprev, cA, zA[xA+1]);
 556+      cAprev = cA;
 557+    }
 558+    return res;
 559+  }
 560+
 561+  /* A is a prefix of B */
 562+  if( zA[0]=='*' && zA[1]==0 ) return 0;
 563+
 564+  /* Allocate and initialize the Wagner matrix */
 565+  if( nB<(sizeof(mStack)*4)/(sizeof(mStack[0])*5) ){
 566+    m = mStack;
 567+  }else{
 568+    m = toFree = sqlite3_malloc64( (nB+1)*5*sizeof(m[0])/4 );
 569+    if( m==0 ) return -3;
 570+  }
 571+  cx = (char*)&m[nB+1];
 572+
 573+  /* Compute the Wagner edit distance */
 574+  m[0] = 0;
 575+  cx[0] = (char)dc;
 576+  cBprev = (char)dc;
 577+  for(xB=1; xB<=nB; xB++){
 578+    cBnext = zB[xB];
 579+    cB = zB[xB-1];
 580+    cx[xB] = cB;
 581+    m[xB] = m[xB-1] + insertOrDeleteCost(cBprev, cB, cBnext);
 582+    cBprev = cB;
 583+  }
 584+  cAprev = (char)dc;
 585+  for(xA=1; xA<=nA; xA++){
 586+    int lastA = (xA==nA);
 587+    cA = zA[xA-1];
 588+    cAnext = zA[xA];
 589+    if( cA=='*' && lastA ) break;
 590+    d = m[0];
 591+    dc = cx[0];
 592+    m[0] = d + insertOrDeleteCost(cAprev, cA, cAnext);
 593+    cBprev = 0;
 594+    for(xB=1; xB<=nB; xB++){
 595+      int totalCost, insCost, delCost, subCost, ncx;
 596+      cB = zB[xB-1];
 597+      cBnext = zB[xB];
 598+
 599+      /* Cost to insert cB */
 600+      insCost = insertOrDeleteCost(cx[xB-1], cB, cBnext);
 601+      if( lastA ) insCost /= FINAL_INS_COST_DIV;
 602+
 603+      /* Cost to delete cA */
 604+      delCost = insertOrDeleteCost(cx[xB], cA, cBnext);
 605+
 606+      /* Cost to substitute cA->cB */
 607+      subCost = substituteCost(cx[xB-1], cA, cB);
 608+
 609+      /* Best cost */
 610+      totalCost = insCost + m[xB-1];
 611+      ncx = cB;
 612+      if( (delCost + m[xB])<totalCost ){
 613+        totalCost = delCost + m[xB];
 614+        ncx = cA;
 615+      }
 616+      if( (subCost + d)<totalCost ){
 617+        totalCost = subCost + d;
 618+      }
 619+
 620+#if 0
 621+      printf("%d,%d d=%4d u=%4d r=%4d dc=%c cA=%c cB=%c"
 622+             " ins=%4d del=%4d sub=%4d t=%4d ncx=%c\n",
 623+             xA, xB, d, m[xB], m[xB-1], dc?dc:' ', cA, cB,
 624+             insCost, delCost, subCost, totalCost, ncx?ncx:' ');
 625+#endif
 626+
 627+      /* Update the matrix */
 628+      d = m[xB];
 629+      dc = cx[xB];
 630+      m[xB] = totalCost;
 631+      cx[xB] = (char)ncx;
 632+      cBprev = cB;
 633+    }
 634+    cAprev = cA;
 635+  }
 636+
 637+  /* Free the wagner matrix and return the result */
 638+  if( cA=='*' ){
 639+    res = m[1];
 640+    for(xB=1; xB<=nB; xB++){
 641+      if( m[xB]<res ){
 642+        res = m[xB];
 643+        if( pnMatch ) *pnMatch = xB+nMatch;
 644+      }
 645+    }
 646+  }else{
 647+    res = m[nB];
 648+    /* In the current implementation, pnMatch is always NULL if zA does
 649+    ** not end in "*" */
 650+    assert( pnMatch==0 );
 651+  }
 652+  sqlite3_free(toFree);
 653+  return res;
 654+}
 655+
 656+/*
 657+** Function:    editdist(A,B)
 658+**
 659+** Return the cost of transforming string A into string B.  Both strings
 660+** must be pure ASCII text.  If A ends with '*' then it is assumed to be
 661+** a prefix of B and extra characters on the end of B have minimal additional
 662+** cost.
 663+*/
 664+static void editdistSqlFunc(
 665+  sqlite3_context *context,
 666+  int argc,
 667+  sqlite3_value **argv
 668+){
 669+  int res = editdist1(
 670+                    (const char*)sqlite3_value_text(argv[0]),
 671+                    (const char*)sqlite3_value_text(argv[1]),
 672+                    0);
 673+  if( res<0 ){
 674+    if( res==(-3) ){
 675+      sqlite3_result_error_nomem(context);
 676+    }else if( res==(-2) ){
 677+      sqlite3_result_error(context, "non-ASCII input to editdist()", -1);
 678+    }else{
 679+      sqlite3_result_error(context, "NULL input to editdist()", -1);
 680+    }
 681+  }else{ 
 682+    sqlite3_result_int(context, res);
 683+  }
 684+}
 685+
 686+/* End of the fixed-cost edit distance implementation
 687+******************************************************************************
 688+*****************************************************************************
 689+** Begin: Configurable cost unicode edit distance routines
 690+*/
 691+/* Forward declaration of structures */
 692+typedef struct EditDist3Cost EditDist3Cost;
 693+typedef struct EditDist3Config EditDist3Config;
 694+typedef struct EditDist3Point EditDist3Point;
 695+typedef struct EditDist3From EditDist3From;
 696+typedef struct EditDist3FromString EditDist3FromString;
 697+typedef struct EditDist3To EditDist3To;
 698+typedef struct EditDist3ToString EditDist3ToString;
 699+typedef struct EditDist3Lang EditDist3Lang;
 700+
 701+
 702+/*
 703+** An entry in the edit cost table
 704+*/
 705+struct EditDist3Cost {
 706+  EditDist3Cost *pNext;     /* Next cost element */
 707+  u8 nFrom;                 /* Number of bytes in aFrom */
 708+  u8 nTo;                   /* Number of bytes in aTo */
 709+  u16 iCost;                /* Cost of this transformation */
 710+  char a[4]    ;            /* FROM string followed by TO string */
 711+  /* Additional TO and FROM string bytes appended as necessary */
 712+};
 713+
 714+/*
 715+** Edit costs for a particular language ID 
 716+*/
 717+struct EditDist3Lang {
 718+  int iLang;             /* Language ID */
 719+  int iInsCost;          /* Default insertion cost */
 720+  int iDelCost;          /* Default deletion cost */
 721+  int iSubCost;          /* Default substitution cost */
 722+  EditDist3Cost *pCost;  /* Costs */
 723+};
 724+
 725+
 726+/*
 727+** The default EditDist3Lang object, with default costs.
 728+*/
 729+static const EditDist3Lang editDist3Lang = { 0, 100, 100, 150, 0 };
 730+
 731+/*
 732+** Complete configuration
 733+*/
 734+struct EditDist3Config {
 735+  int nLang;             /* Number of language IDs.  Size of a[] */
 736+  EditDist3Lang *a;      /* One for each distinct language ID */
 737+};
 738+
 739+/*
 740+** Extra information about each character in the FROM string.
 741+*/
 742+struct EditDist3From {
 743+  int nSubst;              /* Number of substitution cost entries */
 744+  int nDel;                /* Number of deletion cost entries */
 745+  int nByte;               /* Number of bytes in this character */
 746+  EditDist3Cost **apSubst; /* Array of substitution costs for this element */
 747+  EditDist3Cost **apDel;   /* Array of deletion cost entries */
 748+};
 749+
 750+/*
 751+** A precompiled FROM string.
 752+*
 753+** In the common case we expect the FROM string to be reused multiple times.
 754+** In other words, the common case will be to measure the edit distance
 755+** from a single origin string to multiple target strings.
 756+*/
 757+struct EditDist3FromString {
 758+  char *z;                 /* The complete text of the FROM string */
 759+  int n;                   /* Number of characters in the FROM string */
 760+  int isPrefix;            /* True if ends with '*' character */
 761+  EditDist3From *a;        /* Extra info about each char of the FROM string */
 762+};
 763+
 764+/*
 765+** Extra information about each character in the TO string.
 766+*/
 767+struct EditDist3To {
 768+  int nIns;                /* Number of insertion cost entries */
 769+  int nByte;               /* Number of bytes in this character */
 770+  EditDist3Cost **apIns;   /* Array of deletion cost entries */
 771+};
 772+
 773+/*
 774+** A precompiled FROM string
 775+*/
 776+struct EditDist3ToString {
 777+  char *z;                 /* The complete text of the TO string */
 778+  int n;                   /* Number of characters in the TO string */
 779+  EditDist3To *a;          /* Extra info about each char of the TO string */
 780+};
 781+
 782+/*
 783+** Clear or delete an instance of the object that records all edit-distance
 784+** weights.
 785+*/
 786+static void editDist3ConfigClear(EditDist3Config *p){
 787+  int i;
 788+  if( p==0 ) return;
 789+  for(i=0; i<p->nLang; i++){
 790+    EditDist3Cost *pCost, *pNext;
 791+    pCost = p->a[i].pCost;
 792+    while( pCost ){
 793+      pNext = pCost->pNext;
 794+      sqlite3_free(pCost);
 795+      pCost = pNext;
 796+    }
 797+  }
 798+  sqlite3_free(p->a);
 799+  memset(p, 0, sizeof(*p));
 800+}
 801+static void editDist3ConfigDelete(void *pIn){
 802+  EditDist3Config *p = (EditDist3Config*)pIn;
 803+  editDist3ConfigClear(p);
 804+  sqlite3_free(p);
 805+}
 806+
 807+/* Compare the FROM values of two EditDist3Cost objects, for sorting.
 808+** Return negative, zero, or positive if the A is less than, equal to,
 809+** or greater than B.
 810+*/
 811+static int editDist3CostCompare(EditDist3Cost *pA, EditDist3Cost *pB){
 812+  int n = pA->nFrom;
 813+  int rc;
 814+  if( n>pB->nFrom ) n = pB->nFrom;
 815+  rc = strncmp(pA->a, pB->a, n);
 816+  if( rc==0 ) rc = pA->nFrom - pB->nFrom;
 817+  return rc;
 818+}
 819+
 820+/*
 821+** Merge together two sorted lists of EditDist3Cost objects, in order
 822+** of increasing FROM.
 823+*/
 824+static EditDist3Cost *editDist3CostMerge(
 825+  EditDist3Cost *pA,
 826+  EditDist3Cost *pB
 827+){
 828+  EditDist3Cost *pHead = 0;
 829+  EditDist3Cost **ppTail = &pHead;
 830+  EditDist3Cost *p;
 831+  while( pA && pB ){
 832+    if( editDist3CostCompare(pA,pB)<=0 ){
 833+      p = pA;
 834+      pA = pA->pNext;
 835+    }else{
 836+      p = pB;
 837+      pB = pB->pNext;
 838+    }
 839+    *ppTail = p;
 840+    ppTail =  &p->pNext;
 841+  }
 842+  if( pA ){
 843+    *ppTail = pA;
 844+  }else{
 845+    *ppTail = pB;
 846+  }
 847+  return pHead;
 848+}
 849+
 850+/*
 851+** Sort a list of EditDist3Cost objects into order of increasing FROM
 852+*/
 853+static EditDist3Cost *editDist3CostSort(EditDist3Cost *pList){
 854+  EditDist3Cost *ap[60], *p;
 855+  int i;
 856+  int mx = 0;
 857+  ap[0] = 0;
 858+  ap[1] = 0;
 859+  while( pList ){
 860+    p = pList;
 861+    pList = p->pNext;
 862+    p->pNext = 0;
 863+    for(i=0; ap[i]; i++){
 864+      p = editDist3CostMerge(ap[i],p);
 865+      ap[i] = 0;
 866+    }
 867+    ap[i] = p;
 868+    if( i>mx ){
 869+      mx = i;
 870+      ap[i+1] = 0;
 871+    }
 872+  }
 873+  p = 0;
 874+  for(i=0; i<=mx; i++){
 875+    if( ap[i] ) p = editDist3CostMerge(p,ap[i]);
 876+  }
 877+  return p;
 878+}
 879+
 880+/*
 881+** Load all edit-distance weights from a table.
 882+*/
 883+static int editDist3ConfigLoad(
 884+  EditDist3Config *p,      /* The edit distance configuration to load */
 885+  sqlite3 *db,            /* Load from this database */
 886+  const char *zTable      /* Name of the table from which to load */
 887+){
 888+  sqlite3_stmt *pStmt;
 889+  int rc, rc2;
 890+  char *zSql;
 891+  int iLangPrev = -9999;
 892+  EditDist3Lang *pLang = 0;
 893+
 894+  zSql = sqlite3_mprintf("SELECT iLang, cFrom, cTo, iCost"
 895+                         " FROM \"%w\" WHERE iLang>=0 ORDER BY iLang", zTable);
 896+  if( zSql==0 ) return SQLITE_NOMEM;
 897+  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
 898+  sqlite3_free(zSql);
 899+  if( rc ) return rc;
 900+  editDist3ConfigClear(p);
 901+  while( sqlite3_step(pStmt)==SQLITE_ROW ){
 902+    int iLang = sqlite3_column_int(pStmt, 0);
 903+    const char *zFrom = (const char*)sqlite3_column_text(pStmt, 1);
 904+    int nFrom = zFrom ? sqlite3_column_bytes(pStmt, 1) : 0;
 905+    const char *zTo = (const char*)sqlite3_column_text(pStmt, 2);
 906+    int nTo = zTo ? sqlite3_column_bytes(pStmt, 2) : 0;
 907+    int iCost = sqlite3_column_int(pStmt, 3);
 908+
 909+    assert( zFrom!=0 || nFrom==0 );
 910+    assert( zTo!=0 || nTo==0 );
 911+    if( nFrom>100 || nTo>100 ) continue;
 912+    if( iCost<0 ) continue;
 913+    if( iCost>=10000 ) continue;  /* Costs above 10K are considered infinite */
 914+    if( pLang==0 || iLang!=iLangPrev ){
 915+      EditDist3Lang *pNew;
 916+      pNew = sqlite3_realloc64(p->a, (p->nLang+1)*sizeof(p->a[0]));
 917+      if( pNew==0 ){ rc = SQLITE_NOMEM; break; }
 918+      p->a = pNew;
 919+      pLang = &p->a[p->nLang];
 920+      p->nLang++;
 921+      pLang->iLang = iLang;
 922+      pLang->iInsCost = 100;
 923+      pLang->iDelCost = 100;
 924+      pLang->iSubCost = 150;
 925+      pLang->pCost = 0;
 926+      iLangPrev = iLang;
 927+    }
 928+    if( nFrom==1 && zFrom[0]=='?' && nTo==0 ){
 929+      pLang->iDelCost = iCost;
 930+    }else if( nFrom==0 && nTo==1 && zTo[0]=='?' ){
 931+      pLang->iInsCost = iCost;
 932+    }else if( nFrom==1 && nTo==1 && zFrom[0]=='?' && zTo[0]=='?' ){
 933+      pLang->iSubCost = iCost;
 934+    }else{
 935+      EditDist3Cost *pCost;
 936+      int nExtra = nFrom + nTo - 4;
 937+      if( nExtra<0 ) nExtra = 0;
 938+      pCost = sqlite3_malloc64( sizeof(*pCost) + nExtra );
 939+      if( pCost==0 ){ rc = SQLITE_NOMEM; break; }
 940+      pCost->nFrom = (u8)nFrom;
 941+      pCost->nTo = (u8)nTo;
 942+      pCost->iCost = (u16)iCost;
 943+      memcpy(pCost->a, zFrom, nFrom);
 944+      memcpy(pCost->a + nFrom, zTo, nTo);
 945+      pCost->pNext = pLang->pCost;
 946+      pLang->pCost = pCost; 
 947+    }
 948+  }
 949+  rc2 = sqlite3_finalize(pStmt);
 950+  if( rc==SQLITE_OK ) rc = rc2;
 951+  if( rc==SQLITE_OK ){
 952+    int iLang;
 953+    for(iLang=0; iLang<p->nLang; iLang++){
 954+      p->a[iLang].pCost = editDist3CostSort(p->a[iLang].pCost);
 955+    }
 956+  }
 957+  return rc;
 958+}
 959+
 960+/*
 961+** Return the length (in bytes) of a utf-8 character.  Or return a maximum
 962+** of N.
 963+*/
 964+static int utf8Len(unsigned char c, int N){
 965+  int len = 1;
 966+  if( c>0x7f ){
 967+    if( (c&0xe0)==0xc0 ){
 968+      len = 2;
 969+    }else if( (c&0xf0)==0xe0 ){
 970+      len = 3;
 971+    }else{
 972+      len = 4;
 973+    }
 974+  }
 975+  if( len>N ) len = N;
 976+  return len;
 977+}
 978+
 979+/*
 980+** Return TRUE (non-zero) if the To side of the given cost matches
 981+** the given string.
 982+*/
 983+static int matchTo(EditDist3Cost *p, const char *z, int n){
 984+  assert( n>0 );
 985+  if( p->a[p->nFrom]!=z[0] ) return 0;
 986+  if( p->nTo>n ) return 0;
 987+  if( strncmp(p->a+p->nFrom, z, p->nTo)!=0 ) return 0;
 988+  return 1;
 989+}
 990+
 991+/*
 992+** Return TRUE (non-zero) if the From side of the given cost matches
 993+** the given string.
 994+*/
 995+static int matchFrom(EditDist3Cost *p, const char *z, int n){
 996+  assert( p->nFrom<=n );
 997+  if( p->nFrom ){
 998+    if( p->a[0]!=z[0] ) return 0;
 999+    if( strncmp(p->a, z, p->nFrom)!=0 ) return 0;
1000+  }
1001+  return 1;
1002+}
1003+
1004+/*
1005+** Return TRUE (non-zero) of the next FROM character and the next TO
1006+** character are the same.
1007+*/
1008+static int matchFromTo(
1009+  EditDist3FromString *pStr,  /* Left hand string */
1010+  int n1,                     /* Index of comparison character on the left */
1011+  const char *z2,             /* Right-handl comparison character */
1012+  int n2                      /* Bytes remaining in z2[] */
1013+){
1014+  int b1 = pStr->a[n1].nByte;
1015+  if( b1>n2 ) return 0;
1016+  assert( b1>0 );
1017+  if( pStr->z[n1]!=z2[0] ) return 0;
1018+  if( strncmp(pStr->z+n1, z2, b1)!=0 ) return 0;
1019+  return 1;
1020+}
1021+
1022+/*
1023+** Delete an EditDist3FromString objecct
1024+*/
1025+static void editDist3FromStringDelete(EditDist3FromString *p){
1026+  int i;
1027+  if( p ){
1028+    for(i=0; i<p->n; i++){
1029+      sqlite3_free(p->a[i].apDel);
1030+      sqlite3_free(p->a[i].apSubst);
1031+    }
1032+    sqlite3_free(p);
1033+  }
1034+}
1035+
1036+/*
1037+** Create a EditDist3FromString object.
1038+*/
1039+static EditDist3FromString *editDist3FromStringNew(
1040+  const EditDist3Lang *pLang,
1041+  const char *z,
1042+  int n
1043+){
1044+  EditDist3FromString *pStr;
1045+  EditDist3Cost *p;
1046+  int i;
1047+
1048+  if( z==0 ) return 0;
1049+  if( n<0 ) n = (int)strlen(z);
1050+  pStr = sqlite3_malloc64( sizeof(*pStr) + sizeof(pStr->a[0])*n + n + 1 );
1051+  if( pStr==0 ) return 0;
1052+  pStr->a = (EditDist3From*)&pStr[1];
1053+  memset(pStr->a, 0, sizeof(pStr->a[0])*n);
1054+  pStr->n = n;
1055+  pStr->z = (char*)&pStr->a[n];
1056+  memcpy(pStr->z, z, n+1);
1057+  if( n && z[n-1]=='*' ){
1058+    pStr->isPrefix = 1;
1059+    n--;
1060+    pStr->n--;
1061+    pStr->z[n] = 0;
1062+  }else{
1063+    pStr->isPrefix = 0;
1064+  }
1065+
1066+  for(i=0; i<n; i++){
1067+    EditDist3From *pFrom = &pStr->a[i];
1068+    memset(pFrom, 0, sizeof(*pFrom));
1069+    pFrom->nByte = utf8Len((unsigned char)z[i], n-i);
1070+    for(p=pLang->pCost; p; p=p->pNext){
1071+      EditDist3Cost **apNew;
1072+      if( i+p->nFrom>n ) continue;
1073+      if( matchFrom(p, z+i, n-i)==0 ) continue;
1074+      if( p->nTo==0 ){
1075+        apNew = sqlite3_realloc64(pFrom->apDel,
1076+                                sizeof(*apNew)*(pFrom->nDel+1));
1077+        if( apNew==0 ) break;
1078+        pFrom->apDel = apNew;
1079+        apNew[pFrom->nDel++] = p;
1080+      }else{
1081+        apNew = sqlite3_realloc64(pFrom->apSubst,
1082+                                sizeof(*apNew)*(pFrom->nSubst+1));
1083+        if( apNew==0 ) break;
1084+        pFrom->apSubst = apNew;
1085+        apNew[pFrom->nSubst++] = p;
1086+      }
1087+    }
1088+    if( p ){
1089+      editDist3FromStringDelete(pStr);
1090+      pStr = 0;
1091+      break;
1092+    }
1093+  }
1094+  return pStr;
1095+}
1096+
1097+/*
1098+** Update entry m[i] such that it is the minimum of its current value
1099+** and m[j]+iCost.
1100+*/
1101+static void updateCost(
1102+  unsigned int *m,
1103+  int i,
1104+  int j,
1105+  int iCost
1106+){
1107+  unsigned int b;
1108+  assert( iCost>=0 );
1109+  assert( iCost<10000 );
1110+  b = m[j] + iCost;
1111+  if( b<m[i] ) m[i] = b;
1112+}
1113+
1114+/*
1115+** How much stack space (int bytes) to use for Wagner matrix in 
1116+** editDist3Core().  If more space than this is required, the entire
1117+** matrix is taken from the heap.  To reduce the load on the memory
1118+** allocator, make this value as large as practical for the
1119+** architecture in use.
1120+*/
1121+#ifndef SQLITE_SPELLFIX_STACKALLOC_SZ
1122+# define SQLITE_SPELLFIX_STACKALLOC_SZ  (1024)
1123+#endif
1124+
1125+/* Compute the edit distance between two strings.
1126+**
1127+** If an error occurs, return a negative number which is the error code.
1128+**
1129+** If pnMatch is not NULL, then *pnMatch is set to the number of characters
1130+** (not bytes) in z2 that matched the search pattern in *pFrom. If pFrom does
1131+** not contain the pattern for a prefix-search, then this is always the number
1132+** of characters in z2. If pFrom does contain a prefix search pattern, then
1133+** it is the number of characters in the prefix of z2 that was deemed to 
1134+** match pFrom.
1135+*/
1136+static int editDist3Core(
1137+  EditDist3FromString *pFrom,  /* The FROM string */
1138+  const char *z2,              /* The TO string */
1139+  int n2,                      /* Length of the TO string */
1140+  const EditDist3Lang *pLang,  /* Edit weights for a particular language ID */
1141+  int *pnMatch                 /* OUT: Characters in matched prefix */
1142+){
1143+  int k, n;
1144+  int i1, b1;
1145+  int i2, b2;
1146+  EditDist3FromString f = *pFrom;
1147+  EditDist3To *a2;
1148+  unsigned int *m;
1149+  unsigned int *pToFree;
1150+  int szRow;
1151+  EditDist3Cost *p;
1152+  int res;
1153+  sqlite3_uint64 nByte;
1154+  unsigned int stackSpace[SQLITE_SPELLFIX_STACKALLOC_SZ/sizeof(unsigned int)];
1155+
1156+  /* allocate the Wagner matrix and the aTo[] array for the TO string */
1157+  n = (f.n+1)*(n2+1);
1158+  n = (n+1)&~1;
1159+  nByte = n*sizeof(m[0]) + sizeof(a2[0])*n2;
1160+  if( nByte<=sizeof(stackSpace) ){
1161+    m = stackSpace;
1162+    pToFree = 0;
1163+  }else{
1164+    m = pToFree = sqlite3_malloc64( nByte );
1165+    if( m==0 ) return -1;            /* Out of memory */
1166+  }
1167+  a2 = (EditDist3To*)&m[n];
1168+  memset(a2, 0, sizeof(a2[0])*n2);
1169+
1170+  /* Fill in the a1[] matrix for all characters of the TO string */
1171+  for(i2=0; i2<n2; i2++){
1172+    a2[i2].nByte = utf8Len((unsigned char)z2[i2], n2-i2);
1173+    for(p=pLang->pCost; p; p=p->pNext){
1174+      EditDist3Cost **apNew;
1175+      if( p->nFrom>0 ) break;
1176+      if( i2+p->nTo>n2 ) continue;
1177+      if( p->a[0]>z2[i2] ) break;
1178+      if( matchTo(p, z2+i2, n2-i2)==0 ) continue;
1179+      a2[i2].nIns++;
1180+      apNew = sqlite3_realloc64(a2[i2].apIns, sizeof(*apNew)*a2[i2].nIns);
1181+      if( apNew==0 ){
1182+        res = -1;  /* Out of memory */
1183+        goto editDist3Abort;
1184+      }
1185+      a2[i2].apIns = apNew;
1186+      a2[i2].apIns[a2[i2].nIns-1] = p;
1187+    }
1188+  }
1189+
1190+  /* Prepare to compute the minimum edit distance */
1191+  szRow = f.n+1;
1192+  memset(m, 0x01, (n2+1)*szRow*sizeof(m[0]));
1193+  m[0] = 0;
1194+
1195+  /* First fill in the top-row of the matrix with FROM deletion costs */
1196+  for(i1=0; i1<f.n; i1 += b1){
1197+    b1 = f.a[i1].nByte;
1198+    updateCost(m, i1+b1, i1, pLang->iDelCost);
1199+    for(k=0; k<f.a[i1].nDel; k++){
1200+      p = f.a[i1].apDel[k];
1201+      updateCost(m, i1+p->nFrom, i1, p->iCost);
1202+    }
1203+  }
1204+
1205+  /* Fill in all subsequent rows, top-to-bottom, left-to-right */
1206+  for(i2=0; i2<n2; i2 += b2){
1207+    int rx;      /* Starting index for current row */
1208+    int rxp;     /* Starting index for previous row */
1209+    b2 = a2[i2].nByte;
1210+    rx = szRow*(i2+b2);
1211+    rxp = szRow*i2;
1212+    updateCost(m, rx, rxp, pLang->iInsCost);
1213+    for(k=0; k<a2[i2].nIns; k++){
1214+      p = a2[i2].apIns[k];
1215+      updateCost(m, szRow*(i2+p->nTo), rxp, p->iCost);
1216+    }
1217+    for(i1=0; i1<f.n; i1+=b1){
1218+      int cx;    /* Index of current cell */
1219+      int cxp;   /* Index of cell immediately to the left */
1220+      int cxd;   /* Index of cell to the left and one row above */
1221+      int cxu;   /* Index of cell immediately above */
1222+      b1 = f.a[i1].nByte;
1223+      cxp = rx + i1;
1224+      cx = cxp + b1;
1225+      cxd = rxp + i1;
1226+      cxu = cxd + b1;
1227+      updateCost(m, cx, cxp, pLang->iDelCost);
1228+      for(k=0; k<f.a[i1].nDel; k++){
1229+        p = f.a[i1].apDel[k];
1230+        updateCost(m, cxp+p->nFrom, cxp, p->iCost);
1231+      }
1232+      updateCost(m, cx, cxu, pLang->iInsCost);
1233+      if( matchFromTo(&f, i1, z2+i2, n2-i2) ){
1234+        updateCost(m, cx, cxd, 0);
1235+      }
1236+      updateCost(m, cx, cxd, pLang->iSubCost);
1237+      for(k=0; k<f.a[i1].nSubst; k++){
1238+        p = f.a[i1].apSubst[k];
1239+        if( matchTo(p, z2+i2, n2-i2) ){
1240+          updateCost(m, cxd+p->nFrom+szRow*p->nTo, cxd, p->iCost);
1241+        }
1242+      }
1243+    }
1244+  }
1245+
1246+#if 0  /* Enable for debugging */
1247+  printf("         ^");
1248+  for(i1=0; i1<f.n; i1++) printf(" %c-%2x", f.z[i1], f.z[i1]&0xff);
1249+  printf("\n   ^:");
1250+  for(i1=0; i1<szRow; i1++){
1251+    int v = m[i1];
1252+    if( v>9999 ) printf(" ****");
1253+    else         printf(" %4d", v);
1254+  }
1255+  printf("\n");
1256+  for(i2=0; i2<n2; i2++){
1257+    printf("%c-%02x:", z2[i2], z2[i2]&0xff);
1258+    for(i1=0; i1<szRow; i1++){
1259+      int v = m[(i2+1)*szRow+i1];
1260+      if( v>9999 ) printf(" ****");
1261+      else         printf(" %4d", v);
1262+    }
1263+    printf("\n");
1264+  }
1265+#endif
1266+
1267+  /* Free memory allocations and return the result */
1268+  res = (int)m[szRow*(n2+1)-1];
1269+  n = n2;
1270+  if( f.isPrefix ){
1271+    for(i2=1; i2<=n2; i2++){
1272+      int b = m[szRow*i2-1];
1273+      if( b<=res ){ 
1274+        res = b;
1275+        n = i2 - 1;
1276+      }
1277+    }
1278+  }
1279+  if( pnMatch ){
1280+    int nExtra = 0;
1281+    for(k=0; k<n; k++){
1282+      if( (z2[k] & 0xc0)==0x80 ) nExtra++;
1283+    }
1284+    *pnMatch = n - nExtra;
1285+  }
1286+
1287+editDist3Abort:
1288+  for(i2=0; i2<n2; i2++) sqlite3_free(a2[i2].apIns);
1289+  sqlite3_free(pToFree);
1290+  return res;
1291+}
1292+
1293+/*
1294+** Get an appropriate EditDist3Lang object.
1295+*/
1296+static const EditDist3Lang *editDist3FindLang(
1297+  EditDist3Config *pConfig,
1298+  int iLang
1299+){
1300+  int i;
1301+  for(i=0; i<pConfig->nLang; i++){
1302+    if( pConfig->a[i].iLang==iLang ) return &pConfig->a[i];
1303+  }
1304+  return &editDist3Lang;
1305+}
1306+
1307+/*
1308+** Function:    editdist3(A,B,iLang)
1309+**              editdist3(tablename)
1310+**
1311+** Return the cost of transforming string A into string B using edit
1312+** weights for iLang.
1313+**
1314+** The second form loads edit weights into memory from a table.
1315+*/
1316+static void editDist3SqlFunc(
1317+  sqlite3_context *context,
1318+  int argc,
1319+  sqlite3_value **argv
1320+){
1321+  EditDist3Config *pConfig = (EditDist3Config*)sqlite3_user_data(context);
1322+  sqlite3 *db = sqlite3_context_db_handle(context);
1323+  int rc;
1324+  if( argc==1 ){
1325+    const char *zTable = (const char*)sqlite3_value_text(argv[0]);
1326+    rc = editDist3ConfigLoad(pConfig, db, zTable);
1327+    if( rc ) sqlite3_result_error_code(context, rc);
1328+  }else{
1329+    const char *zA = (const char*)sqlite3_value_text(argv[0]);
1330+    const char *zB = (const char*)sqlite3_value_text(argv[1]);
1331+    int nA = sqlite3_value_bytes(argv[0]);
1332+    int nB = sqlite3_value_bytes(argv[1]);
1333+    int iLang = argc==3 ? sqlite3_value_int(argv[2]) : 0;
1334+    const EditDist3Lang *pLang = editDist3FindLang(pConfig, iLang);
1335+    EditDist3FromString *pFrom;
1336+    int dist;
1337+
1338+    pFrom = editDist3FromStringNew(pLang, zA, nA);
1339+    if( pFrom==0 ){
1340+      sqlite3_result_error_nomem(context);
1341+      return;
1342+    }
1343+    dist = editDist3Core(pFrom, zB, nB, pLang, 0);
1344+    editDist3FromStringDelete(pFrom);
1345+    if( dist==(-1) ){
1346+      sqlite3_result_error_nomem(context);
1347+    }else{
1348+      sqlite3_result_int(context, dist);
1349+    }
1350+  } 
1351+}
1352+
1353+/*
1354+** Register the editDist3 function with SQLite
1355+*/
1356+static int editDist3Install(sqlite3 *db){
1357+  int rc;
1358+  EditDist3Config *pConfig = sqlite3_malloc64( sizeof(*pConfig) );
1359+  if( pConfig==0 ) return SQLITE_NOMEM;
1360+  memset(pConfig, 0, sizeof(*pConfig));
1361+  rc = sqlite3_create_function_v2(db, "editdist3",
1362+              2, SQLITE_UTF8|SQLITE_DETERMINISTIC, pConfig,
1363+              editDist3SqlFunc, 0, 0, 0);
1364+  if( rc==SQLITE_OK ){
1365+    rc = sqlite3_create_function_v2(db, "editdist3",
1366+                3, SQLITE_UTF8|SQLITE_DETERMINISTIC, pConfig,
1367+                editDist3SqlFunc, 0, 0, 0);
1368+  }
1369+  if( rc==SQLITE_OK ){
1370+    rc = sqlite3_create_function_v2(db, "editdist3",
1371+                1, SQLITE_UTF8|SQLITE_DETERMINISTIC, pConfig,
1372+                editDist3SqlFunc, 0, 0, editDist3ConfigDelete);
1373+  }else{
1374+    sqlite3_free(pConfig);
1375+  }
1376+  return rc;
1377+}
1378+/* End configurable cost unicode edit distance routines
1379+******************************************************************************
1380+******************************************************************************
1381+** Begin transliterate unicode-to-ascii implementation
1382+*/
1383+
1384+#if !SQLITE_AMALGAMATION
1385+/*
1386+** This lookup table is used to help decode the first byte of
1387+** a multi-byte UTF8 character.
1388+*/
1389+static const unsigned char sqlite3Utf8Trans1[] = {
1390+  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1391+  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
1392+  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
1393+  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
1394+  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1395+  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
1396+  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1397+  0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
1398+};
1399+#endif
1400+
1401+/*
1402+** Return the value of the first UTF-8 character in the string.
1403+*/
1404+static int utf8Read(const unsigned char *z, int n, int *pSize){
1405+  int c, i;
1406+
1407+  /* All callers to this routine (in the current implementation)
1408+  ** always have n>0. */
1409+  if( NEVER(n==0) ){
1410+    c = i = 0;
1411+  }else{
1412+    c = z[0];
1413+    i = 1;
1414+    if( c>=0xc0 ){
1415+      c = sqlite3Utf8Trans1[c-0xc0];
1416+      while( i<n && (z[i] & 0xc0)==0x80 ){
1417+        c = (c<<6) + (0x3f & z[i++]);
1418+      }
1419+    }
1420+  }
1421+  *pSize = i;
1422+  return c;
1423+}
1424+
1425+/*
1426+** Return the number of characters in the utf-8 string in the nIn byte
1427+** buffer pointed to by zIn.
1428+*/
1429+static int utf8Charlen(const char *zIn, int nIn){
1430+  int i;
1431+  int nChar = 0;
1432+  for(i=0; i<nIn; nChar++){
1433+    int sz;
1434+    utf8Read((const unsigned char *)&zIn[i], nIn-i, &sz);
1435+    i += sz;
1436+  }
1437+  return nChar;
1438+}
1439+
1440+typedef struct Transliteration Transliteration;
1441+struct Transliteration {
1442+ unsigned short int cFrom;
1443+ unsigned char cTo0, cTo1, cTo2, cTo3;
1444+#ifdef SQLITE_SPELLFIX_5BYTE_MAPPINGS
1445+ unsigned char cTo4;
1446+#endif
1447+};
1448+
1449+/*
1450+** Table of translations from unicode characters into ASCII.
1451+*/
1452+static const Transliteration translit[] = {
1453+  { 0x00A0,  0x20, 0x00, 0x00, 0x00 },  /*   to   */
1454+  { 0x00B5,  0x75, 0x00, 0x00, 0x00 },  /* µ to u */
1455+  { 0x00C0,  0x41, 0x00, 0x00, 0x00 },  /* À to A */
1456+  { 0x00C1,  0x41, 0x00, 0x00, 0x00 },  /* Á to A */
1457+  { 0x00C2,  0x41, 0x00, 0x00, 0x00 },  /* Â to A */
1458+  { 0x00C3,  0x41, 0x00, 0x00, 0x00 },  /* Ã to A */
1459+  { 0x00C4,  0x41, 0x65, 0x00, 0x00 },  /* Ä to Ae */
1460+  { 0x00C5,  0x41, 0x61, 0x00, 0x00 },  /* Å to Aa */
1461+  { 0x00C6,  0x41, 0x45, 0x00, 0x00 },  /* Æ to AE */
1462+  { 0x00C7,  0x43, 0x00, 0x00, 0x00 },  /* Ç to C */
1463+  { 0x00C8,  0x45, 0x00, 0x00, 0x00 },  /* È to E */
1464+  { 0x00C9,  0x45, 0x00, 0x00, 0x00 },  /* É to E */
1465+  { 0x00CA,  0x45, 0x00, 0x00, 0x00 },  /* Ê to E */
1466+  { 0x00CB,  0x45, 0x00, 0x00, 0x00 },  /* Ë to E */
1467+  { 0x00CC,  0x49, 0x00, 0x00, 0x00 },  /* Ì to I */
1468+  { 0x00CD,  0x49, 0x00, 0x00, 0x00 },  /* Í to I */
1469+  { 0x00CE,  0x49, 0x00, 0x00, 0x00 },  /* Î to I */
1470+  { 0x00CF,  0x49, 0x00, 0x00, 0x00 },  /* Ï to I */
1471+  { 0x00D0,  0x44, 0x00, 0x00, 0x00 },  /* Ð to D */
1472+  { 0x00D1,  0x4E, 0x00, 0x00, 0x00 },  /* Ñ to N */
1473+  { 0x00D2,  0x4F, 0x00, 0x00, 0x00 },  /* Ò to O */
1474+  { 0x00D3,  0x4F, 0x00, 0x00, 0x00 },  /* Ó to O */
1475+  { 0x00D4,  0x4F, 0x00, 0x00, 0x00 },  /* Ô to O */
1476+  { 0x00D5,  0x4F, 0x00, 0x00, 0x00 },  /* Õ to O */
1477+  { 0x00D6,  0x4F, 0x65, 0x00, 0x00 },  /* Ö to Oe */
1478+  { 0x00D7,  0x78, 0x00, 0x00, 0x00 },  /* × to x */
1479+  { 0x00D8,  0x4F, 0x00, 0x00, 0x00 },  /* Ø to O */
1480+  { 0x00D9,  0x55, 0x00, 0x00, 0x00 },  /* Ù to U */
1481+  { 0x00DA,  0x55, 0x00, 0x00, 0x00 },  /* Ú to U */
1482+  { 0x00DB,  0x55, 0x00, 0x00, 0x00 },  /* Û to U */
1483+  { 0x00DC,  0x55, 0x65, 0x00, 0x00 },  /* Ü to Ue */
1484+  { 0x00DD,  0x59, 0x00, 0x00, 0x00 },  /* Ý to Y */
1485+  { 0x00DE,  0x54, 0x68, 0x00, 0x00 },  /* Þ to Th */
1486+  { 0x00DF,  0x73, 0x73, 0x00, 0x00 },  /* ß to ss */
1487+  { 0x00E0,  0x61, 0x00, 0x00, 0x00 },  /* à to a */
1488+  { 0x00E1,  0x61, 0x00, 0x00, 0x00 },  /* á to a */
1489+  { 0x00E2,  0x61, 0x00, 0x00, 0x00 },  /* â to a */
1490+  { 0x00E3,  0x61, 0x00, 0x00, 0x00 },  /* ã to a */
1491+  { 0x00E4,  0x61, 0x65, 0x00, 0x00 },  /* ä to ae */
1492+  { 0x00E5,  0x61, 0x61, 0x00, 0x00 },  /* å to aa */
1493+  { 0x00E6,  0x61, 0x65, 0x00, 0x00 },  /* æ to ae */
1494+  { 0x00E7,  0x63, 0x00, 0x00, 0x00 },  /* ç to c */
1495+  { 0x00E8,  0x65, 0x00, 0x00, 0x00 },  /* è to e */
1496+  { 0x00E9,  0x65, 0x00, 0x00, 0x00 },  /* é to e */
1497+  { 0x00EA,  0x65, 0x00, 0x00, 0x00 },  /* ê to e */
1498+  { 0x00EB,  0x65, 0x00, 0x00, 0x00 },  /* ë to e */
1499+  { 0x00EC,  0x69, 0x00, 0x00, 0x00 },  /* ì to i */
1500+  { 0x00ED,  0x69, 0x00, 0x00, 0x00 },  /* í to i */
1501+  { 0x00EE,  0x69, 0x00, 0x00, 0x00 },  /* î to i */
1502+  { 0x00EF,  0x69, 0x00, 0x00, 0x00 },  /* ï to i */
1503+  { 0x00F0,  0x64, 0x00, 0x00, 0x00 },  /* ð to d */
1504+  { 0x00F1,  0x6E, 0x00, 0x00, 0x00 },  /* ñ to n */
1505+  { 0x00F2,  0x6F, 0x00, 0x00, 0x00 },  /* ò to o */
1506+  { 0x00F3,  0x6F, 0x00, 0x00, 0x00 },  /* ó to o */
1507+  { 0x00F4,  0x6F, 0x00, 0x00, 0x00 },  /* ô to o */
1508+  { 0x00F5,  0x6F, 0x00, 0x00, 0x00 },  /* õ to o */
1509+  { 0x00F6,  0x6F, 0x65, 0x00, 0x00 },  /* ö to oe */
1510+  { 0x00F7,  0x3A, 0x00, 0x00, 0x00 },  /* ÷ to : */
1511+  { 0x00F8,  0x6F, 0x00, 0x00, 0x00 },  /* ø to o */
1512+  { 0x00F9,  0x75, 0x00, 0x00, 0x00 },  /* ù to u */
1513+  { 0x00FA,  0x75, 0x00, 0x00, 0x00 },  /* ú to u */
1514+  { 0x00FB,  0x75, 0x00, 0x00, 0x00 },  /* û to u */
1515+  { 0x00FC,  0x75, 0x65, 0x00, 0x00 },  /* ü to ue */
1516+  { 0x00FD,  0x79, 0x00, 0x00, 0x00 },  /* ý to y */
1517+  { 0x00FE,  0x74, 0x68, 0x00, 0x00 },  /* þ to th */
1518+  { 0x00FF,  0x79, 0x00, 0x00, 0x00 },  /* ÿ to y */
1519+  { 0x0100,  0x41, 0x00, 0x00, 0x00 },  /* Ā to A */
1520+  { 0x0101,  0x61, 0x00, 0x00, 0x00 },  /* ā to a */
1521+  { 0x0102,  0x41, 0x00, 0x00, 0x00 },  /* Ă to A */
1522+  { 0x0103,  0x61, 0x00, 0x00, 0x00 },  /* ă to a */
1523+  { 0x0104,  0x41, 0x00, 0x00, 0x00 },  /* Ą to A */
1524+  { 0x0105,  0x61, 0x00, 0x00, 0x00 },  /* ą to a */
1525+  { 0x0106,  0x43, 0x00, 0x00, 0x00 },  /* Ć to C */
1526+  { 0x0107,  0x63, 0x00, 0x00, 0x00 },  /* ć to c */
1527+  { 0x0108,  0x43, 0x68, 0x00, 0x00 },  /* Ĉ to Ch */
1528+  { 0x0109,  0x63, 0x68, 0x00, 0x00 },  /* ĉ to ch */
1529+  { 0x010A,  0x43, 0x00, 0x00, 0x00 },  /* Ċ to C */
1530+  { 0x010B,  0x63, 0x00, 0x00, 0x00 },  /* ċ to c */
1531+  { 0x010C,  0x43, 0x00, 0x00, 0x00 },  /* Č to C */
1532+  { 0x010D,  0x63, 0x00, 0x00, 0x00 },  /* č to c */
1533+  { 0x010E,  0x44, 0x00, 0x00, 0x00 },  /* Ď to D */
1534+  { 0x010F,  0x64, 0x00, 0x00, 0x00 },  /* ď to d */
1535+  { 0x0110,  0x44, 0x00, 0x00, 0x00 },  /* Đ to D */
1536+  { 0x0111,  0x64, 0x00, 0x00, 0x00 },  /* đ to d */
1537+  { 0x0112,  0x45, 0x00, 0x00, 0x00 },  /* Ē to E */
1538+  { 0x0113,  0x65, 0x00, 0x00, 0x00 },  /* ē to e */
1539+  { 0x0114,  0x45, 0x00, 0x00, 0x00 },  /* Ĕ to E */
1540+  { 0x0115,  0x65, 0x00, 0x00, 0x00 },  /* ĕ to e */
1541+  { 0x0116,  0x45, 0x00, 0x00, 0x00 },  /* Ė to E */
1542+  { 0x0117,  0x65, 0x00, 0x00, 0x00 },  /* ė to e */
1543+  { 0x0118,  0x45, 0x00, 0x00, 0x00 },  /* Ę to E */
1544+  { 0x0119,  0x65, 0x00, 0x00, 0x00 },  /* ę to e */
1545+  { 0x011A,  0x45, 0x00, 0x00, 0x00 },  /* Ě to E */
1546+  { 0x011B,  0x65, 0x00, 0x00, 0x00 },  /* ě to e */
1547+  { 0x011C,  0x47, 0x68, 0x00, 0x00 },  /* Ĝ to Gh */
1548+  { 0x011D,  0x67, 0x68, 0x00, 0x00 },  /* ĝ to gh */
1549+  { 0x011E,  0x47, 0x00, 0x00, 0x00 },  /* Ğ to G */
1550+  { 0x011F,  0x67, 0x00, 0x00, 0x00 },  /* ğ to g */
1551+  { 0x0120,  0x47, 0x00, 0x00, 0x00 },  /* Ġ to G */
1552+  { 0x0121,  0x67, 0x00, 0x00, 0x00 },  /* ġ to g */
1553+  { 0x0122,  0x47, 0x00, 0x00, 0x00 },  /* Ģ to G */
1554+  { 0x0123,  0x67, 0x00, 0x00, 0x00 },  /* ģ to g */
1555+  { 0x0124,  0x48, 0x68, 0x00, 0x00 },  /* Ĥ to Hh */
1556+  { 0x0125,  0x68, 0x68, 0x00, 0x00 },  /* ĥ to hh */
1557+  { 0x0126,  0x48, 0x00, 0x00, 0x00 },  /* Ħ to H */
1558+  { 0x0127,  0x68, 0x00, 0x00, 0x00 },  /* ħ to h */
1559+  { 0x0128,  0x49, 0x00, 0x00, 0x00 },  /* Ĩ to I */
1560+  { 0x0129,  0x69, 0x00, 0x00, 0x00 },  /* ĩ to i */
1561+  { 0x012A,  0x49, 0x00, 0x00, 0x00 },  /* Ī to I */
1562+  { 0x012B,  0x69, 0x00, 0x00, 0x00 },  /* ī to i */
1563+  { 0x012C,  0x49, 0x00, 0x00, 0x00 },  /* Ĭ to I */
1564+  { 0x012D,  0x69, 0x00, 0x00, 0x00 },  /* ĭ to i */
1565+  { 0x012E,  0x49, 0x00, 0x00, 0x00 },  /* Į to I */
1566+  { 0x012F,  0x69, 0x00, 0x00, 0x00 },  /* į to i */
1567+  { 0x0130,  0x49, 0x00, 0x00, 0x00 },  /* İ to I */
1568+  { 0x0131,  0x69, 0x00, 0x00, 0x00 },  /* ı to i */
1569+  { 0x0132,  0x49, 0x4A, 0x00, 0x00 },  /* IJ to IJ */
1570+  { 0x0133,  0x69, 0x6A, 0x00, 0x00 },  /* ij to ij */
1571+  { 0x0134,  0x4A, 0x68, 0x00, 0x00 },  /* Ĵ to Jh */
1572+  { 0x0135,  0x6A, 0x68, 0x00, 0x00 },  /* ĵ to jh */
1573+  { 0x0136,  0x4B, 0x00, 0x00, 0x00 },  /* Ķ to K */
1574+  { 0x0137,  0x6B, 0x00, 0x00, 0x00 },  /* ķ to k */
1575+  { 0x0138,  0x6B, 0x00, 0x00, 0x00 },  /* ĸ to k */
1576+  { 0x0139,  0x4C, 0x00, 0x00, 0x00 },  /* Ĺ to L */
1577+  { 0x013A,  0x6C, 0x00, 0x00, 0x00 },  /* ĺ to l */
1578+  { 0x013B,  0x4C, 0x00, 0x00, 0x00 },  /* Ļ to L */
1579+  { 0x013C,  0x6C, 0x00, 0x00, 0x00 },  /* ļ to l */
1580+  { 0x013D,  0x4C, 0x00, 0x00, 0x00 },  /* Ľ to L */
1581+  { 0x013E,  0x6C, 0x00, 0x00, 0x00 },  /* ľ to l */
1582+  { 0x013F,  0x4C, 0x2E, 0x00, 0x00 },  /* Ŀ to L. */
1583+  { 0x0140,  0x6C, 0x2E, 0x00, 0x00 },  /* ŀ to l. */
1584+  { 0x0141,  0x4C, 0x00, 0x00, 0x00 },  /* Ł to L */
1585+  { 0x0142,  0x6C, 0x00, 0x00, 0x00 },  /* ł to l */
1586+  { 0x0143,  0x4E, 0x00, 0x00, 0x00 },  /* Ń to N */
1587+  { 0x0144,  0x6E, 0x00, 0x00, 0x00 },  /* ń to n */
1588+  { 0x0145,  0x4E, 0x00, 0x00, 0x00 },  /* Ņ to N */
1589+  { 0x0146,  0x6E, 0x00, 0x00, 0x00 },  /* ņ to n */
1590+  { 0x0147,  0x4E, 0x00, 0x00, 0x00 },  /* Ň to N */
1591+  { 0x0148,  0x6E, 0x00, 0x00, 0x00 },  /* ň to n */
1592+  { 0x0149,  0x27, 0x6E, 0x00, 0x00 },  /* ʼn to 'n */
1593+  { 0x014A,  0x4E, 0x47, 0x00, 0x00 },  /* Ŋ to NG */
1594+  { 0x014B,  0x6E, 0x67, 0x00, 0x00 },  /* ŋ to ng */
1595+  { 0x014C,  0x4F, 0x00, 0x00, 0x00 },  /* Ō to O */
1596+  { 0x014D,  0x6F, 0x00, 0x00, 0x00 },  /* ō to o */
1597+  { 0x014E,  0x4F, 0x00, 0x00, 0x00 },  /* Ŏ to O */
1598+  { 0x014F,  0x6F, 0x00, 0x00, 0x00 },  /* ŏ to o */
1599+  { 0x0150,  0x4F, 0x00, 0x00, 0x00 },  /* Ő to O */
1600+  { 0x0151,  0x6F, 0x00, 0x00, 0x00 },  /* ő to o */
1601+  { 0x0152,  0x4F, 0x45, 0x00, 0x00 },  /* Œ to OE */
1602+  { 0x0153,  0x6F, 0x65, 0x00, 0x00 },  /* œ to oe */
1603+  { 0x0154,  0x52, 0x00, 0x00, 0x00 },  /* Ŕ to R */
1604+  { 0x0155,  0x72, 0x00, 0x00, 0x00 },  /* ŕ to r */
1605+  { 0x0156,  0x52, 0x00, 0x00, 0x00 },  /* Ŗ to R */
1606+  { 0x0157,  0x72, 0x00, 0x00, 0x00 },  /* ŗ to r */
1607+  { 0x0158,  0x52, 0x00, 0x00, 0x00 },  /* Ř to R */
1608+  { 0x0159,  0x72, 0x00, 0x00, 0x00 },  /* ř to r */
1609+  { 0x015A,  0x53, 0x00, 0x00, 0x00 },  /* Ś to S */
1610+  { 0x015B,  0x73, 0x00, 0x00, 0x00 },  /* ś to s */
1611+  { 0x015C,  0x53, 0x68, 0x00, 0x00 },  /* Ŝ to Sh */
1612+  { 0x015D,  0x73, 0x68, 0x00, 0x00 },  /* ŝ to sh */
1613+  { 0x015E,  0x53, 0x00, 0x00, 0x00 },  /* Ş to S */
1614+  { 0x015F,  0x73, 0x00, 0x00, 0x00 },  /* ş to s */
1615+  { 0x0160,  0x53, 0x00, 0x00, 0x00 },  /* Š to S */
1616+  { 0x0161,  0x73, 0x00, 0x00, 0x00 },  /* š to s */
1617+  { 0x0162,  0x54, 0x00, 0x00, 0x00 },  /* Ţ to T */
1618+  { 0x0163,  0x74, 0x00, 0x00, 0x00 },  /* ţ to t */
1619+  { 0x0164,  0x54, 0x00, 0x00, 0x00 },  /* Ť to T */
1620+  { 0x0165,  0x74, 0x00, 0x00, 0x00 },  /* ť to t */
1621+  { 0x0166,  0x54, 0x00, 0x00, 0x00 },  /* Ŧ to T */
1622+  { 0x0167,  0x74, 0x00, 0x00, 0x00 },  /* ŧ to t */
1623+  { 0x0168,  0x55, 0x00, 0x00, 0x00 },  /* Ũ to U */
1624+  { 0x0169,  0x75, 0x00, 0x00, 0x00 },  /* ũ to u */
1625+  { 0x016A,  0x55, 0x00, 0x00, 0x00 },  /* Ū to U */
1626+  { 0x016B,  0x75, 0x00, 0x00, 0x00 },  /* ū to u */
1627+  { 0x016C,  0x55, 0x00, 0x00, 0x00 },  /* Ŭ to U */
1628+  { 0x016D,  0x75, 0x00, 0x00, 0x00 },  /* ŭ to u */
1629+  { 0x016E,  0x55, 0x00, 0x00, 0x00 },  /* Ů to U */
1630+  { 0x016F,  0x75, 0x00, 0x00, 0x00 },  /* ů to u */
1631+  { 0x0170,  0x55, 0x00, 0x00, 0x00 },  /* Ű to U */
1632+  { 0x0171,  0x75, 0x00, 0x00, 0x00 },  /* ű to u */
1633+  { 0x0172,  0x55, 0x00, 0x00, 0x00 },  /* Ų to U */
1634+  { 0x0173,  0x75, 0x00, 0x00, 0x00 },  /* ų to u */
1635+  { 0x0174,  0x57, 0x00, 0x00, 0x00 },  /* Ŵ to W */
1636+  { 0x0175,  0x77, 0x00, 0x00, 0x00 },  /* ŵ to w */
1637+  { 0x0176,  0x59, 0x00, 0x00, 0x00 },  /* Ŷ to Y */
1638+  { 0x0177,  0x79, 0x00, 0x00, 0x00 },  /* ŷ to y */
1639+  { 0x0178,  0x59, 0x00, 0x00, 0x00 },  /* Ÿ to Y */
1640+  { 0x0179,  0x5A, 0x00, 0x00, 0x00 },  /* Ź to Z */
1641+  { 0x017A,  0x7A, 0x00, 0x00, 0x00 },  /* ź to z */
1642+  { 0x017B,  0x5A, 0x00, 0x00, 0x00 },  /* Ż to Z */
1643+  { 0x017C,  0x7A, 0x00, 0x00, 0x00 },  /* ż to z */
1644+  { 0x017D,  0x5A, 0x00, 0x00, 0x00 },  /* Ž to Z */
1645+  { 0x017E,  0x7A, 0x00, 0x00, 0x00 },  /* ž to z */
1646+  { 0x017F,  0x73, 0x00, 0x00, 0x00 },  /* ſ to s */
1647+  { 0x0192,  0x66, 0x00, 0x00, 0x00 },  /* ƒ to f */
1648+  { 0x0218,  0x53, 0x00, 0x00, 0x00 },  /* Ș to S */
1649+  { 0x0219,  0x73, 0x00, 0x00, 0x00 },  /* ș to s */
1650+  { 0x021A,  0x54, 0x00, 0x00, 0x00 },  /* Ț to T */
1651+  { 0x021B,  0x74, 0x00, 0x00, 0x00 },  /* ț to t */
1652+  { 0x0386,  0x41, 0x00, 0x00, 0x00 },  /* Ά to A */
1653+  { 0x0388,  0x45, 0x00, 0x00, 0x00 },  /* Έ to E */
1654+  { 0x0389,  0x49, 0x00, 0x00, 0x00 },  /* Ή to I */
1655+  { 0x038A,  0x49, 0x00, 0x00, 0x00 },  /* Ί to I */
1656+  { 0x038C,  0x4f, 0x00, 0x00, 0x00 },  /* Ό to O */
1657+  { 0x038E,  0x59, 0x00, 0x00, 0x00 },  /* Ύ to Y */
1658+  { 0x038F,  0x4f, 0x00, 0x00, 0x00 },  /* Ώ to O */
1659+  { 0x0390,  0x69, 0x00, 0x00, 0x00 },  /* ΐ to i */
1660+  { 0x0391,  0x41, 0x00, 0x00, 0x00 },  /* Α to A */
1661+  { 0x0392,  0x42, 0x00, 0x00, 0x00 },  /* Β to B */
1662+  { 0x0393,  0x47, 0x00, 0x00, 0x00 },  /* Γ to G */
1663+  { 0x0394,  0x44, 0x00, 0x00, 0x00 },  /* Δ to D */
1664+  { 0x0395,  0x45, 0x00, 0x00, 0x00 },  /* Ε to E */
1665+  { 0x0396,  0x5a, 0x00, 0x00, 0x00 },  /* Ζ to Z */
1666+  { 0x0397,  0x49, 0x00, 0x00, 0x00 },  /* Η to I */
1667+  { 0x0398,  0x54, 0x68, 0x00, 0x00 },  /* Θ to Th */
1668+  { 0x0399,  0x49, 0x00, 0x00, 0x00 },  /* Ι to I */
1669+  { 0x039A,  0x4b, 0x00, 0x00, 0x00 },  /* Κ to K */
1670+  { 0x039B,  0x4c, 0x00, 0x00, 0x00 },  /* Λ to L */
1671+  { 0x039C,  0x4d, 0x00, 0x00, 0x00 },  /* Μ to M */
1672+  { 0x039D,  0x4e, 0x00, 0x00, 0x00 },  /* Ν to N */
1673+  { 0x039E,  0x58, 0x00, 0x00, 0x00 },  /* Ξ to X */
1674+  { 0x039F,  0x4f, 0x00, 0x00, 0x00 },  /* Ο to O */
1675+  { 0x03A0,  0x50, 0x00, 0x00, 0x00 },  /* Π to P */
1676+  { 0x03A1,  0x52, 0x00, 0x00, 0x00 },  /* Ρ to R */
1677+  { 0x03A3,  0x53, 0x00, 0x00, 0x00 },  /* Σ to S */
1678+  { 0x03A4,  0x54, 0x00, 0x00, 0x00 },  /* Τ to T */
1679+  { 0x03A5,  0x59, 0x00, 0x00, 0x00 },  /* Υ to Y */
1680+  { 0x03A6,  0x46, 0x00, 0x00, 0x00 },  /* Φ to F */
1681+  { 0x03A7,  0x43, 0x68, 0x00, 0x00 },  /* Χ to Ch */
1682+  { 0x03A8,  0x50, 0x73, 0x00, 0x00 },  /* Ψ to Ps */
1683+  { 0x03A9,  0x4f, 0x00, 0x00, 0x00 },  /* Ω to O */
1684+  { 0x03AA,  0x49, 0x00, 0x00, 0x00 },  /* Ϊ to I */
1685+  { 0x03AB,  0x59, 0x00, 0x00, 0x00 },  /* Ϋ to Y */
1686+  { 0x03AC,  0x61, 0x00, 0x00, 0x00 },  /* ά to a */
1687+  { 0x03AD,  0x65, 0x00, 0x00, 0x00 },  /* έ to e */
1688+  { 0x03AE,  0x69, 0x00, 0x00, 0x00 },  /* ή to i */
1689+  { 0x03AF,  0x69, 0x00, 0x00, 0x00 },  /* ί to i */
1690+  { 0x03B1,  0x61, 0x00, 0x00, 0x00 },  /* α to a */
1691+  { 0x03B2,  0x62, 0x00, 0x00, 0x00 },  /* β to b */
1692+  { 0x03B3,  0x67, 0x00, 0x00, 0x00 },  /* γ to g */
1693+  { 0x03B4,  0x64, 0x00, 0x00, 0x00 },  /* δ to d */
1694+  { 0x03B5,  0x65, 0x00, 0x00, 0x00 },  /* ε to e */
1695+  { 0x03B6,  0x7a, 0x00, 0x00, 0x00 },  /* ζ to z */
1696+  { 0x03B7,  0x69, 0x00, 0x00, 0x00 },  /* η to i */
1697+  { 0x03B8,  0x74, 0x68, 0x00, 0x00 },  /* θ to th */
1698+  { 0x03B9,  0x69, 0x00, 0x00, 0x00 },  /* ι to i */
1699+  { 0x03BA,  0x6b, 0x00, 0x00, 0x00 },  /* κ to k */
1700+  { 0x03BB,  0x6c, 0x00, 0x00, 0x00 },  /* λ to l */
1701+  { 0x03BC,  0x6d, 0x00, 0x00, 0x00 },  /* μ to m */
1702+  { 0x03BD,  0x6e, 0x00, 0x00, 0x00 },  /* ν to n */
1703+  { 0x03BE,  0x78, 0x00, 0x00, 0x00 },  /* ξ to x */
1704+  { 0x03BF,  0x6f, 0x00, 0x00, 0x00 },  /* ο to o */
1705+  { 0x03C0,  0x70, 0x00, 0x00, 0x00 },  /* π to p */
1706+  { 0x03C1,  0x72, 0x00, 0x00, 0x00 },  /* ρ to r */
1707+  { 0x03C3,  0x73, 0x00, 0x00, 0x00 },  /* σ to s */
1708+  { 0x03C4,  0x74, 0x00, 0x00, 0x00 },  /* τ to t */
1709+  { 0x03C5,  0x79, 0x00, 0x00, 0x00 },  /* υ to y */
1710+  { 0x03C6,  0x66, 0x00, 0x00, 0x00 },  /* φ to f */
1711+  { 0x03C7,  0x63, 0x68, 0x00, 0x00 },  /* χ to ch */
1712+  { 0x03C8,  0x70, 0x73, 0x00, 0x00 },  /* ψ to ps */
1713+  { 0x03C9,  0x6f, 0x00, 0x00, 0x00 },  /* ω to o */
1714+  { 0x03CA,  0x69, 0x00, 0x00, 0x00 },  /* ϊ to i */
1715+  { 0x03CB,  0x79, 0x00, 0x00, 0x00 },  /* ϋ to y */
1716+  { 0x03CC,  0x6f, 0x00, 0x00, 0x00 },  /* ό to o */
1717+  { 0x03CD,  0x79, 0x00, 0x00, 0x00 },  /* ύ to y */
1718+  { 0x03CE,  0x69, 0x00, 0x00, 0x00 },  /* ώ to i */
1719+  { 0x0400,  0x45, 0x00, 0x00, 0x00 },  /* Ѐ to E */
1720+  { 0x0401,  0x45, 0x00, 0x00, 0x00 },  /* Ё to E */
1721+  { 0x0402,  0x44, 0x00, 0x00, 0x00 },  /* Ђ to D */
1722+  { 0x0403,  0x47, 0x00, 0x00, 0x00 },  /* Ѓ to G */
1723+  { 0x0404,  0x45, 0x00, 0x00, 0x00 },  /* Є to E */
1724+  { 0x0405,  0x5a, 0x00, 0x00, 0x00 },  /* Ѕ to Z */
1725+  { 0x0406,  0x49, 0x00, 0x00, 0x00 },  /* І to I */
1726+  { 0x0407,  0x49, 0x00, 0x00, 0x00 },  /* Ї to I */
1727+  { 0x0408,  0x4a, 0x00, 0x00, 0x00 },  /* Ј to J */
1728+  { 0x0409,  0x49, 0x00, 0x00, 0x00 },  /* Љ to I */
1729+  { 0x040A,  0x4e, 0x00, 0x00, 0x00 },  /* Њ to N */
1730+  { 0x040B,  0x44, 0x00, 0x00, 0x00 },  /* Ћ to D */
1731+  { 0x040C,  0x4b, 0x00, 0x00, 0x00 },  /* Ќ to K */
1732+  { 0x040D,  0x49, 0x00, 0x00, 0x00 },  /* Ѝ to I */
1733+  { 0x040E,  0x55, 0x00, 0x00, 0x00 },  /* Ў to U */
1734+  { 0x040F,  0x44, 0x00, 0x00, 0x00 },  /* Џ to D */
1735+  { 0x0410,  0x41, 0x00, 0x00, 0x00 },  /* А to A */
1736+  { 0x0411,  0x42, 0x00, 0x00, 0x00 },  /* Б to B */
1737+  { 0x0412,  0x56, 0x00, 0x00, 0x00 },  /* В to V */
1738+  { 0x0413,  0x47, 0x00, 0x00, 0x00 },  /* Г to G */
1739+  { 0x0414,  0x44, 0x00, 0x00, 0x00 },  /* Д to D */
1740+  { 0x0415,  0x45, 0x00, 0x00, 0x00 },  /* Е to E */
1741+  { 0x0416,  0x5a, 0x68, 0x00, 0x00 },  /* Ж to Zh */
1742+  { 0x0417,  0x5a, 0x00, 0x00, 0x00 },  /* З to Z */
1743+  { 0x0418,  0x49, 0x00, 0x00, 0x00 },  /* И to I */
1744+  { 0x0419,  0x49, 0x00, 0x00, 0x00 },  /* Й to I */
1745+  { 0x041A,  0x4b, 0x00, 0x00, 0x00 },  /* К to K */
1746+  { 0x041B,  0x4c, 0x00, 0x00, 0x00 },  /* Л to L */
1747+  { 0x041C,  0x4d, 0x00, 0x00, 0x00 },  /* М to M */
1748+  { 0x041D,  0x4e, 0x00, 0x00, 0x00 },  /* Н to N */
1749+  { 0x041E,  0x4f, 0x00, 0x00, 0x00 },  /* О to O */
1750+  { 0x041F,  0x50, 0x00, 0x00, 0x00 },  /* П to P */
1751+  { 0x0420,  0x52, 0x00, 0x00, 0x00 },  /* Р to R */
1752+  { 0x0421,  0x53, 0x00, 0x00, 0x00 },  /* С to S */
1753+  { 0x0422,  0x54, 0x00, 0x00, 0x00 },  /* Т to T */
1754+  { 0x0423,  0x55, 0x00, 0x00, 0x00 },  /* У to U */
1755+  { 0x0424,  0x46, 0x00, 0x00, 0x00 },  /* Ф to F */
1756+  { 0x0425,  0x4b, 0x68, 0x00, 0x00 },  /* Х to Kh */
1757+  { 0x0426,  0x54, 0x63, 0x00, 0x00 },  /* Ц to Tc */
1758+  { 0x0427,  0x43, 0x68, 0x00, 0x00 },  /* Ч to Ch */
1759+  { 0x0428,  0x53, 0x68, 0x00, 0x00 },  /* Ш to Sh */
1760+  { 0x0429,  0x53, 0x68, 0x63, 0x68 },  /* Щ to Shch */
1761+  { 0x042A,  0x61, 0x00, 0x00, 0x00 },  /*  to A */
1762+  { 0x042B,  0x59, 0x00, 0x00, 0x00 },  /* Ы to Y */
1763+  { 0x042C,  0x59, 0x00, 0x00, 0x00 },  /*  to Y */
1764+  { 0x042D,  0x45, 0x00, 0x00, 0x00 },  /* Э to E */
1765+  { 0x042E,  0x49, 0x75, 0x00, 0x00 },  /* Ю to Iu */
1766+  { 0x042F,  0x49, 0x61, 0x00, 0x00 },  /* Я to Ia */
1767+  { 0x0430,  0x61, 0x00, 0x00, 0x00 },  /* а to a */
1768+  { 0x0431,  0x62, 0x00, 0x00, 0x00 },  /* б to b */
1769+  { 0x0432,  0x76, 0x00, 0x00, 0x00 },  /* в to v */
1770+  { 0x0433,  0x67, 0x00, 0x00, 0x00 },  /* г to g */
1771+  { 0x0434,  0x64, 0x00, 0x00, 0x00 },  /* д to d */
1772+  { 0x0435,  0x65, 0x00, 0x00, 0x00 },  /* е to e */
1773+  { 0x0436,  0x7a, 0x68, 0x00, 0x00 },  /* ж to zh */
1774+  { 0x0437,  0x7a, 0x00, 0x00, 0x00 },  /* з to z */
1775+  { 0x0438,  0x69, 0x00, 0x00, 0x00 },  /* и to i */
1776+  { 0x0439,  0x69, 0x00, 0x00, 0x00 },  /* й to i */
1777+  { 0x043A,  0x6b, 0x00, 0x00, 0x00 },  /* к to k */
1778+  { 0x043B,  0x6c, 0x00, 0x00, 0x00 },  /* л to l */
1779+  { 0x043C,  0x6d, 0x00, 0x00, 0x00 },  /* м to m */
1780+  { 0x043D,  0x6e, 0x00, 0x00, 0x00 },  /* н to n */
1781+  { 0x043E,  0x6f, 0x00, 0x00, 0x00 },  /* о to o */
1782+  { 0x043F,  0x70, 0x00, 0x00, 0x00 },  /* п to p */
1783+  { 0x0440,  0x72, 0x00, 0x00, 0x00 },  /* р to r */
1784+  { 0x0441,  0x73, 0x00, 0x00, 0x00 },  /* с to s */
1785+  { 0x0442,  0x74, 0x00, 0x00, 0x00 },  /* т to t */
1786+  { 0x0443,  0x75, 0x00, 0x00, 0x00 },  /* у to u */
1787+  { 0x0444,  0x66, 0x00, 0x00, 0x00 },  /* ф to f */
1788+  { 0x0445,  0x6b, 0x68, 0x00, 0x00 },  /* х to kh */
1789+  { 0x0446,  0x74, 0x63, 0x00, 0x00 },  /* ц to tc */
1790+  { 0x0447,  0x63, 0x68, 0x00, 0x00 },  /* ч to ch */
1791+  { 0x0448,  0x73, 0x68, 0x00, 0x00 },  /* ш to sh */
1792+  { 0x0449,  0x73, 0x68, 0x63, 0x68 },  /* щ to shch */
1793+  { 0x044A,  0x61, 0x00, 0x00, 0x00 },  /*  to a */
1794+  { 0x044B,  0x79, 0x00, 0x00, 0x00 },  /* ы to y */
1795+  { 0x044C,  0x79, 0x00, 0x00, 0x00 },  /*  to y */
1796+  { 0x044D,  0x65, 0x00, 0x00, 0x00 },  /* э to e */
1797+  { 0x044E,  0x69, 0x75, 0x00, 0x00 },  /* ю to iu */
1798+  { 0x044F,  0x69, 0x61, 0x00, 0x00 },  /* я to ia */
1799+  { 0x0450,  0x65, 0x00, 0x00, 0x00 },  /* ѐ to e */
1800+  { 0x0451,  0x65, 0x00, 0x00, 0x00 },  /* ё to e */
1801+  { 0x0452,  0x64, 0x00, 0x00, 0x00 },  /* ђ to d */
1802+  { 0x0453,  0x67, 0x00, 0x00, 0x00 },  /* ѓ to g */
1803+  { 0x0454,  0x65, 0x00, 0x00, 0x00 },  /* є to e */
1804+  { 0x0455,  0x7a, 0x00, 0x00, 0x00 },  /* ѕ to z */
1805+  { 0x0456,  0x69, 0x00, 0x00, 0x00 },  /* і to i */
1806+  { 0x0457,  0x69, 0x00, 0x00, 0x00 },  /* ї to i */
1807+  { 0x0458,  0x6a, 0x00, 0x00, 0x00 },  /* ј to j */
1808+  { 0x0459,  0x69, 0x00, 0x00, 0x00 },  /* љ to i */
1809+  { 0x045A,  0x6e, 0x00, 0x00, 0x00 },  /* њ to n */
1810+  { 0x045B,  0x64, 0x00, 0x00, 0x00 },  /* ћ to d */
1811+  { 0x045C,  0x6b, 0x00, 0x00, 0x00 },  /* ќ to k */
1812+  { 0x045D,  0x69, 0x00, 0x00, 0x00 },  /* ѝ to i */
1813+  { 0x045E,  0x75, 0x00, 0x00, 0x00 },  /* ў to u */
1814+  { 0x045F,  0x64, 0x00, 0x00, 0x00 },  /* џ to d */
1815+  { 0x1E02,  0x42, 0x00, 0x00, 0x00 },  /* Ḃ to B */
1816+  { 0x1E03,  0x62, 0x00, 0x00, 0x00 },  /* ḃ to b */
1817+  { 0x1E0A,  0x44, 0x00, 0x00, 0x00 },  /* Ḋ to D */
1818+  { 0x1E0B,  0x64, 0x00, 0x00, 0x00 },  /* ḋ to d */
1819+  { 0x1E1E,  0x46, 0x00, 0x00, 0x00 },  /* Ḟ to F */
1820+  { 0x1E1F,  0x66, 0x00, 0x00, 0x00 },  /* ḟ to f */
1821+  { 0x1E40,  0x4D, 0x00, 0x00, 0x00 },  /* Ṁ to M */
1822+  { 0x1E41,  0x6D, 0x00, 0x00, 0x00 },  /* ṁ to m */
1823+  { 0x1E56,  0x50, 0x00, 0x00, 0x00 },  /* Ṗ to P */
1824+  { 0x1E57,  0x70, 0x00, 0x00, 0x00 },  /* ṗ to p */
1825+  { 0x1E60,  0x53, 0x00, 0x00, 0x00 },  /* Ṡ to S */
1826+  { 0x1E61,  0x73, 0x00, 0x00, 0x00 },  /* ṡ to s */
1827+  { 0x1E6A,  0x54, 0x00, 0x00, 0x00 },  /* Ṫ to T */
1828+  { 0x1E6B,  0x74, 0x00, 0x00, 0x00 },  /* ṫ to t */
1829+  { 0x1E80,  0x57, 0x00, 0x00, 0x00 },  /* Ẁ to W */
1830+  { 0x1E81,  0x77, 0x00, 0x00, 0x00 },  /* ẁ to w */
1831+  { 0x1E82,  0x57, 0x00, 0x00, 0x00 },  /* Ẃ to W */
1832+  { 0x1E83,  0x77, 0x00, 0x00, 0x00 },  /* ẃ to w */
1833+  { 0x1E84,  0x57, 0x00, 0x00, 0x00 },  /* Ẅ to W */
1834+  { 0x1E85,  0x77, 0x00, 0x00, 0x00 },  /* ẅ to w */
1835+  { 0x1EF2,  0x59, 0x00, 0x00, 0x00 },  /* Ỳ to Y */
1836+  { 0x1EF3,  0x79, 0x00, 0x00, 0x00 },  /* ỳ to y */
1837+  { 0xFB00,  0x66, 0x66, 0x00, 0x00 },  /* ff to ff */
1838+  { 0xFB01,  0x66, 0x69, 0x00, 0x00 },  /* fi to fi */
1839+  { 0xFB02,  0x66, 0x6C, 0x00, 0x00 },  /* fl to fl */
1840+  { 0xFB05,  0x73, 0x74, 0x00, 0x00 },  /* ſt to st */
1841+  { 0xFB06,  0x73, 0x74, 0x00, 0x00 },  /* st to st */
1842+};
1843+
1844+static const Transliteration *spellfixFindTranslit(int c, int *pxTop){
1845+  *pxTop = (sizeof(translit)/sizeof(translit[0])) - 1;
1846+  return translit;
1847+}
1848+
1849+/*
1850+** Convert the input string from UTF-8 into pure ASCII by converting
1851+** all non-ASCII characters to some combination of characters in the
1852+** ASCII subset.
1853+**
1854+** The returned string might contain more characters than the input.
1855+**
1856+** Space to hold the returned string comes from sqlite3_malloc() and
1857+** should be freed by the caller.
1858+*/
1859+static unsigned char *transliterate(const unsigned char *zIn, int nIn){
1860+#ifdef SQLITE_SPELLFIX_5BYTE_MAPPINGS
1861+  unsigned char *zOut = sqlite3_malloc64( nIn*5 + 1 );
1862+#else
1863+  unsigned char *zOut = sqlite3_malloc64( nIn*4 + 1 );
1864+#endif
1865+  int c, sz, nOut;
1866+  if( zOut==0 ) return 0;
1867+  nOut = 0;
1868+  while( nIn>0 ){
1869+    c = utf8Read(zIn, nIn, &sz);
1870+    zIn += sz;
1871+    nIn -= sz;
1872+    if( c<=127 ){
1873+      zOut[nOut++] = (unsigned char)c;
1874+    }else{
1875+      int xTop, xBtm, x;
1876+      const Transliteration *tbl = spellfixFindTranslit(c, &xTop);
1877+      xBtm = 0;
1878+      while( xTop>=xBtm ){
1879+        x = (xTop + xBtm)/2;
1880+        if( tbl[x].cFrom==c ){
1881+          zOut[nOut++] = tbl[x].cTo0;
1882+          if( tbl[x].cTo1 ){
1883+            zOut[nOut++] = tbl[x].cTo1;
1884+            if( tbl[x].cTo2 ){
1885+              zOut[nOut++] = tbl[x].cTo2;
1886+              if( tbl[x].cTo3 ){
1887+                zOut[nOut++] = tbl[x].cTo3;
1888+#ifdef SQLITE_SPELLFIX_5BYTE_MAPPINGS
1889+                if( tbl[x].cTo4 ){
1890+                  zOut[nOut++] = tbl[x].cTo4;
1891+                }
1892+#endif /* SQLITE_SPELLFIX_5BYTE_MAPPINGS */
1893+              }
1894+            }
1895+          }
1896+          c = 0;
1897+          break;
1898+        }else if( tbl[x].cFrom>c ){
1899+          xTop = x-1;
1900+        }else{
1901+          xBtm = x+1;
1902+        }
1903+      }
1904+      if( c ) zOut[nOut++] = '?';
1905+    }
1906+  }
1907+  zOut[nOut] = 0;
1908+  return zOut;
1909+}
1910+
1911+/*
1912+** Return the number of characters in the shortest prefix of the input
1913+** string that transliterates to an ASCII string nTrans bytes or longer.
1914+** Or, if the transliteration of the input string is less than nTrans
1915+** bytes in size, return the number of characters in the input string.
1916+*/
1917+static int translen_to_charlen(const char *zIn, int nIn, int nTrans){
1918+  int i, c, sz, nOut;
1919+  int nChar;
1920+
1921+  i = nOut = 0;
1922+  for(nChar=0; i<nIn && nOut<nTrans; nChar++){
1923+    c = utf8Read((const unsigned char *)&zIn[i], nIn-i, &sz);
1924+    i += sz;
1925+
1926+    nOut++;
1927+    if( c>=128 ){
1928+      int xTop, xBtm, x;
1929+      const Transliteration *tbl = spellfixFindTranslit(c, &xTop);
1930+      xBtm = 0;
1931+      while( xTop>=xBtm ){
1932+        x = (xTop + xBtm)/2;
1933+        if( tbl[x].cFrom==c ){
1934+          if( tbl[x].cTo1 ){
1935+            nOut++;
1936+            if( tbl[x].cTo2 ){
1937+              nOut++;
1938+              if( tbl[x].cTo3 ){
1939+                nOut++;
1940+              }
1941+            }
1942+          }
1943+          break;
1944+        }else if( tbl[x].cFrom>c ){
1945+          xTop = x-1;
1946+        }else{
1947+          xBtm = x+1;
1948+        }
1949+      }
1950+    }
1951+  }
1952+
1953+  return nChar;
1954+}
1955+
1956+
1957+/*
1958+**    spellfix1_translit(X)
1959+**
1960+** Convert a string that contains non-ASCII Roman characters into 
1961+** pure ASCII.
1962+*/
1963+static void transliterateSqlFunc(
1964+  sqlite3_context *context,
1965+  int argc,
1966+  sqlite3_value **argv
1967+){
1968+  const unsigned char *zIn = sqlite3_value_text(argv[0]);
1969+  int nIn = sqlite3_value_bytes(argv[0]);
1970+  unsigned char *zOut = transliterate(zIn, nIn);
1971+  if( zOut==0 ){
1972+    sqlite3_result_error_nomem(context);
1973+  }else{
1974+    sqlite3_result_text(context, (char*)zOut, -1, sqlite3_free);
1975+  }
1976+}
1977+
1978+/*
1979+**    spellfix1_scriptcode(X)
1980+**
1981+** Try to determine the dominant script used by the word X and return
1982+** its ISO 15924 numeric code.
1983+**
1984+** The current implementation only understands the following scripts:
1985+**
1986+**    215  (Latin)
1987+**    220  (Cyrillic)
1988+**    200  (Greek)
1989+**
1990+** This routine will return 998 if the input X contains characters from
1991+** two or more of the above scripts or 999 if X contains no characters
1992+** from any of the above scripts.
1993+*/
1994+static void scriptCodeSqlFunc(
1995+  sqlite3_context *context,
1996+  int argc,
1997+  sqlite3_value **argv
1998+){
1999+  const unsigned char *zIn = sqlite3_value_text(argv[0]);
2000+  int nIn = sqlite3_value_bytes(argv[0]);
2001+  int c, sz;
2002+  int scriptMask = 0;
2003+  int res;
2004+  int seenDigit = 0;
2005+# define SCRIPT_LATIN       0x0001
2006+# define SCRIPT_CYRILLIC    0x0002
2007+# define SCRIPT_GREEK       0x0004
2008+# define SCRIPT_HEBREW      0x0008
2009+# define SCRIPT_ARABIC      0x0010
2010+
2011+  while( nIn>0 ){
2012+    c = utf8Read(zIn, nIn, &sz);
2013+    zIn += sz;
2014+    nIn -= sz;
2015+    if( c<0x02af ){
2016+      if( c>=0x80 || midClass[c&0x7f]<CCLASS_DIGIT ){
2017+        scriptMask |= SCRIPT_LATIN;
2018+      }else if( c>='0' && c<='9' ){
2019+        seenDigit = 1;
2020+      }
2021+    }else if( c>=0x0400 && c<=0x04ff ){
2022+      scriptMask |= SCRIPT_CYRILLIC;
2023+    }else if( c>=0x0386 && c<=0x03ce ){
2024+      scriptMask |= SCRIPT_GREEK;
2025+    }else if( c>=0x0590 && c<=0x05ff ){
2026+      scriptMask |= SCRIPT_HEBREW;
2027+    }else if( c>=0x0600 && c<=0x06ff ){
2028+      scriptMask |= SCRIPT_ARABIC;
2029+    }
2030+  }
2031+  if( scriptMask==0 && seenDigit ) scriptMask = SCRIPT_LATIN;
2032+  switch( scriptMask ){
2033+    case 0:                res = 999; break;
2034+    case SCRIPT_LATIN:     res = 215; break;
2035+    case SCRIPT_CYRILLIC:  res = 220; break;
2036+    case SCRIPT_GREEK:     res = 200; break;
2037+    case SCRIPT_HEBREW:    res = 125; break;
2038+    case SCRIPT_ARABIC:    res = 160; break;
2039+    default:               res = 998; break;
2040+  }
2041+  sqlite3_result_int(context, res);
2042+}
2043+
2044+/* End transliterate
2045+******************************************************************************
2046+******************************************************************************
2047+** Begin spellfix1 virtual table.
2048+*/
2049+
2050+/* Maximum length of a phonehash used for querying the shadow table */
2051+#define SPELLFIX_MX_HASH  32
2052+
2053+/* Maximum number of hash strings to examine per query */
2054+#define SPELLFIX_MX_RUN   1
2055+
2056+typedef struct spellfix1_vtab spellfix1_vtab;
2057+typedef struct spellfix1_cursor spellfix1_cursor;
2058+
2059+/* Fuzzy-search virtual table object */
2060+struct spellfix1_vtab {
2061+  sqlite3_vtab base;         /* Base class - must be first */
2062+  sqlite3 *db;               /* Database connection */
2063+  char *zDbName;             /* Name of database holding this table */
2064+  char *zTableName;          /* Name of the virtual table */
2065+  char *zCostTable;          /* Table holding edit-distance cost numbers */
2066+  EditDist3Config *pConfig3; /* Parsed edit distance costs */
2067+};
2068+
2069+/* Fuzzy-search cursor object */
2070+struct spellfix1_cursor {
2071+  sqlite3_vtab_cursor base;    /* Base class - must be first */
2072+  spellfix1_vtab *pVTab;       /* The table to which this cursor belongs */
2073+  char *zPattern;              /* rhs of MATCH clause */
2074+  int idxNum;                  /* idxNum value passed to xFilter() */
2075+  int nRow;                    /* Number of rows of content */
2076+  int nAlloc;                  /* Number of allocated rows */
2077+  int iRow;                    /* Current row of content */
2078+  int iLang;                   /* Value of the langid= constraint */
2079+  int iTop;                    /* Value of the top= constraint */
2080+  int iScope;                  /* Value of the scope= constraint */
2081+  int nSearch;                 /* Number of vocabulary items checked */
2082+  sqlite3_stmt *pFullScan;     /* Shadow query for a full table scan */
2083+  struct spellfix1_row {       /* For each row of content */
2084+    sqlite3_int64 iRowid;         /* Rowid for this row */
2085+    char *zWord;                  /* Text for this row */
2086+    int iRank;                    /* Rank for this row */
2087+    int iDistance;                /* Distance from pattern for this row */
2088+    int iScore;                   /* Score for sorting */
2089+    int iMatchlen;                /* Value of matchlen column (or -1) */
2090+    char zHash[SPELLFIX_MX_HASH]; /* the phonehash used for this match */
2091+  } *a; 
2092+};
2093+
2094+/*
2095+** Construct one or more SQL statements from the format string given
2096+** and then evaluate those statements. The success code is written
2097+** into *pRc.
2098+**
2099+** If *pRc is initially non-zero then this routine is a no-op.
2100+*/
2101+static void spellfix1DbExec(
2102+  int *pRc,              /* Success code */
2103+  sqlite3 *db,           /* Database in which to run SQL */
2104+  const char *zFormat,   /* Format string for SQL */
2105+  ...                    /* Arguments to the format string */
2106+){
2107+  va_list ap;
2108+  char *zSql;
2109+  if( *pRc ) return;
2110+  va_start(ap, zFormat);
2111+  zSql = sqlite3_vmprintf(zFormat, ap);
2112+  va_end(ap);
2113+  if( zSql==0 ){
2114+    *pRc = SQLITE_NOMEM;
2115+  }else{
2116+    *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
2117+    sqlite3_free(zSql);
2118+  }
2119+}
2120+
2121+/*
2122+** xDisconnect/xDestroy method for the fuzzy-search module.
2123+*/
2124+static int spellfix1Uninit(int isDestroy, sqlite3_vtab *pVTab){
2125+  spellfix1_vtab *p = (spellfix1_vtab*)pVTab;
2126+  int rc = SQLITE_OK;
2127+  if( isDestroy ){
2128+    sqlite3 *db = p->db;
2129+    spellfix1DbExec(&rc, db, "DROP TABLE IF EXISTS \"%w\".\"%w_vocab\"",
2130+                  p->zDbName, p->zTableName);
2131+  }
2132+  if( rc==SQLITE_OK ){
2133+    sqlite3_free(p->zTableName);
2134+    editDist3ConfigDelete(p->pConfig3);
2135+    sqlite3_free(p->zCostTable);
2136+    sqlite3_free(p);
2137+  }
2138+  return rc;
2139+}
2140+static int spellfix1Disconnect(sqlite3_vtab *pVTab){
2141+  return spellfix1Uninit(0, pVTab);
2142+}
2143+static int spellfix1Destroy(sqlite3_vtab *pVTab){
2144+  return spellfix1Uninit(1, pVTab);
2145+}
2146+
2147+/*
2148+** Make a copy of a string.  Remove leading and trailing whitespace
2149+** and dequote it.
2150+*/
2151+static char *spellfix1Dequote(const char *zIn){
2152+  char *zOut;
2153+  int i, j;
2154+  char c;
2155+  while( isspace((unsigned char)zIn[0]) ) zIn++;
2156+  zOut = sqlite3_mprintf("%s", zIn);
2157+  if( zOut==0 ) return 0;
2158+  i = (int)strlen(zOut);
2159+#if 0  /* The parser will never leave spaces at the end */
2160+  while( i>0 && isspace(zOut[i-1]) ){ i--; }
2161+#endif
2162+  zOut[i] = 0;
2163+  c = zOut[0];
2164+  if( c=='\'' || c=='"' ){
2165+    for(i=1, j=0; ALWAYS(zOut[i]); i++){
2166+      zOut[j++] = zOut[i];
2167+      if( zOut[i]==c ){
2168+        if( zOut[i+1]==c ){
2169+          i++;
2170+        }else{
2171+          zOut[j-1] = 0;
2172+          break;
2173+        }
2174+      }
2175+    }
2176+  }
2177+  return zOut;
2178+}
2179+
2180+
2181+/*
2182+** xConnect/xCreate method for the spellfix1 module. Arguments are:
2183+**
2184+**   argv[0]   -> module name  ("spellfix1")
2185+**   argv[1]   -> database name
2186+**   argv[2]   -> table name
2187+**   argv[3].. -> optional arguments (i.e. "edit_cost_table" parameter)
2188+*/
2189+static int spellfix1Init(
2190+  int isCreate,
2191+  sqlite3 *db,
2192+  void *pAux,
2193+  int argc, const char *const*argv,
2194+  sqlite3_vtab **ppVTab,
2195+  char **pzErr
2196+){
2197+  spellfix1_vtab *pNew = 0;
2198+  /* const char *zModule = argv[0]; // not used */
2199+  const char *zDbName = argv[1];
2200+  const char *zTableName = argv[2];
2201+  int nDbName;
2202+  int rc = SQLITE_OK;
2203+  int i;
2204+
2205+  nDbName = (int)strlen(zDbName);
2206+  pNew = sqlite3_malloc64( sizeof(*pNew) + nDbName + 1);
2207+  if( pNew==0 ){
2208+    rc = SQLITE_NOMEM;
2209+  }else{
2210+    memset(pNew, 0, sizeof(*pNew));
2211+    pNew->zDbName = (char*)&pNew[1];
2212+    memcpy(pNew->zDbName, zDbName, nDbName+1);
2213+    pNew->zTableName = sqlite3_mprintf("%s", zTableName);
2214+    pNew->db = db;
2215+    if( pNew->zTableName==0 ){
2216+      rc = SQLITE_NOMEM;
2217+    }else{
2218+      sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
2219+      rc = sqlite3_declare_vtab(db, 
2220+           "CREATE TABLE x(word,rank,distance,langid, "
2221+           "score, matchlen, phonehash HIDDEN, "
2222+           "top HIDDEN, scope HIDDEN, srchcnt HIDDEN, "
2223+           "soundslike HIDDEN, command HIDDEN)"
2224+      );
2225+#define SPELLFIX_COL_WORD            0
2226+#define SPELLFIX_COL_RANK            1
2227+#define SPELLFIX_COL_DISTANCE        2
2228+#define SPELLFIX_COL_LANGID          3
2229+#define SPELLFIX_COL_SCORE           4
2230+#define SPELLFIX_COL_MATCHLEN        5
2231+#define SPELLFIX_COL_PHONEHASH       6
2232+#define SPELLFIX_COL_TOP             7
2233+#define SPELLFIX_COL_SCOPE           8
2234+#define SPELLFIX_COL_SRCHCNT         9
2235+#define SPELLFIX_COL_SOUNDSLIKE     10
2236+#define SPELLFIX_COL_COMMAND        11
2237+    }
2238+    if( rc==SQLITE_OK && isCreate ){
2239+      spellfix1DbExec(&rc, db,
2240+         "CREATE TABLE IF NOT EXISTS \"%w\".\"%w_vocab\"(\n"
2241+         "  id INTEGER PRIMARY KEY,\n"
2242+         "  rank INT,\n"
2243+         "  langid INT,\n"
2244+         "  word TEXT,\n"
2245+         "  k1 TEXT,\n"
2246+         "  k2 TEXT\n"
2247+         ");\n",
2248+         zDbName, zTableName
2249+      );
2250+      spellfix1DbExec(&rc, db,
2251+         "CREATE INDEX IF NOT EXISTS \"%w\".\"%w_vocab_index_langid_k2\" "
2252+            "ON \"%w_vocab\"(langid,k2);",
2253+         zDbName, zTableName, zTableName
2254+      );
2255+    }
2256+    for(i=3; rc==SQLITE_OK && i<argc; i++){
2257+      if( strncmp(argv[i],"edit_cost_table=",16)==0 && pNew->zCostTable==0 ){
2258+        pNew->zCostTable = spellfix1Dequote(&argv[i][16]);
2259+        if( pNew->zCostTable==0 ) rc = SQLITE_NOMEM;
2260+        continue;
2261+      }
2262+      *pzErr = sqlite3_mprintf("bad argument to spellfix1(): \"%s\"", argv[i]);
2263+      rc = SQLITE_ERROR; 
2264+    }
2265+  }
2266+
2267+  if( rc && pNew ){
2268+    *ppVTab = 0;
2269+    spellfix1Uninit(0, &pNew->base);
2270+  }else{
2271+    *ppVTab = (sqlite3_vtab *)pNew;
2272+  }
2273+  return rc;
2274+}
2275+
2276+/*
2277+** The xConnect and xCreate methods
2278+*/
2279+static int spellfix1Connect(
2280+  sqlite3 *db,
2281+  void *pAux,
2282+  int argc, const char *const*argv,
2283+  sqlite3_vtab **ppVTab,
2284+  char **pzErr
2285+){
2286+  return spellfix1Init(0, db, pAux, argc, argv, ppVTab, pzErr);
2287+}
2288+static int spellfix1Create(
2289+  sqlite3 *db,
2290+  void *pAux,
2291+  int argc, const char *const*argv,
2292+  sqlite3_vtab **ppVTab,
2293+  char **pzErr
2294+){
2295+  return spellfix1Init(1, db, pAux, argc, argv, ppVTab, pzErr);
2296+}
2297+
2298+/*
2299+** Clear all of the content from a cursor.
2300+*/
2301+static void spellfix1ResetCursor(spellfix1_cursor *pCur){
2302+  int i;
2303+  for(i=0; i<pCur->nRow; i++){
2304+    sqlite3_free(pCur->a[i].zWord);
2305+  }
2306+  pCur->nRow = 0;
2307+  pCur->iRow = 0;
2308+  pCur->nSearch = 0;
2309+  if( pCur->pFullScan ){
2310+    sqlite3_finalize(pCur->pFullScan);
2311+    pCur->pFullScan = 0;
2312+  }
2313+}
2314+
2315+/*
2316+** Resize the cursor to hold up to N rows of content
2317+*/
2318+static void spellfix1ResizeCursor(spellfix1_cursor *pCur, int N){
2319+  struct spellfix1_row *aNew;
2320+  assert( N>=pCur->nRow );
2321+  aNew = sqlite3_realloc64(pCur->a, sizeof(pCur->a[0])*N);
2322+  if( aNew==0 && N>0 ){
2323+    spellfix1ResetCursor(pCur);
2324+    sqlite3_free(pCur->a);
2325+    pCur->nAlloc = 0;
2326+    pCur->a = 0;
2327+  }else{
2328+    pCur->nAlloc = N;
2329+    pCur->a = aNew;
2330+  }
2331+}
2332+
2333+
2334+/*
2335+** Close a fuzzy-search cursor.
2336+*/
2337+static int spellfix1Close(sqlite3_vtab_cursor *cur){
2338+  spellfix1_cursor *pCur = (spellfix1_cursor *)cur;
2339+  spellfix1ResetCursor(pCur);
2340+  spellfix1ResizeCursor(pCur, 0);
2341+  sqlite3_free(pCur->zPattern);
2342+  sqlite3_free(pCur);
2343+  return SQLITE_OK;
2344+}
2345+
2346+#define SPELLFIX_IDXNUM_MATCH  0x01         /* word MATCH $str */
2347+#define SPELLFIX_IDXNUM_LANGID 0x02         /* langid == $langid */
2348+#define SPELLFIX_IDXNUM_TOP    0x04         /* top = $top */
2349+#define SPELLFIX_IDXNUM_SCOPE  0x08         /* scope = $scope */
2350+#define SPELLFIX_IDXNUM_DISTLT 0x10         /* distance < $distance */
2351+#define SPELLFIX_IDXNUM_DISTLE 0x20         /* distance <= $distance */
2352+#define SPELLFIX_IDXNUM_ROWID  0x40         /* rowid = $rowid */
2353+#define SPELLFIX_IDXNUM_DIST   (0x10|0x20)  /* DISTLT and DISTLE */
2354+
2355+/*
2356+**
2357+** The plan number is a bitmask of the SPELLFIX_IDXNUM_* values defined
2358+** above.
2359+**
2360+** filter.argv[*] values contains $str, $langid, $top, $scope and $rowid
2361+** if specified and in that order.
2362+*/
2363+static int spellfix1BestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
2364+  int iPlan = 0;
2365+  int iLangTerm = -1;
2366+  int iTopTerm = -1;
2367+  int iScopeTerm = -1;
2368+  int iDistTerm = -1;
2369+  int iRowidTerm = -1;
2370+  int i;
2371+  const struct sqlite3_index_constraint *pConstraint;
2372+  pConstraint = pIdxInfo->aConstraint;
2373+  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2374+    if( pConstraint->usable==0 ) continue;
2375+
2376+    /* Terms of the form:  word MATCH $str */
2377+    if( (iPlan & SPELLFIX_IDXNUM_MATCH)==0 
2378+     && pConstraint->iColumn==SPELLFIX_COL_WORD
2379+     && pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH
2380+    ){
2381+      iPlan |= SPELLFIX_IDXNUM_MATCH;
2382+      pIdxInfo->aConstraintUsage[i].argvIndex = 1;
2383+      pIdxInfo->aConstraintUsage[i].omit = 1;
2384+    }
2385+
2386+    /* Terms of the form:  langid = $langid  */
2387+    if( (iPlan & SPELLFIX_IDXNUM_LANGID)==0
2388+     && pConstraint->iColumn==SPELLFIX_COL_LANGID
2389+     && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
2390+    ){
2391+      iPlan |= SPELLFIX_IDXNUM_LANGID;
2392+      iLangTerm = i;
2393+    }
2394+
2395+    /* Terms of the form:  top = $top */
2396+    if( (iPlan & SPELLFIX_IDXNUM_TOP)==0
2397+     && pConstraint->iColumn==SPELLFIX_COL_TOP
2398+     && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
2399+    ){
2400+      iPlan |= SPELLFIX_IDXNUM_TOP;
2401+      iTopTerm = i;
2402+    }
2403+
2404+    /* Terms of the form:  scope = $scope */
2405+    if( (iPlan & SPELLFIX_IDXNUM_SCOPE)==0
2406+     && pConstraint->iColumn==SPELLFIX_COL_SCOPE
2407+     && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
2408+    ){
2409+      iPlan |= SPELLFIX_IDXNUM_SCOPE;
2410+      iScopeTerm = i;
2411+    }
2412+
2413+    /* Terms of the form:  distance < $dist or distance <= $dist */
2414+    if( (iPlan & SPELLFIX_IDXNUM_DIST)==0
2415+     && pConstraint->iColumn==SPELLFIX_COL_DISTANCE
2416+     && (pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT
2417+          || pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE)
2418+    ){
2419+      if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT ){
2420+        iPlan |= SPELLFIX_IDXNUM_DISTLT;
2421+      }else{
2422+        iPlan |= SPELLFIX_IDXNUM_DISTLE;
2423+      }
2424+      iDistTerm = i;
2425+    }
2426+
2427+    /* Terms of the form:  distance < $dist or distance <= $dist */
2428+    if( (iPlan & SPELLFIX_IDXNUM_ROWID)==0
2429+     && pConstraint->iColumn<0
2430+     && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
2431+    ){
2432+      iPlan |= SPELLFIX_IDXNUM_ROWID;
2433+      iRowidTerm = i;
2434+    }
2435+  }
2436+  if( iPlan&SPELLFIX_IDXNUM_MATCH ){
2437+    int idx = 2;
2438+    pIdxInfo->idxNum = iPlan;
2439+    if( pIdxInfo->nOrderBy==1
2440+     && pIdxInfo->aOrderBy[0].iColumn==SPELLFIX_COL_SCORE
2441+     && pIdxInfo->aOrderBy[0].desc==0
2442+    ){
2443+      pIdxInfo->orderByConsumed = 1;  /* Default order by iScore */
2444+    }
2445+    if( iPlan&SPELLFIX_IDXNUM_LANGID ){
2446+      pIdxInfo->aConstraintUsage[iLangTerm].argvIndex = idx++;
2447+      pIdxInfo->aConstraintUsage[iLangTerm].omit = 1;
2448+    }
2449+    if( iPlan&SPELLFIX_IDXNUM_TOP ){
2450+      pIdxInfo->aConstraintUsage[iTopTerm].argvIndex = idx++;
2451+      pIdxInfo->aConstraintUsage[iTopTerm].omit = 1;
2452+    }
2453+    if( iPlan&SPELLFIX_IDXNUM_SCOPE ){
2454+      pIdxInfo->aConstraintUsage[iScopeTerm].argvIndex = idx++;
2455+      pIdxInfo->aConstraintUsage[iScopeTerm].omit = 1;
2456+    }
2457+    if( iPlan&SPELLFIX_IDXNUM_DIST ){
2458+      pIdxInfo->aConstraintUsage[iDistTerm].argvIndex = idx++;
2459+      pIdxInfo->aConstraintUsage[iDistTerm].omit = 1;
2460+    }
2461+    pIdxInfo->estimatedCost = 1e5;
2462+  }else if( (iPlan & SPELLFIX_IDXNUM_ROWID) ){
2463+    pIdxInfo->idxNum = SPELLFIX_IDXNUM_ROWID;
2464+    pIdxInfo->aConstraintUsage[iRowidTerm].argvIndex = 1;
2465+    pIdxInfo->aConstraintUsage[iRowidTerm].omit = 1;
2466+    pIdxInfo->estimatedCost = 5;
2467+  }else{
2468+    pIdxInfo->idxNum = 0;
2469+    pIdxInfo->estimatedCost = 1e50;
2470+  }
2471+  return SQLITE_OK;
2472+}
2473+
2474+/*
2475+** Open a new fuzzy-search cursor.
2476+*/
2477+static int spellfix1Open(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
2478+  spellfix1_vtab *p = (spellfix1_vtab*)pVTab;
2479+  spellfix1_cursor *pCur;
2480+  pCur = sqlite3_malloc64( sizeof(*pCur) );
2481+  if( pCur==0 ) return SQLITE_NOMEM;
2482+  memset(pCur, 0, sizeof(*pCur));
2483+  pCur->pVTab = p;
2484+  *ppCursor = &pCur->base;
2485+  return SQLITE_OK;
2486+}
2487+
2488+/*
2489+** Adjust a distance measurement by the words rank in order to show
2490+** preference to common words.
2491+*/
2492+static int spellfix1Score(int iDistance, int iRank){
2493+  int iLog2;
2494+  for(iLog2=0; iRank>0; iLog2++, iRank>>=1){}
2495+  return iDistance + 32 - iLog2;
2496+}
2497+
2498+/*
2499+** Compare two spellfix1_row objects for sorting purposes in qsort() such
2500+** that they sort in order of increasing distance.
2501+*/
2502+static int SQLITE_CDECL spellfix1RowCompare(const void *A, const void *B){
2503+  const struct spellfix1_row *a = (const struct spellfix1_row*)A;
2504+  const struct spellfix1_row *b = (const struct spellfix1_row*)B;
2505+  return a->iScore - b->iScore;
2506+}
2507+
2508+/*
2509+** A structure used to pass information from spellfix1FilterForMatch()
2510+** into spellfix1RunQuery().
2511+*/
2512+typedef struct MatchQuery {
2513+  spellfix1_cursor *pCur;          /* The cursor being queried */
2514+  sqlite3_stmt *pStmt;             /* shadow table query statment */
2515+  char zHash[SPELLFIX_MX_HASH];    /* The current phonehash for zPattern */
2516+  const char *zPattern;            /* Transliterated input string */
2517+  int nPattern;                    /* Length of zPattern */
2518+  EditDist3FromString *pMatchStr3; /* Original unicode string */
2519+  EditDist3Config *pConfig3;       /* Edit-distance cost coefficients */
2520+  const EditDist3Lang *pLang;      /* The selected language coefficients */
2521+  int iLang;                       /* The language id */
2522+  int iScope;                      /* Default scope */
2523+  int iMaxDist;                    /* Maximum allowed edit distance, or -1 */
2524+  int rc;                          /* Error code */
2525+  int nRun;                  /* Number of prior runs for the same zPattern */
2526+  char azPrior[SPELLFIX_MX_RUN][SPELLFIX_MX_HASH];  /* Prior hashes */
2527+} MatchQuery;
2528+
2529+/*
2530+** Run a query looking for the best matches against zPattern using
2531+** zHash as the character class seed hash.
2532+*/
2533+static void spellfix1RunQuery(MatchQuery *p, const char *zQuery, int nQuery){
2534+  const char *zK1;
2535+  const char *zWord;
2536+  int iDist;
2537+  int iRank;
2538+  int iScore;
2539+  int iWorst = 0;
2540+  int idx;
2541+  int idxWorst = -1;
2542+  int i;
2543+  int iScope = p->iScope;
2544+  spellfix1_cursor *pCur = p->pCur;
2545+  sqlite3_stmt *pStmt = p->pStmt;
2546+  char zHash1[SPELLFIX_MX_HASH];
2547+  char zHash2[SPELLFIX_MX_HASH];
2548+  char *zClass;
2549+  int nClass;
2550+  int rc;
2551+
2552+  if( pCur->a==0 || p->rc ) return;   /* Prior memory allocation failure */
2553+  zClass = (char*)phoneticHash((unsigned char*)zQuery, nQuery);
2554+  if( zClass==0 ){
2555+    p->rc = SQLITE_NOMEM;
2556+    return;
2557+  }
2558+  nClass = (int)strlen(zClass);
2559+  if( nClass>SPELLFIX_MX_HASH-2 ){
2560+    nClass = SPELLFIX_MX_HASH-2;
2561+    zClass[nClass] = 0;
2562+  }
2563+  if( nClass<=iScope ){
2564+    if( nClass>2 ){
2565+      iScope = nClass-1;
2566+    }else{
2567+      iScope = nClass;
2568+    }
2569+  }
2570+  memcpy(zHash1, zClass, iScope);
2571+  sqlite3_free(zClass);
2572+  zHash1[iScope] = 0;
2573+  memcpy(zHash2, zHash1, iScope);
2574+  zHash2[iScope] = 'Z';
2575+  zHash2[iScope+1] = 0;
2576+#if SPELLFIX_MX_RUN>1
2577+  for(i=0; i<p->nRun; i++){
2578+    if( strcmp(p->azPrior[i], zHash1)==0 ) return;
2579+  }
2580+#endif
2581+  assert( p->nRun<SPELLFIX_MX_RUN );
2582+  memcpy(p->azPrior[p->nRun++], zHash1, iScope+1);
2583+  if( sqlite3_bind_text(pStmt, 1, zHash1, -1, SQLITE_STATIC)==SQLITE_NOMEM
2584+   || sqlite3_bind_text(pStmt, 2, zHash2, -1, SQLITE_STATIC)==SQLITE_NOMEM
2585+  ){
2586+    p->rc = SQLITE_NOMEM;
2587+    return;
2588+  }
2589+#if SPELLFIX_MX_RUN>1
2590+  for(i=0; i<pCur->nRow; i++){
2591+    if( pCur->a[i].iScore>iWorst ){
2592+      iWorst = pCur->a[i].iScore;
2593+      idxWorst = i;
2594+    }
2595+  }
2596+#endif
2597+  while( sqlite3_step(pStmt)==SQLITE_ROW ){
2598+    int iMatchlen = -1;
2599+    iRank = sqlite3_column_int(pStmt, 2);
2600+    if( p->pMatchStr3 ){
2601+      int nWord = sqlite3_column_bytes(pStmt, 1);
2602+      zWord = (const char*)sqlite3_column_text(pStmt, 1);
2603+      iDist = editDist3Core(p->pMatchStr3, zWord, nWord, p->pLang, &iMatchlen);
2604+    }else{
2605+      zK1 = (const char*)sqlite3_column_text(pStmt, 3);
2606+      if( zK1==0 ) continue;
2607+      iDist = editdist1(p->zPattern, zK1, 0);
2608+    }
2609+    if( iDist<0 ){
2610+      p->rc = SQLITE_NOMEM;
2611+      break;
2612+    }
2613+    pCur->nSearch++;
2614+    
2615+    /* If there is a "distance < $dist" or "distance <= $dist" constraint,
2616+    ** check if this row meets it. If not, jump back up to the top of the
2617+    ** loop to process the next row. Otherwise, if the row does match the
2618+    ** distance constraint, check if the pCur->a[] array is already full.
2619+    ** If it is and no explicit "top = ?" constraint was present in the
2620+    ** query, grow the array to ensure there is room for the new entry. */
2621+    assert( (p->iMaxDist>=0)==((pCur->idxNum & SPELLFIX_IDXNUM_DIST) ? 1 : 0) );
2622+    if( p->iMaxDist>=0 ){
2623+      if( iDist>p->iMaxDist ) continue;
2624+      if( pCur->nRow>=pCur->nAlloc && (pCur->idxNum & SPELLFIX_IDXNUM_TOP)==0 ){
2625+        spellfix1ResizeCursor(pCur, pCur->nAlloc*2 + 10);
2626+        if( pCur->a==0 ) break;
2627+      }
2628+    }
2629+
2630+    iScore = spellfix1Score(iDist,iRank);
2631+    if( pCur->nRow<pCur->nAlloc ){
2632+      idx = pCur->nRow;
2633+    }else if( iScore<iWorst ){
2634+      idx = idxWorst;
2635+      sqlite3_free(pCur->a[idx].zWord);
2636+    }else{
2637+      continue;
2638+    }
2639+
2640+    pCur->a[idx].zWord = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
2641+    if( pCur->a[idx].zWord==0 ){
2642+      p->rc = SQLITE_NOMEM;
2643+      break;
2644+    }
2645+    pCur->a[idx].iRowid = sqlite3_column_int64(pStmt, 0);
2646+    pCur->a[idx].iRank = iRank;
2647+    pCur->a[idx].iDistance = iDist;
2648+    pCur->a[idx].iScore = iScore;
2649+    pCur->a[idx].iMatchlen = iMatchlen;
2650+    memcpy(pCur->a[idx].zHash, zHash1, iScope+1);
2651+    if( pCur->nRow<pCur->nAlloc ) pCur->nRow++;
2652+    if( pCur->nRow==pCur->nAlloc ){
2653+      iWorst = pCur->a[0].iScore;
2654+      idxWorst = 0;
2655+      for(i=1; i<pCur->nRow; i++){
2656+        iScore = pCur->a[i].iScore;
2657+        if( iWorst<iScore ){
2658+          iWorst = iScore;
2659+          idxWorst = i;
2660+        }
2661+      }
2662+    }
2663+  }
2664+  rc = sqlite3_reset(pStmt);
2665+  if( rc ) p->rc = rc;
2666+}
2667+
2668+/*
2669+** This version of the xFilter method work if the MATCH term is present
2670+** and we are doing a scan.
2671+*/
2672+static int spellfix1FilterForMatch(
2673+  spellfix1_cursor *pCur,
2674+  int argc,
2675+  sqlite3_value **argv
2676+){
2677+  int idxNum = pCur->idxNum;
2678+  const unsigned char *zMatchThis;   /* RHS of the MATCH operator */
2679+  EditDist3FromString *pMatchStr3 = 0; /* zMatchThis as an editdist string */
2680+  char *zPattern;                    /* Transliteration of zMatchThis */
2681+  int nPattern;                      /* Length of zPattern */
2682+  int iLimit = 20;                   /* Max number of rows of output */
2683+  int iScope = 3;                    /* Use this many characters of zClass */
2684+  int iLang = 0;                     /* Language code */
2685+  char *zSql;                        /* SQL of shadow table query */
2686+  sqlite3_stmt *pStmt = 0;           /* Shadow table query */
2687+  int rc;                            /* Result code */
2688+  int idx = 1;                       /* Next available filter parameter */
2689+  spellfix1_vtab *p = pCur->pVTab;   /* The virtual table that owns pCur */
2690+  MatchQuery x;                      /* For passing info to RunQuery() */
2691+
2692+  /* Load the cost table if we have not already done so */
2693+  if( p->zCostTable!=0 && p->pConfig3==0 ){
2694+    p->pConfig3 = sqlite3_malloc64( sizeof(p->pConfig3[0]) );
2695+    if( p->pConfig3==0 ) return SQLITE_NOMEM;
2696+    memset(p->pConfig3, 0, sizeof(p->pConfig3[0]));
2697+    rc = editDist3ConfigLoad(p->pConfig3, p->db, p->zCostTable);
2698+    if( rc ) return rc;
2699+  }
2700+  memset(&x, 0, sizeof(x));
2701+  x.iScope = 3;  /* Default scope if none specified by "WHERE scope=N" */
2702+  x.iMaxDist = -1;   /* Maximum allowed edit distance */
2703+
2704+  if( idxNum&2 ){
2705+    iLang = sqlite3_value_int(argv[idx++]);
2706+  }
2707+  if( idxNum&4 ){
2708+    iLimit = sqlite3_value_int(argv[idx++]);
2709+    if( iLimit<1 ) iLimit = 1;
2710+  }
2711+  if( idxNum&8 ){
2712+    x.iScope = sqlite3_value_int(argv[idx++]);
2713+    if( x.iScope<1 ) x.iScope = 1;
2714+    if( x.iScope>SPELLFIX_MX_HASH-2 ) x.iScope = SPELLFIX_MX_HASH-2;
2715+  }
2716+  if( idxNum&(16|32) ){
2717+    x.iMaxDist = sqlite3_value_int(argv[idx++]);
2718+    if( idxNum&16 ) x.iMaxDist--;
2719+    if( x.iMaxDist<0 ) x.iMaxDist = 0;
2720+  }
2721+  spellfix1ResetCursor(pCur);
2722+  spellfix1ResizeCursor(pCur, iLimit);
2723+  zMatchThis = sqlite3_value_text(argv[0]);
2724+  if( zMatchThis==0 ) return SQLITE_OK;
2725+  if( p->pConfig3 ){
2726+    x.pLang = editDist3FindLang(p->pConfig3, iLang);
2727+    pMatchStr3 = editDist3FromStringNew(x.pLang, (const char*)zMatchThis, -1);
2728+    if( pMatchStr3==0 ){
2729+      x.rc = SQLITE_NOMEM;
2730+      goto filter_exit;
2731+    }
2732+  }else{
2733+    x.pLang = 0;
2734+  }
2735+  zPattern = (char*)transliterate(zMatchThis, sqlite3_value_bytes(argv[0]));
2736+  sqlite3_free(pCur->zPattern);
2737+  pCur->zPattern = zPattern;
2738+  if( zPattern==0 ){
2739+    x.rc = SQLITE_NOMEM;
2740+    goto filter_exit;
2741+  }
2742+  nPattern = (int)strlen(zPattern);
2743+  if( zPattern[nPattern-1]=='*' ) nPattern--;
2744+  zSql = sqlite3_mprintf(
2745+     "SELECT id, word, rank, coalesce(k1,word)"
2746+     "  FROM \"%w\".\"%w_vocab\""
2747+     " WHERE langid=%d AND k2>=?1 AND k2<?2",
2748+     p->zDbName, p->zTableName, iLang
2749+  );
2750+  if( zSql==0 ){
2751+    x.rc = SQLITE_NOMEM;
2752+    pStmt = 0;
2753+    goto filter_exit;
2754+  }
2755+  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2756+  sqlite3_free(zSql);
2757+  pCur->iLang = iLang;
2758+  x.pCur = pCur;
2759+  x.pStmt = pStmt;
2760+  x.zPattern = zPattern;
2761+  x.nPattern = nPattern;
2762+  x.pMatchStr3 = pMatchStr3;
2763+  x.iLang = iLang;
2764+  x.rc = rc;
2765+  x.pConfig3 = p->pConfig3;
2766+  if( x.rc==SQLITE_OK ){
2767+    spellfix1RunQuery(&x, zPattern, nPattern);
2768+  }
2769+
2770+  if( pCur->a ){
2771+    qsort(pCur->a, pCur->nRow, sizeof(pCur->a[0]), spellfix1RowCompare);
2772+    pCur->iTop = iLimit;
2773+    pCur->iScope = iScope;
2774+  }else{
2775+    x.rc = SQLITE_NOMEM;
2776+  }
2777+
2778+filter_exit:
2779+  sqlite3_finalize(pStmt);
2780+  editDist3FromStringDelete(pMatchStr3);
2781+  return x.rc;
2782+}
2783+
2784+/*
2785+** This version of xFilter handles a full-table scan case
2786+*/
2787+static int spellfix1FilterForFullScan(
2788+  spellfix1_cursor *pCur,
2789+  int argc,
2790+  sqlite3_value **argv
2791+){
2792+  int rc = SQLITE_OK;
2793+  int idxNum = pCur->idxNum;
2794+  char *zSql;
2795+  spellfix1_vtab *pVTab = pCur->pVTab;
2796+  spellfix1ResetCursor(pCur);
2797+  assert( idxNum==0 || idxNum==64 );
2798+  zSql = sqlite3_mprintf(
2799+     "SELECT word, rank, NULL, langid, id FROM \"%w\".\"%w_vocab\"%s",
2800+     pVTab->zDbName, pVTab->zTableName,
2801+     ((idxNum & 64) ? " WHERE rowid=?" : "")
2802+  );
2803+  if( zSql==0 ) return SQLITE_NOMEM;
2804+  rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pFullScan, 0);
2805+  sqlite3_free(zSql);
2806+  if( rc==SQLITE_OK && (idxNum & 64) ){
2807+    assert( argc==1 );
2808+    rc = sqlite3_bind_value(pCur->pFullScan, 1, argv[0]);
2809+  }
2810+  pCur->nRow = pCur->iRow = 0;
2811+  if( rc==SQLITE_OK ){
2812+    rc = sqlite3_step(pCur->pFullScan);
2813+    if( rc==SQLITE_ROW ){ pCur->iRow = -1; rc = SQLITE_OK; }
2814+    if( rc==SQLITE_DONE ){ rc = SQLITE_OK; }
2815+  }else{
2816+    pCur->iRow = 0;
2817+  }
2818+  return rc;
2819+}
2820+
2821+
2822+/*
2823+** Called to "rewind" a cursor back to the beginning so that
2824+** it starts its output over again.  Always called at least once
2825+** prior to any spellfix1Column, spellfix1Rowid, or spellfix1Eof call.
2826+*/
2827+static int spellfix1Filter(
2828+  sqlite3_vtab_cursor *cur, 
2829+  int idxNum, const char *idxStr,
2830+  int argc, sqlite3_value **argv
2831+){
2832+  spellfix1_cursor *pCur = (spellfix1_cursor *)cur;
2833+  int rc;
2834+  pCur->idxNum = idxNum;
2835+  if( idxNum & 1 ){
2836+    rc = spellfix1FilterForMatch(pCur, argc, argv);
2837+  }else{
2838+    rc = spellfix1FilterForFullScan(pCur, argc, argv);
2839+  }
2840+  return rc;
2841+}
2842+
2843+
2844+/*
2845+** Advance a cursor to its next row of output
2846+*/
2847+static int spellfix1Next(sqlite3_vtab_cursor *cur){
2848+  spellfix1_cursor *pCur = (spellfix1_cursor *)cur;
2849+  int rc = SQLITE_OK;
2850+  if( pCur->iRow < pCur->nRow ){
2851+    if( pCur->pFullScan ){
2852+      rc = sqlite3_step(pCur->pFullScan);
2853+      if( rc!=SQLITE_ROW ) pCur->iRow = pCur->nRow;
2854+      if( rc==SQLITE_ROW || rc==SQLITE_DONE ) rc = SQLITE_OK;
2855+    }else{
2856+      pCur->iRow++;
2857+    }
2858+  }
2859+  return rc;
2860+}
2861+
2862+/*
2863+** Return TRUE if we are at the end-of-file
2864+*/
2865+static int spellfix1Eof(sqlite3_vtab_cursor *cur){
2866+  spellfix1_cursor *pCur = (spellfix1_cursor *)cur;
2867+  return pCur->iRow>=pCur->nRow;
2868+}
2869+
2870+/*
2871+** Return columns from the current row.
2872+*/
2873+static int spellfix1Column(
2874+  sqlite3_vtab_cursor *cur,
2875+  sqlite3_context *ctx,
2876+  int i
2877+){
2878+  spellfix1_cursor *pCur = (spellfix1_cursor*)cur;
2879+  if( pCur->pFullScan ){
2880+    if( i<=SPELLFIX_COL_LANGID ){
2881+      sqlite3_result_value(ctx, sqlite3_column_value(pCur->pFullScan, i));
2882+    }else{
2883+      sqlite3_result_null(ctx);
2884+    }
2885+    return SQLITE_OK;
2886+  }
2887+  switch( i ){
2888+    case SPELLFIX_COL_WORD: {
2889+      sqlite3_result_text(ctx, pCur->a[pCur->iRow].zWord, -1, SQLITE_STATIC);
2890+      break;
2891+    }
2892+    case SPELLFIX_COL_RANK: {
2893+      sqlite3_result_int(ctx, pCur->a[pCur->iRow].iRank);
2894+      break;
2895+    }
2896+    case SPELLFIX_COL_DISTANCE: {
2897+      sqlite3_result_int(ctx, pCur->a[pCur->iRow].iDistance);
2898+      break;
2899+    }
2900+    case SPELLFIX_COL_LANGID: {
2901+      sqlite3_result_int(ctx, pCur->iLang);
2902+      break;
2903+    }
2904+    case SPELLFIX_COL_SCORE: {
2905+      sqlite3_result_int(ctx, pCur->a[pCur->iRow].iScore);
2906+      break;
2907+    }
2908+    case SPELLFIX_COL_MATCHLEN: {
2909+      int iMatchlen = pCur->a[pCur->iRow].iMatchlen;
2910+      if( iMatchlen<0 ){
2911+        int nPattern = (int)strlen(pCur->zPattern);
2912+        char *zWord = pCur->a[pCur->iRow].zWord;
2913+        int nWord = (int)strlen(zWord);
2914+
2915+        if( nPattern>0 && pCur->zPattern[nPattern-1]=='*' ){
2916+          char *zTranslit;
2917+          int res;
2918+          zTranslit = (char *)transliterate((unsigned char *)zWord, nWord);
2919+          if( !zTranslit ) return SQLITE_NOMEM;
2920+          res = editdist1(pCur->zPattern, zTranslit, &iMatchlen);
2921+          sqlite3_free(zTranslit);
2922+          if( res<0 ) return SQLITE_NOMEM;
2923+          iMatchlen = translen_to_charlen(zWord, nWord, iMatchlen);
2924+        }else{
2925+          iMatchlen = utf8Charlen(zWord, nWord);
2926+        }
2927+      }
2928+
2929+      sqlite3_result_int(ctx, iMatchlen);
2930+      break;
2931+    }
2932+    case SPELLFIX_COL_PHONEHASH: {
2933+      sqlite3_result_text(ctx, pCur->a[pCur->iRow].zHash, -1, SQLITE_STATIC);
2934+      break;
2935+    }
2936+    case SPELLFIX_COL_TOP: {
2937+      sqlite3_result_int(ctx, pCur->iTop);
2938+      break;
2939+    }
2940+    case SPELLFIX_COL_SCOPE: {
2941+      sqlite3_result_int(ctx, pCur->iScope);
2942+      break;
2943+    }
2944+    case SPELLFIX_COL_SRCHCNT: {
2945+      sqlite3_result_int(ctx, pCur->nSearch);
2946+      break;
2947+    }
2948+    default: {
2949+      sqlite3_result_null(ctx);
2950+      break;
2951+    }
2952+  }
2953+  return SQLITE_OK;
2954+}
2955+
2956+/*
2957+** The rowid.
2958+*/
2959+static int spellfix1Rowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2960+  spellfix1_cursor *pCur = (spellfix1_cursor*)cur;
2961+  if( pCur->pFullScan ){
2962+    *pRowid = sqlite3_column_int64(pCur->pFullScan, 4);
2963+  }else{
2964+    *pRowid = pCur->a[pCur->iRow].iRowid;
2965+  }
2966+  return SQLITE_OK;
2967+}
2968+
2969+/*
2970+** This function is called by the xUpdate() method. It returns a string
2971+** containing the conflict mode that xUpdate() should use for the current
2972+** operation. One of: "ROLLBACK", "IGNORE", "ABORT" or "REPLACE".
2973+*/
2974+static const char *spellfix1GetConflict(sqlite3 *db){
2975+  static const char *azConflict[] = {
2976+    /* Note: Instead of "FAIL" - "ABORT". */
2977+    "ROLLBACK", "IGNORE", "ABORT", "ABORT", "REPLACE"
2978+  };
2979+  int eConflict = sqlite3_vtab_on_conflict(db);
2980+
2981+  assert( eConflict==SQLITE_ROLLBACK || eConflict==SQLITE_IGNORE
2982+       || eConflict==SQLITE_FAIL || eConflict==SQLITE_ABORT
2983+       || eConflict==SQLITE_REPLACE
2984+  );
2985+  assert( SQLITE_ROLLBACK==1 );
2986+  assert( SQLITE_IGNORE==2 );
2987+  assert( SQLITE_FAIL==3 );
2988+  assert( SQLITE_ABORT==4 );
2989+  assert( SQLITE_REPLACE==5 );
2990+
2991+  return azConflict[eConflict-1];
2992+}
2993+
2994+/*
2995+** The xUpdate() method.
2996+*/
2997+static int spellfix1Update(
2998+  sqlite3_vtab *pVTab,
2999+  int argc,
3000+  sqlite3_value **argv,
3001+  sqlite_int64 *pRowid
3002+){
3003+  int rc = SQLITE_OK;
3004+  sqlite3_int64 rowid, newRowid;
3005+  spellfix1_vtab *p = (spellfix1_vtab*)pVTab;
3006+  sqlite3 *db = p->db;
3007+
3008+  if( argc==1 ){
3009+    /* A delete operation on the rowid given by argv[0] */
3010+    rowid = *pRowid = sqlite3_value_int64(argv[0]);
3011+    spellfix1DbExec(&rc, db, "DELETE FROM \"%w\".\"%w_vocab\" "
3012+                           " WHERE id=%lld",
3013+                  p->zDbName, p->zTableName, rowid);
3014+  }else{
3015+    const unsigned char *zWord = sqlite3_value_text(argv[SPELLFIX_COL_WORD+2]);
3016+    int nWord = sqlite3_value_bytes(argv[SPELLFIX_COL_WORD+2]);
3017+    int iLang = sqlite3_value_int(argv[SPELLFIX_COL_LANGID+2]);
3018+    int iRank = sqlite3_value_int(argv[SPELLFIX_COL_RANK+2]);
3019+    const unsigned char *zSoundslike =
3020+           sqlite3_value_text(argv[SPELLFIX_COL_SOUNDSLIKE+2]);
3021+    int nSoundslike = sqlite3_value_bytes(argv[SPELLFIX_COL_SOUNDSLIKE+2]);
3022+    char *zK1, *zK2;
3023+    int i;
3024+    char c;
3025+    const char *zConflict = spellfix1GetConflict(db);
3026+
3027+    if( zWord==0 ){
3028+      /* Inserts of the form:  INSERT INTO table(command) VALUES('xyzzy');
3029+      ** cause zWord to be NULL, so we look at the "command" column to see
3030+      ** what special actions to take */
3031+      const char *zCmd = 
3032+         (const char*)sqlite3_value_text(argv[SPELLFIX_COL_COMMAND+2]);
3033+      if( zCmd==0 ){
3034+        pVTab->zErrMsg = sqlite3_mprintf("NOT NULL constraint failed: %s.word",
3035+                                         p->zTableName);
3036+        return SQLITE_CONSTRAINT_NOTNULL;
3037+      }
3038+      if( strcmp(zCmd,"reset")==0 ){
3039+        /* Reset the  edit cost table (if there is one). */
3040+        editDist3ConfigDelete(p->pConfig3);
3041+        p->pConfig3 = 0;
3042+        return SQLITE_OK;
3043+      }
3044+      if( strncmp(zCmd,"edit_cost_table=",16)==0 ){
3045+        editDist3ConfigDelete(p->pConfig3);
3046+        p->pConfig3 = 0;
3047+        sqlite3_free(p->zCostTable);
3048+        p->zCostTable = spellfix1Dequote(zCmd+16);
3049+        if( p->zCostTable==0 ) return SQLITE_NOMEM;
3050+        if( p->zCostTable[0]==0 || sqlite3_stricmp(p->zCostTable,"null")==0 ){
3051+          sqlite3_free(p->zCostTable);
3052+          p->zCostTable = 0;
3053+        }
3054+        return SQLITE_OK;
3055+      }
3056+      pVTab->zErrMsg = sqlite3_mprintf("unknown value for %s.command: \"%w\"",
3057+                                       p->zTableName, zCmd);
3058+      return SQLITE_ERROR;
3059+    }
3060+    if( iRank<1 ) iRank = 1;
3061+    if( zSoundslike ){
3062+      zK1 = (char*)transliterate(zSoundslike, nSoundslike);
3063+    }else{
3064+      zK1 = (char*)transliterate(zWord, nWord);
3065+    }
3066+    if( zK1==0 ) return SQLITE_NOMEM;
3067+    for(i=0; (c = zK1[i])!=0; i++){
3068+       if( c>='A' && c<='Z' ) zK1[i] += 'a' - 'A';
3069+    }
3070+    zK2 = (char*)phoneticHash((const unsigned char*)zK1, i);
3071+    if( zK2==0 ){
3072+      sqlite3_free(zK1);
3073+      return SQLITE_NOMEM;
3074+    }
3075+    if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
3076+      if( sqlite3_value_type(argv[1])==SQLITE_NULL ){
3077+        spellfix1DbExec(&rc, db,
3078+               "INSERT INTO \"%w\".\"%w_vocab\"(rank,langid,word,k1,k2) "
3079+               "VALUES(%d,%d,%Q,nullif(%Q,%Q),%Q)",
3080+               p->zDbName, p->zTableName,
3081+               iRank, iLang, zWord, zK1, zWord, zK2
3082+        );
3083+      }else{
3084+        newRowid = sqlite3_value_int64(argv[1]);
3085+        spellfix1DbExec(&rc, db,
3086+            "INSERT OR %s INTO \"%w\".\"%w_vocab\"(id,rank,langid,word,k1,k2) "
3087+            "VALUES(%lld,%d,%d,%Q,nullif(%Q,%Q),%Q)",
3088+            zConflict, p->zDbName, p->zTableName,
3089+            newRowid, iRank, iLang, zWord, zK1, zWord, zK2
3090+        );
3091+      }
3092+      *pRowid = sqlite3_last_insert_rowid(db);
3093+    }else{
3094+      rowid = sqlite3_value_int64(argv[0]);
3095+      newRowid = *pRowid = sqlite3_value_int64(argv[1]);
3096+      spellfix1DbExec(&rc, db,
3097+             "UPDATE OR %s \"%w\".\"%w_vocab\" SET id=%lld, rank=%d, langid=%d,"
3098+             " word=%Q, k1=nullif(%Q,%Q), k2=%Q WHERE id=%lld",
3099+             zConflict, p->zDbName, p->zTableName, newRowid, iRank, iLang,
3100+             zWord, zK1, zWord, zK2, rowid
3101+      );
3102+    }
3103+    sqlite3_free(zK1);
3104+    sqlite3_free(zK2);
3105+  }
3106+  return rc;
3107+}
3108+
3109+/*
3110+** Rename the spellfix1 table.
3111+*/
3112+static int spellfix1Rename(sqlite3_vtab *pVTab, const char *zNew){
3113+  spellfix1_vtab *p = (spellfix1_vtab*)pVTab;
3114+  sqlite3 *db = p->db;
3115+  int rc = SQLITE_OK;
3116+  char *zNewName = sqlite3_mprintf("%s", zNew);
3117+  if( zNewName==0 ){
3118+    return SQLITE_NOMEM;
3119+  }
3120+  spellfix1DbExec(&rc, db, 
3121+     "ALTER TABLE \"%w\".\"%w_vocab\" RENAME TO \"%w_vocab\"",
3122+     p->zDbName, p->zTableName, zNewName
3123+  );
3124+  if( rc==SQLITE_OK ){
3125+    sqlite3_free(p->zTableName);
3126+    p->zTableName = zNewName;
3127+  }else{
3128+    sqlite3_free(zNewName);
3129+  }
3130+  return rc;
3131+}
3132+
3133+
3134+/*
3135+** A virtual table module that provides fuzzy search.
3136+*/
3137+static sqlite3_module spellfix1Module = {
3138+  0,                       /* iVersion */
3139+  spellfix1Create,         /* xCreate - handle CREATE VIRTUAL TABLE */
3140+  spellfix1Connect,        /* xConnect - reconnected to an existing table */
3141+  spellfix1BestIndex,      /* xBestIndex - figure out how to do a query */
3142+  spellfix1Disconnect,     /* xDisconnect - close a connection */
3143+  spellfix1Destroy,        /* xDestroy - handle DROP TABLE */
3144+  spellfix1Open,           /* xOpen - open a cursor */
3145+  spellfix1Close,          /* xClose - close a cursor */
3146+  spellfix1Filter,         /* xFilter - configure scan constraints */
3147+  spellfix1Next,           /* xNext - advance a cursor */
3148+  spellfix1Eof,            /* xEof - check for end of scan */
3149+  spellfix1Column,         /* xColumn - read data */
3150+  spellfix1Rowid,          /* xRowid - read data */
3151+  spellfix1Update,         /* xUpdate */
3152+  0,                       /* xBegin */
3153+  0,                       /* xSync */
3154+  0,                       /* xCommit */
3155+  0,                       /* xRollback */
3156+  0,                       /* xFindMethod */
3157+  spellfix1Rename,         /* xRename */
3158+  0,                       /* xSavepoint */
3159+  0,                       /* xRelease */
3160+  0,                       /* xRollbackTo */
3161+  0,                       /* xShadowName */
3162+  0                        /* xIntegrity */
3163+};
3164+
3165+/*
3166+** Register the various functions and the virtual table.
3167+*/
3168+static int spellfix1Register(sqlite3 *db){
3169+  int rc = SQLITE_OK;
3170+  int i;
3171+  rc = sqlite3_create_function(db, "spellfix1_translit", 1,
3172+                               SQLITE_UTF8|SQLITE_DETERMINISTIC, 0,
3173+                                transliterateSqlFunc, 0, 0);
3174+  if( rc==SQLITE_OK ){
3175+    rc = sqlite3_create_function(db, "spellfix1_editdist", 2,
3176+                                 SQLITE_UTF8|SQLITE_DETERMINISTIC, 0,
3177+                                  editdistSqlFunc, 0, 0);
3178+  }
3179+  if( rc==SQLITE_OK ){
3180+    rc = sqlite3_create_function(db, "spellfix1_phonehash", 1,
3181+                                 SQLITE_UTF8|SQLITE_DETERMINISTIC, 0,
3182+                                  phoneticHashSqlFunc, 0, 0);
3183+  }
3184+  if( rc==SQLITE_OK ){
3185+    rc = sqlite3_create_function(db, "spellfix1_scriptcode", 1,
3186+                                  SQLITE_UTF8|SQLITE_DETERMINISTIC, 0,
3187+                                  scriptCodeSqlFunc, 0, 0);
3188+  }
3189+  if( rc==SQLITE_OK ){
3190+    rc = sqlite3_create_module(db, "spellfix1", &spellfix1Module, 0);
3191+  }
3192+  if( rc==SQLITE_OK ){
3193+    rc = editDist3Install(db);
3194+  }
3195+
3196+  /* Verify sanity of the translit[] table */
3197+  for(i=0; i<sizeof(translit)/sizeof(translit[0])-1; i++){
3198+    assert( translit[i].cFrom<translit[i+1].cFrom );
3199+  }
3200+
3201+  return rc;
3202+}
3203+
3204+#endif /* SQLITE_OMIT_VIRTUALTABLE */
3205+
3206+/*
3207+** Extension load function.
3208+*/
3209+#ifdef _WIN32
3210+__declspec(dllexport)
3211+#endif
3212+int sqlite3_spellfix_init(
3213+  sqlite3 *db, 
3214+  char **pzErrMsg, 
3215+  const sqlite3_api_routines *pApi
3216+){
3217+  SQLITE_EXTENSION_INIT2(pApi);
3218+#ifndef SQLITE_OMIT_VIRTUALTABLE
3219+  return spellfix1Register(db);
3220+#endif
3221+  return SQLITE_OK;
3222+}
3223diff --git a/main.c b/main.c
3224index 69a68b8de4df79435dead3ef0535736d7c6422e0..f95a4659d4627d23ca5804896b624110400ada9c 100644
3225--- a/main.c
3226+++ b/main.c
3227@@ -11,13 +11,14 @@ unsigned int count_lines(FILE* file);
3228 int load_or_save_db(sqlite3 *pInMemory, const char *zFilename, int isSave);
3229 
3230 int main() {
3231+
3232+    Data *data = new_data(":memory:");
3233+    bootstrap(data);
3234+
3235     setlocale(LC_ALL, "");
3236     initscr(); 
3237     noecho();
3238     cbreak();
3239-
3240-    Data *data = new_data(":memory:");
3241-    bootstrap(data);
3242 
3243     FILE *f = fopen("dict.txt", "r");
3244     unsigned int lines = count_lines(f);
3245diff --git a/ui.c b/ui.c
3246index 8837bab19ca394e22205abe5312b37350ddad5e5..2e779e3a4c4dc055987446d7be311e27ba0b7a7e 100644
3247--- a/ui.c
3248+++ b/ui.c
3249@@ -5,30 +5,11 @@ #include "ui.h"
3250 
3251 const char *uload = "█";
3252 
3253-
3254 PROGRESS_BAR* new_progress_bar(WINDOW* scr, float total) {
3255     PROGRESS_BAR *bar = (PROGRESS_BAR*)malloc(sizeof(PROGRESS_BAR));
3256     bar->scr = scr;
3257     bar->total = total;
3258     bar->current = 0;
3259-
3260-    int x, y;
3261-    int hx, hy;
3262-
3263-    getmaxyx(scr, y, x);
3264-
3265-    hx = x/2;
3266-    hy = y/2;
3267-
3268-    wmove(scr, hy-1, 0);
3269-    wprintw(scr, uload);
3270-
3271-    wmove(scr, hy, hx-4);
3272-    wprintw(scr, "000%%");
3273-
3274-    wmove(scr, hy+1, hx);
3275-    wprintw(scr, "%.0f/%.0f", 0.0, total);
3276-
3277     return bar;
3278 }
3279