nextchar.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. ** 2013-02-28
  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. ** This file contains code to implement the next_char(A,T,F,W,C) SQL function.
  14. **
  15. ** The next_char(A,T,F,W,C) function finds all valid "next" characters for
  16. ** string A given the vocabulary in T.F. If the W value exists and is a
  17. ** non-empty string, then it is an SQL expression that limits the entries
  18. ** in T.F that will be considered. If C exists and is a non-empty string,
  19. ** then it is the name of the collating sequence to use for comparison. If
  20. **
  21. ** Only the first three arguments are required. If the C parameter is
  22. ** omitted or is NULL or is an empty string, then the default collating
  23. ** sequence of T.F is used for comparision. If the W parameter is omitted
  24. ** or is NULL or is an empty string, then no filtering of the output is
  25. ** done.
  26. **
  27. ** The T.F column should be indexed using collation C or else this routine
  28. ** will be quite slow.
  29. **
  30. ** For example, suppose an application has a dictionary like this:
  31. **
  32. ** CREATE TABLE dictionary(word TEXT UNIQUE);
  33. **
  34. ** Further suppose that for user keypad entry, it is desired to disable
  35. ** (gray out) keys that are not valid as the next character. If the
  36. ** the user has previously entered (say) 'cha' then to find all allowed
  37. ** next characters (and thereby determine when keys should not be grayed
  38. ** out) run the following query:
  39. **
  40. ** SELECT next_char('cha','dictionary','word');
  41. **
  42. ** IMPLEMENTATION NOTES:
  43. **
  44. ** The next_char function is implemented using recursive SQL that makes
  45. ** use of the table name and column name as part of a query. If either
  46. ** the table name or column name are keywords or contain special characters,
  47. ** then they should be escaped. For example:
  48. **
  49. ** SELECT next_char('cha','[dictionary]','[word]');
  50. **
  51. ** This also means that the table name can be a subquery:
  52. **
  53. ** SELECT next_char('cha','(SELECT word AS w FROM dictionary)','w');
  54. */
  55. #include "sqlite3ext.h"
  56. SQLITE_EXTENSION_INIT1
  57. #include <string.h>
  58. /*
  59. ** A structure to hold context of the next_char() computation across
  60. ** nested function calls.
  61. */
  62. typedef struct nextCharContext nextCharContext;
  63. struct nextCharContext {
  64. sqlite3 *db; /* Database connection */
  65. sqlite3_stmt *pStmt; /* Prepared statement used to query */
  66. const unsigned char *zPrefix; /* Prefix to scan */
  67. int nPrefix; /* Size of zPrefix in bytes */
  68. int nAlloc; /* Space allocated to aResult */
  69. int nUsed; /* Space used in aResult */
  70. unsigned int *aResult; /* Array of next characters */
  71. int mallocFailed; /* True if malloc fails */
  72. int otherError; /* True for any other failure */
  73. };
  74. /*
  75. ** Append a result character if the character is not already in the
  76. ** result.
  77. */
  78. static void nextCharAppend(nextCharContext *p, unsigned c){
  79. int i;
  80. for(i=0; i<p->nUsed; i++){
  81. if( p->aResult[i]==c ) return;
  82. }
  83. if( p->nUsed+1 > p->nAlloc ){
  84. unsigned int *aNew;
  85. int n = p->nAlloc*2 + 30;
  86. aNew = sqlite3_realloc(p->aResult, n*sizeof(unsigned int));
  87. if( aNew==0 ){
  88. p->mallocFailed = 1;
  89. return;
  90. }else{
  91. p->aResult = aNew;
  92. p->nAlloc = n;
  93. }
  94. }
  95. p->aResult[p->nUsed++] = c;
  96. }
  97. /*
  98. ** Write a character into z[] as UTF8. Return the number of bytes needed
  99. ** to hold the character
  100. */
  101. static int writeUtf8(unsigned char *z, unsigned c){
  102. if( c<0x00080 ){
  103. z[0] = (unsigned char)(c&0xff);
  104. return 1;
  105. }
  106. if( c<0x00800 ){
  107. z[0] = 0xC0 + (unsigned char)((c>>6)&0x1F);
  108. z[1] = 0x80 + (unsigned char)(c & 0x3F);
  109. return 2;
  110. }
  111. if( c<0x10000 ){
  112. z[0] = 0xE0 + (unsigned char)((c>>12)&0x0F);
  113. z[1] = 0x80 + (unsigned char)((c>>6) & 0x3F);
  114. z[2] = 0x80 + (unsigned char)(c & 0x3F);
  115. return 3;
  116. }
  117. z[0] = 0xF0 + (unsigned char)((c>>18) & 0x07);
  118. z[1] = 0x80 + (unsigned char)((c>>12) & 0x3F);
  119. z[2] = 0x80 + (unsigned char)((c>>6) & 0x3F);
  120. z[3] = 0x80 + (unsigned char)(c & 0x3F);
  121. return 4;
  122. }
  123. /*
  124. ** Read a UTF8 character out of z[] and write it into *pOut. Return
  125. ** the number of bytes in z[] that were used to construct the character.
  126. */
  127. static int readUtf8(const unsigned char *z, unsigned *pOut){
  128. static const unsigned char validBits[] = {
  129. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  130. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  131. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  132. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  133. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  134. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  135. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  136. 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
  137. };
  138. unsigned c = z[0];
  139. if( c<0xc0 ){
  140. *pOut = c;
  141. return 1;
  142. }else{
  143. int n = 1;
  144. c = validBits[c-0xc0];
  145. while( (z[n] & 0xc0)==0x80 ){
  146. c = (c<<6) + (0x3f & z[n++]);
  147. }
  148. if( c<0x80 || (c&0xFFFFF800)==0xD800 || (c&0xFFFFFFFE)==0xFFFE ){
  149. c = 0xFFFD;
  150. }
  151. *pOut = c;
  152. return n;
  153. }
  154. }
  155. /*
  156. ** The nextCharContext structure has been set up. Add all "next" characters
  157. ** to the result set.
  158. */
  159. static void findNextChars(nextCharContext *p){
  160. unsigned cPrev = 0;
  161. unsigned char zPrev[8];
  162. int n, rc;
  163. for(;;){
  164. sqlite3_bind_text(p->pStmt, 1, (char*)p->zPrefix, p->nPrefix,
  165. SQLITE_STATIC);
  166. n = writeUtf8(zPrev, cPrev+1);
  167. sqlite3_bind_text(p->pStmt, 2, (char*)zPrev, n, SQLITE_STATIC);
  168. rc = sqlite3_step(p->pStmt);
  169. if( rc==SQLITE_DONE ){
  170. sqlite3_reset(p->pStmt);
  171. return;
  172. }else if( rc!=SQLITE_ROW ){
  173. p->otherError = rc;
  174. return;
  175. }else{
  176. const unsigned char *zOut = sqlite3_column_text(p->pStmt, 0);
  177. unsigned cNext;
  178. n = readUtf8(zOut+p->nPrefix, &cNext);
  179. sqlite3_reset(p->pStmt);
  180. nextCharAppend(p, cNext);
  181. cPrev = cNext;
  182. if( p->mallocFailed ) return;
  183. }
  184. }
  185. }
  186. /*
  187. ** next_character(A,T,F,W)
  188. **
  189. ** Return a string composted of all next possible characters after
  190. ** A for elements of T.F. If W is supplied, then it is an SQL expression
  191. ** that limits the elements in T.F that are considered.
  192. */
  193. static void nextCharFunc(
  194. sqlite3_context *context,
  195. int argc,
  196. sqlite3_value **argv
  197. ){
  198. nextCharContext c;
  199. const unsigned char *zTable = sqlite3_value_text(argv[1]);
  200. const unsigned char *zField = sqlite3_value_text(argv[2]);
  201. const unsigned char *zWhere;
  202. const unsigned char *zCollName;
  203. char *zWhereClause = 0;
  204. char *zColl = 0;
  205. char *zSql;
  206. int rc;
  207. memset(&c, 0, sizeof(c));
  208. c.db = sqlite3_context_db_handle(context);
  209. c.zPrefix = sqlite3_value_text(argv[0]);
  210. c.nPrefix = sqlite3_value_bytes(argv[0]);
  211. if( zTable==0 || zField==0 || c.zPrefix==0 ) return;
  212. if( argc>=4
  213. && (zWhere = sqlite3_value_text(argv[3]))!=0
  214. && zWhere[0]!=0
  215. ){
  216. zWhereClause = sqlite3_mprintf("AND (%s)", zWhere);
  217. if( zWhereClause==0 ){
  218. sqlite3_result_error_nomem(context);
  219. return;
  220. }
  221. }else{
  222. zWhereClause = "";
  223. }
  224. if( argc>=5
  225. && (zCollName = sqlite3_value_text(argv[4]))!=0
  226. && zCollName[0]!=0
  227. ){
  228. zColl = sqlite3_mprintf("collate \"%w\"", zCollName);
  229. if( zColl==0 ){
  230. sqlite3_result_error_nomem(context);
  231. if( zWhereClause[0] ) sqlite3_free(zWhereClause);
  232. return;
  233. }
  234. }else{
  235. zColl = "";
  236. }
  237. zSql = sqlite3_mprintf(
  238. "SELECT %s FROM %s"
  239. " WHERE %s>=(?1 || ?2) %s"
  240. " AND %s<=(?1 || char(1114111)) %s" /* 1114111 == 0x10ffff */
  241. " %s"
  242. " ORDER BY 1 %s ASC LIMIT 1",
  243. zField, zTable, zField, zColl, zField, zColl, zWhereClause, zColl
  244. );
  245. if( zWhereClause[0] ) sqlite3_free(zWhereClause);
  246. if( zColl[0] ) sqlite3_free(zColl);
  247. if( zSql==0 ){
  248. sqlite3_result_error_nomem(context);
  249. return;
  250. }
  251. rc = sqlite3_prepare_v2(c.db, zSql, -1, &c.pStmt, 0);
  252. sqlite3_free(zSql);
  253. if( rc ){
  254. sqlite3_result_error(context, sqlite3_errmsg(c.db), -1);
  255. return;
  256. }
  257. findNextChars(&c);
  258. if( c.mallocFailed ){
  259. sqlite3_result_error_nomem(context);
  260. }else{
  261. unsigned char *pRes;
  262. pRes = sqlite3_malloc( c.nUsed*4 + 1 );
  263. if( pRes==0 ){
  264. sqlite3_result_error_nomem(context);
  265. }else{
  266. int i;
  267. int n = 0;
  268. for(i=0; i<c.nUsed; i++){
  269. n += writeUtf8(pRes+n, c.aResult[i]);
  270. }
  271. pRes[n] = 0;
  272. sqlite3_result_text(context, (const char*)pRes, n, sqlite3_free);
  273. }
  274. }
  275. sqlite3_finalize(c.pStmt);
  276. sqlite3_free(c.aResult);
  277. }
  278. #ifdef _WIN32
  279. __declspec(dllexport)
  280. #endif
  281. int sqlite3_nextchar_init(
  282. sqlite3 *db,
  283. char **pzErrMsg,
  284. const sqlite3_api_routines *pApi
  285. ){
  286. int rc = SQLITE_OK;
  287. SQLITE_EXTENSION_INIT2(pApi);
  288. (void)pzErrMsg; /* Unused parameter */
  289. rc = sqlite3_create_function(db, "next_char", 3, SQLITE_UTF8, 0,
  290. nextCharFunc, 0, 0);
  291. if( rc==SQLITE_OK ){
  292. rc = sqlite3_create_function(db, "next_char", 4, SQLITE_UTF8, 0,
  293. nextCharFunc, 0, 0);
  294. }
  295. if( rc==SQLITE_OK ){
  296. rc = sqlite3_create_function(db, "next_char", 5, SQLITE_UTF8, 0,
  297. nextCharFunc, 0, 0);
  298. }
  299. return rc;
  300. }