Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Combine the implementation of LIKE and GLOB into a single parameterized function. (CVS 1923) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
0a47c8f86d1649e9ae7edd4c49a6fe5f |
User & Date: | drh 2004-08-31 00:52:37.000 |
Context
2004-08-31
| ||
13:45 | Simplifications and optimizations. Also: disable the corrupt.test for now. (CVS 1924) (check-in: 8fd65e7048 user: drh tags: trunk) | |
00:52 | Combine the implementation of LIKE and GLOB into a single parameterized function. (CVS 1923) (check-in: 0a47c8f86d user: drh tags: trunk) | |
2004-08-30
| ||
16:52 | Better detection and handling of corrupt database files. (CVS 1922) (check-in: 8f5b199e84 user: drh tags: trunk) | |
Changes
Changes to src/func.c.
︙ | ︙ | |||
12 13 14 15 16 17 18 | ** This file contains the C functions that implement various SQL ** functions of SQLite. ** ** There is only one exported symbol in this file - the function ** sqliteRegisterBuildinFunctions() found at the bottom of the file. ** All other code has file scope. ** | | | 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | ** This file contains the C functions that implement various SQL ** functions of SQLite. ** ** There is only one exported symbol in this file - the function ** sqliteRegisterBuildinFunctions() found at the bottom of the file. ** All other code has file scope. ** ** $Id: func.c,v 1.81 2004/08/31 00:52:37 drh Exp $ */ #include <ctype.h> #include <math.h> #include <stdlib.h> #include <assert.h> #include "sqliteInt.h" #include "vdbeInt.h" |
︙ | ︙ | |||
294 295 296 297 298 299 300 | int arg, sqlite3_value **argv ){ sqlite *db = sqlite3_user_data(context); sqlite3_result_int(context, sqlite3_total_changes(db)); } | < < | < | | | < < | > < | < < < < < < < < < | < < < < | < < < < < < < < < < | < < < < < < < < < | < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < > | | < < < < > > > > > | > | | < < < < < < < | < < < | < < < | < < < < < | < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | < < < > > > > > < > < > < < > < < | < > > > > | | < | | | > < | | > > | > | > | < > | < | < < < | | > > > > > > > > > | < | < | | < | < > | < > > > | | | | > | > > > | | < > < | < | | | < > | < > | | < < | < < | < | > | > > | | | | | > > | > | | > > | | 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | int arg, sqlite3_value **argv ){ sqlite *db = sqlite3_user_data(context); sqlite3_result_int(context, sqlite3_total_changes(db)); } /* ** A structure defining how to do GLOB-style comparisons. */ struct compareInfo { u8 matchAll; u8 matchOne; u8 matchSet; u8 noCase; }; static const struct compareInfo globInfo = { '*', '?', '[', 0 }; static const struct compareInfo likeInfo = { '%', '_', 0, 1 }; /* ** X is a pointer to the first byte of a UTF-8 character. Increment ** X so that it points to the next character. This only works right ** if X points to a well-formed UTF-8 string. */ #define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){} #define sqliteCharVal(X) sqlite3ReadUtf8(X) /* ** Compare two UTF-8 strings for equality where the first string can ** potentially be a "glob" expression. Return true (1) if they ** are the same and false (0) if they are different. ** ** Globbing rules: ** ** '*' Matches any sequence of zero or more characters. ** ** '?' Matches exactly one character. ** ** [...] Matches one character from the enclosed list of ** characters. ** ** [^...] Matches one character not in the enclosed list. ** ** With the [...] and [^...] matching, a ']' character can be included ** in the list by making it the first character after '[' or '^'. A ** range of characters can be specified using '-'. Example: ** "[a-z]" matches any single lower-case letter. To match a '-', make ** it the last character in the list. ** ** This routine is usually quick, but can be N**2 in the worst case. ** ** Hints: to match '*' or '?', put them in "[]". Like this: ** ** abc[*]xyz Matches "abc*xyz" only */ int patternCompare( const u8 *zPattern, /* The glob pattern */ const u8 *zString, /* The string to compare against the glob */ const struct compareInfo *pInfo /* Information about how to do the compare */ ){ register int c; int invert; int seen; int c2; u8 matchOne = pInfo->matchOne; u8 matchAll = pInfo->matchAll; u8 matchSet = pInfo->matchSet; u8 noCase = pInfo->noCase; while( (c = *zPattern)!=0 ){ if( c==matchAll ){ while( (c=zPattern[1]) == matchAll || c == matchOne ){ if( c==matchOne ){ if( *zString==0 ) return 0; sqliteNextChar(zString); } zPattern++; } if( c==0 ) return 1; if( c==matchSet ){ while( *zString && patternCompare(&zPattern[1],zString,pInfo)==0 ){ sqliteNextChar(zString); } return *zString!=0; }else{ while( (c2 = *zString)!=0 ){ if( noCase ){ c2 = sqlite3UpperToLower[c2]; c = sqlite3UpperToLower[c]; while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; } }else{ while( c2 != 0 && c2 != c ){ c2 = *++zString; } } if( c2==0 ) return 0; if( patternCompare(&zPattern[1],zString,pInfo) ) return 1; sqliteNextChar(zString); } return 0; } }else if( c==matchOne ){ if( *zString==0 ) return 0; sqliteNextChar(zString); zPattern++; }else if( c==matchSet ){ int prior_c = 0; seen = 0; invert = 0; c = sqliteCharVal(zString); if( c==0 ) return 0; c2 = *++zPattern; if( c2=='^' ){ invert = 1; c2 = *++zPattern; } if( c2==']' ){ if( c==']' ) seen = 1; c2 = *++zPattern; } while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){ if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){ zPattern++; c2 = sqliteCharVal(zPattern); if( c>=prior_c && c<=c2 ) seen = 1; prior_c = 0; }else if( c==c2 ){ seen = 1; prior_c = c2; }else{ prior_c = c2; } sqliteNextChar(zPattern); } if( c2==0 || (seen ^ invert)==0 ) return 0; sqliteNextChar(zString); zPattern++; }else{ if( noCase ){ if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0; }else{ if( c != *zString ) return 0; } zPattern++; zString++; } } return *zString==0; } /* ** Implementation of the like() SQL function. This function implements ** the build-in LIKE operator. The first argument to the function is the ** pattern and the second argument is the string. So, the SQL statements: ** ** A LIKE B |
︙ | ︙ | |||
559 560 561 562 563 564 565 | sqlite3_context *context, int argc, sqlite3_value **argv ){ const unsigned char *zA = sqlite3_value_text(argv[0]); const unsigned char *zB = sqlite3_value_text(argv[1]); if( zA && zB ){ | | | | 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | sqlite3_context *context, int argc, sqlite3_value **argv ){ const unsigned char *zA = sqlite3_value_text(argv[0]); const unsigned char *zB = sqlite3_value_text(argv[1]); if( zA && zB ){ sqlite3_result_int(context, patternCompare(zA, zB, &likeInfo)); } } /* ** Implementation of the glob() SQL function. This function implements ** the build-in GLOB operator. The first argument to the function is the ** string and the second argument is the pattern. So, the SQL statements: ** ** A GLOB B ** ** is implemented as glob(A,B). */ static void globFunc(sqlite3_context *context, int arg, sqlite3_value **argv){ const unsigned char *zA = sqlite3_value_text(argv[0]); const unsigned char *zB = sqlite3_value_text(argv[1]); if( zA && zB ){ sqlite3_result_int(context, patternCompare(zA, zB, &globInfo)); } } /* ** Implementation of the NULLIF(x,y) function. The result is the first ** argument if the arguments are different. The result is NULL if the ** arguments are equal to each other. |
︙ | ︙ | |||
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 | sqlite3_value *pRes; pRes = (sqlite3_value *)sqlite3_aggregate_context(context, sizeof(Mem)); if( pRes->flags ){ sqlite3_result_value(context, pRes); } sqlite3VdbeMemRelease(pRes); } /* ** This function registered all of the above C functions as SQL ** functions. This should be the only routine in this file with ** external linkage. */ void sqlite3RegisterBuiltinFunctions(sqlite *db){ | > | 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 | sqlite3_value *pRes; pRes = (sqlite3_value *)sqlite3_aggregate_context(context, sizeof(Mem)); if( pRes->flags ){ sqlite3_result_value(context, pRes); } sqlite3VdbeMemRelease(pRes); } /* ** This function registered all of the above C functions as SQL ** functions. This should be the only routine in this file with ** external linkage. */ void sqlite3RegisterBuiltinFunctions(sqlite *db){ |
︙ | ︙ |
Changes to src/sqliteInt.h.
1 2 3 4 5 6 7 8 9 10 11 12 13 | /* ** 2001 September 15 ** ** 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. ** ************************************************************************* ** Internal interface definitions for SQLite. ** | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /* ** 2001 September 15 ** ** 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. ** ************************************************************************* ** Internal interface definitions for SQLite. ** ** @(#) $Id: sqliteInt.h,v 1.317 2004/08/31 00:52:37 drh Exp $ */ #ifndef _SQLITEINT_H_ #define _SQLITEINT_H_ #include "config.h" #include "sqlite3.h" #include "hash.h" |
︙ | ︙ | |||
1266 1267 1268 1269 1270 1271 1272 | Table *sqlite3LocateTable(Parse*,const char*, const char*); Index *sqlite3FindIndex(sqlite*,const char*, const char*); void sqlite3UnlinkAndDeleteTable(sqlite*,int,const char*); void sqlite3UnlinkAndDeleteIndex(sqlite*,int,const char*); void sqlite3UnlinkAndDeleteTrigger(sqlite*,int,const char*); void sqlite3Vacuum(Parse*, Token*); int sqlite3RunVacuum(char**, sqlite*); | < | 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 | Table *sqlite3LocateTable(Parse*,const char*, const char*); Index *sqlite3FindIndex(sqlite*,const char*, const char*); void sqlite3UnlinkAndDeleteTable(sqlite*,int,const char*); void sqlite3UnlinkAndDeleteIndex(sqlite*,int,const char*); void sqlite3UnlinkAndDeleteTrigger(sqlite*,int,const char*); void sqlite3Vacuum(Parse*, Token*); int sqlite3RunVacuum(char**, sqlite*); char *sqlite3NameFromToken(Token*); int sqlite3ExprCheck(Parse*, Expr*, int, int*); int sqlite3ExprCompare(Expr*, Expr*); int sqliteFuncId(Token*); int sqlite3ExprResolveIds(Parse*, SrcList*, ExprList*, Expr*); int sqlite3ExprResolveAndCheck(Parse*,SrcList*,ExprList*,Expr*,int,int*); int sqlite3ExprAnalyzeAggregates(Parse*, Expr*); |
︙ | ︙ | |||
1350 1351 1352 1353 1354 1355 1356 | int sqlite3FixTriggerStep(DbFixer*, TriggerStep*); double sqlite3AtoF(const char *z, const char **); char *sqlite3_snprintf(int,char*,const char*,...); int sqlite3GetInt32(const char *, int*); int sqlite3FitsIn64Bits(const char *); int sqlite3utf16ByteLen(const void *pData, int nChar); int sqlite3utf8CharLen(const char *pData, int nByte); | | | 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 | int sqlite3FixTriggerStep(DbFixer*, TriggerStep*); double sqlite3AtoF(const char *z, const char **); char *sqlite3_snprintf(int,char*,const char*,...); int sqlite3GetInt32(const char *, int*); int sqlite3FitsIn64Bits(const char *); int sqlite3utf16ByteLen(const void *pData, int nChar); int sqlite3utf8CharLen(const char *pData, int nByte); int sqlite3ReadUtf8(const unsigned char *); int sqlite3PutVarint(unsigned char *, u64); int sqlite3GetVarint(const unsigned char *, u64 *); int sqlite3GetVarint32(const unsigned char *, u32 *); int sqlite3VarintLen(u64 v); char sqlite3AffinityType(const char *, int); void sqlite3IndexAffinityStr(Vdbe *, Index *); void sqlite3TableAffinityStr(Vdbe *, Table *); |
︙ | ︙ | |||
1384 1385 1386 1387 1388 1389 1390 1391 1392 | const void *sqlite3ValueText(sqlite3_value*, u8); int sqlite3ValueBytes(sqlite3_value*, u8); void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, void(*)(void*)); void sqlite3ValueFree(sqlite3_value*); sqlite3_value *sqlite3ValueNew(); sqlite3_value *sqlite3GetTransientValue(sqlite *db); #endif | > | 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 | const void *sqlite3ValueText(sqlite3_value*, u8); int sqlite3ValueBytes(sqlite3_value*, u8); void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, void(*)(void*)); void sqlite3ValueFree(sqlite3_value*); sqlite3_value *sqlite3ValueNew(); sqlite3_value *sqlite3GetTransientValue(sqlite *db); extern const unsigned char sqlite3UpperToLower[]; #endif |
Changes to src/utf.c.
︙ | ︙ | |||
8 9 10 11 12 13 14 | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This file contains routines used to translate between UTF-8, ** UTF-16, UTF-16BE, and UTF-16LE. ** | | | 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This file contains routines used to translate between UTF-8, ** UTF-16, UTF-16BE, and UTF-16LE. ** ** $Id: utf.c,v 1.28 2004/08/31 00:52:37 drh Exp $ ** ** Notes on UTF-8: ** ** Byte-0 Byte-1 Byte-2 Byte-3 Value ** 0xxxxxxx 00000000 00000000 0xxxxxxx ** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx ** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx |
︙ | ︙ | |||
58 59 60 61 62 63 64 | ** sqlite3utf8LikeCompare() - Do a LIKE match given two UTF8 char* strings. ** */ #include <assert.h> #include "sqliteInt.h" #include "vdbeInt.h" | < < < < < < < < < < < < < < < < < < < < < | 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | ** sqlite3utf8LikeCompare() - Do a LIKE match given two UTF8 char* strings. ** */ #include <assert.h> #include "sqliteInt.h" #include "vdbeInt.h" /* ** This table maps from the first byte of a UTF-8 character to the number ** of trailing bytes expected. A value '255' indicates that the table key ** is not a legal first byte for a UTF-8 character. */ static const u8 xtra_utf8_bytes[256] = { /* 0xxxxxxx */ |
︙ | ︙ | |||
136 137 138 139 140 141 142 143 144 145 146 147 148 149 | switch( xtra ){ \ case 255: c = (int)0xFFFD; break; \ case 3: c = (c<<6) + *(zIn)++; \ case 2: c = (c<<6) + *(zIn)++; \ case 1: c = (c<<6) + *(zIn)++; \ c -= xtra_utf8_bits[xtra]; \ } \ } #define SKIP_UTF8(zIn) { \ zIn += (xtra_utf8_bytes[*(u8 *)zIn] + 1); \ } #define WRITE_UTF8(zOut, c) { \ | > > > > > | 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | switch( xtra ){ \ case 255: c = (int)0xFFFD; break; \ case 3: c = (c<<6) + *(zIn)++; \ case 2: c = (c<<6) + *(zIn)++; \ case 1: c = (c<<6) + *(zIn)++; \ c -= xtra_utf8_bits[xtra]; \ } \ } int sqlite3ReadUtf8(const unsigned char *z){ int c; READ_UTF8(z, c); return c; } #define SKIP_UTF8(zIn) { \ zIn += (xtra_utf8_bytes[*(u8 *)zIn] + 1); \ } #define WRITE_UTF8(zOut, c) { \ |
︙ | ︙ | |||
487 488 489 490 491 492 493 | READ_UTF16LE(z, c); n++; } } return (z-(char const *)zIn)-((c==0)?2:0); } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 471 472 473 474 475 476 477 478 479 480 481 482 483 484 | READ_UTF16LE(z, c); n++; } } return (z-(char const *)zIn)-((c==0)?2:0); } /* ** UTF-16 implementation of the substr() */ void sqlite3utf16Substr( sqlite3_context *context, int argc, sqlite3_value **argv |
︙ | ︙ |
Changes to src/util.c.
︙ | ︙ | |||
10 11 12 13 14 15 16 | ** ************************************************************************* ** Utility functions used throughout sqlite. ** ** This file contains functions for allocating memory, comparing ** strings, and stuff like that. ** | | | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ** ************************************************************************* ** Utility functions used throughout sqlite. ** ** This file contains functions for allocating memory, comparing ** strings, and stuff like that. ** ** $Id: util.c,v 1.114 2004/08/31 00:52:37 drh Exp $ */ #include "sqliteInt.h" #include <stdarg.h> #include <ctype.h> #if SQLITE_DEBUG>2 && defined(__GLIBC__) #include <execinfo.h> |
︙ | ︙ | |||
529 530 531 532 533 534 535 | } } } /* An array to map all upper-case characters into their corresponding ** lower-case character. */ | | > | 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 | } } } /* An array to map all upper-case characters into their corresponding ** lower-case character. */ const unsigned char sqlite3UpperToLower[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103, 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121, 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107, 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161, 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179, 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197, 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215, 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233, 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251, 252,253,254,255 }; #define UpperToLower sqlite3UpperToLower /* ** This function computes a hash on the name of a keyword. ** Case is not significant. */ int sqlite3HashNoCase(const char *z, int n){ int h = 0; |
︙ | ︙ | |||
758 759 760 761 762 763 764 | int sqlite3FitsIn64Bits(const char *zNum){ int i, c; if( *zNum=='-' || *zNum=='+' ) zNum++; for(i=0; (c=zNum[i])>='0' && c<='9'; i++){} return i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0); } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 759 760 761 762 763 764 765 766 767 768 769 770 771 772 | int sqlite3FitsIn64Bits(const char *zNum){ int i, c; if( *zNum=='-' || *zNum=='+' ) zNum++; for(i=0; (c=zNum[i])>='0' && c<='9'; i++){} return i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0); } /* ** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY. ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN ** when this routine is called. ** ** This routine is a attempt to detect if two threads use the |
︙ | ︙ |