blob: b5bf19ca653ca19e493f7e84420296c202dbbb1d [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
2** Copyright (c) 1999, 2000 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
24** Utility functions used throughout sqlite.
25**
26** This file contains functions for allocating memory, comparing
27** strings, and stuff like that.
28**
drhdce2cbe2000-05-31 02:27:49 +000029** $Id: util.c,v 1.7 2000/05/31 02:27:49 drh Exp $
drh75897232000-05-29 14:26:00 +000030*/
31#include "sqliteInt.h"
32#include <stdarg.h>
33#include <ctype.h>
34
drhdcc581c2000-05-30 13:44:19 +000035#ifdef MEMORY_DEBUG
36
37
38/*
39** Allocate new memory and set it to zero. Return NULL if
40** no memory is available.
41*/
42void *sqliteMalloc_(int n, char *zFile, int line){
43 void *p;
44 int *pi;
45 int k;
46 k = (n+sizeof(int)-1)/sizeof(int);
47 pi = malloc( (3+k)*sizeof(int));
48 if( pi==0 ) return 0;
49 pi[0] = 0xdead1122;
50 pi[1] = n;
51 pi[k+2] = 0xdead3344;
52 p = &pi[2];
53 memset(p, 0, n);
54 printf("malloc %d bytes at 0x%x from %s:%d\n", n, (int)p, zFile, line);
55 return p;
56}
57
58/*
59** Free memory previously obtained from sqliteMalloc()
60*/
61void sqliteFree_(void *p, char *zFile, int line){
62 if( p ){
63 int *pi, k, n;
64 pi = p;
65 pi -= 2;
66 if( pi[0]!=0xdead1122 ){
67 printf("Low-end memory corruption at 0x%x\n", (int)p);
68 return;
69 }
70 n = pi[1];
71 k = (n+sizeof(int)-1)/sizeof(int);
72 if( pi[k+2]!=0xdead3344 ){
73 printf("High-end memory corruption at 0x%x\n", (int)p);
74 return;
75 }
76 memset(pi, 0, (k+3)*sizeof(int));
77 printf("free %d bytes at 0x%x from %s:%d\n", n, (int)p, zFile, line);
78 free(pi);
79 }
80}
81
82/*
83** Resize a prior allocation. If p==0, then this routine
84** works just like sqliteMalloc(). If n==0, then this routine
85** works just like sqliteFree().
86*/
87void *sqliteRealloc_(void *oldP, int n, char *zFile, int line){
88 int *oldPi, *pi, k, oldN, oldK;
89 void *p;
90 if( oldP==0 ){
91 return sqliteMalloc_(n,zFile,line);
92 }
93 if( n==0 ){
94 sqliteFree_(oldP,zFile,line);
95 return 0;
96 }
97 oldPi = oldP;
98 oldPi -= 2;
99 if( oldPi[0]!=0xdead1122 ){
100 printf("Low-end memory corruption in realloc at 0x%x\n", (int)p);
101 return;
102 }
103 oldN = oldPi[1];
104 oldK = (oldN+sizeof(int)-1)/sizeof(int);
105 if( oldPi[oldK+2]!=0xdead3344 ){
106 printf("High-end memory corruption in realloc at 0x%x\n", (int)p);
107 return;
108 }
109 k = (n + sizeof(int) - 1)/sizeof(int);
110 pi = malloc( (k+3)*sizeof(int) );
111 pi[0] = 0xdead1122;
112 pi[1] = n;
113 pi[k+2] = 0xdead3344;
114 p = &pi[2];
115 memcpy(p, oldP, n>oldN ? oldN : n);
116 if( n>oldN ){
117 memset(&((char*)p)[oldN], 0, n-oldN);
118 }
119 memset(oldPi, 0, (oldK+3)*sizeof(int));
120 free(oldPi);
121 printf("realloc %d->%d bytes at 0x%x->0x%x at %s:%d\n", oldN, n,
122 (int)oldP, (int)p, zFile, line);
123 return p;
124}
125#else /* !defined(MEMORY_DEBUG) */
drh75897232000-05-29 14:26:00 +0000126/*
127** Allocate new memory and set it to zero. Return NULL if
128** no memory is available.
129*/
130void *sqliteMalloc(int n){
131 void *p = malloc(n);
132 if( p==0 ) return 0;
133 memset(p, 0, n);
134 return p;
135}
136
137/*
138** Free memory previously obtained from sqliteMalloc()
139*/
140void sqliteFree(void *p){
drh305cea62000-05-29 17:44:25 +0000141 if( p ){
drh305cea62000-05-29 17:44:25 +0000142 free(p);
143 }
drh75897232000-05-29 14:26:00 +0000144}
145
146/*
147** Resize a prior allocation. If p==0, then this routine
148** works just like sqliteMalloc(). If n==0, then this routine
149** works just like sqliteFree().
150*/
151void *sqliteRealloc(void *p, int n){
152 if( p==0 ){
153 return sqliteMalloc(n);
154 }
155 if( n==0 ){
156 sqliteFree(p);
157 return 0;
158 }
drh75897232000-05-29 14:26:00 +0000159 return realloc(p, n);
160}
drhdcc581c2000-05-30 13:44:19 +0000161#endif /* MEMORY_DEBUG */
drh75897232000-05-29 14:26:00 +0000162
163/*
164** Create a string from the 2nd and subsequent arguments (up to the
165** first NULL argument), store the string in memory obtained from
166** sqliteMalloc() and make the pointer indicated by the 1st argument
167** point to that string.
168*/
169void sqliteSetString(char **pz, const char *zFirst, ...){
170 va_list ap;
171 int nByte;
172 const char *z;
173 char *zResult;
174
175 if( pz==0 ) return;
176 nByte = strlen(zFirst) + 1;
177 va_start(ap, zFirst);
178 while( (z = va_arg(ap, const char*))!=0 ){
179 nByte += strlen(z);
180 }
181 va_end(ap);
182 sqliteFree(*pz);
183 *pz = zResult = sqliteMalloc( nByte );
184 if( zResult==0 ) return;
185 strcpy(zResult, zFirst);
186 zResult += strlen(zResult);
187 va_start(ap, zFirst);
188 while( (z = va_arg(ap, const char*))!=0 ){
189 strcpy(zResult, z);
190 zResult += strlen(zResult);
191 }
192 va_end(ap);
193}
194
195/*
196** Works like sqliteSetString, but each string is now followed by
197** a length integer. -1 means use the whole string.
198*/
199void sqliteSetNString(char **pz, ...){
200 va_list ap;
201 int nByte;
202 const char *z;
203 char *zResult;
204 int n;
205
206 if( pz==0 ) return;
207 nByte = 0;
208 va_start(ap, pz);
209 while( (z = va_arg(ap, const char*))!=0 ){
210 n = va_arg(ap, int);
211 if( n<=0 ) n = strlen(z);
212 nByte += n;
213 }
214 va_end(ap);
215 sqliteFree(*pz);
216 *pz = zResult = sqliteMalloc( nByte + 1 );
217 if( zResult==0 ) return;
218 va_start(ap, pz);
219 while( (z = va_arg(ap, const char*))!=0 ){
220 n = va_arg(ap, int);
221 if( n<=0 ) n = strlen(z);
222 strncpy(zResult, z, n);
223 zResult += n;
224 }
225 *zResult = 0;
226 va_end(ap);
227}
228
drh982cef72000-05-30 16:27:03 +0000229/*
230** Convert an SQL-style quoted string into a normal string by removing
231** the quote characters. The conversion is done in-place. If the
232** input does not begin with a quote character, then this routine
233** is a no-op.
234*/
235void sqliteDequote(char *z){
236 int quote;
237 int i, j;
238 quote = z[0];
239 if( quote!='\'' && quote!='"' ) return;
240 for(i=1, j=0; z[i]; i++){
241 if( z[i]==quote ){
242 if( z[i+1]==quote ){
243 z[j++] = quote;
244 i++;
245 }else{
246 z[j++] = 0;
247 break;
248 }
249 }else{
250 z[j++] = z[i];
251 }
252 }
253}
254
drh75897232000-05-29 14:26:00 +0000255/* An array to map all upper-case characters into their corresponding
256** lower-case character.
257*/
258static unsigned char UpperToLower[] = {
259 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
260 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
261 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
262 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
263 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
264 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
265 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
266 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
267 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
268 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
269 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
270 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
271 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
272 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
273 252,253,254,255
274};
275
276/*
277** This function computes a hash on the name of a keyword.
278** Case is not significant.
279*/
280int sqliteHashNoCase(const char *z, int n){
281 int h = 0;
282 int c;
283 if( n<=0 ) n = strlen(z);
284 while( n-- > 0 && (c = *z++)!=0 ){
285 h = h<<3 ^ h ^ UpperToLower[c];
286 }
287 if( h<0 ) h = -h;
288 return h;
289}
290
291/*
292** Some system shave stricmp(). Others have strcasecmp(). Because
293** there is no consistency, we will define our own.
294*/
295int sqliteStrICmp(const char *zLeft, const char *zRight){
296 register unsigned char *a, *b;
297 a = (unsigned char *)zLeft;
298 b = (unsigned char *)zRight;
299 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
300 return *a - *b;
301}
302int sqliteStrNICmp(const char *zLeft, const char *zRight, int N){
303 register unsigned char *a, *b;
304 a = (unsigned char *)zLeft;
305 b = (unsigned char *)zRight;
306 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
drhbec2bf42000-05-29 23:48:22 +0000307 return N<0 ? 0 : *a - *b;
drh75897232000-05-29 14:26:00 +0000308}
309
310/* Notes on string comparisions.
311**
312** We want the main string comparision function used for sorting to
313** sort both numbers and alphanumeric words into the correct sequence.
314** The same routine should do both without prior knowledge of which
315** type of text the input represents. It should even work for strings
316** which are a mixture of text and numbers.
317**
318** To accomplish this, we keep track of a state number while scanning
319** the two strings. The states are as follows:
320**
321** 1 Beginning of word
322** 2 Arbitrary text
323** 3 Integer
324** 4 Negative integer
325** 5 Real number
326** 6 Negative real
327**
328** The scan begins in state 1, beginning of word. Transitions to other
329** states are determined by characters seen, as shown in the following
330** chart:
331**
332** Current State Character Seen New State
333** -------------------- -------------- -------------------
334** 0 Beginning of word "-" 3 Negative integer
335** digit 2 Integer
336** space 0 Beginning of word
337** otherwise 1 Arbitrary text
338**
339** 1 Arbitrary text space 0 Beginning of word
340** digit 2 Integer
341** otherwise 1 Arbitrary text
342**
343** 2 Integer space 0 Beginning of word
344** "." 4 Real number
345** digit 2 Integer
346** otherwise 1 Arbitrary text
347**
348** 3 Negative integer space 0 Beginning of word
349** "." 5 Negative Real num
350** digit 3 Negative integer
351** otherwise 1 Arbitrary text
352**
353** 4 Real number space 0 Beginning of word
354** digit 4 Real number
355** otherwise 1 Arbitrary text
356**
357** 5 Negative real num space 0 Beginning of word
358** digit 5 Negative real num
359** otherwise 1 Arbitrary text
360**
361** To implement this state machine, we first classify each character
362** into on of the following categories:
363**
364** 0 Text
365** 1 Space
366** 2 Digit
367** 3 "-"
368** 4 "."
369**
370** Given an arbitrary character, the array charClass[] maps that character
371** into one of the atove categories.
372*/
373static const unsigned char charClass[] = {
374 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
375/* 0x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
376/* 1x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
377/* 2x */ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0,
378/* 3x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0,
379/* 4x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
380/* 5x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
381/* 6x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
382/* 7x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
383/* 8x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
384/* 9x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
385/* Ax */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
386/* Bx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
387/* Cx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
388/* Dx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
389/* Ex */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
390/* Fx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
391};
392#define N_CHAR_CLASS 5
393
394/*
395** Given the current state number (0 thru 5), this array figures
396** the new state number given the character class.
397*/
398static const unsigned char stateMachine[] = {
399 /* Text, Space, Digit, "-", "." */
400 1, 0, 2, 3, 1, /* State 0: Beginning of word */
401 1, 0, 2, 1, 1, /* State 1: Arbitrary text */
402 1, 0, 2, 1, 4, /* State 2: Integer */
403 1, 0, 3, 1, 5, /* State 3: Negative integer */
404 1, 0, 4, 1, 1, /* State 4: Real number */
405 1, 0, 5, 1, 1, /* State 5: Negative real num */
406};
407
408/* This routine does a comparison of two strings. Case is used only
409** if useCase!=0. Numbers compare in numerical order.
410*/
411static int privateStrCmp(const char *atext, const char *btext, int useCase){
412 register unsigned char *a, *b, *map, ca, cb;
413 int result;
414 register int cclass = 0;
415
416 a = (unsigned char *)atext;
417 b = (unsigned char *)btext;
418 if( useCase ){
419 do{
420 if( (ca= *a++)!=(cb= *b++) ) break;
421 cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]];
422 }while( ca!=0 );
423 }else{
424 map = UpperToLower;
425 do{
426 if( (ca=map[*a++])!=(cb=map[*b++]) ) break;
427 cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]];
428 }while( ca!=0 );
429 }
430 switch( cclass ){
431 case 0:
432 case 1: {
433 if( isdigit(ca) && isdigit(cb) ){
434 cclass = 2;
435 }
436 break;
437 }
438 default: {
439 break;
440 }
441 }
442 switch( cclass ){
443 case 2:
444 case 3: {
445 if( isdigit(ca) ){
446 if( isdigit(cb) ){
447 int acnt, bcnt;
448 acnt = bcnt = 0;
449 while( isdigit(*a++) ) acnt++;
450 while( isdigit(*b++) ) bcnt++;
451 result = acnt - bcnt;
452 if( result==0 ) result = ca-cb;
453 }else{
454 result = 1;
455 }
456 }else if( isdigit(cb) ){
457 result = -1;
458 }else if( ca=='.' ){
459 result = 1;
460 }else if( cb=='.' ){
461 result = -1;
462 }else{
463 result = ca - cb;
464 cclass = 2;
465 }
466 if( cclass==3 ) result = -result;
467 break;
468 }
469 case 0:
470 case 1:
471 case 4: {
472 result = ca - cb;
473 break;
474 }
475 case 5: {
476 result = cb - ca;
477 };
478 }
479 return result;
480}
481
482/* This comparison routine is what we use for comparison operations
483** in an SQL expression. (Ex: name<'Hello' or value<5). Compare two
484** strings. Use case only as a tie-breaker. Numbers compare in
485** numerical order.
486*/
487int sqliteCompare(const char *atext, const char *btext){
488 int result;
489 result = privateStrCmp(atext, btext, 0);
490 if( result==0 ) result = privateStrCmp(atext, btext, 1);
491 return result;
492}
493
494/*
495** If you compile just this one file with the -DTEST_COMPARE=1 option,
496** it generates a program to test the comparisons routines.
497*/
498#ifdef TEST_COMPARE
499#include <stdlib.h>
500#include <stdio.h>
501int sortCmp(const char **a, const char **b){
502 return sqliteCompare(*a, *b);
503}
504int main(int argc, char **argv){
505 int i, j, k, n;
506 static char *azStr[] = {
507 "abc", "aBc", "abcd", "aBcd",
508 "123", "124", "1234", "-123", "-124", "-1234",
509 "123.45", "123.456", "123.46", "-123.45", "-123.46", "-123.456",
510 "x9", "x10", "x-9", "x-10", "X9", "X10",
511 };
512 n = sizeof(azStr)/sizeof(azStr[0]);
513 qsort(azStr, n, sizeof(azStr[0]), sortCmp);
514 for(i=0; i<n; i++){
515 printf("%s\n", azStr[i]);
516 }
517 printf("Sanity1...");
518 fflush(stdout);
519 for(i=0; i<n-1; i++){
520 char *a = azStr[i];
521 for(j=i+1; j<n; j++){
522 char *b = azStr[j];
523 if( sqliteCompare(a,b) != -sqliteCompare(b,a) ){
524 printf("Failed! \"%s\" vs \"%s\"\n", a, b);
525 i = j = n;
526 }
527 }
528 }
529 if( i<n ){
530 printf(" OK\n");
531 }
532 return 0;
533}
534#endif
535
536/*
537** This routine is used for sorting. Each key is a list one or more
538** null-terminated strings. The list is terminated by two null in
539** a row. For example, the following text is strings:
540**
541** +one\000-two\000+three\000\000
542**
543** Both arguments will have the same number of strings. This routine
544** returns negative, zero, or positive if the first argument is less
545** than, equal to, or greater than the first. (Result is a-b).
546**
547** Every string begins with either a "+" or "-" character. If the
548** character is "-" then the return value is negated. This is done
549** to implement a sort in descending order.
550*/
551int sqliteSortCompare(const char *a, const char *b){
552 int len;
553 int res = 0;
554
555 while( res==0 && *a && *b ){
556 res = sqliteCompare(&a[1], &b[1]);
557 if( res==0 ){
558 len = strlen(a) + 1;
559 a += len;
560 b += len;
561 }
562 }
563 if( *a=='-' ) res = -res;
564 return res;
565}
drhdce2cbe2000-05-31 02:27:49 +0000566
567/*
568** Compare two strings for equality where the first string can
569** potentially be a "glob" expression. Return true (1) if they
570** are the same and false (0) if they are different.
571**
572** Globbing rules:
573**
574** '*' Matches any sequence of zero or more characters.
575**
576** '?' Matches exactly one character.
577**
578** [...] Matches one character from the enclosed list of
579** characters.
580**
581** [^...] Matches one character not in the enclosed list.
582**
583** With the [...] and [^...] matching, a ']' character can be included
584** in the list by making it the first character after '[' or '^'. A
585** range of characters can be specified using '-'. Example:
586** "[a-z]" matches any single lower-case letter. To match a '-', make
587** it the last character in the list.
588**
589** This routine is usually quick, but can be N**2 in the worst case.
590**
591** Hints: to match '*' or '?', put them in "[]". Like this:
592**
593** abc[*]xyz Matches "abc*xyz" only
594*/
595int sqliteGlobCompare(const char *zPattern, const char *zString){
596 register char c;
597 int invert;
598 int seen;
599 char c2;
600
601 while( (c = *zPattern)!=0 ){
602 switch( c ){
603 case '*':
604 while( zPattern[1]=='*' ) zPattern++;
605 if( zPattern[1]==0 ) return 1;
606 c = zPattern[1];
607 if( c=='[' || c=='?' ){
608 while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){
609 zString++;
610 }
611 return *zString!=0;
612 }else{
613 while( (c2 = *zString)!=0 ){
614 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
615 if( sqliteGlobCompare(&zPattern[1],zString) ) return 1;
616 zString++;
617 }
618 return 0;
619 }
620 case '?':
621 if( *zString==0 ) return 0;
622 break;
623 case '[':
624 seen = 0;
625 invert = 0;
626 c = *zString;
627 if( c==0 ) return 0;
628 c2 = *++zPattern;
629 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
630 if( c2==']' ){
631 if( c==']' ) seen = 1;
632 c2 = *++zPattern;
633 }
634 while( (c2 = *zPattern)!=0 && c2!=']' ){
635 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 ){
636 if( c>zPattern[-1] && c<zPattern[1] ) seen = 1;
637 }else if( c==c2 ){
638 seen = 1;
639 }
640 zPattern++;
641 }
642 if( c2==0 || (seen ^ invert)==0 ) return 0;
643 break;
644 default:
645 if( c != *zString ) return 0;
646 break;
647 }
648 zPattern++;
649 zString++;
650 }
651 return *zString==0;
652}
653
654/*
655** Compare two strings for equality using the "LIKE" operator of
656** SQL. The '%' character matches any sequence of 0 or more
657** characters and '_' matches any single character. Case is
658** not significant.
659**
660** This routine is just an adaptation of the sqliteGlobCompare()
661** routine above.
662*/
663int
664sqliteLikeCompare(const unsigned char *zPattern, const unsigned char *zString){
665 register char c;
666 int invert;
667 int seen;
668 char c2;
669
670 while( (c = UpperToLower[*zPattern])!=0 ){
671 switch( c ){
672 case '%':
673 while( zPattern[1]=='%' ) zPattern++;
674 if( zPattern[1]==0 ) return 1;
675 c = UpperToLower[0xff & zPattern[1]];
676 if( c=='_' ){
677 while( *zString && sqliteLikeCompare(&zPattern[1],zString)==0 ){
678 zString++;
679 }
680 return *zString!=0;
681 }else{
682 while( (c2 = UpperToLower[*zString])!=0 ){
683 while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
684 if( sqliteLikeCompare(&zPattern[1],zString) ) return 1;
685 zString++;
686 }
687 return 0;
688 }
689 case '_':
690 if( *zString==0 ) return 0;
691 break;
692 default:
693 if( c != UpperToLower[*zString] ) return 0;
694 break;
695 }
696 zPattern++;
697 zString++;
698 }
699 return *zString==0;
700}