2 ** The author disclaims copyright to this source code.
4 *************************************************************************
5 ** Implementation of the "simple" full-text-search tokenizer.
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)
26 #include "fts1_tokenizer.h"
28 typedef struct simple_tokenizer
{
29 sqlite3_tokenizer base
;
30 char delim
[128]; /* flag ASCII delimiters */
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
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.
69 int i
, n
= strlen(argv
[1]);
71 unsigned char ch
= argv
[1][i
];
72 /* We explicitly don't support UTF-8 delimiters for now. */
80 /* Mark non-alphanumeric ASCII characters as delimiters */
82 for(i
=1; i
<0x80; i
++){
83 t
->delim
[i
] = !isalnum(i
);
87 *ppTokenizer
= &t
->base
;
92 ** Destroy a tokenizer
94 static int simpleDestroy(sqlite3_tokenizer
*pTokenizer
){
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
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
;
118 }else if( nBytes
<0 ){
119 c
->nBytes
= (int)strlen(pInput
);
123 c
->iOffset
= 0; /* start tokenizing at the beginning */
125 c
->pToken
= NULL
; /* no space allocated, yet. */
126 c
->nTokenAllocated
= 0;
128 *ppCursor
= &c
->base
;
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
;
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
){
162 /* Scan past delimiter characters */
163 while( c
->iOffset
<c
->nBytes
&& isDelim(t
, p
[c
->iOffset
]) ){
167 /* Count non-delimiter characters. */
168 iStartOffset
= c
->iOffset
;
169 while( c
->iOffset
<c
->nBytes
&& !isDelim(t
, p
[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
;
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
;
189 *piStartOffset
= iStartOffset
;
190 *piEndOffset
= c
->iOffset
;
191 *piPosition
= c
->iToken
++;
200 ** The set of routines that implement the simple tokenizer
202 static const sqlite3_tokenizer_module simpleTokenizerModule
= {
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) */