Enhance the command-line completion extension to return the names of
[sqlite.git] / ext / fts1 / fts1_tokenizer1.c
blobf58fba8f8e613ffed6764ffd0868d9646cb96011
1 /*
2 ** The author disclaims copyright to this source code.
3 **
4 *************************************************************************
5 ** Implementation of the "simple" full-text-search tokenizer.
6 */
8 /*
9 ** The code in this file is only compiled if:
11 ** * The FTS1 module is being built as an extension
12 ** (in which case SQLITE_CORE is not defined), or
14 ** * The FTS1 module is being built into the core of
15 ** SQLite (in which case SQLITE_ENABLE_FTS1 is defined).
17 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1)
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
26 #include "fts1_tokenizer.h"
28 typedef struct simple_tokenizer {
29 sqlite3_tokenizer base;
30 char delim[128]; /* flag ASCII delimiters */
31 } simple_tokenizer;
33 typedef struct simple_tokenizer_cursor {
34 sqlite3_tokenizer_cursor base;
35 const char *pInput; /* input we are tokenizing */
36 int nBytes; /* size of the input */
37 int iOffset; /* current position in pInput */
38 int iToken; /* index of next token to be returned */
39 char *pToken; /* storage for current token */
40 int nTokenAllocated; /* space allocated to zToken buffer */
41 } simple_tokenizer_cursor;
44 /* Forward declaration */
45 static const sqlite3_tokenizer_module simpleTokenizerModule;
47 static int isDelim(simple_tokenizer *t, unsigned char c){
48 return c<0x80 && t->delim[c];
52 ** Create a new tokenizer instance.
54 static int simpleCreate(
55 int argc, const char * const *argv,
56 sqlite3_tokenizer **ppTokenizer
58 simple_tokenizer *t;
60 t = (simple_tokenizer *) calloc(sizeof(*t), 1);
61 if( t==NULL ) return SQLITE_NOMEM;
63 /* TODO(shess) Delimiters need to remain the same from run to run,
64 ** else we need to reindex. One solution would be a meta-table to
65 ** track such information in the database, then we'd only want this
66 ** information on the initial create.
68 if( argc>1 ){
69 int i, n = strlen(argv[1]);
70 for(i=0; i<n; i++){
71 unsigned char ch = argv[1][i];
72 /* We explicitly don't support UTF-8 delimiters for now. */
73 if( ch>=0x80 ){
74 free(t);
75 return SQLITE_ERROR;
77 t->delim[ch] = 1;
79 } else {
80 /* Mark non-alphanumeric ASCII characters as delimiters */
81 int i;
82 for(i=1; i<0x80; i++){
83 t->delim[i] = !isalnum(i);
87 *ppTokenizer = &t->base;
88 return SQLITE_OK;
92 ** Destroy a tokenizer
94 static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
95 free(pTokenizer);
96 return SQLITE_OK;
100 ** Prepare to begin tokenizing a particular string. The input
101 ** string to be tokenized is pInput[0..nBytes-1]. A cursor
102 ** used to incrementally tokenize this string is returned in
103 ** *ppCursor.
105 static int simpleOpen(
106 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
107 const char *pInput, int nBytes, /* String to be tokenized */
108 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
110 simple_tokenizer_cursor *c;
112 c = (simple_tokenizer_cursor *) malloc(sizeof(*c));
113 if( c==NULL ) return SQLITE_NOMEM;
115 c->pInput = pInput;
116 if( pInput==0 ){
117 c->nBytes = 0;
118 }else if( nBytes<0 ){
119 c->nBytes = (int)strlen(pInput);
120 }else{
121 c->nBytes = nBytes;
123 c->iOffset = 0; /* start tokenizing at the beginning */
124 c->iToken = 0;
125 c->pToken = NULL; /* no space allocated, yet. */
126 c->nTokenAllocated = 0;
128 *ppCursor = &c->base;
129 return SQLITE_OK;
133 ** Close a tokenization cursor previously opened by a call to
134 ** simpleOpen() above.
136 static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
137 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
138 free(c->pToken);
139 free(c);
140 return SQLITE_OK;
144 ** Extract the next token from a tokenization cursor. The cursor must
145 ** have been opened by a prior call to simpleOpen().
147 static int simpleNext(
148 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
149 const char **ppToken, /* OUT: *ppToken is the token text */
150 int *pnBytes, /* OUT: Number of bytes in token */
151 int *piStartOffset, /* OUT: Starting offset of token */
152 int *piEndOffset, /* OUT: Ending offset of token */
153 int *piPosition /* OUT: Position integer of token */
155 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
156 simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
157 unsigned char *p = (unsigned char *)c->pInput;
159 while( c->iOffset<c->nBytes ){
160 int iStartOffset;
162 /* Scan past delimiter characters */
163 while( c->iOffset<c->nBytes && isDelim(t, p[c->iOffset]) ){
164 c->iOffset++;
167 /* Count non-delimiter characters. */
168 iStartOffset = c->iOffset;
169 while( c->iOffset<c->nBytes && !isDelim(t, p[c->iOffset]) ){
170 c->iOffset++;
173 if( c->iOffset>iStartOffset ){
174 int i, n = c->iOffset-iStartOffset;
175 if( n>c->nTokenAllocated ){
176 c->nTokenAllocated = n+20;
177 c->pToken = realloc(c->pToken, c->nTokenAllocated);
178 if( c->pToken==NULL ) return SQLITE_NOMEM;
180 for(i=0; i<n; i++){
181 /* TODO(shess) This needs expansion to handle UTF-8
182 ** case-insensitivity.
184 unsigned char ch = p[iStartOffset+i];
185 c->pToken[i] = ch<0x80 ? tolower(ch) : ch;
187 *ppToken = c->pToken;
188 *pnBytes = n;
189 *piStartOffset = iStartOffset;
190 *piEndOffset = c->iOffset;
191 *piPosition = c->iToken++;
193 return SQLITE_OK;
196 return SQLITE_DONE;
200 ** The set of routines that implement the simple tokenizer
202 static const sqlite3_tokenizer_module simpleTokenizerModule = {
204 simpleCreate,
205 simpleDestroy,
206 simpleOpen,
207 simpleClose,
208 simpleNext,
212 ** Allocate a new simple tokenizer. Return a pointer to the new
213 ** tokenizer in *ppModule
215 void sqlite3Fts1SimpleTokenizerModule(
216 sqlite3_tokenizer_module const**ppModule
218 *ppModule = &simpleTokenizerModule;
221 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1) */