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 / fts3 / fts3_unicode.c

393 lines · 12682 bytes Raw Blame History
1/*
2** 2012 May 24
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** Implementation of the "unicode" full-text-search tokenizer.
14*/
15
16#ifndef SQLITE_DISABLE_FTS3_UNICODE
17
18#include "fts3Int.h"
19#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
20
21#include <assert.h>
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25
26#include "fts3_tokenizer.h"
27
28/*
29** The following two macros - FTS3_READ_UTF8 and FTS3_WRITE_UTF8 - have
30** been copied from the sqlite3 source file utf.c.
31*/
32
33static const unsigned char fts3Utf8Trans1[] = {
34 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
35 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
36 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
37 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
38 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
39 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
40 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
41 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
42};
43
44#define FTS3_READ_UTF8(zIn, zTerm, c) \
45 c = *(zIn++); \
46 if( c>=0xc0 ){ \
47 c = fts3Utf8Trans1[c-0xc0]; \
48 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
49 c = (c<<6) + (0x3f & *(zIn++)); \
50 } \
51 if( c<0x80 \
52 || (c&0xFFFFF800)==0xD800 \
53 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
54 }
55
56#define FTS3_WRITE_UTF8(zOut, c) { \
57 if( c<0x00080 ){ \
58 *zOut++ = (u8)(c&0xFF); \
59 } \
60 else if( c<0x00800 ){ \
61 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
62 *zOut++ = 0x80 + (u8)(c & 0x3F); \
63 } \
64 else if( c<0x10000 ){ \
65 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
66 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
67 *zOut++ = 0x80 + (u8)(c & 0x3F); \
68 }else{ \
69 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
70 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
71 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
72 *zOut++ = 0x80 + (u8)(c & 0x3F); \
73 } \
74}
75
76typedef struct unicode_tokenizer unicode_tokenizer;
77typedef struct unicode_cursor unicode_cursor;
78
79struct unicode_tokenizer {
80 sqlite3_tokenizer base;
81 int eRemoveDiacritic;
82 int nException;
83 int *aiException;
84};
85
86struct unicode_cursor {
87 sqlite3_tokenizer_cursor base;
88 const unsigned char *aInput; /* Input text being tokenized */
89 int nInput; /* Size of aInput[] in bytes */
90 int iOff; /* Current offset within aInput[] */
91 int iToken; /* Index of next token to be returned */
92 char *zToken; /* storage for current token */
93 int nAlloc; /* space allocated at zToken */
94};
95
96
97/*
98** Destroy a tokenizer allocated by unicodeCreate().
99*/
100static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
101 if( pTokenizer ){
102 unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
103 sqlite3_free(p->aiException);
104 sqlite3_free(p);
105 }
106 return SQLITE_OK;
107}
108
109/*
110** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
111** statement has specified that the tokenizer for this table shall consider
112** all characters in string zIn/nIn to be separators (if bAlnum==0) or
113** token characters (if bAlnum==1).
114**
115** For each codepoint in the zIn/nIn string, this function checks if the
116** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
117** If so, no action is taken. Otherwise, the codepoint is added to the
118** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
119** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
120** codepoints in the aiException[] array.
121**
122** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
123** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
124** It is not possible to change the behavior of the tokenizer with respect
125** to these codepoints.
126*/
127static int unicodeAddExceptions(
128 unicode_tokenizer *p, /* Tokenizer to add exceptions to */
129 int bAlnum, /* Replace Isalnum() return value with this */
130 const char *zIn, /* Array of characters to make exceptions */
131 int nIn /* Length of z in bytes */
132){
133 const unsigned char *z = (const unsigned char *)zIn;
134 const unsigned char *zTerm = &z[nIn];
135 unsigned int iCode;
136 int nEntry = 0;
137
138 assert( bAlnum==0 || bAlnum==1 );
139
140 while( z<zTerm ){
141 FTS3_READ_UTF8(z, zTerm, iCode);
142 assert( (sqlite3FtsUnicodeIsalnum((int)iCode) & 0xFFFFFFFE)==0 );
143 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
144 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
145 ){
146 nEntry++;
147 }
148 }
149
150 if( nEntry ){
151 int *aNew; /* New aiException[] array */
152 int nNew; /* Number of valid entries in array aNew[] */
153
154 aNew = sqlite3_realloc64(p->aiException,(p->nException+nEntry)*sizeof(int));
155 if( aNew==0 ) return SQLITE_NOMEM;
156 nNew = p->nException;
157
158 z = (const unsigned char *)zIn;
159 while( z<zTerm ){
160 FTS3_READ_UTF8(z, zTerm, iCode);
161 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
162 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
163 ){
164 int i, j;
165 for(i=0; i<nNew && aNew[i]<(int)iCode; i++);
166 for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
167 aNew[i] = (int)iCode;
168 nNew++;
169 }
170 }
171 p->aiException = aNew;
172 p->nException = nNew;
173 }
174
175 return SQLITE_OK;
176}
177
178/*
179** Return true if the p->aiException[] array contains the value iCode.
180*/
181static int unicodeIsException(unicode_tokenizer *p, int iCode){
182 if( p->nException>0 ){
183 int *a = p->aiException;
184 int iLo = 0;
185 int iHi = p->nException-1;
186
187 while( iHi>=iLo ){
188 int iTest = (iHi + iLo) / 2;
189 if( iCode==a[iTest] ){
190 return 1;
191 }else if( iCode>a[iTest] ){
192 iLo = iTest+1;
193 }else{
194 iHi = iTest-1;
195 }
196 }
197 }
198
199 return 0;
200}
201
202/*
203** Return true if, for the purposes of tokenization, codepoint iCode is
204** considered a token character (not a separator).
205*/
206static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
207 assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
208 return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
209}
210
211/*
212** Create a new tokenizer instance.
213*/
214static int unicodeCreate(
215 int nArg, /* Size of array argv[] */
216 const char * const *azArg, /* Tokenizer creation arguments */
217 sqlite3_tokenizer **pp /* OUT: New tokenizer handle */
218){
219 unicode_tokenizer *pNew; /* New tokenizer object */
220 int i;
221 int rc = SQLITE_OK;
222
223 pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
224 if( pNew==NULL ) return SQLITE_NOMEM;
225 memset(pNew, 0, sizeof(unicode_tokenizer));
226 pNew->eRemoveDiacritic = 1;
227
228 for(i=0; rc==SQLITE_OK && i<nArg; i++){
229 const char *z = azArg[i];
230 int n = (int)strlen(z);
231
232 if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
233 pNew->eRemoveDiacritic = 1;
234 }
235 else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
236 pNew->eRemoveDiacritic = 0;
237 }
238 else if( n==19 && memcmp("remove_diacritics=2", z, 19)==0 ){
239 pNew->eRemoveDiacritic = 2;
240 }
241 else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
242 rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
243 }
244 else if( n>=11 && memcmp("separators=", z, 11)==0 ){
245 rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
246 }
247 else{
248 /* Unrecognized argument */
249 rc = SQLITE_ERROR;
250 }
251 }
252
253 if( rc!=SQLITE_OK ){
254 unicodeDestroy((sqlite3_tokenizer *)pNew);
255 pNew = 0;
256 }
257 *pp = (sqlite3_tokenizer *)pNew;
258 return rc;
259}
260
261/*
262** Prepare to begin tokenizing a particular string. The input
263** string to be tokenized is pInput[0..nBytes-1]. A cursor
264** used to incrementally tokenize this string is returned in
265** *ppCursor.
266*/
267static int unicodeOpen(
268 sqlite3_tokenizer *p, /* The tokenizer */
269 const char *aInput, /* Input string */
270 int nInput, /* Size of string aInput in bytes */
271 sqlite3_tokenizer_cursor **pp /* OUT: New cursor object */
272){
273 unicode_cursor *pCsr;
274
275 pCsr = (unicode_cursor *)sqlite3_malloc(sizeof(unicode_cursor));
276 if( pCsr==0 ){
277 return SQLITE_NOMEM;
278 }
279 memset(pCsr, 0, sizeof(unicode_cursor));
280
281 pCsr->aInput = (const unsigned char *)aInput;
282 if( aInput==0 ){
283 pCsr->nInput = 0;
284 pCsr->aInput = (const unsigned char*)"";
285 }else if( nInput<0 ){
286 pCsr->nInput = (int)strlen(aInput);
287 }else{
288 pCsr->nInput = nInput;
289 }
290
291 *pp = &pCsr->base;
292 UNUSED_PARAMETER(p);
293 return SQLITE_OK;
294}
295
296/*
297** Close a tokenization cursor previously opened by a call to
298** simpleOpen() above.
299*/
300static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
301 unicode_cursor *pCsr = (unicode_cursor *) pCursor;
302 sqlite3_free(pCsr->zToken);
303 sqlite3_free(pCsr);
304 return SQLITE_OK;
305}
306
307/*
308** Extract the next token from a tokenization cursor. The cursor must
309** have been opened by a prior call to simpleOpen().
310*/
311static int unicodeNext(
312 sqlite3_tokenizer_cursor *pC, /* Cursor returned by simpleOpen */
313 const char **paToken, /* OUT: Token text */
314 int *pnToken, /* OUT: Number of bytes at *paToken */
315 int *piStart, /* OUT: Starting offset of token */
316 int *piEnd, /* OUT: Ending offset of token */
317 int *piPos /* OUT: Position integer of token */
318){
319 unicode_cursor *pCsr = (unicode_cursor *)pC;
320 unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
321 unsigned int iCode = 0;
322 char *zOut;
323 const unsigned char *z = &pCsr->aInput[pCsr->iOff];
324 const unsigned char *zStart = z;
325 const unsigned char *zEnd;
326 const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
327
328 /* Scan past any delimiter characters before the start of the next token.
329 ** Return SQLITE_DONE early if this takes us all the way to the end of
330 ** the input. */
331 while( z<zTerm ){
332 FTS3_READ_UTF8(z, zTerm, iCode);
333 if( unicodeIsAlnum(p, (int)iCode) ) break;
334 zStart = z;
335 }
336 if( zStart>=zTerm ) return SQLITE_DONE;
337
338 zOut = pCsr->zToken;
339 do {
340 int iOut;
341
342 /* Grow the output buffer if required. */
343 if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
344 char *zNew = sqlite3_realloc64(pCsr->zToken, pCsr->nAlloc+64);
345 if( !zNew ) return SQLITE_NOMEM;
346 zOut = &zNew[zOut - pCsr->zToken];
347 pCsr->zToken = zNew;
348 pCsr->nAlloc += 64;
349 }
350
351 /* Write the folded case of the last character read to the output */
352 zEnd = z;
353 iOut = sqlite3FtsUnicodeFold((int)iCode, p->eRemoveDiacritic);
354 if( iOut ){
355 FTS3_WRITE_UTF8(zOut, iOut);
356 }
357
358 /* If the cursor is not at EOF, read the next character */
359 if( z>=zTerm ) break;
360 FTS3_READ_UTF8(z, zTerm, iCode);
361 }while( unicodeIsAlnum(p, (int)iCode)
362 || sqlite3FtsUnicodeIsdiacritic((int)iCode)
363 );
364
365 /* Set the output variables and return. */
366 pCsr->iOff = (int)(z - pCsr->aInput);
367 *paToken = pCsr->zToken;
368 *pnToken = (int)(zOut - pCsr->zToken);
369 *piStart = (int)(zStart - pCsr->aInput);
370 *piEnd = (int)(zEnd - pCsr->aInput);
371 *piPos = pCsr->iToken++;
372 return SQLITE_OK;
373}
374
375/*
376** Set *ppModule to a pointer to the sqlite3_tokenizer_module
377** structure for the unicode tokenizer.
378*/
379void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
380 static const sqlite3_tokenizer_module module = {
381 0,
382 unicodeCreate,
383 unicodeDestroy,
384 unicodeOpen,
385 unicodeClose,
386 unicodeNext,
387 0,
388 };
389 *ppModule = &module;
390}
391
392#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
393#endif /* ifndef SQLITE_DISABLE_FTS3_UNICODE */