Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: third_party/sqlite/ext/icu/icu.c

Issue 3108030: Move bundled copy of sqlite one level deeper to better separate it... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/sqlite/ext/icu/README.txt ('k') | third_party/sqlite/ext/icu/sqliteicu.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 ** 2007 May 6
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 ** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
13 **
14 ** This file implements an integration between the ICU library
15 ** ("International Components for Unicode", an open-source library
16 ** for handling unicode data) and SQLite. The integration uses
17 ** ICU to provide the following to SQLite:
18 **
19 ** * An implementation of the SQL regexp() function (and hence REGEXP
20 ** operator) using the ICU uregex_XX() APIs.
21 **
22 ** * Implementations of the SQL scalar upper() and lower() functions
23 ** for case mapping.
24 **
25 ** * Integration of ICU and SQLite collation seqences.
26 **
27 ** * An implementation of the LIKE operator that uses ICU to
28 ** provide case-independent matching.
29 */
30
31 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
32
33 /* Include ICU headers */
34 #include <unicode/utypes.h>
35 #include <unicode/uregex.h>
36 #include <unicode/ustring.h>
37 #include <unicode/ucol.h>
38
39 #include <assert.h>
40
41 // TODO(evanm): this is cut'n'pasted from fts2.c. Why is it necessary?
42 #if !defined(SQLITE_CORE)
43 # define SQLITE_CORE 1
44 #endif
45
46 #ifndef SQLITE_CORE
47 #include "sqlite3ext.h"
48 SQLITE_EXTENSION_INIT1
49 #else
50 #include "sqlite3.h"
51 #endif
52
53 /*
54 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
55 ** operator.
56 */
57 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
58 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
59 #endif
60
61 /*
62 ** Version of sqlite3_free() that is always a function, never a macro.
63 */
64 static void xFree(void *p){
65 sqlite3_free(p);
66 }
67
68 /*
69 ** Compare two UTF-8 strings for equality where the first string is
70 ** a "LIKE" expression. Return true (1) if they are the same and
71 ** false (0) if they are different.
72 */
73 static int icuLikeCompare(
74 const uint8_t *zPattern, /* LIKE pattern */
75 const uint8_t *zString, /* The UTF-8 string to compare against */
76 const UChar32 uEsc /* The escape character */
77 ){
78 static const int MATCH_ONE = (UChar32)'_';
79 static const int MATCH_ALL = (UChar32)'%';
80
81 int iPattern = 0; /* Current byte index in zPattern */
82 int iString = 0; /* Current byte index in zString */
83
84 int prevEscape = 0; /* True if the previous character was uEsc */
85
86 while( zPattern[iPattern]!=0 ){
87
88 /* Read (and consume) the next character from the input pattern. */
89 UChar32 uPattern;
90 U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
91 assert(uPattern!=0);
92
93 /* There are now 4 possibilities:
94 **
95 ** 1. uPattern is an unescaped match-all character "%",
96 ** 2. uPattern is an unescaped match-one character "_",
97 ** 3. uPattern is an unescaped escape character, or
98 ** 4. uPattern is to be handled as an ordinary character
99 */
100 if( !prevEscape && uPattern==MATCH_ALL ){
101 /* Case 1. */
102 uint8_t c;
103
104 /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
105 ** MATCH_ALL. For each MATCH_ONE, skip one character in the
106 ** test string.
107 */
108 while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){
109 if( c==MATCH_ONE ){
110 if( zString[iString]==0 ) return 0;
111 U8_FWD_1_UNSAFE(zString, iString);
112 }
113 iPattern++;
114 }
115
116 if( zPattern[iPattern]==0 ) return 1;
117
118 while( zString[iString] ){
119 if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){
120 return 1;
121 }
122 U8_FWD_1_UNSAFE(zString, iString);
123 }
124 return 0;
125
126 }else if( !prevEscape && uPattern==MATCH_ONE ){
127 /* Case 2. */
128 if( zString[iString]==0 ) return 0;
129 U8_FWD_1_UNSAFE(zString, iString);
130
131 }else if( !prevEscape && uPattern==uEsc){
132 /* Case 3. */
133 prevEscape = 1;
134
135 }else{
136 /* Case 4. */
137 UChar32 uString;
138 U8_NEXT_UNSAFE(zString, iString, uString);
139 uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT);
140 uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT);
141 if( uString!=uPattern ){
142 return 0;
143 }
144 prevEscape = 0;
145 }
146 }
147
148 return zString[iString]==0;
149 }
150
151 /*
152 ** Implementation of the like() SQL function. This function implements
153 ** the build-in LIKE operator. The first argument to the function is the
154 ** pattern and the second argument is the string. So, the SQL statements:
155 **
156 ** A LIKE B
157 **
158 ** is implemented as like(B, A). If there is an escape character E,
159 **
160 ** A LIKE B ESCAPE E
161 **
162 ** is mapped to like(B, A, E).
163 */
164 static void icuLikeFunc(
165 sqlite3_context *context,
166 int argc,
167 sqlite3_value **argv
168 ){
169 const unsigned char *zA = sqlite3_value_text(argv[0]);
170 const unsigned char *zB = sqlite3_value_text(argv[1]);
171 UChar32 uEsc = 0;
172
173 /* Limit the length of the LIKE or GLOB pattern to avoid problems
174 ** of deep recursion and N*N behavior in patternCompare().
175 */
176 if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
177 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
178 return;
179 }
180
181
182 if( argc==3 ){
183 /* The escape character string must consist of a single UTF-8 character.
184 ** Otherwise, return an error.
185 */
186 int nE= sqlite3_value_bytes(argv[2]);
187 const unsigned char *zE = sqlite3_value_text(argv[2]);
188 int i = 0;
189 if( zE==0 ) return;
190 U8_NEXT(zE, i, nE, uEsc);
191 if( i!=nE){
192 sqlite3_result_error(context,
193 "ESCAPE expression must be a single character", -1);
194 return;
195 }
196 }
197
198 if( zA && zB ){
199 sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
200 }
201 }
202
203 /*
204 ** This function is called when an ICU function called from within
205 ** the implementation of an SQL scalar function returns an error.
206 **
207 ** The scalar function context passed as the first argument is
208 ** loaded with an error message based on the following two args.
209 */
210 static void icuFunctionError(
211 sqlite3_context *pCtx, /* SQLite scalar function context */
212 const char *zName, /* Name of ICU function that failed */
213 UErrorCode e /* Error code returned by ICU function */
214 ){
215 char zBuf[128];
216 sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
217 zBuf[127] = '\0';
218 sqlite3_result_error(pCtx, zBuf, -1);
219 }
220
221 /*
222 ** Function to delete compiled regexp objects. Registered as
223 ** a destructor function with sqlite3_set_auxdata().
224 */
225 static void icuRegexpDelete(void *p){
226 URegularExpression *pExpr = (URegularExpression *)p;
227 uregex_close(pExpr);
228 }
229
230 /*
231 ** Implementation of SQLite REGEXP operator. This scalar function takes
232 ** two arguments. The first is a regular expression pattern to compile
233 ** the second is a string to match against that pattern. If either
234 ** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
235 ** is 1 if the string matches the pattern, or 0 otherwise.
236 **
237 ** SQLite maps the regexp() function to the regexp() operator such
238 ** that the following two are equivalent:
239 **
240 ** zString REGEXP zPattern
241 ** regexp(zPattern, zString)
242 **
243 ** Uses the following ICU regexp APIs:
244 **
245 ** uregex_open()
246 ** uregex_matches()
247 ** uregex_close()
248 */
249 static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
250 UErrorCode status = U_ZERO_ERROR;
251 URegularExpression *pExpr;
252 UBool res;
253 const UChar *zString = sqlite3_value_text16(apArg[1]);
254
255 /* If the left hand side of the regexp operator is NULL,
256 ** then the result is also NULL.
257 */
258 if( !zString ){
259 return;
260 }
261
262 pExpr = sqlite3_get_auxdata(p, 0);
263 if( !pExpr ){
264 const UChar *zPattern = sqlite3_value_text16(apArg[0]);
265 if( !zPattern ){
266 return;
267 }
268 pExpr = uregex_open(zPattern, -1, 0, 0, &status);
269
270 if( U_SUCCESS(status) ){
271 sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
272 }else{
273 assert(!pExpr);
274 icuFunctionError(p, "uregex_open", status);
275 return;
276 }
277 }
278
279 /* Configure the text that the regular expression operates on. */
280 uregex_setText(pExpr, zString, -1, &status);
281 if( !U_SUCCESS(status) ){
282 icuFunctionError(p, "uregex_setText", status);
283 return;
284 }
285
286 /* Attempt the match */
287 res = uregex_matches(pExpr, 0, &status);
288 if( !U_SUCCESS(status) ){
289 icuFunctionError(p, "uregex_matches", status);
290 return;
291 }
292
293 /* Set the text that the regular expression operates on to a NULL
294 ** pointer. This is not really necessary, but it is tidier than
295 ** leaving the regular expression object configured with an invalid
296 ** pointer after this function returns.
297 */
298 uregex_setText(pExpr, 0, 0, &status);
299
300 /* Return 1 or 0. */
301 sqlite3_result_int(p, res ? 1 : 0);
302 }
303
304 /*
305 ** Implementations of scalar functions for case mapping - upper() and
306 ** lower(). Function upper() converts its input to upper-case (ABC).
307 ** Function lower() converts to lower-case (abc).
308 **
309 ** ICU provides two types of case mapping, "general" case mapping and
310 ** "language specific". Refer to ICU documentation for the differences
311 ** between the two.
312 **
313 ** To utilise "general" case mapping, the upper() or lower() scalar
314 ** functions are invoked with one argument:
315 **
316 ** upper('ABC') -> 'abc'
317 ** lower('abc') -> 'ABC'
318 **
319 ** To access ICU "language specific" case mapping, upper() or lower()
320 ** should be invoked with two arguments. The second argument is the name
321 ** of the locale to use. Passing an empty string ("") or SQL NULL value
322 ** as the second argument is the same as invoking the 1 argument version
323 ** of upper() or lower().
324 **
325 ** lower('I', 'en_us') -> 'i'
326 ** lower('I', 'tr_tr') -> 'ı' (small dotless i)
327 **
328 ** http://www.icu-project.org/userguide/posix.html#case_mappings
329 */
330 static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
331 const UChar *zInput;
332 UChar *zOutput;
333 int nInput;
334 int nOutput;
335
336 UErrorCode status = U_ZERO_ERROR;
337 const char *zLocale = 0;
338
339 assert(nArg==1 || nArg==2);
340 if( nArg==2 ){
341 zLocale = (const char *)sqlite3_value_text(apArg[1]);
342 }
343
344 zInput = sqlite3_value_text16(apArg[0]);
345 if( !zInput ){
346 return;
347 }
348 nInput = sqlite3_value_bytes16(apArg[0]);
349
350 nOutput = nInput * 2 + 2;
351 zOutput = sqlite3_malloc(nOutput);
352 if( !zOutput ){
353 return;
354 }
355
356 if( sqlite3_user_data(p) ){
357 u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
358 }else{
359 u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
360 }
361
362 if( !U_SUCCESS(status) ){
363 icuFunctionError(p, "u_strToLower()/u_strToUpper", status);
364 return;
365 }
366
367 sqlite3_result_text16(p, zOutput, -1, xFree);
368 }
369
370 /*
371 ** Collation sequence destructor function. The pCtx argument points to
372 ** a UCollator structure previously allocated using ucol_open().
373 */
374 static void icuCollationDel(void *pCtx){
375 UCollator *p = (UCollator *)pCtx;
376 ucol_close(p);
377 }
378
379 /*
380 ** Collation sequence comparison function. The pCtx argument points to
381 ** a UCollator structure previously allocated using ucol_open().
382 */
383 static int icuCollationColl(
384 void *pCtx,
385 int nLeft,
386 const void *zLeft,
387 int nRight,
388 const void *zRight
389 ){
390 UCollationResult res;
391 UCollator *p = (UCollator *)pCtx;
392 res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
393 switch( res ){
394 case UCOL_LESS: return -1;
395 case UCOL_GREATER: return +1;
396 case UCOL_EQUAL: return 0;
397 }
398 assert(!"Unexpected return value from ucol_strcoll()");
399 return 0;
400 }
401
402 /*
403 ** Implementation of the scalar function icu_load_collation().
404 **
405 ** This scalar function is used to add ICU collation based collation
406 ** types to an SQLite database connection. It is intended to be called
407 ** as follows:
408 **
409 ** SELECT icu_load_collation(<locale>, <collation-name>);
410 **
411 ** Where <locale> is a string containing an ICU locale identifier (i.e.
412 ** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
413 ** collation sequence to create.
414 */
415 static void icuLoadCollation(
416 sqlite3_context *p,
417 int nArg,
418 sqlite3_value **apArg
419 ){
420 sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
421 UErrorCode status = U_ZERO_ERROR;
422 const char *zLocale; /* Locale identifier - (eg. "jp_JP") */
423 const char *zName; /* SQL Collation sequence name (eg. "japanese") */
424 UCollator *pUCollator; /* ICU library collation object */
425 int rc; /* Return code from sqlite3_create_collation_x() */
426
427 assert(nArg==2);
428 zLocale = (const char *)sqlite3_value_text(apArg[0]);
429 zName = (const char *)sqlite3_value_text(apArg[1]);
430
431 if( !zLocale || !zName ){
432 return;
433 }
434
435 pUCollator = ucol_open(zLocale, &status);
436 if( !U_SUCCESS(status) ){
437 icuFunctionError(p, "ucol_open", status);
438 return;
439 }
440 assert(p);
441
442 rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator,
443 icuCollationColl, icuCollationDel
444 );
445 if( rc!=SQLITE_OK ){
446 ucol_close(pUCollator);
447 sqlite3_result_error(p, "Error registering collation function", -1);
448 }
449 }
450
451 /*
452 ** Register the ICU extension functions with database db.
453 */
454 int sqlite3IcuInit(sqlite3 *db){
455 struct IcuScalar {
456 const char *zName; /* Function name */
457 int nArg; /* Number of arguments */
458 int enc; /* Optimal text encoding */
459 void *pContext; /* sqlite3_user_data() context */
460 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
461 } scalars[] = {
462 {"regexp", 2, SQLITE_ANY, 0, icuRegexpFunc},
463
464 {"lower", 1, SQLITE_UTF16, 0, icuCaseFunc16},
465 {"lower", 2, SQLITE_UTF16, 0, icuCaseFunc16},
466 {"upper", 1, SQLITE_UTF16, (void*)1, icuCaseFunc16},
467 {"upper", 2, SQLITE_UTF16, (void*)1, icuCaseFunc16},
468
469 {"lower", 1, SQLITE_UTF8, 0, icuCaseFunc16},
470 {"lower", 2, SQLITE_UTF8, 0, icuCaseFunc16},
471 {"upper", 1, SQLITE_UTF8, (void*)1, icuCaseFunc16},
472 {"upper", 2, SQLITE_UTF8, (void*)1, icuCaseFunc16},
473
474 {"like", 2, SQLITE_UTF8, 0, icuLikeFunc},
475 {"like", 3, SQLITE_UTF8, 0, icuLikeFunc},
476
477 {"icu_load_collation", 2, SQLITE_UTF8, (void*)db, icuLoadCollation},
478 };
479
480 int rc = SQLITE_OK;
481 int i;
482
483 for(i=0; rc==SQLITE_OK && i<(sizeof(scalars)/sizeof(struct IcuScalar)); i++){
484 struct IcuScalar *p = &scalars[i];
485 rc = sqlite3_create_function(
486 db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
487 );
488 }
489
490 return rc;
491 }
492
493 #if !SQLITE_CORE
494 int sqlite3_extension_init(
495 sqlite3 *db,
496 char **pzErrMsg,
497 const sqlite3_api_routines *pApi
498 ){
499 SQLITE_EXTENSION_INIT2(pApi)
500 return sqlite3IcuInit(db);
501 }
502 #endif
503
504 #endif
OLDNEW
« no previous file with comments | « third_party/sqlite/ext/icu/README.txt ('k') | third_party/sqlite/ext/icu/sqliteicu.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698