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