| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2007 May 6 | 2 ** 2007 May 6 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 #endif | 54 #endif |
| 55 | 55 |
| 56 /* | 56 /* |
| 57 ** Version of sqlite3_free() that is always a function, never a macro. | 57 ** Version of sqlite3_free() that is always a function, never a macro. |
| 58 */ | 58 */ |
| 59 static void xFree(void *p){ | 59 static void xFree(void *p){ |
| 60 sqlite3_free(p); | 60 sqlite3_free(p); |
| 61 } | 61 } |
| 62 | 62 |
| 63 /* | 63 /* |
| 64 ** This lookup table is used to help decode the first byte of |
| 65 ** a multi-byte UTF8 character. It is copied here from SQLite source |
| 66 ** code file utf8.c. |
| 67 */ |
| 68 static const unsigned char icuUtf8Trans1[] = { |
| 69 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 70 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 71 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 72 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 73 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 74 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 75 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 76 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00, |
| 77 }; |
| 78 |
| 79 #define SQLITE_ICU_READ_UTF8(zIn, c) \ |
| 80 c = *(zIn++); \ |
| 81 if( c>=0xc0 ){ \ |
| 82 c = icuUtf8Trans1[c-0xc0]; \ |
| 83 while( (*zIn & 0xc0)==0x80 ){ \ |
| 84 c = (c<<6) + (0x3f & *(zIn++)); \ |
| 85 } \ |
| 86 } |
| 87 |
| 88 #define SQLITE_ICU_SKIP_UTF8(zIn) \ |
| 89 assert( *zIn ); \ |
| 90 if( *(zIn++)>=0xc0 ){ \ |
| 91 while( (*zIn & 0xc0)==0x80 ){zIn++;} \ |
| 92 } |
| 93 |
| 94 |
| 95 /* |
| 64 ** Compare two UTF-8 strings for equality where the first string is | 96 ** Compare two UTF-8 strings for equality where the first string is |
| 65 ** a "LIKE" expression. Return true (1) if they are the same and | 97 ** a "LIKE" expression. Return true (1) if they are the same and |
| 66 ** false (0) if they are different. | 98 ** false (0) if they are different. |
| 67 */ | 99 */ |
| 68 static int icuLikeCompare( | 100 static int icuLikeCompare( |
| 69 const uint8_t *zPattern, /* LIKE pattern */ | 101 const uint8_t *zPattern, /* LIKE pattern */ |
| 70 const uint8_t *zString, /* The UTF-8 string to compare against */ | 102 const uint8_t *zString, /* The UTF-8 string to compare against */ |
| 71 const UChar32 uEsc /* The escape character */ | 103 const UChar32 uEsc /* The escape character */ |
| 72 ){ | 104 ){ |
| 73 static const int MATCH_ONE = (UChar32)'_'; | 105 static const int MATCH_ONE = (UChar32)'_'; |
| 74 static const int MATCH_ALL = (UChar32)'%'; | 106 static const int MATCH_ALL = (UChar32)'%'; |
| 75 | 107 |
| 76 int iPattern = 0; /* Current byte index in zPattern */ | |
| 77 int iString = 0; /* Current byte index in zString */ | |
| 78 | |
| 79 int prevEscape = 0; /* True if the previous character was uEsc */ | 108 int prevEscape = 0; /* True if the previous character was uEsc */ |
| 80 | 109 |
| 81 while( zPattern[iPattern]!=0 ){ | 110 while( 1 ){ |
| 82 | 111 |
| 83 /* Read (and consume) the next character from the input pattern. */ | 112 /* Read (and consume) the next character from the input pattern. */ |
| 84 UChar32 uPattern; | 113 UChar32 uPattern; |
| 85 U8_NEXT_OR_FFFD(zPattern, iPattern, -1, uPattern); | 114 SQLITE_ICU_READ_UTF8(zPattern, uPattern); |
| 115 if( uPattern==0 ) break; |
| 86 | 116 |
| 87 /* There are now 4 possibilities: | 117 /* There are now 4 possibilities: |
| 88 ** | 118 ** |
| 89 ** 1. uPattern is an unescaped match-all character "%", | 119 ** 1. uPattern is an unescaped match-all character "%", |
| 90 ** 2. uPattern is an unescaped match-one character "_", | 120 ** 2. uPattern is an unescaped match-one character "_", |
| 91 ** 3. uPattern is an unescaped escape character, or | 121 ** 3. uPattern is an unescaped escape character, or |
| 92 ** 4. uPattern is to be handled as an ordinary character | 122 ** 4. uPattern is to be handled as an ordinary character |
| 93 */ | 123 */ |
| 94 if( !prevEscape && uPattern==MATCH_ALL ){ | 124 if( !prevEscape && uPattern==MATCH_ALL ){ |
| 95 /* Case 1. */ | 125 /* Case 1. */ |
| 96 uint8_t c; | 126 uint8_t c; |
| 97 | 127 |
| 98 /* Skip any MATCH_ALL or MATCH_ONE characters that follow a | 128 /* Skip any MATCH_ALL or MATCH_ONE characters that follow a |
| 99 ** MATCH_ALL. For each MATCH_ONE, skip one character in the | 129 ** MATCH_ALL. For each MATCH_ONE, skip one character in the |
| 100 ** test string. | 130 ** test string. |
| 101 */ | 131 */ |
| 102 while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){ | 132 while( (c=*zPattern) == MATCH_ALL || c == MATCH_ONE ){ |
| 103 if( c==MATCH_ONE ){ | 133 if( c==MATCH_ONE ){ |
| 104 if( zString[iString]==0 ) return 0; | 134 if( *zString==0 ) return 0; |
| 105 U8_FWD_1(zString, iString, -1); | 135 SQLITE_ICU_SKIP_UTF8(zString); |
| 106 } | 136 } |
| 107 iPattern++; | 137 zPattern++; |
| 108 } | 138 } |
| 109 | 139 |
| 110 if( zPattern[iPattern]==0 ) return 1; | 140 if( *zPattern==0 ) return 1; |
| 111 | 141 |
| 112 while( zString[iString] ){ | 142 while( *zString ){ |
| 113 if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){ | 143 if( icuLikeCompare(zPattern, zString, uEsc) ){ |
| 114 return 1; | 144 return 1; |
| 115 } | 145 } |
| 116 U8_FWD_1(zString, iString, -1); | 146 SQLITE_ICU_SKIP_UTF8(zString); |
| 117 } | 147 } |
| 118 return 0; | 148 return 0; |
| 119 | 149 |
| 120 }else if( !prevEscape && uPattern==MATCH_ONE ){ | 150 }else if( !prevEscape && uPattern==MATCH_ONE ){ |
| 121 /* Case 2. */ | 151 /* Case 2. */ |
| 122 if( zString[iString]==0 ) return 0; | 152 if( *zString==0 ) return 0; |
| 123 U8_FWD_1(zString, iString, -1); | 153 SQLITE_ICU_SKIP_UTF8(zString); |
| 124 | 154 |
| 125 }else if( !prevEscape && uPattern==uEsc){ | 155 }else if( !prevEscape && uPattern==uEsc){ |
| 126 /* Case 3. */ | 156 /* Case 3. */ |
| 127 prevEscape = 1; | 157 prevEscape = 1; |
| 128 | 158 |
| 129 }else{ | 159 }else{ |
| 130 /* Case 4. */ | 160 /* Case 4. */ |
| 131 UChar32 uString; | 161 UChar32 uString; |
| 132 U8_NEXT_OR_FFFD(zString, iString, -1, uString); | 162 SQLITE_ICU_READ_UTF8(zString, uString); |
| 133 uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT); | 163 uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT); |
| 134 uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT); | 164 uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT); |
| 135 if( uString!=uPattern ){ | 165 if( uString!=uPattern ){ |
| 136 return 0; | 166 return 0; |
| 137 } | 167 } |
| 138 prevEscape = 0; | 168 prevEscape = 0; |
| 139 } | 169 } |
| 140 } | 170 } |
| 141 | 171 |
| 142 return zString[iString]==0; | 172 return *zString==0; |
| 143 } | 173 } |
| 144 | 174 |
| 145 /* | 175 /* |
| 146 ** Implementation of the like() SQL function. This function implements | 176 ** Implementation of the like() SQL function. This function implements |
| 147 ** the build-in LIKE operator. The first argument to the function is the | 177 ** the build-in LIKE operator. The first argument to the function is the |
| 148 ** pattern and the second argument is the string. So, the SQL statements: | 178 ** pattern and the second argument is the string. So, the SQL statements: |
| 149 ** | 179 ** |
| 150 ** A LIKE B | 180 ** A LIKE B |
| 151 ** | 181 ** |
| 152 ** is implemented as like(B, A). If there is an escape character E, | 182 ** is implemented as like(B, A). If there is an escape character E, |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 ** upper('ABC') -> 'abc' | 342 ** upper('ABC') -> 'abc' |
| 313 ** lower('abc') -> 'ABC' | 343 ** lower('abc') -> 'ABC' |
| 314 ** | 344 ** |
| 315 ** To access ICU "language specific" case mapping, upper() or lower() | 345 ** To access ICU "language specific" case mapping, upper() or lower() |
| 316 ** should be invoked with two arguments. The second argument is the name | 346 ** should be invoked with two arguments. The second argument is the name |
| 317 ** of the locale to use. Passing an empty string ("") or SQL NULL value | 347 ** of the locale to use. Passing an empty string ("") or SQL NULL value |
| 318 ** as the second argument is the same as invoking the 1 argument version | 348 ** as the second argument is the same as invoking the 1 argument version |
| 319 ** of upper() or lower(). | 349 ** of upper() or lower(). |
| 320 ** | 350 ** |
| 321 ** lower('I', 'en_us') -> 'i' | 351 ** lower('I', 'en_us') -> 'i' |
| 322 ** lower('I', 'tr_tr') -> 'ı' (small dotless i) | 352 ** lower('I', 'tr_tr') -> '\u131' (small dotless i) |
| 323 ** | 353 ** |
| 324 ** http://www.icu-project.org/userguide/posix.html#case_mappings | 354 ** http://www.icu-project.org/userguide/posix.html#case_mappings |
| 325 */ | 355 */ |
| 326 static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){ | 356 static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){ |
| 327 const UChar *zInput; | 357 const UChar *zInput; /* Pointer to input string */ |
| 328 UChar *zOutput = 0; | 358 UChar *zOutput = 0; /* Pointer to output buffer */ |
| 329 int nInput; | 359 int nInput; /* Size of utf-16 input string in bytes */ |
| 330 int nOut; | 360 int nOut; /* Size of output buffer in bytes */ |
| 331 int cnt; | 361 int cnt; |
| 362 int bToUpper; /* True for toupper(), false for tolower() */ |
| 332 UErrorCode status; | 363 UErrorCode status; |
| 333 const char *zLocale = 0; | 364 const char *zLocale = 0; |
| 334 | 365 |
| 335 assert(nArg==1 || nArg==2); | 366 assert(nArg==1 || nArg==2); |
| 367 bToUpper = (sqlite3_user_data(p)!=0); |
| 336 if( nArg==2 ){ | 368 if( nArg==2 ){ |
| 337 zLocale = (const char *)sqlite3_value_text(apArg[1]); | 369 zLocale = (const char *)sqlite3_value_text(apArg[1]); |
| 338 } | 370 } |
| 339 | 371 |
| 340 zInput = sqlite3_value_text16(apArg[0]); | 372 zInput = sqlite3_value_text16(apArg[0]); |
| 341 if( !zInput ){ | 373 if( !zInput ){ |
| 342 return; | 374 return; |
| 343 } | 375 } |
| 344 nOut = nInput = sqlite3_value_bytes16(apArg[0]); | 376 nOut = nInput = sqlite3_value_bytes16(apArg[0]); |
| 345 if( nOut==0 ){ | 377 if( nOut==0 ){ |
| 346 sqlite3_result_text16(p, "", 0, SQLITE_STATIC); | 378 sqlite3_result_text16(p, "", 0, SQLITE_STATIC); |
| 347 return; | 379 return; |
| 348 } | 380 } |
| 349 | 381 |
| 350 for(cnt=0; cnt<2; cnt++){ | 382 for(cnt=0; cnt<2; cnt++){ |
| 351 UChar *zNew = sqlite3_realloc(zOutput, nOut); | 383 UChar *zNew = sqlite3_realloc(zOutput, nOut); |
| 352 if( zNew==0 ){ | 384 if( zNew==0 ){ |
| 353 sqlite3_free(zOutput); | 385 sqlite3_free(zOutput); |
| 354 sqlite3_result_error_nomem(p); | 386 sqlite3_result_error_nomem(p); |
| 355 return; | 387 return; |
| 356 } | 388 } |
| 357 zOutput = zNew; | 389 zOutput = zNew; |
| 358 status = U_ZERO_ERROR; | 390 status = U_ZERO_ERROR; |
| 359 if( sqlite3_user_data(p) ){ | 391 if( bToUpper ){ |
| 360 nOut = 2*u_strToUpper(zOutput,nOut/2,zInput,nInput/2,zLocale,&status); | 392 nOut = 2*u_strToUpper(zOutput,nOut/2,zInput,nInput/2,zLocale,&status); |
| 361 }else{ | 393 }else{ |
| 362 nOut = 2*u_strToLower(zOutput,nOut/2,zInput,nInput/2,zLocale,&status); | 394 nOut = 2*u_strToLower(zOutput,nOut/2,zInput,nInput/2,zLocale,&status); |
| 363 } | 395 } |
| 364 if( !U_SUCCESS(status) ){ | 396 |
| 365 if( status==U_BUFFER_OVERFLOW_ERROR ) continue; | 397 if( U_SUCCESS(status) ){ |
| 366 icuFunctionError(p, | 398 sqlite3_result_text16(p, zOutput, nOut, xFree); |
| 367 sqlite3_user_data(p) ? "u_strToUpper()" : "u_strToLower", status); | 399 }else if( status==U_BUFFER_OVERFLOW_ERROR ){ |
| 368 return; | 400 assert( cnt==0 ); |
| 401 continue; |
| 402 }else{ |
| 403 icuFunctionError(p, bToUpper ? "u_strToUpper" : "u_strToLower", status); |
| 369 } | 404 } |
| 405 return; |
| 370 } | 406 } |
| 371 sqlite3_result_text16(p, zOutput, nOut, xFree); | 407 assert( 0 ); /* Unreachable */ |
| 372 } | 408 } |
| 373 | 409 |
| 374 /* | 410 /* |
| 375 ** Collation sequence destructor function. The pCtx argument points to | 411 ** Collation sequence destructor function. The pCtx argument points to |
| 376 ** a UCollator structure previously allocated using ucol_open(). | 412 ** a UCollator structure previously allocated using ucol_open(). |
| 377 */ | 413 */ |
| 378 static void icuCollationDel(void *pCtx){ | 414 static void icuCollationDel(void *pCtx){ |
| 379 UCollator *p = (UCollator *)pCtx; | 415 UCollator *p = (UCollator *)pCtx; |
| 380 ucol_close(p); | 416 ucol_close(p); |
| 381 } | 417 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 if( rc!=SQLITE_OK ){ | 486 if( rc!=SQLITE_OK ){ |
| 451 ucol_close(pUCollator); | 487 ucol_close(pUCollator); |
| 452 sqlite3_result_error(p, "Error registering collation function", -1); | 488 sqlite3_result_error(p, "Error registering collation function", -1); |
| 453 } | 489 } |
| 454 } | 490 } |
| 455 | 491 |
| 456 /* | 492 /* |
| 457 ** Register the ICU extension functions with database db. | 493 ** Register the ICU extension functions with database db. |
| 458 */ | 494 */ |
| 459 int sqlite3IcuInit(sqlite3 *db){ | 495 int sqlite3IcuInit(sqlite3 *db){ |
| 460 struct IcuScalar { | 496 static const struct IcuScalar { |
| 461 const char *zName; /* Function name */ | 497 const char *zName; /* Function name */ |
| 462 int nArg; /* Number of arguments */ | 498 unsigned char nArg; /* Number of arguments */ |
| 463 int enc; /* Optimal text encoding */ | 499 unsigned short enc; /* Optimal text encoding */ |
| 464 void *pContext; /* sqlite3_user_data() context */ | 500 unsigned char iContext; /* sqlite3_user_data() context */ |
| 465 void (*xFunc)(sqlite3_context*,int,sqlite3_value**); | 501 void (*xFunc)(sqlite3_context*,int,sqlite3_value**); |
| 466 } scalars[] = { | 502 } scalars[] = { |
| 467 {"regexp", 2, SQLITE_ANY, 0, icuRegexpFunc}, | 503 {"icu_load_collation", 2, SQLITE_UTF8, 1, icuLoadCollation}, |
| 468 | 504 {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC, 0, icuRegexpFunc}, |
| 469 {"lower", 1, SQLITE_UTF16, 0, icuCaseFunc16}, | 505 {"lower", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16}, |
| 470 {"lower", 2, SQLITE_UTF16, 0, icuCaseFunc16}, | 506 {"lower", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16}, |
| 471 {"upper", 1, SQLITE_UTF16, (void*)1, icuCaseFunc16}, | 507 {"upper", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 1, icuCaseFunc16}, |
| 472 {"upper", 2, SQLITE_UTF16, (void*)1, icuCaseFunc16}, | 508 {"upper", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 1, icuCaseFunc16}, |
| 473 | 509 {"lower", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16}, |
| 474 {"lower", 1, SQLITE_UTF8, 0, icuCaseFunc16}, | 510 {"lower", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16}, |
| 475 {"lower", 2, SQLITE_UTF8, 0, icuCaseFunc16}, | 511 {"upper", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1, icuCaseFunc16}, |
| 476 {"upper", 1, SQLITE_UTF8, (void*)1, icuCaseFunc16}, | 512 {"upper", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1, icuCaseFunc16}, |
| 477 {"upper", 2, SQLITE_UTF8, (void*)1, icuCaseFunc16}, | 513 {"like", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc}, |
| 478 | 514 {"like", 3, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc}, |
| 479 {"like", 2, SQLITE_UTF8, 0, icuLikeFunc}, | |
| 480 {"like", 3, SQLITE_UTF8, 0, icuLikeFunc}, | |
| 481 | |
| 482 {"icu_load_collation", 2, SQLITE_UTF8, (void*)db, icuLoadCollation}, | |
| 483 }; | 515 }; |
| 484 | |
| 485 int rc = SQLITE_OK; | 516 int rc = SQLITE_OK; |
| 486 int i; | 517 int i; |
| 487 | 518 |
| 519 |
| 488 for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){ | 520 for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){ |
| 489 struct IcuScalar *p = &scalars[i]; | 521 const struct IcuScalar *p = &scalars[i]; |
| 490 rc = sqlite3_create_function( | 522 rc = sqlite3_create_function( |
| 491 db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0 | 523 db, p->zName, p->nArg, p->enc, |
| 524 p->iContext ? (void*)db : (void*)0, |
| 525 p->xFunc, 0, 0 |
| 492 ); | 526 ); |
| 493 } | 527 } |
| 494 | 528 |
| 495 return rc; | 529 return rc; |
| 496 } | 530 } |
| 497 | 531 |
| 498 #if !SQLITE_CORE | 532 #if !SQLITE_CORE |
| 499 #ifdef _WIN32 | 533 #ifdef _WIN32 |
| 500 __declspec(dllexport) | 534 __declspec(dllexport) |
| 501 #endif | 535 #endif |
| 502 int sqlite3_icu_init( | 536 int sqlite3_icu_init( |
| 503 sqlite3 *db, | 537 sqlite3 *db, |
| 504 char **pzErrMsg, | 538 char **pzErrMsg, |
| 505 const sqlite3_api_routines *pApi | 539 const sqlite3_api_routines *pApi |
| 506 ){ | 540 ){ |
| 507 SQLITE_EXTENSION_INIT2(pApi) | 541 SQLITE_EXTENSION_INIT2(pApi) |
| 508 return sqlite3IcuInit(db); | 542 return sqlite3IcuInit(db); |
| 509 } | 543 } |
| 510 #endif | 544 #endif |
| 511 | 545 |
| 512 #endif | 546 #endif |
| OLD | NEW |