blob: a281f13c3af6cf51c02133a5f70b459546c9922f [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**
drha5c2ad02000-09-14 01:21:10 +000029** $Id: util.c,v 1.15 2000/09/14 01:21:10 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;
drh6e142f52000-06-08 13:36:40 +000046 sqlite_nMalloc++;
47 if( sqlite_iMallocFail>=0 ){
48 sqlite_iMallocFail--;
49 if( sqlite_iMallocFail==0 ) return 0;
50 }
drhdcc581c2000-05-30 13:44:19 +000051 k = (n+sizeof(int)-1)/sizeof(int);
52 pi = malloc( (3+k)*sizeof(int));
53 if( pi==0 ) return 0;
54 pi[0] = 0xdead1122;
55 pi[1] = n;
56 pi[k+2] = 0xdead3344;
57 p = &pi[2];
58 memset(p, 0, n);
drhc3c2fc92000-05-31 22:58:39 +000059#if MEMORY_DEBUG>1
60 fprintf(stderr,"malloc %d bytes at 0x%x from %s:%d\n", n, (int)p, zFile,line);
61#endif
drhdcc581c2000-05-30 13:44:19 +000062 return p;
63}
64
65/*
66** Free memory previously obtained from sqliteMalloc()
67*/
68void sqliteFree_(void *p, char *zFile, int line){
69 if( p ){
70 int *pi, k, n;
71 pi = p;
72 pi -= 2;
drh6e142f52000-06-08 13:36:40 +000073 sqlite_nFree++;
drhdcc581c2000-05-30 13:44:19 +000074 if( pi[0]!=0xdead1122 ){
drhc3c2fc92000-05-31 22:58:39 +000075 fprintf(stderr,"Low-end memory corruption at 0x%x\n", (int)p);
drhdcc581c2000-05-30 13:44:19 +000076 return;
77 }
78 n = pi[1];
79 k = (n+sizeof(int)-1)/sizeof(int);
80 if( pi[k+2]!=0xdead3344 ){
drhc3c2fc92000-05-31 22:58:39 +000081 fprintf(stderr,"High-end memory corruption at 0x%x\n", (int)p);
drhdcc581c2000-05-30 13:44:19 +000082 return;
83 }
drhc3c2fc92000-05-31 22:58:39 +000084 memset(pi, 0xff, (k+3)*sizeof(int));
85#if MEMORY_DEBUG>1
86 fprintf(stderr,"free %d bytes at 0x%x from %s:%d\n", n, (int)p, zFile,line);
87#endif
drhdcc581c2000-05-30 13:44:19 +000088 free(pi);
89 }
90}
91
92/*
93** Resize a prior allocation. If p==0, then this routine
94** works just like sqliteMalloc(). If n==0, then this routine
95** works just like sqliteFree().
96*/
97void *sqliteRealloc_(void *oldP, int n, char *zFile, int line){
98 int *oldPi, *pi, k, oldN, oldK;
99 void *p;
100 if( oldP==0 ){
101 return sqliteMalloc_(n,zFile,line);
102 }
103 if( n==0 ){
104 sqliteFree_(oldP,zFile,line);
105 return 0;
106 }
107 oldPi = oldP;
108 oldPi -= 2;
109 if( oldPi[0]!=0xdead1122 ){
drhc3c2fc92000-05-31 22:58:39 +0000110 fprintf(stderr,"Low-end memory corruption in realloc at 0x%x\n", (int)p);
drh9bb61fe2000-06-05 16:01:39 +0000111 return 0;
drhdcc581c2000-05-30 13:44:19 +0000112 }
113 oldN = oldPi[1];
114 oldK = (oldN+sizeof(int)-1)/sizeof(int);
115 if( oldPi[oldK+2]!=0xdead3344 ){
drhc3c2fc92000-05-31 22:58:39 +0000116 fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n", (int)p);
drh9bb61fe2000-06-05 16:01:39 +0000117 return 0;
drhdcc581c2000-05-30 13:44:19 +0000118 }
119 k = (n + sizeof(int) - 1)/sizeof(int);
120 pi = malloc( (k+3)*sizeof(int) );
121 pi[0] = 0xdead1122;
122 pi[1] = n;
123 pi[k+2] = 0xdead3344;
124 p = &pi[2];
125 memcpy(p, oldP, n>oldN ? oldN : n);
126 if( n>oldN ){
127 memset(&((char*)p)[oldN], 0, n-oldN);
128 }
129 memset(oldPi, 0, (oldK+3)*sizeof(int));
130 free(oldPi);
drhc3c2fc92000-05-31 22:58:39 +0000131#if MEMORY_DEBUG>1
drh6e142f52000-06-08 13:36:40 +0000132 fprintf(stderr,"realloc %d to %d bytes at 0x%x to 0x%x at %s:%d\n", oldN, n,
drhdcc581c2000-05-30 13:44:19 +0000133 (int)oldP, (int)p, zFile, line);
drhc3c2fc92000-05-31 22:58:39 +0000134#endif
drhdcc581c2000-05-30 13:44:19 +0000135 return p;
136}
drhc3c2fc92000-05-31 22:58:39 +0000137
138/*
139** Make a duplicate of a string into memory obtained from malloc()
140** Free the original string using sqliteFree().
141*/
142void sqliteStrRealloc(char **pz){
143 char *zNew;
144 if( pz==0 || *pz==0 ) return;
145 zNew = malloc( strlen(*pz) + 1 );
146 if( zNew ) strcpy(zNew, *pz);
147 sqliteFree(*pz);
148 *pz = zNew;
149}
150
drh6e142f52000-06-08 13:36:40 +0000151/*
152** Make a copy of a string in memory obtained from sqliteMalloc()
153*/
154char *sqliteStrDup_(const char *z, char *zFile, int line){
155 char *zNew = sqliteMalloc_(strlen(z)+1, zFile, line);
156 if( zNew ) strcpy(zNew, z);
157 return zNew;
158}
159char *sqliteStrNDup_(const char *z, int n, char *zFile, int line){
160 char *zNew = sqliteMalloc_(n+1, zFile, line);
161 if( zNew ){
162 memcpy(zNew, z, n);
163 zNew[n] = 0;
164 }
165 return zNew;
166}
167
168
drhdcc581c2000-05-30 13:44:19 +0000169#else /* !defined(MEMORY_DEBUG) */
drh75897232000-05-29 14:26:00 +0000170/*
171** Allocate new memory and set it to zero. Return NULL if
172** no memory is available.
173*/
174void *sqliteMalloc(int n){
175 void *p = malloc(n);
176 if( p==0 ) return 0;
177 memset(p, 0, n);
178 return p;
179}
180
181/*
182** Free memory previously obtained from sqliteMalloc()
183*/
184void sqliteFree(void *p){
drh305cea62000-05-29 17:44:25 +0000185 if( p ){
drh305cea62000-05-29 17:44:25 +0000186 free(p);
187 }
drh75897232000-05-29 14:26:00 +0000188}
189
190/*
191** Resize a prior allocation. If p==0, then this routine
192** works just like sqliteMalloc(). If n==0, then this routine
193** works just like sqliteFree().
194*/
195void *sqliteRealloc(void *p, int n){
196 if( p==0 ){
197 return sqliteMalloc(n);
198 }
199 if( n==0 ){
200 sqliteFree(p);
201 return 0;
202 }
drh75897232000-05-29 14:26:00 +0000203 return realloc(p, n);
204}
drh6e142f52000-06-08 13:36:40 +0000205
206/*
207** Make a copy of a string in memory obtained from sqliteMalloc()
208*/
209char *sqliteStrDup(const char *z){
210 char *zNew = sqliteMalloc(strlen(z)+1);
211 if( zNew ) strcpy(zNew, z);
212 return zNew;
213}
214char *sqliteStrNDup(const char *z, int n){
215 char *zNew = sqliteMalloc(n+1);
216 if( zNew ){
217 memcpy(zNew, z, n);
218 zNew[n] = 0;
219 }
220 return zNew;
221}
drhdcc581c2000-05-30 13:44:19 +0000222#endif /* MEMORY_DEBUG */
drh75897232000-05-29 14:26:00 +0000223
224/*
225** Create a string from the 2nd and subsequent arguments (up to the
226** first NULL argument), store the string in memory obtained from
227** sqliteMalloc() and make the pointer indicated by the 1st argument
228** point to that string.
229*/
230void sqliteSetString(char **pz, const char *zFirst, ...){
231 va_list ap;
232 int nByte;
233 const char *z;
234 char *zResult;
235
236 if( pz==0 ) return;
237 nByte = strlen(zFirst) + 1;
238 va_start(ap, zFirst);
239 while( (z = va_arg(ap, const char*))!=0 ){
240 nByte += strlen(z);
241 }
242 va_end(ap);
243 sqliteFree(*pz);
244 *pz = zResult = sqliteMalloc( nByte );
245 if( zResult==0 ) return;
246 strcpy(zResult, zFirst);
247 zResult += strlen(zResult);
248 va_start(ap, zFirst);
249 while( (z = va_arg(ap, const char*))!=0 ){
250 strcpy(zResult, z);
251 zResult += strlen(zResult);
252 }
253 va_end(ap);
drh6e142f52000-06-08 13:36:40 +0000254#ifdef MEMORY_DEBUG
255#if MEMORY_DEBUG>1
256 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
257#endif
258#endif
drh75897232000-05-29 14:26:00 +0000259}
260
261/*
262** Works like sqliteSetString, but each string is now followed by
263** a length integer. -1 means use the whole string.
264*/
265void sqliteSetNString(char **pz, ...){
266 va_list ap;
267 int nByte;
268 const char *z;
269 char *zResult;
270 int n;
271
272 if( pz==0 ) return;
273 nByte = 0;
274 va_start(ap, pz);
275 while( (z = va_arg(ap, const char*))!=0 ){
276 n = va_arg(ap, int);
277 if( n<=0 ) n = strlen(z);
278 nByte += n;
279 }
280 va_end(ap);
281 sqliteFree(*pz);
282 *pz = zResult = sqliteMalloc( nByte + 1 );
283 if( zResult==0 ) return;
284 va_start(ap, pz);
285 while( (z = va_arg(ap, const char*))!=0 ){
286 n = va_arg(ap, int);
287 if( n<=0 ) n = strlen(z);
288 strncpy(zResult, z, n);
289 zResult += n;
290 }
291 *zResult = 0;
drh6e142f52000-06-08 13:36:40 +0000292#ifdef MEMORY_DEBUG
293#if MEMORY_DEBUG>1
294 fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
295#endif
296#endif
drh75897232000-05-29 14:26:00 +0000297 va_end(ap);
298}
299
drh982cef72000-05-30 16:27:03 +0000300/*
301** Convert an SQL-style quoted string into a normal string by removing
302** the quote characters. The conversion is done in-place. If the
303** input does not begin with a quote character, then this routine
304** is a no-op.
305*/
306void sqliteDequote(char *z){
307 int quote;
308 int i, j;
309 quote = z[0];
310 if( quote!='\'' && quote!='"' ) return;
311 for(i=1, j=0; z[i]; i++){
312 if( z[i]==quote ){
313 if( z[i+1]==quote ){
314 z[j++] = quote;
315 i++;
316 }else{
317 z[j++] = 0;
318 break;
319 }
320 }else{
321 z[j++] = z[i];
322 }
323 }
324}
325
drh75897232000-05-29 14:26:00 +0000326/* An array to map all upper-case characters into their corresponding
327** lower-case character.
328*/
329static unsigned char UpperToLower[] = {
330 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
331 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
332 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
333 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
334 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
335 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
336 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
337 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
338 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
339 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
340 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
341 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
342 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
343 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
344 252,253,254,255
345};
346
347/*
348** This function computes a hash on the name of a keyword.
349** Case is not significant.
350*/
351int sqliteHashNoCase(const char *z, int n){
352 int h = 0;
353 int c;
354 if( n<=0 ) n = strlen(z);
355 while( n-- > 0 && (c = *z++)!=0 ){
356 h = h<<3 ^ h ^ UpperToLower[c];
357 }
358 if( h<0 ) h = -h;
359 return h;
360}
361
362/*
drh967e8b72000-06-21 13:59:10 +0000363** Some systems have stricmp(). Others have strcasecmp(). Because
drh75897232000-05-29 14:26:00 +0000364** there is no consistency, we will define our own.
365*/
366int sqliteStrICmp(const char *zLeft, const char *zRight){
367 register unsigned char *a, *b;
368 a = (unsigned char *)zLeft;
369 b = (unsigned char *)zRight;
370 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
371 return *a - *b;
372}
373int sqliteStrNICmp(const char *zLeft, const char *zRight, int N){
374 register unsigned char *a, *b;
375 a = (unsigned char *)zLeft;
376 b = (unsigned char *)zRight;
377 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
drhbec2bf42000-05-29 23:48:22 +0000378 return N<0 ? 0 : *a - *b;
drh75897232000-05-29 14:26:00 +0000379}
380
381/* Notes on string comparisions.
382**
383** We want the main string comparision function used for sorting to
384** sort both numbers and alphanumeric words into the correct sequence.
385** The same routine should do both without prior knowledge of which
386** type of text the input represents. It should even work for strings
387** which are a mixture of text and numbers.
388**
389** To accomplish this, we keep track of a state number while scanning
390** the two strings. The states are as follows:
391**
392** 1 Beginning of word
393** 2 Arbitrary text
394** 3 Integer
395** 4 Negative integer
396** 5 Real number
397** 6 Negative real
398**
399** The scan begins in state 1, beginning of word. Transitions to other
400** states are determined by characters seen, as shown in the following
401** chart:
402**
403** Current State Character Seen New State
404** -------------------- -------------- -------------------
405** 0 Beginning of word "-" 3 Negative integer
406** digit 2 Integer
407** space 0 Beginning of word
408** otherwise 1 Arbitrary text
409**
410** 1 Arbitrary text space 0 Beginning of word
411** digit 2 Integer
412** otherwise 1 Arbitrary text
413**
414** 2 Integer space 0 Beginning of word
415** "." 4 Real number
416** digit 2 Integer
417** otherwise 1 Arbitrary text
418**
419** 3 Negative integer space 0 Beginning of word
420** "." 5 Negative Real num
421** digit 3 Negative integer
422** otherwise 1 Arbitrary text
423**
424** 4 Real number space 0 Beginning of word
425** digit 4 Real number
426** otherwise 1 Arbitrary text
427**
428** 5 Negative real num space 0 Beginning of word
429** digit 5 Negative real num
430** otherwise 1 Arbitrary text
431**
432** To implement this state machine, we first classify each character
433** into on of the following categories:
434**
435** 0 Text
436** 1 Space
437** 2 Digit
438** 3 "-"
439** 4 "."
440**
441** Given an arbitrary character, the array charClass[] maps that character
442** into one of the atove categories.
443*/
444static const unsigned char charClass[] = {
445 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
446/* 0x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
447/* 1x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
448/* 2x */ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0,
449/* 3x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0,
450/* 4x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
451/* 5x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
452/* 6x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
453/* 7x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
454/* 8x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
455/* 9x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
456/* Ax */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
457/* Bx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
458/* Cx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
459/* Dx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
460/* Ex */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
461/* Fx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
462};
463#define N_CHAR_CLASS 5
464
465/*
466** Given the current state number (0 thru 5), this array figures
467** the new state number given the character class.
468*/
469static const unsigned char stateMachine[] = {
470 /* Text, Space, Digit, "-", "." */
471 1, 0, 2, 3, 1, /* State 0: Beginning of word */
472 1, 0, 2, 1, 1, /* State 1: Arbitrary text */
473 1, 0, 2, 1, 4, /* State 2: Integer */
474 1, 0, 3, 1, 5, /* State 3: Negative integer */
475 1, 0, 4, 1, 1, /* State 4: Real number */
476 1, 0, 5, 1, 1, /* State 5: Negative real num */
477};
478
479/* This routine does a comparison of two strings. Case is used only
480** if useCase!=0. Numbers compare in numerical order.
481*/
482static int privateStrCmp(const char *atext, const char *btext, int useCase){
483 register unsigned char *a, *b, *map, ca, cb;
484 int result;
485 register int cclass = 0;
486
487 a = (unsigned char *)atext;
488 b = (unsigned char *)btext;
489 if( useCase ){
490 do{
491 if( (ca= *a++)!=(cb= *b++) ) break;
492 cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]];
493 }while( ca!=0 );
494 }else{
495 map = UpperToLower;
496 do{
497 if( (ca=map[*a++])!=(cb=map[*b++]) ) break;
498 cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]];
499 }while( ca!=0 );
500 }
501 switch( cclass ){
502 case 0:
503 case 1: {
504 if( isdigit(ca) && isdigit(cb) ){
505 cclass = 2;
506 }
507 break;
508 }
509 default: {
510 break;
511 }
512 }
513 switch( cclass ){
514 case 2:
515 case 3: {
516 if( isdigit(ca) ){
517 if( isdigit(cb) ){
518 int acnt, bcnt;
519 acnt = bcnt = 0;
520 while( isdigit(*a++) ) acnt++;
521 while( isdigit(*b++) ) bcnt++;
522 result = acnt - bcnt;
523 if( result==0 ) result = ca-cb;
524 }else{
525 result = 1;
526 }
527 }else if( isdigit(cb) ){
528 result = -1;
529 }else if( ca=='.' ){
530 result = 1;
531 }else if( cb=='.' ){
532 result = -1;
533 }else{
534 result = ca - cb;
535 cclass = 2;
536 }
537 if( cclass==3 ) result = -result;
538 break;
539 }
540 case 0:
541 case 1:
542 case 4: {
543 result = ca - cb;
544 break;
545 }
546 case 5: {
547 result = cb - ca;
548 };
549 }
550 return result;
551}
552
drha5c2ad02000-09-14 01:21:10 +0000553/*
554** Do a comparison of pure numerics. If either string is not a pure
555** numeric, then return 0. Otherwise return 1 and set *pResult to be
556** negative, zero or positive if the first string are numerially less than
557** equal to, or greater than the second.
558*/
559static int privateCompareNum(const char *a, const char *b, int *pResult){
560 char *endPtr;
561 double rA, rB;
562 int isNumA, isNumB;
563 if( isdigit(*a) || ((*a=='-' || *a=='+') && isdigit(a[1])) ){
564 rA = strtod(a, &endPtr);
565 isNumA = *endPtr==0;
566 }else{
567 isNumA = 0;
568 }
569 if( isdigit(*b) || ((*b=='-' || *b=='+') && isdigit(b[1])) ){
570 rB = strtod(b, &endPtr);
571 isNumB = *endPtr==0;
572 }else{
573 isNumB = 0;
574 }
575 if( isNumB==0 && isNumA==0 ) return 0;
576 if( isNumA!=isNumB ){
577 *pResult = isNumA - isNumB;
578 }else if( rA<rB ){
579 *pResult = -1;
580 }else if( rA>rB ){
581 *pResult = 1;
582 }else{
583 *pResult = 0;
584 }
585 return 1;
586}
587
drh75897232000-05-29 14:26:00 +0000588/* This comparison routine is what we use for comparison operations
589** in an SQL expression. (Ex: name<'Hello' or value<5). Compare two
590** strings. Use case only as a tie-breaker. Numbers compare in
591** numerical order.
592*/
593int sqliteCompare(const char *atext, const char *btext){
594 int result;
drha5c2ad02000-09-14 01:21:10 +0000595 if( !privateCompareNum(atext, btext, &result) || result==0 ){
596 result = privateStrCmp(atext, btext, 0);
597 if( result==0 ) result = privateStrCmp(atext, btext, 1);
598 }
drh75897232000-05-29 14:26:00 +0000599 return result;
600}
601
602/*
603** If you compile just this one file with the -DTEST_COMPARE=1 option,
604** it generates a program to test the comparisons routines.
605*/
606#ifdef TEST_COMPARE
607#include <stdlib.h>
608#include <stdio.h>
609int sortCmp(const char **a, const char **b){
610 return sqliteCompare(*a, *b);
611}
612int main(int argc, char **argv){
drha5c2ad02000-09-14 01:21:10 +0000613 int i, j, k, n, cnt;
drh75897232000-05-29 14:26:00 +0000614 static char *azStr[] = {
615 "abc", "aBc", "abcd", "aBcd",
drha5c2ad02000-09-14 01:21:10 +0000616 "123", "124", "1234", "-123", "-124", "-1234", "+124",
drh75897232000-05-29 14:26:00 +0000617 "123.45", "123.456", "123.46", "-123.45", "-123.46", "-123.456",
618 "x9", "x10", "x-9", "x-10", "X9", "X10",
drha5c2ad02000-09-14 01:21:10 +0000619 "1.234e+02", "+123", "1.23E2", "1.2345e+2", "-1.2345e2", "+w"
drh75897232000-05-29 14:26:00 +0000620 };
621 n = sizeof(azStr)/sizeof(azStr[0]);
622 qsort(azStr, n, sizeof(azStr[0]), sortCmp);
623 for(i=0; i<n; i++){
624 printf("%s\n", azStr[i]);
625 }
626 printf("Sanity1...");
627 fflush(stdout);
drha5c2ad02000-09-14 01:21:10 +0000628 cnt = 0;
drh75897232000-05-29 14:26:00 +0000629 for(i=0; i<n-1; i++){
630 char *a = azStr[i];
631 for(j=i+1; j<n; j++){
632 char *b = azStr[j];
633 if( sqliteCompare(a,b) != -sqliteCompare(b,a) ){
634 printf("Failed! \"%s\" vs \"%s\"\n", a, b);
635 i = j = n;
636 }
drha5c2ad02000-09-14 01:21:10 +0000637 cnt++;
drh75897232000-05-29 14:26:00 +0000638 }
639 }
640 if( i<n ){
drha5c2ad02000-09-14 01:21:10 +0000641 printf(" OK (%d)\n", cnt);
642 }
643 printf("Sanity2...");
644 fflush(stdout);
645 cnt = 0;
646 for(i=0; i<n; i++){
647 char *a = azStr[i];
648 for(j=0; j<n; j++){
649 char *b = azStr[j];
650 for(k=0; k<n; k++){
651 char *c = azStr[k];
652 int x1, x2, x3, success;
653 x1 = sqliteCompare(a,b);
654 x2 = sqliteCompare(b,c);
655 x3 = sqliteCompare(a,c);
656 if( x1==0 ){
657 success = x2==x3;
658 }else if( x1<0 ){
659 success = (x2<=0 && x3<=0) || x2>0;
660 }else{
661 success = (x2>=0 && x3>=0) || x2<0;
662 }
663 if( !success ){
664 printf("Failed! \"%s\" vs \"%s\" vs \"%s\"\n", a, b, c);
665 i = j = k = n+1;
666 }
667 cnt++;
668 }
669 }
670 }
671 if( i<n+1 ){
672 printf(" OK (%d)\n", cnt);
drh75897232000-05-29 14:26:00 +0000673 }
674 return 0;
675}
676#endif
677
678/*
drh16e59552000-07-31 11:57:37 +0000679** This routine is used for sorting. Each key is a list of one or more
680** null-terminated strings. The list is terminated by two nulls in
681** a row. For example, the following text is key with three strings:
drh75897232000-05-29 14:26:00 +0000682**
683** +one\000-two\000+three\000\000
684**
685** Both arguments will have the same number of strings. This routine
686** returns negative, zero, or positive if the first argument is less
687** than, equal to, or greater than the first. (Result is a-b).
688**
689** Every string begins with either a "+" or "-" character. If the
690** character is "-" then the return value is negated. This is done
691** to implement a sort in descending order.
692*/
693int sqliteSortCompare(const char *a, const char *b){
694 int len;
695 int res = 0;
696
697 while( res==0 && *a && *b ){
698 res = sqliteCompare(&a[1], &b[1]);
699 if( res==0 ){
700 len = strlen(a) + 1;
701 a += len;
702 b += len;
703 }
704 }
705 if( *a=='-' ) res = -res;
706 return res;
707}
drhdce2cbe2000-05-31 02:27:49 +0000708
709/*
710** Compare two strings for equality where the first string can
711** potentially be a "glob" expression. Return true (1) if they
712** are the same and false (0) if they are different.
713**
714** Globbing rules:
715**
716** '*' Matches any sequence of zero or more characters.
717**
718** '?' Matches exactly one character.
719**
720** [...] Matches one character from the enclosed list of
721** characters.
722**
723** [^...] Matches one character not in the enclosed list.
724**
725** With the [...] and [^...] matching, a ']' character can be included
726** in the list by making it the first character after '[' or '^'. A
727** range of characters can be specified using '-'. Example:
728** "[a-z]" matches any single lower-case letter. To match a '-', make
729** it the last character in the list.
730**
731** This routine is usually quick, but can be N**2 in the worst case.
732**
733** Hints: to match '*' or '?', put them in "[]". Like this:
734**
735** abc[*]xyz Matches "abc*xyz" only
736*/
737int sqliteGlobCompare(const char *zPattern, const char *zString){
738 register char c;
739 int invert;
740 int seen;
741 char c2;
742
743 while( (c = *zPattern)!=0 ){
744 switch( c ){
745 case '*':
746 while( zPattern[1]=='*' ) zPattern++;
747 if( zPattern[1]==0 ) return 1;
748 c = zPattern[1];
749 if( c=='[' || c=='?' ){
750 while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){
751 zString++;
752 }
753 return *zString!=0;
754 }else{
755 while( (c2 = *zString)!=0 ){
756 while( c2 != 0 && c2 != c ){ c2 = *++zString; }
drhc61053b2000-06-04 12:58:36 +0000757 if( c2==0 ) return 0;
drhdce2cbe2000-05-31 02:27:49 +0000758 if( sqliteGlobCompare(&zPattern[1],zString) ) return 1;
759 zString++;
760 }
761 return 0;
762 }
763 case '?':
764 if( *zString==0 ) return 0;
765 break;
766 case '[':
767 seen = 0;
768 invert = 0;
769 c = *zString;
770 if( c==0 ) return 0;
771 c2 = *++zPattern;
772 if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
773 if( c2==']' ){
774 if( c==']' ) seen = 1;
775 c2 = *++zPattern;
776 }
777 while( (c2 = *zPattern)!=0 && c2!=']' ){
778 if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 ){
779 if( c>zPattern[-1] && c<zPattern[1] ) seen = 1;
780 }else if( c==c2 ){
781 seen = 1;
782 }
783 zPattern++;
784 }
785 if( c2==0 || (seen ^ invert)==0 ) return 0;
786 break;
787 default:
788 if( c != *zString ) return 0;
789 break;
790 }
791 zPattern++;
792 zString++;
793 }
794 return *zString==0;
795}
796
797/*
798** Compare two strings for equality using the "LIKE" operator of
799** SQL. The '%' character matches any sequence of 0 or more
800** characters and '_' matches any single character. Case is
801** not significant.
802**
803** This routine is just an adaptation of the sqliteGlobCompare()
804** routine above.
805*/
806int
807sqliteLikeCompare(const unsigned char *zPattern, const unsigned char *zString){
808 register char c;
drhdce2cbe2000-05-31 02:27:49 +0000809 char c2;
810
811 while( (c = UpperToLower[*zPattern])!=0 ){
812 switch( c ){
813 case '%':
814 while( zPattern[1]=='%' ) zPattern++;
815 if( zPattern[1]==0 ) return 1;
816 c = UpperToLower[0xff & zPattern[1]];
817 if( c=='_' ){
818 while( *zString && sqliteLikeCompare(&zPattern[1],zString)==0 ){
819 zString++;
820 }
821 return *zString!=0;
822 }else{
823 while( (c2 = UpperToLower[*zString])!=0 ){
824 while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
drhc61053b2000-06-04 12:58:36 +0000825 if( c2==0 ) return 0;
drhdce2cbe2000-05-31 02:27:49 +0000826 if( sqliteLikeCompare(&zPattern[1],zString) ) return 1;
827 zString++;
828 }
829 return 0;
830 }
831 case '_':
832 if( *zString==0 ) return 0;
833 break;
834 default:
835 if( c != UpperToLower[*zString] ) return 0;
836 break;
837 }
838 zPattern++;
839 zString++;
840 }
841 return *zString==0;
842}