|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "fts3Int.h" |
|
|
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
|
|
|
|
|
#include <assert.h> |
|
|
#include <stdlib.h> |
|
|
#include <stdio.h> |
|
|
#include <string.h> |
|
|
|
|
|
#include "fts3_tokenizer.h" |
|
|
|
|
|
typedef struct simple_tokenizer { |
|
|
sqlite3_tokenizer base; |
|
|
char delim[128]; |
|
|
} simple_tokenizer; |
|
|
|
|
|
typedef struct simple_tokenizer_cursor { |
|
|
sqlite3_tokenizer_cursor base; |
|
|
const char *pInput; |
|
|
int nBytes; |
|
|
int iOffset; |
|
|
int iToken; |
|
|
char *pToken; |
|
|
int nTokenAllocated; |
|
|
} simple_tokenizer_cursor; |
|
|
|
|
|
|
|
|
static int simpleDelim(simple_tokenizer *t, unsigned char c){ |
|
|
return c<0x80 && t->delim[c]; |
|
|
} |
|
|
static int fts3_isalnum(int x){ |
|
|
return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z'); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int simpleCreate( |
|
|
int argc, const char * const *argv, |
|
|
sqlite3_tokenizer **ppTokenizer |
|
|
){ |
|
|
simple_tokenizer *t; |
|
|
|
|
|
t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t)); |
|
|
if( t==NULL ) return SQLITE_NOMEM; |
|
|
memset(t, 0, sizeof(*t)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( argc>1 ){ |
|
|
int i, n = (int)strlen(argv[1]); |
|
|
for(i=0; i<n; i++){ |
|
|
unsigned char ch = argv[1][i]; |
|
|
|
|
|
if( ch>=0x80 ){ |
|
|
sqlite3_free(t); |
|
|
return SQLITE_ERROR; |
|
|
} |
|
|
t->delim[ch] = 1; |
|
|
} |
|
|
} else { |
|
|
|
|
|
int i; |
|
|
for(i=1; i<0x80; i++){ |
|
|
t->delim[i] = !fts3_isalnum(i) ? -1 : 0; |
|
|
} |
|
|
} |
|
|
|
|
|
*ppTokenizer = &t->base; |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int simpleDestroy(sqlite3_tokenizer *pTokenizer){ |
|
|
sqlite3_free(pTokenizer); |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int simpleOpen( |
|
|
sqlite3_tokenizer *pTokenizer, |
|
|
const char *pInput, int nBytes, |
|
|
sqlite3_tokenizer_cursor **ppCursor |
|
|
){ |
|
|
simple_tokenizer_cursor *c; |
|
|
|
|
|
UNUSED_PARAMETER(pTokenizer); |
|
|
|
|
|
c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c)); |
|
|
if( c==NULL ) return SQLITE_NOMEM; |
|
|
|
|
|
c->pInput = pInput; |
|
|
if( pInput==0 ){ |
|
|
c->nBytes = 0; |
|
|
}else if( nBytes<0 ){ |
|
|
c->nBytes = (int)strlen(pInput); |
|
|
}else{ |
|
|
c->nBytes = nBytes; |
|
|
} |
|
|
c->iOffset = 0; |
|
|
c->iToken = 0; |
|
|
c->pToken = NULL; |
|
|
c->nTokenAllocated = 0; |
|
|
|
|
|
*ppCursor = &c->base; |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int simpleClose(sqlite3_tokenizer_cursor *pCursor){ |
|
|
simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; |
|
|
sqlite3_free(c->pToken); |
|
|
sqlite3_free(c); |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int simpleNext( |
|
|
sqlite3_tokenizer_cursor *pCursor, |
|
|
const char **ppToken, |
|
|
int *pnBytes, |
|
|
int *piStartOffset, |
|
|
int *piEndOffset, |
|
|
int *piPosition |
|
|
){ |
|
|
simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; |
|
|
simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer; |
|
|
unsigned char *p = (unsigned char *)c->pInput; |
|
|
|
|
|
while( c->iOffset<c->nBytes ){ |
|
|
int iStartOffset; |
|
|
|
|
|
|
|
|
while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){ |
|
|
c->iOffset++; |
|
|
} |
|
|
|
|
|
|
|
|
iStartOffset = c->iOffset; |
|
|
while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){ |
|
|
c->iOffset++; |
|
|
} |
|
|
|
|
|
if( c->iOffset>iStartOffset ){ |
|
|
int i, n = c->iOffset-iStartOffset; |
|
|
if( n>c->nTokenAllocated ){ |
|
|
char *pNew; |
|
|
c->nTokenAllocated = n+20; |
|
|
pNew = sqlite3_realloc64(c->pToken, c->nTokenAllocated); |
|
|
if( !pNew ) return SQLITE_NOMEM; |
|
|
c->pToken = pNew; |
|
|
} |
|
|
for(i=0; i<n; i++){ |
|
|
|
|
|
|
|
|
|
|
|
unsigned char ch = p[iStartOffset+i]; |
|
|
c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch); |
|
|
} |
|
|
*ppToken = c->pToken; |
|
|
*pnBytes = n; |
|
|
*piStartOffset = iStartOffset; |
|
|
*piEndOffset = c->iOffset; |
|
|
*piPosition = c->iToken++; |
|
|
|
|
|
return SQLITE_OK; |
|
|
} |
|
|
} |
|
|
return SQLITE_DONE; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const sqlite3_tokenizer_module simpleTokenizerModule = { |
|
|
0, |
|
|
simpleCreate, |
|
|
simpleDestroy, |
|
|
simpleOpen, |
|
|
simpleClose, |
|
|
simpleNext, |
|
|
0, |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts3SimpleTokenizerModule( |
|
|
sqlite3_tokenizer_module const**ppModule |
|
|
){ |
|
|
*ppModule = &simpleTokenizerModule; |
|
|
} |
|
|
|
|
|
#endif |
|
|
|