Added the nextchar.c extension. Minor changes to the spellfix.c extension

so that it can be appended to an amalgamation and compiled without duplicating
symbols.

FossilOrigin-Name: 56b9a417f5451631f11c5206d625f11472ee65f9
This commit is contained in:
drh 2013-04-25 19:31:33 +00:00
parent e67f418d10
commit ea41dc44c5
9 changed files with 336 additions and 41 deletions

View File

@ -392,6 +392,7 @@ TESTSRC += \
$(TOP)/ext/misc/closure.c \
$(TOP)/ext/misc/fuzzer.c \
$(TOP)/ext/misc/ieee754.c \
$(TOP)/ext/misc/nextchar.c \
$(TOP)/ext/misc/regexp.c \
$(TOP)/ext/misc/spellfix.c \
$(TOP)/ext/misc/wholenumber.c

View File

@ -712,6 +712,7 @@ TESTEXT = \
$(TOP)\ext\misc\closure.c \
$(TOP)\ext\misc\fuzzer.c \
$(TOP)\ext\misc\ieee754.c \
$(TOP)\ext\misc\nextchar.c \
$(TOP)\ext\misc\regexp.c \
$(TOP)\ext\misc\spellfix.c \
$(TOP)\ext\misc\wholenumber.c

265
ext/misc/nextchar.c Normal file
View File

