interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / fts5 / fts5_tokenize.c

1491 lines · 40642 bytes Raw Blame History
1/*
2** 2014 May 31
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
14
15#include "fts5Int.h"
16
17/**************************************************************************
18** Start of ascii tokenizer implementation.
19*/
20
21/*
22** For tokenizers with no "unicode" modifier, the set of token characters
23** is the same as the set of ASCII range alphanumeric characters.
24*/
25static unsigned char aAsciiTokenChar[128] = {
26 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00..0x0F */
27 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10..0x1F */
28 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20..0x2F */
29 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30..0x3F */
30 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40..0x4F */
31 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x50..0x5F */
32 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60..0x6F */
33 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x70..0x7F */
34};
35
36typedef struct AsciiTokenizer AsciiTokenizer;
37struct AsciiTokenizer {
38 unsigned char aTokenChar[128];
39};
40
41static void fts5AsciiAddExceptions(
42 AsciiTokenizer *p,
43 const char *zArg,
44 int bTokenChars
45){
46 int i;
47 for(i=0; zArg[i]; i++){
48 if( (zArg[i] & 0x80)==0 ){
49 p->aTokenChar[(int)zArg[i]] = (unsigned char)bTokenChars;
50 }
51 }
52}
53
54/*
55** Delete a "ascii" tokenizer.
56*/
57static void fts5AsciiDelete(Fts5Tokenizer *p){
58 sqlite3_free(p);
59}
60
61/*
62** Create an "ascii" tokenizer.
63*/
64static int fts5AsciiCreate(
65 void *pUnused,
66 const char **azArg, int nArg,
67 Fts5Tokenizer **ppOut
68){
69 int rc = SQLITE_OK;
70 AsciiTokenizer *p = 0;
71 UNUSED_PARAM(pUnused);
72 if( nArg%2 ){
73 rc = SQLITE_ERROR;
74 }else{
75 p = sqlite3_malloc64(sizeof(AsciiTokenizer));
76 if( p==0 ){
77 rc = SQLITE_NOMEM;
78 }else{
79 int i;
80 memset(p, 0, sizeof(AsciiTokenizer));
81 memcpy(p->aTokenChar, aAsciiTokenChar, sizeof(aAsciiTokenChar));
82 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
83 const char *zArg = azArg[i+1];
84 if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
85 fts5AsciiAddExceptions(p, zArg, 1);
86 }else
87 if( 0==sqlite3_stricmp(azArg[i], "separators") ){
88 fts5AsciiAddExceptions(p, zArg, 0);
89 }else{
90 rc = SQLITE_ERROR;
91 }
92 }
93 if( rc!=SQLITE_OK ){
94 fts5AsciiDelete((Fts5Tokenizer*)p);
95 p = 0;
96 }
97 }
98 }
99
100 *ppOut = (Fts5Tokenizer*)p;
101 return rc;
102}
103
104
105static void asciiFold(char *aOut, const char *aIn, int nByte){
106 int i;
107 for(i=0; i<nByte; i++){
108 char c = aIn[i];
109 if( c>='A' && c<='Z' ) c += 32;
110 aOut[i] = c;
111 }
112}
113
114/*
115** Tokenize some text using the ascii tokenizer.
116*/
117static int fts5AsciiTokenize(
118 Fts5Tokenizer *pTokenizer,
119 void *pCtx,
120 int iUnused,
121 const char *pText, int nText,
122 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
123){
124 AsciiTokenizer *p = (AsciiTokenizer*)pTokenizer;
125 int rc = SQLITE_OK;
126 int ie;
127 int is = 0;
128
129 char aFold[64];
130 int nFold = sizeof(aFold);
131 char *pFold = aFold;
132 unsigned char *a = p->aTokenChar;
133
134 UNUSED_PARAM(iUnused);
135
136 while( is<nText && rc==SQLITE_OK ){
137 int nByte;
138
139 /* Skip any leading divider characters. */
140 while( is<nText && ((pText[is]&0x80)==0 && a[(int)pText[is]]==0) ){
141 is++;
142 }
143 if( is==nText ) break;
144
145 /* Count the token characters */
146 ie = is+1;
147 while( ie<nText && ((pText[ie]&0x80) || a[(int)pText[ie]] ) ){
148 ie++;
149 }
150
151 /* Fold to lower case */
152 nByte = ie-is;
153 if( nByte>nFold ){
154 if( pFold!=aFold ) sqlite3_free(pFold);
155 pFold = sqlite3_malloc64((sqlite3_int64)nByte*2);
156 if( pFold==0 ){
157 rc = SQLITE_NOMEM;
158 break;
159 }
160 nFold = nByte*2;
161 }
162 asciiFold(pFold, &pText[is], nByte);
163
164 /* Invoke the token callback */
165 rc = xToken(pCtx, 0, pFold, nByte, is, ie);
166 is = ie+1;
167 }
168
169 if( pFold!=aFold ) sqlite3_free(pFold);
170 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
171 return rc;
172}
173
174/**************************************************************************
175** Start of unicode61 tokenizer implementation.
176*/
177
178
179/*
180** The following two macros - FTS5_READ_UTF8 and FTS5_WRITE_UTF8 - have
181** been copied from the sqlite3 source file utf.c.
182*/
183static const unsigned char fts5Utf8Trans1[] = {
184 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
185 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
186 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
187 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
188 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
189 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
190 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
191 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
192};
193
194#define FTS5_READ_UTF8(zIn, zTerm, c) \
195 c = *(zIn++); \
196 if( c>=0xc0 ){ \
197 c = fts5Utf8Trans1[c-0xc0]; \
198 while( zIn<zTerm && (*zIn & 0xc0)==0x80 ){ \
199 c = (c<<6) + (0x3f & *(zIn++)); \
200 } \
201 if( c<0x80 \
202 || (c&0xFFFFF800)==0xD800 \
203 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
204 }
205
206
207#define FTS5_WRITE_UTF8(zOut, c) { \
208 if( c<0x00080 ){ \
209 *zOut++ = (unsigned char)(c&0xFF); \
210 } \
211 else if( c<0x00800 ){ \
212 *zOut++ = 0xC0 + (unsigned char)((c>>6)&0x1F); \
213 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
214 } \
215 else if( c<0x10000 ){ \
216 *zOut++ = 0xE0 + (unsigned char)((c>>12)&0x0F); \
217 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
218 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
219 }else{ \
220 *zOut++ = 0xF0 + (unsigned char)((c>>18) & 0x07); \
221 *zOut++ = 0x80 + (unsigned char)((c>>12) & 0x3F); \
222 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
223 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
224 } \
225}
226
227#define FTS5_SKIP_UTF8(zIn) { \
228 if( ((unsigned char)(*(zIn++)))>=0xc0 ){ \
229 while( (((unsigned char)*zIn) & 0xc0)==0x80 ){zIn++;} \
230 } \
231}
232
233typedef struct Unicode61Tokenizer Unicode61Tokenizer;
234struct Unicode61Tokenizer {
235 unsigned char aTokenChar[128]; /* ASCII range token characters */
236 char *aFold; /* Buffer to fold text into */
237 int nFold; /* Size of aFold[] in bytes */
238 int eRemoveDiacritic; /* True if remove_diacritics=1 is set */
239 int nException;
240 int *aiException;
241
242 unsigned char aCategory[32]; /* True for token char categories */
243};
244
245/* Values for eRemoveDiacritic (must match internals of fts5_unicode2.c) */
246#define FTS5_REMOVE_DIACRITICS_NONE 0
247#define FTS5_REMOVE_DIACRITICS_SIMPLE 1
248#define FTS5_REMOVE_DIACRITICS_COMPLEX 2
249
250static int fts5UnicodeAddExceptions(
251 Unicode61Tokenizer *p, /* Tokenizer object */
252 const char *z, /* Characters to treat as exceptions */
253 int bTokenChars /* 1 for 'tokenchars', 0 for 'separators' */
254){
255 int rc = SQLITE_OK;
256 int n = (int)strlen(z);
257 int *aNew;
258
259 if( n>0 ){
260 aNew = (int*)sqlite3_realloc64(p->aiException,
261 (n+p->nException)*sizeof(int));
262 if( aNew ){
263 int nNew = p->nException;
264 const unsigned char *zCsr = (const unsigned char*)z;
265 const unsigned char *zTerm = (const unsigned char*)&z[n];
266 while( zCsr<zTerm ){
267 u32 iCode;
268 int bToken;
269 FTS5_READ_UTF8(zCsr, zTerm, iCode);
270 if( iCode<128 ){
271 p->aTokenChar[iCode] = (unsigned char)bTokenChars;
272 }else{
273 bToken = p->aCategory[sqlite3Fts5UnicodeCategory(iCode)];
274 assert( (bToken==0 || bToken==1) );
275 assert( (bTokenChars==0 || bTokenChars==1) );
276 if( bToken!=bTokenChars && sqlite3Fts5UnicodeIsdiacritic(iCode)==0 ){
277 int i;
278 for(i=0; i<nNew; i++){
279 if( (u32)aNew[i]>iCode ) break;
280 }
281 memmove(&aNew[i+1], &aNew[i], (nNew-i)*sizeof(int));
282 aNew[i] = iCode;
283 nNew++;
284 }
285 }
286 }
287 p->aiException = aNew;
288 p->nException = nNew;
289 }else{
290 rc = SQLITE_NOMEM;
291 }
292 }
293
294 return rc;
295}
296
297/*
298** Return true if the p->aiException[] array contains the value iCode.
299*/
300static int fts5UnicodeIsException(Unicode61Tokenizer *p, int iCode){
301 if( p->nException>0 ){
302 int *a = p->aiException;
303 int iLo = 0;
304 int iHi = p->nException-1;
305
306 while( iHi>=iLo ){
307 int iTest = (iHi + iLo) / 2;
308 if( iCode==a[iTest] ){
309 return 1;
310 }else if( iCode>a[iTest] ){
311 iLo = iTest+1;
312 }else{
313 iHi = iTest-1;
314 }
315 }
316 }
317
318 return 0;
319}
320
321/*
322** Delete a "unicode61" tokenizer.
323*/
324static void fts5UnicodeDelete(Fts5Tokenizer *pTok){
325 if( pTok ){
326 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTok;
327 sqlite3_free(p->aiException);
328 sqlite3_free(p->aFold);
329 sqlite3_free(p);
330 }
331 return;
332}
333
334static int unicodeSetCategories(Unicode61Tokenizer *p, const char *zCat){
335 const char *z = zCat;
336
337 while( *z ){
338 while( *z==' ' || *z=='\t' ) z++;
339 if( *z && sqlite3Fts5UnicodeCatParse(z, p->aCategory) ){
340 return SQLITE_ERROR;
341 }
342 while( *z!=' ' && *z!='\t' && *z!='\0' ) z++;
343 }
344
345 sqlite3Fts5UnicodeAscii(p->aCategory, p->aTokenChar);
346 return SQLITE_OK;
347}
348
349/*
350** Create a "unicode61" tokenizer.
351*/
352static int fts5UnicodeCreate(
353 void *pUnused,
354 const char **azArg, int nArg,
355 Fts5Tokenizer **ppOut
356){
357 int rc = SQLITE_OK; /* Return code */
358 Unicode61Tokenizer *p = 0; /* New tokenizer object */
359
360 UNUSED_PARAM(pUnused);
361
362 if( nArg%2 ){
363 rc = SQLITE_ERROR;
364 }else{
365 p = (Unicode61Tokenizer*)sqlite3_malloc64(sizeof(Unicode61Tokenizer));
366 if( p ){
367 const char *zCat = "L* N* Co";
368 int i;
369 memset(p, 0, sizeof(Unicode61Tokenizer));
370
371 p->eRemoveDiacritic = FTS5_REMOVE_DIACRITICS_SIMPLE;
372 p->nFold = 64;
373 p->aFold = sqlite3_malloc64(p->nFold * sizeof(char));
374 if( p->aFold==0 ){
375 rc = SQLITE_NOMEM;
376 }
377
378 /* Search for a "categories" argument */
379 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
380 if( 0==sqlite3_stricmp(azArg[i], "categories") ){
381 zCat = azArg[i+1];
382 }
383 }
384 if( rc==SQLITE_OK ){
385 rc = unicodeSetCategories(p, zCat);
386 }
387
388 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
389 const char *zArg = azArg[i+1];
390 if( 0==sqlite3_stricmp(azArg[i], "remove_diacritics") ){
391 if( (zArg[0]!='0' && zArg[0]!='1' && zArg[0]!='2') || zArg[1] ){
392 rc = SQLITE_ERROR;
393 }else{
394 p->eRemoveDiacritic = (zArg[0] - '0');
395 assert( p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_NONE
396 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_SIMPLE
397 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_COMPLEX
398 );
399 }
400 }else
401 if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
402 rc = fts5UnicodeAddExceptions(p, zArg, 1);
403 }else
404 if( 0==sqlite3_stricmp(azArg[i], "separators") ){
405 rc = fts5UnicodeAddExceptions(p, zArg, 0);
406 }else
407 if( 0==sqlite3_stricmp(azArg[i], "categories") ){
408 /* no-op */
409 }else{
410 rc = SQLITE_ERROR;
411 }
412 }
413 }else{
414 rc = SQLITE_NOMEM;
415 }
416 if( rc!=SQLITE_OK ){
417 fts5UnicodeDelete((Fts5Tokenizer*)p);
418 p = 0;
419 }
420 *ppOut = (Fts5Tokenizer*)p;
421 }
422 return rc;
423}
424
425/*
426** Return true if, for the purposes of tokenizing with the tokenizer
427** passed as the first argument, codepoint iCode is considered a token
428** character (not a separator).
429*/
430static int fts5UnicodeIsAlnum(Unicode61Tokenizer *p, int iCode){
431 return (
432 p->aCategory[sqlite3Fts5UnicodeCategory((u32)iCode)]
433 ^ fts5UnicodeIsException(p, iCode)
434 );
435}
436
437static int fts5UnicodeTokenize(
438 Fts5Tokenizer *pTokenizer,
439 void *pCtx,
440 int iUnused,
441 const char *pText, int nText,
442 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
443){
444 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTokenizer;
445 int rc = SQLITE_OK;
446 unsigned char *a = p->aTokenChar;
447
448 unsigned char *zTerm = (unsigned char*)&pText[nText];
449 unsigned char *zCsr = (unsigned char *)pText;
450
451 /* Output buffer */
452 char *aFold = p->aFold;
453 int nFold = p->nFold;
454 const char *pEnd = &aFold[nFold-6];
455
456 UNUSED_PARAM(iUnused);
457
458 /* Each iteration of this loop gobbles up a contiguous run of separators,
459 ** then the next token. */
460 while( rc==SQLITE_OK ){
461 u32 iCode; /* non-ASCII codepoint read from input */
462 char *zOut = aFold;
463 int is;
464 int ie;
465
466 /* Skip any separator characters. */
467 while( 1 ){
468 if( zCsr>=zTerm ) goto tokenize_done;
469 if( *zCsr & 0x80 ) {
470 /* A character outside of the ascii range. Skip past it if it is
471 ** a separator character. Or break out of the loop if it is not. */
472 is = zCsr - (unsigned char*)pText;
473 FTS5_READ_UTF8(zCsr, zTerm, iCode);
474 if( fts5UnicodeIsAlnum(p, iCode) ){
475 goto non_ascii_tokenchar;
476 }
477 }else{
478 if( a[*zCsr] ){
479 is = zCsr - (unsigned char*)pText;
480 goto ascii_tokenchar;
481 }
482 zCsr++;
483 }
484 }
485
486 /* Run through the tokenchars. Fold them into the output buffer along
487 ** the way. */
488 while( zCsr<zTerm ){
489
490 /* Grow the output buffer so that there is sufficient space to fit the
491 ** largest possible utf-8 character. */
492 if( zOut>pEnd ){
493 aFold = sqlite3_malloc64((sqlite3_int64)nFold*2);
494 if( aFold==0 ){
495 rc = SQLITE_NOMEM;
496 goto tokenize_done;
497 }
498 zOut = &aFold[zOut - p->aFold];
499 memcpy(aFold, p->aFold, nFold);
500 sqlite3_free(p->aFold);
501 p->aFold = aFold;
502 p->nFold = nFold = nFold*2;
503 pEnd = &aFold[nFold-6];
504 }
505
506 if( *zCsr & 0x80 ){
507 /* An non-ascii-range character. Fold it into the output buffer if
508 ** it is a token character, or break out of the loop if it is not. */
509 FTS5_READ_UTF8(zCsr, zTerm, iCode);
510 if( fts5UnicodeIsAlnum(p,iCode)||sqlite3Fts5UnicodeIsdiacritic(iCode) ){
511 non_ascii_tokenchar:
512 iCode = sqlite3Fts5UnicodeFold(iCode, p->eRemoveDiacritic);
513 if( iCode ) FTS5_WRITE_UTF8(zOut, iCode);
514 }else{
515 break;
516 }
517 }else if( a[*zCsr]==0 ){
518 /* An ascii-range separator character. End of token. */
519 break;
520 }else{
521 ascii_tokenchar:
522 if( *zCsr>='A' && *zCsr<='Z' ){
523 *zOut++ = *zCsr + 32;
524 }else{
525 *zOut++ = *zCsr;
526 }
527 zCsr++;
528 }
529 ie = zCsr - (unsigned char*)pText;
530 }
531
532 /* Invoke the token callback */
533 rc = xToken(pCtx, 0, aFold, zOut-aFold, is, ie);
534 }
535
536 tokenize_done:
537 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
538 return rc;
539}
540
541/**************************************************************************
542** Start of porter stemmer implementation.
543*/
544
545/* Any tokens larger than this (in bytes) are passed through without
546** stemming. */
547#define FTS5_PORTER_MAX_TOKEN 64
548
549typedef struct PorterTokenizer PorterTokenizer;
550struct PorterTokenizer {
551 fts5_tokenizer_v2 tokenizer_v2; /* Parent tokenizer module */
552 Fts5Tokenizer *pTokenizer; /* Parent tokenizer instance */
553 char aBuf[FTS5_PORTER_MAX_TOKEN + 64];
554};
555
556/*
557** Delete a "porter" tokenizer.
558*/
559static void fts5PorterDelete(Fts5Tokenizer *pTok){
560 if( pTok ){
561 PorterTokenizer *p = (PorterTokenizer*)pTok;
562 if( p->pTokenizer ){
563 p->tokenizer_v2.xDelete(p->pTokenizer);
564 }
565 sqlite3_free(p);
566 }
567}
568
569/*
570** Create a "porter" tokenizer.
571*/
572static int fts5PorterCreate(
573 void *pCtx,
574 const char **azArg, int nArg,
575 Fts5Tokenizer **ppOut
576){
577 fts5_api *pApi = (fts5_api*)pCtx;
578 int rc = SQLITE_OK;
579 PorterTokenizer *pRet;
580 void *pUserdata = 0;
581 const char *zBase = "unicode61";
582 fts5_tokenizer_v2 *pV2 = 0;
583
584 while( nArg>0 ){
585 if( sqlite3_stricmp(azArg[0],"porter")==0 ){
586 nArg--;
587 azArg++;
588 }else{
589 zBase = azArg[0];
590 break;
591 }
592 }
593
594 pRet = (PorterTokenizer*)sqlite3_malloc64(sizeof(PorterTokenizer));
595 if( pRet ){
596 memset(pRet, 0, sizeof(PorterTokenizer));
597 rc = pApi->xFindTokenizer_v2(pApi, zBase, &pUserdata, &pV2);
598 }else{
599 rc = SQLITE_NOMEM;
600 }
601 if( rc==SQLITE_OK ){
602 int nArg2 = (nArg>0 ? nArg-1 : 0);
603 const char **az2 = (nArg2 ? &azArg[1] : 0);
604 memcpy(&pRet->tokenizer_v2, pV2, sizeof(fts5_tokenizer_v2));
605 rc = pRet->tokenizer_v2.xCreate(pUserdata, az2, nArg2, &pRet->pTokenizer);
606 }
607
608 if( rc!=SQLITE_OK ){
609 fts5PorterDelete((Fts5Tokenizer*)pRet);
610 pRet = 0;
611 }
612 *ppOut = (Fts5Tokenizer*)pRet;
613 return rc;
614}
615
616typedef struct PorterContext PorterContext;
617struct PorterContext {
618 void *pCtx;
619 int (*xToken)(void*, int, const char*, int, int, int);
620 char *aBuf;
621};
622
623typedef struct PorterRule PorterRule;
624struct PorterRule {
625 const char *zSuffix;
626 int nSuffix;
627 int (*xCond)(char *zStem, int nStem);
628 const char *zOutput;
629 int nOutput;
630};
631
632#if 0
633static int fts5PorterApply(char *aBuf, int *pnBuf, PorterRule *aRule){
634 int ret = -1;
635 int nBuf = *pnBuf;
636 PorterRule *p;
637
638 for(p=aRule; p->zSuffix; p++){
639 assert( strlen(p->zSuffix)==p->nSuffix );
640 assert( strlen(p->zOutput)==p->nOutput );
641 if( nBuf<p->nSuffix ) continue;
642 if( 0==memcmp(&aBuf[nBuf - p->nSuffix], p->zSuffix, p->nSuffix) ) break;
643 }
644
645 if( p->zSuffix ){
646 int nStem = nBuf - p->nSuffix;
647 if( p->xCond==0 || p->xCond(aBuf, nStem) ){
648 memcpy(&aBuf[nStem], p->zOutput, p->nOutput);
649 *pnBuf = nStem + p->nOutput;
650 ret = p - aRule;
651 }
652 }
653
654 return ret;
655}
656#endif
657
658static int fts5PorterIsVowel(char c, int bYIsVowel){
659 return (
660 c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || (bYIsVowel && c=='y')
661 );
662}
663
664static int fts5PorterGobbleVC(char *zStem, int nStem, int bPrevCons){
665 int i;
666 int bCons = bPrevCons;
667
668 /* Scan for a vowel */
669 for(i=0; i<nStem; i++){
670 if( 0==(bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) break;
671 }
672
673 /* Scan for a consonent */
674 for(i++; i<nStem; i++){
675 if( (bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) return i+1;
676 }
677 return 0;
678}
679
680/* porter rule condition: (m > 0) */
681static int fts5Porter_MGt0(char *zStem, int nStem){
682 return !!fts5PorterGobbleVC(zStem, nStem, 0);
683}
684
685/* porter rule condition: (m > 1) */
686static int fts5Porter_MGt1(char *zStem, int nStem){
687 int n;
688 n = fts5PorterGobbleVC(zStem, nStem, 0);
689 if( n && fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
690 return 1;
691 }
692 return 0;
693}
694
695/* porter rule condition: (m = 1) */
696static int fts5Porter_MEq1(char *zStem, int nStem){
697 int n;
698 n = fts5PorterGobbleVC(zStem, nStem, 0);
699 if( n && 0==fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
700 return 1;
701 }
702 return 0;
703}
704
705/* porter rule condition: (*o) */
706static int fts5Porter_Ostar(char *zStem, int nStem){
707 if( zStem[nStem-1]=='w' || zStem[nStem-1]=='x' || zStem[nStem-1]=='y' ){
708 return 0;
709 }else{
710 int i;
711 int mask = 0;
712 int bCons = 0;
713 for(i=0; i<nStem; i++){
714 bCons = !fts5PorterIsVowel(zStem[i], bCons);
715 assert( bCons==0 || bCons==1 );
716 mask = ((mask << 1) + bCons) & 0x0007;
717 }
718 return (mask==0x0005);
719 }
720}
721
722/* porter rule condition: (m > 1 and (*S or *T)) */
723static int fts5Porter_MGt1_and_S_or_T(char *zStem, int nStem){
724 assert( nStem>0 );
725 return (zStem[nStem-1]=='s' || zStem[nStem-1]=='t')
726 && fts5Porter_MGt1(zStem, nStem);
727}
728
729/* porter rule condition: (*v*) */
730static int fts5Porter_Vowel(char *zStem, int nStem){
731 int i;
732 for(i=0; i<nStem; i++){
733 if( fts5PorterIsVowel(zStem[i], i>0) ){
734 return 1;
735 }
736 }
737 return 0;
738}
739
740
741/**************************************************************************
742***************************************************************************
743** GENERATED CODE STARTS HERE (mkportersteps.tcl)
744*/
745
746static int fts5PorterStep4(char *aBuf, int *pnBuf){
747 int ret = 0;
748 int nBuf = *pnBuf;
749 switch( aBuf[nBuf-2] ){
750
751 case 'a':
752 if( nBuf>2 && 0==memcmp("al", &aBuf[nBuf-2], 2) ){
753 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
754 *pnBuf = nBuf - 2;
755 }
756 }
757 break;
758
759 case 'c':
760 if( nBuf>4 && 0==memcmp("ance", &aBuf[nBuf-4], 4) ){
761 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
762 *pnBuf = nBuf - 4;
763 }
764 }else if( nBuf>4 && 0==memcmp("ence", &aBuf[nBuf-4], 4) ){
765 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
766 *pnBuf = nBuf - 4;
767 }
768 }
769 break;
770
771 case 'e':
772 if( nBuf>2 && 0==memcmp("er", &aBuf[nBuf-2], 2) ){
773 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
774 *pnBuf = nBuf - 2;
775 }
776 }
777 break;
778
779 case 'i':
780 if( nBuf>2 && 0==memcmp("ic", &aBuf[nBuf-2], 2) ){
781 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
782 *pnBuf = nBuf - 2;
783 }
784 }
785 break;
786
787 case 'l':
788 if( nBuf>4 && 0==memcmp("able", &aBuf[nBuf-4], 4) ){
789 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
790 *pnBuf = nBuf - 4;
791 }
792 }else if( nBuf>4 && 0==memcmp("ible", &aBuf[nBuf-4], 4) ){
793 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
794 *pnBuf = nBuf - 4;
795 }
796 }
797 break;
798
799 case 'n':
800 if( nBuf>3 && 0==memcmp("ant", &aBuf[nBuf-3], 3) ){
801 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
802 *pnBuf = nBuf - 3;
803 }
804 }else if( nBuf>5 && 0==memcmp("ement", &aBuf[nBuf-5], 5) ){
805 if( fts5Porter_MGt1(aBuf, nBuf-5) ){
806 *pnBuf = nBuf - 5;
807 }
808 }else if( nBuf>4 && 0==memcmp("ment", &aBuf[nBuf-4], 4) ){
809 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
810 *pnBuf = nBuf - 4;
811 }
812 }else if( nBuf>3 && 0==memcmp("ent", &aBuf[nBuf-3], 3) ){
813 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
814 *pnBuf = nBuf - 3;
815 }
816 }
817 break;
818
819 case 'o':
820 if( nBuf>3 && 0==memcmp("ion", &aBuf[nBuf-3], 3) ){
821 if( fts5Porter_MGt1_and_S_or_T(aBuf, nBuf-3) ){
822 *pnBuf = nBuf - 3;
823 }
824 }else if( nBuf>2 && 0==memcmp("ou", &aBuf[nBuf-2], 2) ){
825 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
826 *pnBuf = nBuf - 2;
827 }
828 }
829 break;
830
831 case 's':
832 if( nBuf>3 && 0==memcmp("ism", &aBuf[nBuf-3], 3) ){
833 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
834 *pnBuf = nBuf - 3;
835 }
836 }
837 break;
838
839 case 't':
840 if( nBuf>3 && 0==memcmp("ate", &aBuf[nBuf-3], 3) ){
841 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
842 *pnBuf = nBuf - 3;
843 }
844 }else if( nBuf>3 && 0==memcmp("iti", &aBuf[nBuf-3], 3) ){
845 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
846 *pnBuf = nBuf - 3;
847 }
848 }
849 break;
850
851 case 'u':
852 if( nBuf>3 && 0==memcmp("ous", &aBuf[nBuf-3], 3) ){
853 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
854 *pnBuf = nBuf - 3;
855 }
856 }
857 break;
858
859 case 'v':
860 if( nBuf>3 && 0==memcmp("ive", &aBuf[nBuf-3], 3) ){
861 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
862 *pnBuf = nBuf - 3;
863 }
864 }
865 break;
866
867 case 'z':
868 if( nBuf>3 && 0==memcmp("ize", &aBuf[nBuf-3], 3) ){
869 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
870 *pnBuf = nBuf - 3;
871 }
872 }
873 break;
874
875 }
876 return ret;
877}
878
879
880static int fts5PorterStep1B2(char *aBuf, int *pnBuf){
881 int ret = 0;
882 int nBuf = *pnBuf;
883 switch( aBuf[nBuf-2] ){
884
885 case 'a':
886 if( nBuf>2 && 0==memcmp("at", &aBuf[nBuf-2], 2) ){
887 memcpy(&aBuf[nBuf-2], "ate", 3);
888 *pnBuf = nBuf - 2 + 3;
889 ret = 1;
890 }
891 break;
892
893 case 'b':
894 if( nBuf>2 && 0==memcmp("bl", &aBuf[nBuf-2], 2) ){
895 memcpy(&aBuf[nBuf-2], "ble", 3);
896 *pnBuf = nBuf - 2 + 3;
897 ret = 1;
898 }
899 break;
900
901 case 'i':
902 if( nBuf>2 && 0==memcmp("iz", &aBuf[nBuf-2], 2) ){
903 memcpy(&aBuf[nBuf-2], "ize", 3);
904 *pnBuf = nBuf - 2 + 3;
905 ret = 1;
906 }
907 break;
908
909 }
910 return ret;
911}
912
913
914static int fts5PorterStep2(char *aBuf, int *pnBuf){
915 int ret = 0;
916 int nBuf = *pnBuf;
917 switch( aBuf[nBuf-2] ){
918
919 case 'a':
920 if( nBuf>7 && 0==memcmp("ational", &aBuf[nBuf-7], 7) ){
921 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
922 memcpy(&aBuf[nBuf-7], "ate", 3);
923 *pnBuf = nBuf - 7 + 3;
924 }
925 }else if( nBuf>6 && 0==memcmp("tional", &aBuf[nBuf-6], 6) ){
926 if( fts5Porter_MGt0(aBuf, nBuf-6) ){
927 memcpy(&aBuf[nBuf-6], "tion", 4);
928 *pnBuf = nBuf - 6 + 4;
929 }
930 }
931 break;
932
933 case 'c':
934 if( nBuf>4 && 0==memcmp("enci", &aBuf[nBuf-4], 4) ){
935 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
936 memcpy(&aBuf[nBuf-4], "ence", 4);
937 *pnBuf = nBuf - 4 + 4;
938 }
939 }else if( nBuf>4 && 0==memcmp("anci", &aBuf[nBuf-4], 4) ){
940 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
941 memcpy(&aBuf[nBuf-4], "ance", 4);
942 *pnBuf = nBuf - 4 + 4;
943 }
944 }
945 break;
946
947 case 'e':
948 if( nBuf>4 && 0==memcmp("izer", &aBuf[nBuf-4], 4) ){
949 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
950 memcpy(&aBuf[nBuf-4], "ize", 3);
951 *pnBuf = nBuf - 4 + 3;
952 }
953 }
954 break;
955
956 case 'g':
957 if( nBuf>4 && 0==memcmp("logi", &aBuf[nBuf-4], 4) ){
958 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
959 memcpy(&aBuf[nBuf-4], "log", 3);
960 *pnBuf = nBuf - 4 + 3;
961 }
962 }
963 break;
964
965 case 'l':
966 if( nBuf>3 && 0==memcmp("bli", &aBuf[nBuf-3], 3) ){
967 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
968 memcpy(&aBuf[nBuf-3], "ble", 3);
969 *pnBuf = nBuf - 3 + 3;
970 }
971 }else if( nBuf>4 && 0==memcmp("alli", &aBuf[nBuf-4], 4) ){
972 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
973 memcpy(&aBuf[nBuf-4], "al", 2);
974 *pnBuf = nBuf - 4 + 2;
975 }
976 }else if( nBuf>5 && 0==memcmp("entli", &aBuf[nBuf-5], 5) ){
977 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
978 memcpy(&aBuf[nBuf-5], "ent", 3);
979 *pnBuf = nBuf - 5 + 3;
980 }
981 }else if( nBuf>3 && 0==memcmp("eli", &aBuf[nBuf-3], 3) ){
982 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
983 memcpy(&aBuf[nBuf-3], "e", 1);
984 *pnBuf = nBuf - 3 + 1;
985 }
986 }else if( nBuf>5 && 0==memcmp("ousli", &aBuf[nBuf-5], 5) ){
987 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
988 memcpy(&aBuf[nBuf-5], "ous", 3);
989 *pnBuf = nBuf - 5 + 3;
990 }
991 }
992 break;
993
994 case 'o':
995 if( nBuf>7 && 0==memcmp("ization", &aBuf[nBuf-7], 7) ){
996 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
997 memcpy(&aBuf[nBuf-7], "ize", 3);
998 *pnBuf = nBuf - 7 + 3;
999 }
1000 }else if( nBuf>5 && 0==memcmp("ation", &aBuf[nBuf-5], 5) ){
1001 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
1002 memcpy(&aBuf[nBuf-5], "ate", 3);
1003 *pnBuf = nBuf - 5 + 3;
1004 }
1005 }else if( nBuf>4 && 0==memcmp("ator", &aBuf[nBuf-4], 4) ){
1006 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
1007 memcpy(&aBuf[nBuf-4], "ate", 3);
1008 *pnBuf = nBuf - 4 + 3;
1009 }
1010 }
1011 break;
1012
1013 case 's':
1014 if( nBuf>5 && 0==memcmp("alism", &aBuf[nBuf-5], 5) ){
1015 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
1016 memcpy(&aBuf[nBuf-5], "al", 2);
1017 *pnBuf = nBuf - 5 + 2;
1018 }
1019 }else if( nBuf>7 && 0==memcmp("iveness", &aBuf[nBuf-7], 7) ){
1020 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
1021 memcpy(&aBuf[nBuf-7], "ive", 3);
1022 *pnBuf = nBuf - 7 + 3;
1023 }
1024 }else if( nBuf>7 && 0==memcmp("fulness", &aBuf[nBuf-7], 7) ){
1025 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
1026 memcpy(&aBuf[nBuf-7], "ful", 3);
1027 *pnBuf = nBuf - 7 + 3;
1028 }
1029 }else if( nBuf>7 && 0==memcmp("ousness", &aBuf[nBuf-7], 7) ){
1030 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
1031 memcpy(&aBuf[nBuf-7], "ous", 3);
1032 *pnBuf = nBuf - 7 + 3;
1033 }
1034 }
1035 break;
1036
1037 case 't':
1038 if( nBuf>5 && 0==memcmp("aliti", &aBuf[nBuf-5], 5) ){
1039 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
1040 memcpy(&aBuf[nBuf-5], "al", 2);
1041 *pnBuf = nBuf - 5 + 2;
1042 }
1043 }else if( nBuf>5 && 0==memcmp("iviti", &aBuf[nBuf-5], 5) ){
1044 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
1045 memcpy(&aBuf[nBuf-5], "ive", 3);
1046 *pnBuf = nBuf - 5 + 3;
1047 }
1048 }else if( nBuf>6 && 0==memcmp("biliti", &aBuf[nBuf-6], 6) ){
1049 if( fts5Porter_MGt0(aBuf, nBuf-6) ){
1050 memcpy(&aBuf[nBuf-6], "ble", 3);
1051 *pnBuf = nBuf - 6 + 3;
1052 }
1053 }
1054 break;
1055
1056 }
1057 return ret;
1058}
1059
1060
1061static int fts5PorterStep3(char *aBuf, int *pnBuf){
1062 int ret = 0;
1063 int nBuf = *pnBuf;
1064 switch( aBuf[nBuf-2] ){
1065
1066 case 'a':
1067 if( nBuf>4 && 0==memcmp("ical", &aBuf[nBuf-4], 4) ){
1068 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
1069 memcpy(&aBuf[nBuf-4], "ic", 2);
1070 *pnBuf = nBuf - 4 + 2;
1071 }
1072 }
1073 break;
1074
1075 case 's':
1076 if( nBuf>4 && 0==memcmp("ness", &aBuf[nBuf-4], 4) ){
1077 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
1078 *pnBuf = nBuf - 4;
1079 }
1080 }
1081 break;
1082
1083 case 't':
1084 if( nBuf>5 && 0==memcmp("icate", &aBuf[nBuf-5], 5) ){
1085 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
1086 memcpy(&aBuf[nBuf-5], "ic", 2);
1087 *pnBuf = nBuf - 5 + 2;
1088 }
1089 }else if( nBuf>5 && 0==memcmp("iciti", &aBuf[nBuf-5], 5) ){
1090 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
1091 memcpy(&aBuf[nBuf-5], "ic", 2);
1092 *pnBuf = nBuf - 5 + 2;
1093 }
1094 }
1095 break;
1096
1097 case 'u':
1098 if( nBuf>3 && 0==memcmp("ful", &aBuf[nBuf-3], 3) ){
1099 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
1100 *pnBuf = nBuf - 3;
1101 }
1102 }
1103 break;
1104
1105 case 'v':
1106 if( nBuf>5 && 0==memcmp("ative", &aBuf[nBuf-5], 5) ){
1107 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
1108 *pnBuf = nBuf - 5;
1109 }
1110 }
1111 break;
1112
1113 case 'z':
1114 if( nBuf>5 && 0==memcmp("alize", &aBuf[nBuf-5], 5) ){
1115 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
1116 memcpy(&aBuf[nBuf-5], "al", 2);
1117 *pnBuf = nBuf - 5 + 2;
1118 }
1119 }
1120 break;
1121
1122 }
1123 return ret;
1124}
1125
1126
1127static int fts5PorterStep1B(char *aBuf, int *pnBuf){
1128 int ret = 0;
1129 int nBuf = *pnBuf;
1130 switch( aBuf[nBuf-2] ){
1131
1132 case 'e':
1133 if( nBuf>3 && 0==memcmp("eed", &aBuf[nBuf-3], 3) ){
1134 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
1135 memcpy(&aBuf[nBuf-3], "ee", 2);
1136 *pnBuf = nBuf - 3 + 2;
1137 }
1138 }else if( nBuf>2 && 0==memcmp("ed", &aBuf[nBuf-2], 2) ){
1139 if( fts5Porter_Vowel(aBuf, nBuf-2) ){
1140 *pnBuf = nBuf - 2;
1141 ret = 1;
1142 }
1143 }
1144 break;
1145
1146 case 'n':
1147 if( nBuf>3 && 0==memcmp("ing", &aBuf[nBuf-3], 3) ){
1148 if( fts5Porter_Vowel(aBuf, nBuf-3) ){
1149 *pnBuf = nBuf - 3;
1150 ret = 1;
1151 }
1152 }
1153 break;
1154
1155 }
1156 return ret;
1157}
1158
1159/*
1160** GENERATED CODE ENDS HERE (mkportersteps.tcl)
1161***************************************************************************
1162**************************************************************************/
1163
1164static void fts5PorterStep1A(char *aBuf, int *pnBuf){
1165 int nBuf = *pnBuf;
1166 if( aBuf[nBuf-1]=='s' ){
1167 if( aBuf[nBuf-2]=='e' ){
1168 if( (nBuf>4 && aBuf[nBuf-4]=='s' && aBuf[nBuf-3]=='s')
1169 || (nBuf>3 && aBuf[nBuf-3]=='i' )
1170 ){
1171 *pnBuf = nBuf-2;
1172 }else{
1173 *pnBuf = nBuf-1;
1174 }
1175 }
1176 else if( aBuf[nBuf-2]!='s' ){
1177 *pnBuf = nBuf-1;
1178 }
1179 }
1180}
1181
1182static int fts5PorterCb(
1183 void *pCtx,
1184 int tflags,
1185 const char *pToken,
1186 int nToken,
1187 int iStart,
1188 int iEnd
1189){
1190 PorterContext *p = (PorterContext*)pCtx;
1191
1192 char *aBuf;
1193 int nBuf;
1194
1195 if( nToken>FTS5_PORTER_MAX_TOKEN || nToken<3 ) goto pass_through;
1196 aBuf = p->aBuf;
1197 nBuf = nToken;
1198 memcpy(aBuf, pToken, nBuf);
1199
1200 /* Step 1. */
1201 fts5PorterStep1A(aBuf, &nBuf);
1202 if( fts5PorterStep1B(aBuf, &nBuf) ){
1203 if( fts5PorterStep1B2(aBuf, &nBuf)==0 ){
1204 char c = aBuf[nBuf-1];
1205 if( fts5PorterIsVowel(c, 0)==0
1206 && c!='l' && c!='s' && c!='z' && c==aBuf[nBuf-2]
1207 ){
1208 nBuf--;
1209 }else if( fts5Porter_MEq1(aBuf, nBuf) && fts5Porter_Ostar(aBuf, nBuf) ){
1210 aBuf[nBuf++] = 'e';
1211 }
1212 }
1213 }
1214
1215 /* Step 1C. */
1216 if( aBuf[nBuf-1]=='y' && fts5Porter_Vowel(aBuf, nBuf-1) ){
1217 aBuf[nBuf-1] = 'i';
1218 }
1219
1220 /* Steps 2 through 4. */
1221 fts5PorterStep2(aBuf, &nBuf);
1222 fts5PorterStep3(aBuf, &nBuf);
1223 fts5PorterStep4(aBuf, &nBuf);
1224
1225 /* Step 5a. */
1226 assert( nBuf>0 );
1227 if( aBuf[nBuf-1]=='e' ){
1228 if( fts5Porter_MGt1(aBuf, nBuf-1)
1229 || (fts5Porter_MEq1(aBuf, nBuf-1) && !fts5Porter_Ostar(aBuf, nBuf-1))
1230 ){
1231 nBuf--;
1232 }
1233 }
1234
1235 /* Step 5b. */
1236 if( nBuf>1 && aBuf[nBuf-1]=='l'
1237 && aBuf[nBuf-2]=='l' && fts5Porter_MGt1(aBuf, nBuf-1)
1238 ){
1239 nBuf--;
1240 }
1241
1242 return p->xToken(p->pCtx, tflags, aBuf, nBuf, iStart, iEnd);
1243
1244 pass_through:
1245 return p->xToken(p->pCtx, tflags, pToken, nToken, iStart, iEnd);
1246}
1247
1248/*
1249** Tokenize using the porter tokenizer.
1250*/
1251static int fts5PorterTokenize(
1252 Fts5Tokenizer *pTokenizer,
1253 void *pCtx,
1254 int flags,
1255 const char *pText, int nText,
1256 const char *pLoc, int nLoc,
1257 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
1258){
1259 PorterTokenizer *p = (PorterTokenizer*)pTokenizer;
1260 PorterContext sCtx;
1261 sCtx.xToken = xToken;
1262 sCtx.pCtx = pCtx;
1263 sCtx.aBuf = p->aBuf;
1264 return p->tokenizer_v2.xTokenize(
1265 p->pTokenizer, (void*)&sCtx, flags, pText, nText, pLoc, nLoc, fts5PorterCb
1266 );
1267}
1268
1269/**************************************************************************
1270** Start of trigram implementation.
1271*/
1272typedef struct TrigramTokenizer TrigramTokenizer;
1273struct TrigramTokenizer {
1274 int bFold; /* True to fold to lower-case */
1275 int iFoldParam; /* Parameter to pass to Fts5UnicodeFold() */
1276};
1277
1278/*
1279** Free a trigram tokenizer.
1280*/
1281static void fts5TriDelete(Fts5Tokenizer *p){
1282 sqlite3_free(p);
1283}
1284
1285/*
1286** Allocate a trigram tokenizer.
1287*/
1288static int fts5TriCreate(
1289 void *pUnused,
1290 const char **azArg,
1291 int nArg,
1292 Fts5Tokenizer **ppOut
1293){
1294 int rc = SQLITE_OK;
1295 TrigramTokenizer *pNew = 0;
1296 UNUSED_PARAM(pUnused);
1297 if( nArg%2 ){
1298 rc = SQLITE_ERROR;
1299 }else{
1300 int i;
1301 pNew = (TrigramTokenizer*)sqlite3_malloc64(sizeof(*pNew));
1302 if( pNew==0 ){
1303 rc = SQLITE_NOMEM;
1304 }else{
1305 pNew->bFold = 1;
1306 pNew->iFoldParam = 0;
1307
1308 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
1309 const char *zArg = azArg[i+1];
1310 if( 0==sqlite3_stricmp(azArg[i], "case_sensitive") ){
1311 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1] ){
1312 rc = SQLITE_ERROR;
1313 }else{
1314 pNew->bFold = (zArg[0]=='0');
1315 }
1316 }else if( 0==sqlite3_stricmp(azArg[i], "remove_diacritics") ){
1317 if( (zArg[0]!='0' && zArg[0]!='1' && zArg[0]!='2') || zArg[1] ){
1318 rc = SQLITE_ERROR;
1319 }else{
1320 pNew->iFoldParam = (zArg[0]!='0') ? 2 : 0;
1321 }
1322 }else{
1323 rc = SQLITE_ERROR;
1324 }
1325 }
1326
1327 if( pNew->iFoldParam!=0 && pNew->bFold==0 ){
1328 rc = SQLITE_ERROR;
1329 }
1330
1331 if( rc!=SQLITE_OK ){
1332 fts5TriDelete((Fts5Tokenizer*)pNew);
1333 pNew = 0;
1334 }
1335 }
1336 }
1337 *ppOut = (Fts5Tokenizer*)pNew;
1338 return rc;
1339}
1340
1341/*
1342** Trigram tokenizer tokenize routine.
1343*/
1344static int fts5TriTokenize(
1345 Fts5Tokenizer *pTok,
1346 void *pCtx,
1347 int unusedFlags,
1348 const char *pText, int nText,
1349 int (*xToken)(void*, int, const char*, int, int, int)
1350){
1351 TrigramTokenizer *p = (TrigramTokenizer*)pTok;
1352 int rc = SQLITE_OK;
1353 char aBuf[32];
1354 char *zOut = aBuf;
1355 int ii;
1356 const unsigned char *zIn = (const unsigned char*)pText;
1357 const unsigned char *zEof = (zIn ? &zIn[nText] : 0);
1358 u32 iCode = 0;
1359 int aStart[3]; /* Input offset of each character in aBuf[] */
1360
1361 UNUSED_PARAM(unusedFlags);
1362
1363 /* Populate aBuf[] with the characters for the first trigram. */
1364 for(ii=0; ii<3; ii++){
1365 do {
1366 aStart[ii] = zIn - (const unsigned char*)pText;
1367 if( zIn>=zEof ) return SQLITE_OK;
1368 FTS5_READ_UTF8(zIn, zEof, iCode);
1369 if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(iCode, p->iFoldParam);
1370 }while( iCode==0 );
1371 FTS5_WRITE_UTF8(zOut, iCode);
1372 }
1373
1374 /* At the start of each iteration of this loop:
1375 **
1376 ** aBuf: Contains 3 characters. The 3 characters of the next trigram.
1377 ** zOut: Points to the byte following the last character in aBuf.
1378 ** aStart[3]: Contains the byte offset in the input text corresponding
1379 ** to the start of each of the three characters in the buffer.
1380 */
1381 assert( zIn<=zEof );
1382 while( 1 ){
1383 int iNext; /* Start of character following current tri */
1384 const char *z1;
1385
1386 /* Read characters from the input up until the first non-diacritic */
1387 do {
1388 iNext = zIn - (const unsigned char*)pText;
1389 if( zIn>=zEof ){
1390 iCode = 0;
1391 break;
1392 }
1393 FTS5_READ_UTF8(zIn, zEof, iCode);
1394 if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(iCode, p->iFoldParam);
1395 }while( iCode==0 );
1396
1397 /* Pass the current trigram back to fts5 */
1398 rc = xToken(pCtx, 0, aBuf, zOut-aBuf, aStart[0], iNext);
1399 if( iCode==0 || rc!=SQLITE_OK ) break;
1400
1401 /* Remove the first character from buffer aBuf[]. Append the character
1402 ** with codepoint iCode. */
1403 z1 = aBuf;
1404 FTS5_SKIP_UTF8(z1);
1405 memmove(aBuf, z1, zOut - z1);
1406 zOut -= (z1 - aBuf);
1407 FTS5_WRITE_UTF8(zOut, iCode);
1408
1409 /* Update the aStart[] array */
1410 aStart[0] = aStart[1];
1411 aStart[1] = aStart[2];
1412 aStart[2] = iNext;
1413 }
1414
1415 return rc;
1416}
1417
1418/*
1419** Argument xCreate is a pointer to a constructor function for a tokenizer.
1420** pTok is a tokenizer previously created using the same method. This function
1421** returns one of FTS5_PATTERN_NONE, FTS5_PATTERN_LIKE or FTS5_PATTERN_GLOB
1422** indicating the style of pattern matching that the tokenizer can support.
1423** In practice, this is:
1424**
1425** "trigram" tokenizer, case_sensitive=1 - FTS5_PATTERN_GLOB
1426** "trigram" tokenizer, case_sensitive=0 (the default) - FTS5_PATTERN_LIKE
1427** all other tokenizers - FTS5_PATTERN_NONE
1428*/
1429int sqlite3Fts5TokenizerPattern(
1430 int (*xCreate)(void*, const char**, int, Fts5Tokenizer**),
1431 Fts5Tokenizer *pTok
1432){
1433 if( xCreate==fts5TriCreate ){
1434 TrigramTokenizer *p = (TrigramTokenizer*)pTok;
1435 if( p->iFoldParam==0 ){
1436 return p->bFold ? FTS5_PATTERN_LIKE : FTS5_PATTERN_GLOB;
1437 }
1438 }
1439 return FTS5_PATTERN_NONE;
1440}
1441
1442/*
1443** Return true if the tokenizer described by p->azArg[] is the trigram
1444** tokenizer. This tokenizer needs to be loaded before xBestIndex is
1445** called for the first time in order to correctly handle LIKE/GLOB.
1446*/
1447int sqlite3Fts5TokenizerPreload(Fts5TokenizerConfig *p){
1448 return (p->nArg>=1 && 0==sqlite3_stricmp(p->azArg[0], "trigram"));
1449}
1450
1451
1452/*
1453** Register all built-in tokenizers with FTS5.
1454*/
1455int sqlite3Fts5TokenizerInit(fts5_api *pApi){
1456 struct BuiltinTokenizer {
1457 const char *zName;
1458 fts5_tokenizer x;
1459 } aBuiltin[] = {
1460 { "unicode61", {fts5UnicodeCreate, fts5UnicodeDelete, fts5UnicodeTokenize}},
1461 { "ascii", {fts5AsciiCreate, fts5AsciiDelete, fts5AsciiTokenize }},
1462 { "trigram", {fts5TriCreate, fts5TriDelete, fts5TriTokenize}},
1463 };
1464
1465 int rc = SQLITE_OK; /* Return code */
1466 int i; /* To iterate through builtin functions */
1467
1468 for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
1469 rc = pApi->xCreateTokenizer(pApi,
1470 aBuiltin[i].zName,
1471 (void*)pApi,
1472 &aBuiltin[i].x,
1473 0
1474 );
1475 }
1476 if( rc==SQLITE_OK ){
1477 fts5_tokenizer_v2 sPorter = {
1478 2,
1479 fts5PorterCreate,
1480 fts5PorterDelete,
1481 fts5PorterTokenize
1482 };
1483 rc = pApi->xCreateTokenizer_v2(pApi,
1484 "porter",
1485 (void*)pApi,
1486 &sPorter,
1487 0
1488 );
1489 }
1490 return rc;
1491}