| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 ** 2006 Oct 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 ** Implementation of the "simple" full-text-search tokenizer. | |
| 14 */ | |
| 15 | |
| 16 /* | |
| 17 ** The code in this file is only compiled if: | |
| 18 ** | |
| 19 ** * The FTS3 module is being built as an extension | |
| 20 ** (in which case SQLITE_CORE is not defined), or | |
| 21 ** | |
| 22 ** * The FTS3 module is being built into the core of | |
| 23 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). | |
| 24 */ | |
| 25 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) | |
| 26 | |
| 27 | |
| 28 #include <assert.h> | |
| 29 #include <stdlib.h> | |
| 30 #include <stdio.h> | |
| 31 #include <string.h> | |
| 32 | |
| 33 #include "fts3_tokenizer.h" | |
| 34 | |
| 35 typedef struct simple_tokenizer { | |
| 36 sqlite3_tokenizer base; | |
| 37 char delim[128]; /* flag ASCII delimiters */ | |
| 38 } simple_tokenizer; | |
| 39 | |
| 40 typedef struct simple_tokenizer_cursor { | |
| 41 sqlite3_tokenizer_cursor base; | |
| 42 const char *pInput; /* input we are tokenizing */ | |
| 43 int nBytes; /* size of the input */ | |
| 44 int iOffset; /* current position in pInput */ | |
| 45 int iToken; /* index of next token to be returned */ | |
| 46 char *pToken; /* storage for current token */ | |
| 47 int nTokenAllocated; /* space allocated to zToken buffer */ | |
| 48 } simple_tokenizer_cursor; | |
| 49 | |
| 50 | |
| 51 /* Forward declaration */ | |
| 52 static const sqlite3_tokenizer_module simpleTokenizerModule; | |
| 53 | |
| 54 static int simpleDelim(simple_tokenizer *t, unsigned char c){ | |
| 55 return c<0x80 && t->delim[c]; | |
| 56 } | |
| 57 static int fts3_isalnum(int x){ | |
| 58 return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z'); | |
| 59 } | |
| 60 | |
| 61 /* | |
| 62 ** Create a new tokenizer instance. | |
| 63 */ | |
| 64 static int simpleCreate( | |
| 65 int argc, const char * const *argv, | |
| 66 sqlite3_tokenizer **ppTokenizer | |
| 67 ){ | |
| 68 simple_tokenizer *t; | |
| 69 | |
| 70 t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t)); | |
| 71 if( t==NULL ) return SQLITE_NOMEM; | |
| 72 memset(t, 0, sizeof(*t)); | |
| 73 | |
| 74 /* TODO(shess) Delimiters need to remain the same from run to run, | |
| 75 ** else we need to reindex. One solution would be a meta-table to | |
| 76 ** track such information in the database, then we'd only want this | |
| 77 ** information on the initial create. | |
| 78 */ | |
| 79 if( argc>1 ){ | |
| 80 int i, n = strlen(argv[1]); | |
| 81 for(i=0; i<n; i++){ | |
| 82 unsigned char ch = argv[1][i]; | |
| 83 /* We explicitly don't support UTF-8 delimiters for now. */ | |
| 84 if( ch>=0x80 ){ | |
| 85 sqlite3_free(t); | |
| 86 return SQLITE_ERROR; | |
| 87 } | |
| 88 t->delim[ch] = 1; | |
| 89 } | |
| 90 } else { | |
| 91 /* Mark non-alphanumeric ASCII characters as delimiters */ | |
| 92 int i; | |
| 93 for(i=1; i<0x80; i++){ | |
| 94 t->delim[i] = !fts3_isalnum(i); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 *ppTokenizer = &t->base; | |
| 99 return SQLITE_OK; | |
| 100 } | |
| 101 | |
| 102 /* | |
| 103 ** Destroy a tokenizer | |
| 104 */ | |
| 105 static int simpleDestroy(sqlite3_tokenizer *pTokenizer){ | |
| 106 sqlite3_free(pTokenizer); | |
| 107 return SQLITE_OK; | |
| 108 } | |
| 109 | |
| 110 /* | |
| 111 ** Prepare to begin tokenizing a particular string. The input | |
| 112 ** string to be tokenized is pInput[0..nBytes-1]. A cursor | |
| 113 ** used to incrementally tokenize this string is returned in | |
| 114 ** *ppCursor. | |
| 115 */ | |
| 116 static int simpleOpen( | |
| 117 sqlite3_tokenizer *pTokenizer, /* The tokenizer */ | |
| 118 const char *pInput, int nBytes, /* String to be tokenized */ | |
| 119 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */ | |
| 120 ){ | |
| 121 simple_tokenizer_cursor *c; | |
| 122 | |
| 123 c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c)); | |
| 124 if( c==NULL ) return SQLITE_NOMEM; | |
| 125 | |
| 126 c->pInput = pInput; | |
| 127 if( pInput==0 ){ | |
| 128 c->nBytes = 0; | |
| 129 }else if( nBytes<0 ){ | |
| 130 c->nBytes = (int)strlen(pInput); | |
| 131 }else{ | |
| 132 c->nBytes = nBytes; | |
| 133 } | |
| 134 c->iOffset = 0; /* start tokenizing at the beginning */ | |
| 135 c->iToken = 0; | |
| 136 c->pToken = NULL; /* no space allocated, yet. */ | |
| 137 c->nTokenAllocated = 0; | |
| 138 | |
| 139 *ppCursor = &c->base; | |
| 140 return SQLITE_OK; | |
| 141 } | |
| 142 | |
| 143 /* | |
| 144 ** Close a tokenization cursor previously opened by a call to | |
| 145 ** simpleOpen() above. | |
| 146 */ | |
| 147 static int simpleClose(sqlite3_tokenizer_cursor *pCursor){ | |
| 148 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; | |
| 149 sqlite3_free(c->pToken); | |
| 150 sqlite3_free(c); | |
| 151 return SQLITE_OK; | |
| 152 } | |
| 153 | |
| 154 /* | |
| 155 ** Extract the next token from a tokenization cursor. The cursor must | |
| 156 ** have been opened by a prior call to simpleOpen(). | |
| 157 */ | |
| 158 static int simpleNext( | |
| 159 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */ | |
| 160 const char **ppToken, /* OUT: *ppToken is the token text */ | |
| 161 int *pnBytes, /* OUT: Number of bytes in token */ | |
| 162 int *piStartOffset, /* OUT: Starting offset of token */ | |
| 163 int *piEndOffset, /* OUT: Ending offset of token */ | |
| 164 int *piPosition /* OUT: Position integer of token */ | |
| 165 ){ | |
| 166 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; | |
| 167 simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer; | |
| 168 unsigned char *p = (unsigned char *)c->pInput; | |
| 169 | |
| 170 while( c->iOffset<c->nBytes ){ | |
| 171 int iStartOffset; | |
| 172 | |
| 173 /* Scan past delimiter characters */ | |
| 174 while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){ | |
| 175 c->iOffset++; | |
| 176 } | |
| 177 | |
| 178 /* Count non-delimiter characters. */ | |
| 179 iStartOffset = c->iOffset; | |
| 180 while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){ | |
| 181 c->iOffset++; | |
| 182 } | |
| 183 | |
| 184 if( c->iOffset>iStartOffset ){ | |
| 185 int i, n = c->iOffset-iStartOffset; | |
| 186 if( n>c->nTokenAllocated ){ | |
| 187 c->nTokenAllocated = n+20; | |
| 188 c->pToken = sqlite3_realloc(c->pToken, c->nTokenAllocated); | |
| 189 if( c->pToken==NULL ) return SQLITE_NOMEM; | |
| 190 } | |
| 191 for(i=0; i<n; i++){ | |
| 192 /* TODO(shess) This needs expansion to handle UTF-8 | |
| 193 ** case-insensitivity. | |
| 194 */ | |
| 195 unsigned char ch = p[iStartOffset+i]; | |
| 196 c->pToken[i] = (ch>='A' && ch<='Z') ? ch-'A'+'a' : ch; | |
| 197 } | |
| 198 *ppToken = c->pToken; | |
| 199 *pnBytes = n; | |
| 200 *piStartOffset = iStartOffset; | |
| 201 *piEndOffset = c->iOffset; | |
| 202 *piPosition = c->iToken++; | |
| 203 | |
| 204 return SQLITE_OK; | |
| 205 } | |
| 206 } | |
| 207 return SQLITE_DONE; | |
| 208 } | |
| 209 | |
| 210 /* | |
| 211 ** The set of routines that implement the simple tokenizer | |
| 212 */ | |
| 213 static const sqlite3_tokenizer_module simpleTokenizerModule = { | |
| 214 0, | |
| 215 simpleCreate, | |
| 216 simpleDestroy, | |
| 217 simpleOpen, | |
| 218 simpleClose, | |
| 219 simpleNext, | |
| 220 }; | |
| 221 | |
| 222 /* | |
| 223 ** Allocate a new simple tokenizer. Return a pointer to the new | |
| 224 ** tokenizer in *ppModule | |
| 225 */ | |
| 226 void sqlite3Fts3SimpleTokenizerModule( | |
| 227 sqlite3_tokenizer_module const**ppModule | |
| 228 ){ | |
| 229 *ppModule = &simpleTokenizerModule; | |
| 230 } | |
| 231 | |
| 232 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ | |
| OLD | NEW |