@ -0,0 +1,265 @@
/*
** 2013-02-28
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
******************************************************************************
**
** This file contains code to implement the next_char(A,T,F,W) SQL function.
**
** The next_char(A,T,F,H) function finds all valid "next" characters for
** string A given the vocabulary in T.F. The T.F field should be indexed.
** If the W value exists and is a non-empty string, then it is an SQL
** expression that limits the entries in T.F that will be considered.
**
** For example, suppose an application has a dictionary like this:
**
** CREATE TABLE dictionary(word TEXT UNIQUE);
**
** Further suppose that for user keypad entry, it is desired to disable
** (gray out) keys that are not valid as the next character. If the
** the user has previously entered (say) 'cha' then to find all allowed
** next characters (and thereby determine when keys should not be grayed
** out) run the following query:
**
** SELECT next_char('cha','dictionary','word');
*/
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <string.h>
/*
** A structure to hold context of the next_char() computation across
** nested function calls.
*/
typedef struct nextCharContext nextCharContext;
struct nextCharContext {
sqlite3 *db; /* Database connection */
sqlite3_stmt *pStmt; /* Prepared statement used to query */
const unsigned char *zPrefix; /* Prefix to scan */
int nPrefix; /* Size of zPrefix in bytes */
int nAlloc; /* Space allocated to aResult */
int nUsed; /* Space used in aResult */
unsigned int *aResult; /* Array of next characters */
int mallocFailed; /* True if malloc fails */
int otherError; /* True for any other failure */
};
/*
** Append a result character if the character is not already in the
** result.
*/
static void nextCharAppend(nextCharContext *p, unsigned c){
int i;
for(i=0; i<p->nUsed; i++){
if( p->aResult[i]==c ) return;
}
if( p->nUsed+1 > p->nAlloc ){
unsigned int *aNew;
int n = p->nAlloc*2 + 30;
aNew = sqlite3_realloc(p->aResult, n*sizeof(unsigned int));
if( aNew==0 ){
p->mallocFailed = 1;
return;
}else{
p->aResult = aNew;
p->nAlloc = n;
}
}
p->aResult[p->nUsed++] = c;
}
/*
** Write a character into z[] as UTF8. Return the number of bytes needed
** to hold the character
*/
static int writeUtf8(unsigned char *z, unsigned c){
if( c<0x00080 ){
z[0] = (unsigned char)(c&0xff);
return 1;
}
if( c<0x00800 ){
z[0] = 0xC0 + (unsigned char)((c>>6)&0x1F);
z[1] = 0x80 + (unsigned char)(c & 0x3F);
return 2;
}
if( c<0x10000 ){
z[0] = 0xE0 + (unsigned char)((c>>12)&0x0F);
z[1] = 0x80 + (unsigned char)((c>>6) & 0x3F);
z[2] = 0x80 + (unsigned char)(c & 0x3F);
return 3;
}
z[0] = 0xF0 + (unsigned char)((c>>18) & 0x07);
z[1] = 0x80 + (unsigned char)((c>>12) & 0x3F);
z[2] = 0x80 + (unsigned char)((c>>6) & 0x3F);
z[3] = 0x80 + (unsigned char)(c & 0x3F);
return 4;
}
/*
** Read a UTF8 character out of z[] and write it into *pOut. Return
** the number of bytes in z[] that were used to construct the character.
*/
static int readUtf8(const unsigned char *z, unsigned *pOut){
static const unsigned char validBits[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
};
unsigned c = z[0];
if( c<0xc0 ){
*pOut = c;
return 1;
}else{
int n = 1;
c = validBits[c-0xc0];
while( (z[n] & 0xc0)==0x80 ){
c = (c<<6) + (0x3f & z[n++]);
}
if( c<0x80 || (c&0xFFFFF800)==0xD800 || (c&0xFFFFFFFE)==0xFFFE ){
c = 0xFFFD;
}
*pOut = c;
return n;
}
}
/*
** The nextCharContext structure has been set up. Add all "next" characters
** to the result set.
*/
static void findNextChars(nextCharContext *p){
unsigned cPrev = 0;
unsigned char zPrev[8];
int n, rc;
for(;;){
sqlite3_bind_text(p->pStmt, 1, (char*)p->zPrefix, p->nPrefix,
SQLITE_STATIC);
n = writeUtf8(zPrev, cPrev+1);
sqlite3_bind_text(p->pStmt, 2, (char*)zPrev, n, SQLITE_STATIC);
rc = sqlite3_step(p->pStmt);
if( rc==SQLITE_DONE ){
sqlite3_reset(p->pStmt);
return;
}else if( rc!=SQLITE_ROW ){
p->otherError = rc;
return;
}else{
const unsigned char *zOut = sqlite3_column_text(p->pStmt, 0);
unsigned cNext;
n = readUtf8(zOut+p->nPrefix, &cNext);
sqlite3_reset(p->pStmt);
nextCharAppend(p, cNext);
cPrev = cNext;
if( p->mallocFailed ) return;
}
}
}
/*
** next_character(A,T,F,W)
**
** Return a string composted of all next possible characters after
** A for elements of T.F. If W is supplied, then it is an SQL expression
** that limits the elements in T.F that are considered.
*/
static void nextCharFunc(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
nextCharContext c;
const unsigned char *zTable = sqlite3_value_text(argv[1]);
const unsigned char *zField = sqlite3_value_text(argv[2]);
const unsigned char *zWhere;
char *zSql;
int rc;
memset(&c, 0, sizeof(c));
c.db = sqlite3_context_db_handle(context);
c.zPrefix = sqlite3_value_text(argv[0]);
c.nPrefix = sqlite3_value_bytes(argv[0]);
if( zTable==0 || zField==0 || c.zPrefix==0 ) return;
if( argc<4
|| (zWhere = sqlite3_value_text(argv[3]))==0
|| zWhere[0]==0
){
zSql = sqlite3_mprintf(
"SELECT \"%w\" FROM \"%w\""
" WHERE \"%w\">=(?1 || ?2)"
" AND \"%w\"<=(?1 || char(1114111))" /* 1114111 == 0x10ffff */
" ORDER BY 1 ASC LIMIT 1",
zField, zTable, zField, zField);
}else{
zSql = sqlite3_mprintf(
"SELECT \"%w\" FROM \"%w\""
" WHERE \"%w\">=(?1 || ?2)"
" AND \"%w\"<=(?1 || char(1114111))" /* 1114111 == 0x10ffff */
" AND (%s)"
" ORDER BY 1 ASC LIMIT 1",
zField, zTable, zField, zField, zWhere);
}
if( zSql==0 ){
sqlite3_result_error_nomem(context);
return;
}
rc = sqlite3_prepare_v2(c.db, zSql, -1, &c.pStmt, 0);
sqlite3_free(zSql);
if( rc ){
sqlite3_result_error(context, sqlite3_errmsg(c.db), -1);
return;
}
findNextChars(&c);
if( c.mallocFailed ){
sqlite3_result_error_nomem(context);
}else{
unsigned char *pRes;
pRes = sqlite3_malloc( c.nUsed*4 + 1 );
if( pRes==0 ){
sqlite3_result_error_nomem(context);
}else{
int i;
int n = 0;
for(i=0; i<c.nUsed; i++){
n += writeUtf8(pRes+n, c.aResult[i]);
}
pRes[n] = 0;
sqlite3_result_text(context, (const char*)pRes, n, sqlite3_free);
}
}
sqlite3_finalize(c.pStmt);
sqlite3_free(c.aResult);
}
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_nextchar_init(
sqlite3 *db,
char **pzErrMsg,
const sqlite3_api_routines *pApi
){
int rc = SQLITE_OK;
SQLITE_EXTENSION_INIT2(pApi);
(void)pzErrMsg; /* Unused parameter */
rc = sqlite3_create_function(db, "next_char", 3, SQLITE_UTF8, 0,
nextCharFunc, 0, 0);
if( rc==SQLITE_OK ){
rc = sqlite3_create_function(db, "next_char", 4, SQLITE_UTF8, 0,
nextCharFunc, 0, 0);
}
return rc;
}

View File

