Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/ext/misc/spellfix.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / ext / misc / spellfix.c` 3097 lines · 104315 bytes Raw Blame History 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 28 # include 29 # include 30 # include 31 typedef unsigned char u8; 32 typedef unsigned short u16; 33 typedef sqlite3_int64 i64; 34 #endif 35 #include 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=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])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; iLangnLang; 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; in; 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; ia[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( b10000 ) 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; i2pCost; 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; i1iDelCost); 1068 for(k=0; knFrom, i1, p->iCost); 1071 } 1072 } 1073 1074 /* Fill in all subsequent rows, top-to-bottom, left-to-right */ 1075 for(i2=0; i2iInsCost); 1082 for(k=0; knTo), rxp, p->iCost); 1085 } 1086 for(i1=0; i1iDelCost); 1097 for(k=0; knFrom, 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; knFrom+szRow*p->nTo, cxd, p->iCost); 1110 } 1111 } 1112 } 1113 } 1114 1115 #if 0 /* Enable for debugging */ 1116 printf(" ^"); 1117 for(i1=0; i19999 ) printf(" ****"); 1122 else printf(" %4d", v); 1123 } 1124 printf("\n"); 1125 for(i2=0; i29999 ) 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; knLang; 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( i0 ){ 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=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]='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 && ibase); 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; inRow; 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; inConstraint; 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; inRun; i++){ 2451 if( strcmp(p->azPrior[i], zHash1)==0 ) return; 2452 } 2453 #endif 2454 assert( p->nRunazPrior[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; inRow; 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->nRownAlloc ){ 2505 idx = pCur->nRow; 2506 }else if( iScorea[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->nRownAlloc ) pCur->nRow++; 2525 if( pCur->nRow==pCur->nAlloc ){ 2526 iWorst = pCur->a[0].iScore; 2527 idxWorst = 0; 2528 for(i=1; inRow; i++){ 2529 iScore = pCur->a[i].iScore; 2530 if( iWorstrc = 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 k2zDbName, 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