spellfix.c 101 KB

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