@ -17,15 +17,17 @@
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define ALWAYS(X) 1
#define NEVER(X) 0
typedef unsigned char u8;
typedef unsigned short u16;
#include <ctype.h>
#ifndef SQLITE_AMALGAMATION
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
# define ALWAYS(X) 1
# define NEVER(X) 0
typedef unsigned char u8;
typedef unsigned short u16;
# include <ctype.h>
#endif
/*
** Character classes for ASCII characters:

View File

@ -274,6 +274,7 @@ TESTSRC += \
$(TOP)/ext/misc/closure.c \
$(TOP)/ext/misc/fuzzer.c \
$(TOP)/ext/misc/ieee754.c \
$(TOP)/ext/misc/nextchar.c \
$(TOP)/ext/misc/regexp.c \
$(TOP)/ext/misc/spellfix.c \
$(TOP)/ext/misc/wholenumber.c

View File

@ -1,9 +1,9 @@
C Fix\sthe\stool/build-shell.sh\sscript\sto\sremove\sreferences\sto\sfiles\sthat\sare\nnow\sloadable\sextensions.
D 2013-04-25T17:27:08.281
C Added\sthe\snextchar.c\sextension.\s\sMinor\schanges\sto\sthe\sspellfix.c\sextension\nso\sthat\sit\scan\sbe\sappended\sto\san\samalgamation\sand\scompiled\swithout\sduplicating\nsymbols.
D 2013-04-25T19:31:33.149
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 38a45083d4df568eefd31d27d67381850f35a016
F Makefile.in ce81671efd6223d19d4c8c6b88ac2c4134427111
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
F Makefile.msc d03cde9073d5752d6ddcaa6807a8bdda277e48f2
F Makefile.msc 8f4ee0dab220a5276d5da61149dfd6cd5d1dd5b8
F Makefile.vxworks db21ed42a01d5740e656b16f92cb5d8d5e5dd315
F README cd04a36fbc7ea56932a4052d7d0b7f09f27c33d6
F VERSION 05c7bd63b96f31cfdef5c766ed91307ac121f5aa
@ -88,8 +88,9 @@ F ext/misc/closure.c fec0c8537c69843e0b7631d500a14c0527962cd6
F ext/misc/editdist3.wiki 06100a0c558921a563cbc40e0d0151902b1eef6d
F ext/misc/fuzzer.c fb64a15af978ae73fa9075b9b1dfbe82b8defc6f
F ext/misc/ieee754.c 2565ce373d842977efe0922dc50b8a41b3289556
F ext/misc/nextchar.c 1131e2b36116ffc6fe6b2e3464bfdace27978b1e
F ext/misc/regexp.c c25c65fe775f5d9801fb8573e36ebe73f2c0c2e0
F ext/misc/spellfix.c 8bb699116e36cc5e68d7ddf1810b638a3090c744
F ext/misc/spellfix.c e323eebb877d735bc64404c16a6d758ab17a0b7a
F ext/misc/spellfix1.wiki dd1830444c14cf0f54dd680cc044df2ace2e9d09
F ext/misc/wholenumber.c ce362368b9381ea48cbd951ade8df867eeeab014
F ext/rtree/README 6315c0d73ebf0ec40dedb5aa0e942bc8b54e3761
@ -113,7 +114,7 @@ F ext/rtree/tkt3363.test 142ab96eded44a3615ec79fba98c7bde7d0f96de
F ext/rtree/viewrtree.tcl eea6224b3553599ae665b239bd827e182b466024
F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895 x
F ltmain.sh 3ff0879076df340d2e23ae905484d8c15d5fdea8
F main.mk 9535835509ac58a1902ba85ed77bcce9840d62d4
F main.mk 1b25be82452366abc27cc9ab2acf3244a773d5a1
F mkdll.sh 7d09b23c05d56532e9d44a50868eb4b12ff4f74a
F mkextu.sh 416f9b7089d80e5590a29692c9d9280a10dbad9f
F mkextw.sh 4123480947681d9b434a5e7b1ee08135abe409ac
@ -200,7 +201,7 @@ F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d
F src/status.c bedc37ec1a6bb9399944024d63f4c769971955a9
F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e
F src/tclsqlite.c 2ecec9937e69bc17560ad886da35195daa7261b8
F src/test1.c e9562428421bf0cdbf11bf5f60ab0cdabee45885
F src/test1.c 2b0ec224983403312a4d1db8546e1e1c45694251
F src/test2.c 29e7154112f7448d64204e8d31179cf497ecf425
F src/test3.c 96aed72a8e1d542fed127e3e8350ae357712fa82
F src/test4.c cea2c55110241e4674e66d476d29c914627999f5
@ -756,7 +757,7 @@ F test/speed3.test d32043614c08c53eafdc80f33191d5bd9b920523
F test/speed4.test abc0ad3399dcf9703abed2fff8705e4f8e416715
F test/speed4p.explain 6b5f104ebeb34a038b2f714150f51d01143e59aa
F test/speed4p.test 0e51908951677de5a969b723e03a27a1c45db38b
F test/spellfix.test a85915ab25af7fcfb0d99cb1951e6ef15e26202c
F test/spellfix.test bea537caf587df30d430c2c6a8fe9f64b8712834
F test/sqllimits1.test b1aae27cc98eceb845e7f7adf918561256e31298
F test/stat.test be8d477306006ec696bc86757cfb34bec79447ce
F test/stmt.test 25d64e3dbf9a3ce89558667d7f39d966fe2a71b9
@ -1060,7 +1061,7 @@ F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
P 381564e91bbf619f99a48b0b7a94ac586cb9ee79
R dfc5d9e4a45d231ac8d29e1b5d10bc7e
P aabeea98f53edde68f484f1794ae70789dac3889
R 77b136ceb7e3bfd54c75649b938a3e12
U drh
Z 8c5379cfdd83f28d6a14d88089c63be6
Z fb4502a56cfa201ec6c890343c386675

View File

@ -1 +1 @@
aabeea98f53edde68f484f1794ae70789dac3889
56b9a417f5451631f11c5206d625f11472ee65f9

View File

@ -6047,9 +6047,9 @@ static int optimization_control(
typedef struct sqlite3_api_routines sqlite3_api_routines;
/*
** load_static_extension DB NAME
** load_static_extension DB NAME ...
**
** Load an extension that is statically linked.
** Load one or more statically linked extensions.
*/
static int tclLoadStaticExtensionCmd(
void * clientData,
@ -6061,6 +6061,7 @@ static int tclLoadStaticExtensionCmd(
extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*);
extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*);
extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*);
extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*);
extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*);
extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*);
extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*);
@ -6072,33 +6073,36 @@ static int tclLoadStaticExtensionCmd(
{ "closure", sqlite3_closure_init },
{ "fuzzer", sqlite3_fuzzer_init },
{ "ieee754", sqlite3_ieee_init },
{ "nextchar", sqlite3_nextchar_init },
{ "regexp", sqlite3_regexp_init },
{ "spellfix", sqlite3_spellfix_init },
{ "wholenumber", sqlite3_wholenumber_init },
};
sqlite3 *db;
const char *zName;
int i, rc;
int i, j, rc;
char *zErrMsg = 0;
if( objc!=3 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB NAME");
if( objc<3 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ...");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
zName = Tcl_GetString(objv[2]);
for(i=0; i<ArraySize(aExtension); i++){
if( strcmp(zName, aExtension[i].zExtName)==0 ) break;
}
if( i>=ArraySize(aExtension) ){
Tcl_AppendResult(interp, "no such extension: ", zName, (char*)0);
return TCL_ERROR;
}
rc = aExtension[i].pInit(db, &zErrMsg, 0);
if( rc!=SQLITE_OK || zErrMsg ){
Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg,
(char*)0);
sqlite3_free(zErrMsg);
return TCL_ERROR;
for(j=2; j<objc; j++){
zName = Tcl_GetString(objv[j]);
for(i=0; i<ArraySize(aExtension); i++){
if( strcmp(zName, aExtension[i].zExtName)==0 ) break;
}
if( i>=ArraySize(aExtension) ){
Tcl_AppendResult(interp, "no such extension: ", zName, (char*)0);
return TCL_ERROR;
}
rc = aExtension[i].pInit(db, &zErrMsg, 0);
if( rc!=SQLITE_OK || zErrMsg ){
Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg,
(char*)0);
sqlite3_free(zErrMsg);
return TCL_ERROR;
}
}
return TCL_OK;
}

View File

@ -16,7 +16,7 @@ set testprefix spellfix
ifcapable !vtab { finish_test ; return }
load_static_extension db spellfix
load_static_extension db spellfix nextchar
set vocab {
rabbi rabbit rabbits rabble rabid rabies raccoon raccoons race raced racer
@ -84,6 +84,26 @@ foreach {tn word res} {
} $res
}
# Tests of the next_char function.
#
do_test 1.10 {
db eval {
CREATE TABLE vocab(w TEXT PRIMARY KEY);
INSERT INTO vocab SELECT word FROM t1;
}
} {}
do_execsql_test 1.11 {
SELECT next_char('re','vocab','w');
} {a}
do_execsql_test 1.12 {
SELECT next_char('r','vocab','w');
} {ae}
do_execsql_test 1.13 {
SELECT next_char('','vocab','w');
} {r}
do_test 1.14 {
catchsql {SELECT next_char('','xyzzy','a')}
} {1 {no such table: xyzzy}}
do_execsql_test 2.1 {
CREATE VIRTUAL TABLE t2 USING spellfix1;