drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** Utility functions used throughout sqlite. |
| 13 | ** |
| 14 | ** This file contains functions for allocating memory, comparing |
| 15 | ** strings, and stuff like that. |
| 16 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 17 | ** $Id: util.c,v 1.25 2001/09/16 00:13:27 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 18 | */ |
| 19 | #include "sqliteInt.h" |
| 20 | #include <stdarg.h> |
| 21 | #include <ctype.h> |
| 22 | |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 23 | /* |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 24 | ** If malloc() ever fails, this global variable gets set to 1. |
| 25 | ** This causes the library to abort and never again function. |
| 26 | */ |
| 27 | int sqlite_malloc_failed = 0; |
| 28 | |
| 29 | /* |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 30 | ** If MEMORY_DEBUG is defined, then use versions of malloc() and |
| 31 | ** free() that track memory usage and check for buffer overruns. |
| 32 | */ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 33 | #ifdef MEMORY_DEBUG |
| 34 | |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 35 | /* |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 36 | ** For keeping track of the number of mallocs and frees. This |
| 37 | ** is used to check for memory leaks. |
| 38 | */ |
| 39 | int sqlite_nMalloc; /* Number of sqliteMalloc() calls */ |
| 40 | int sqlite_nFree; /* Number of sqliteFree() calls */ |
| 41 | int sqlite_iMallocFail; /* Fail sqliteMalloc() after this many calls */ |
| 42 | |
| 43 | |
| 44 | /* |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 45 | ** Allocate new memory and set it to zero. Return NULL if |
| 46 | ** no memory is available. |
| 47 | */ |
| 48 | void *sqliteMalloc_(int n, char *zFile, int line){ |
| 49 | void *p; |
| 50 | int *pi; |
| 51 | int k; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 52 | sqlite_nMalloc++; |
| 53 | if( sqlite_iMallocFail>=0 ){ |
| 54 | sqlite_iMallocFail--; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 55 | if( sqlite_iMallocFail==0 ){ |
| 56 | sqlite_malloc_failed++; |
| 57 | return 0; |
| 58 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 59 | } |
drh | b072950 | 2001-03-14 12:35:57 +0000 | [diff] [blame] | 60 | if( n==0 ) return 0; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 61 | k = (n+sizeof(int)-1)/sizeof(int); |
| 62 | pi = malloc( (3+k)*sizeof(int)); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 63 | if( pi==0 ){ |
| 64 | sqlite_malloc_failed++; |
| 65 | return 0; |
| 66 | } |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 67 | pi[0] = 0xdead1122; |
| 68 | pi[1] = n; |
| 69 | pi[k+2] = 0xdead3344; |
| 70 | p = &pi[2]; |
| 71 | memset(p, 0, n); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 72 | #if MEMORY_DEBUG>1 |
| 73 | fprintf(stderr,"malloc %d bytes at 0x%x from %s:%d\n", n, (int)p, zFile,line); |
| 74 | #endif |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 75 | return p; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | ** Free memory previously obtained from sqliteMalloc() |
| 80 | */ |
| 81 | void sqliteFree_(void *p, char *zFile, int line){ |
| 82 | if( p ){ |
| 83 | int *pi, k, n; |
| 84 | pi = p; |
| 85 | pi -= 2; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 86 | sqlite_nFree++; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 87 | if( pi[0]!=0xdead1122 ){ |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 88 | fprintf(stderr,"Low-end memory corruption at 0x%x\n", (int)p); |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 89 | return; |
| 90 | } |
| 91 | n = pi[1]; |
| 92 | k = (n+sizeof(int)-1)/sizeof(int); |
| 93 | if( pi[k+2]!=0xdead3344 ){ |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 94 | fprintf(stderr,"High-end memory corruption at 0x%x\n", (int)p); |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 95 | return; |
| 96 | } |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 97 | memset(pi, 0xff, (k+3)*sizeof(int)); |
| 98 | #if MEMORY_DEBUG>1 |
| 99 | fprintf(stderr,"free %d bytes at 0x%x from %s:%d\n", n, (int)p, zFile,line); |
| 100 | #endif |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 101 | free(pi); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | ** Resize a prior allocation. If p==0, then this routine |
| 107 | ** works just like sqliteMalloc(). If n==0, then this routine |
| 108 | ** works just like sqliteFree(). |
| 109 | */ |
| 110 | void *sqliteRealloc_(void *oldP, int n, char *zFile, int line){ |
| 111 | int *oldPi, *pi, k, oldN, oldK; |
| 112 | void *p; |
| 113 | if( oldP==0 ){ |
| 114 | return sqliteMalloc_(n,zFile,line); |
| 115 | } |
| 116 | if( n==0 ){ |
| 117 | sqliteFree_(oldP,zFile,line); |
| 118 | return 0; |
| 119 | } |
| 120 | oldPi = oldP; |
| 121 | oldPi -= 2; |
| 122 | if( oldPi[0]!=0xdead1122 ){ |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 123 | fprintf(stderr,"Low-end memory corruption in realloc at 0x%x\n", (int)p); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 124 | return 0; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 125 | } |
| 126 | oldN = oldPi[1]; |
| 127 | oldK = (oldN+sizeof(int)-1)/sizeof(int); |
| 128 | if( oldPi[oldK+2]!=0xdead3344 ){ |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 129 | fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n", (int)p); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 130 | return 0; |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 131 | } |
| 132 | k = (n + sizeof(int) - 1)/sizeof(int); |
| 133 | pi = malloc( (k+3)*sizeof(int) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 134 | if( pi==0 ){ |
| 135 | sqlite_malloc_failed++; |
| 136 | return 0; |
| 137 | } |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 138 | pi[0] = 0xdead1122; |
| 139 | pi[1] = n; |
| 140 | pi[k+2] = 0xdead3344; |
| 141 | p = &pi[2]; |
| 142 | memcpy(p, oldP, n>oldN ? oldN : n); |
| 143 | if( n>oldN ){ |
| 144 | memset(&((char*)p)[oldN], 0, n-oldN); |
| 145 | } |
| 146 | memset(oldPi, 0, (oldK+3)*sizeof(int)); |
| 147 | free(oldPi); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 148 | #if MEMORY_DEBUG>1 |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 149 | fprintf(stderr,"realloc %d to %d bytes at 0x%x to 0x%x at %s:%d\n", oldN, n, |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 150 | (int)oldP, (int)p, zFile, line); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 151 | #endif |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 152 | return p; |
| 153 | } |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | ** Make a duplicate of a string into memory obtained from malloc() |
| 157 | ** Free the original string using sqliteFree(). |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 158 | ** |
| 159 | ** This routine is called on all strings that are passed outside of |
| 160 | ** the SQLite library. That way clients can free the string using free() |
| 161 | ** rather than having to call sqliteFree(). |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 162 | */ |
| 163 | void sqliteStrRealloc(char **pz){ |
| 164 | char *zNew; |
| 165 | if( pz==0 || *pz==0 ) return; |
| 166 | zNew = malloc( strlen(*pz) + 1 ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 167 | if( zNew==0 ){ |
| 168 | sqlite_malloc_failed++; |
| 169 | sqliteFree(*pz); |
| 170 | *pz = 0; |
| 171 | } |
| 172 | strcpy(zNew, *pz); |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 173 | sqliteFree(*pz); |
| 174 | *pz = zNew; |
| 175 | } |
| 176 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 177 | /* |
| 178 | ** Make a copy of a string in memory obtained from sqliteMalloc() |
| 179 | */ |
| 180 | char *sqliteStrDup_(const char *z, char *zFile, int line){ |
| 181 | char *zNew = sqliteMalloc_(strlen(z)+1, zFile, line); |
| 182 | if( zNew ) strcpy(zNew, z); |
| 183 | return zNew; |
| 184 | } |
| 185 | char *sqliteStrNDup_(const char *z, int n, char *zFile, int line){ |
| 186 | char *zNew = sqliteMalloc_(n+1, zFile, line); |
| 187 | if( zNew ){ |
| 188 | memcpy(zNew, z, n); |
| 189 | zNew[n] = 0; |
| 190 | } |
| 191 | return zNew; |
| 192 | } |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 193 | #endif /* MEMORY_DEBUG */ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 194 | |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 195 | /* |
| 196 | ** The following versions of malloc() and free() are for use in a |
| 197 | ** normal build. |
| 198 | */ |
| 199 | #if !defined(MEMORY_DEBUG) |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 200 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 201 | /* |
| 202 | ** Allocate new memory and set it to zero. Return NULL if |
| 203 | ** no memory is available. |
| 204 | */ |
| 205 | void *sqliteMalloc(int n){ |
| 206 | void *p = malloc(n); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 207 | if( p==0 ){ |
| 208 | sqlite_malloc_failed++; |
| 209 | return 0; |
| 210 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 211 | memset(p, 0, n); |
| 212 | return p; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | ** Free memory previously obtained from sqliteMalloc() |
| 217 | */ |
| 218 | void sqliteFree(void *p){ |
drh | 305cea6 | 2000-05-29 17:44:25 +0000 | [diff] [blame] | 219 | if( p ){ |
drh | 305cea6 | 2000-05-29 17:44:25 +0000 | [diff] [blame] | 220 | free(p); |
| 221 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* |
| 225 | ** Resize a prior allocation. If p==0, then this routine |
| 226 | ** works just like sqliteMalloc(). If n==0, then this routine |
| 227 | ** works just like sqliteFree(). |
| 228 | */ |
| 229 | void *sqliteRealloc(void *p, int n){ |
| 230 | if( p==0 ){ |
| 231 | return sqliteMalloc(n); |
| 232 | } |
| 233 | if( n==0 ){ |
| 234 | sqliteFree(p); |
| 235 | return 0; |
| 236 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 237 | p = realloc(p, n); |
| 238 | if( p==0 ){ |
| 239 | sqlite_malloc_failed++; |
| 240 | } |
| 241 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 242 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 243 | |
| 244 | /* |
| 245 | ** Make a copy of a string in memory obtained from sqliteMalloc() |
| 246 | */ |
| 247 | char *sqliteStrDup(const char *z){ |
| 248 | char *zNew = sqliteMalloc(strlen(z)+1); |
| 249 | if( zNew ) strcpy(zNew, z); |
| 250 | return zNew; |
| 251 | } |
| 252 | char *sqliteStrNDup(const char *z, int n){ |
| 253 | char *zNew = sqliteMalloc(n+1); |
| 254 | if( zNew ){ |
| 255 | memcpy(zNew, z, n); |
| 256 | zNew[n] = 0; |
| 257 | } |
| 258 | return zNew; |
| 259 | } |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 260 | #endif /* !defined(MEMORY_DEBUG) */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 261 | |
| 262 | /* |
| 263 | ** Create a string from the 2nd and subsequent arguments (up to the |
| 264 | ** first NULL argument), store the string in memory obtained from |
| 265 | ** sqliteMalloc() and make the pointer indicated by the 1st argument |
| 266 | ** point to that string. |
| 267 | */ |
| 268 | void sqliteSetString(char **pz, const char *zFirst, ...){ |
| 269 | va_list ap; |
| 270 | int nByte; |
| 271 | const char *z; |
| 272 | char *zResult; |
| 273 | |
| 274 | if( pz==0 ) return; |
| 275 | nByte = strlen(zFirst) + 1; |
| 276 | va_start(ap, zFirst); |
| 277 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 278 | nByte += strlen(z); |
| 279 | } |
| 280 | va_end(ap); |
| 281 | sqliteFree(*pz); |
| 282 | *pz = zResult = sqliteMalloc( nByte ); |
| 283 | if( zResult==0 ) return; |
| 284 | strcpy(zResult, zFirst); |
| 285 | zResult += strlen(zResult); |
| 286 | va_start(ap, zFirst); |
| 287 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 288 | strcpy(zResult, z); |
| 289 | zResult += strlen(zResult); |
| 290 | } |
| 291 | va_end(ap); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 292 | #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 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* |
| 300 | ** Works like sqliteSetString, but each string is now followed by |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 301 | ** a length integer which specifies how much of the source string |
| 302 | ** to copy (in bytes). -1 means use the whole string. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 303 | */ |
| 304 | void sqliteSetNString(char **pz, ...){ |
| 305 | va_list ap; |
| 306 | int nByte; |
| 307 | const char *z; |
| 308 | char *zResult; |
| 309 | int n; |
| 310 | |
| 311 | if( pz==0 ) return; |
| 312 | nByte = 0; |
| 313 | va_start(ap, pz); |
| 314 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 315 | n = va_arg(ap, int); |
| 316 | if( n<=0 ) n = strlen(z); |
| 317 | nByte += n; |
| 318 | } |
| 319 | va_end(ap); |
| 320 | sqliteFree(*pz); |
| 321 | *pz = zResult = sqliteMalloc( nByte + 1 ); |
| 322 | if( zResult==0 ) return; |
| 323 | va_start(ap, pz); |
| 324 | while( (z = va_arg(ap, const char*))!=0 ){ |
| 325 | n = va_arg(ap, int); |
| 326 | if( n<=0 ) n = strlen(z); |
| 327 | strncpy(zResult, z, n); |
| 328 | zResult += n; |
| 329 | } |
| 330 | *zResult = 0; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 331 | #ifdef MEMORY_DEBUG |
| 332 | #if MEMORY_DEBUG>1 |
| 333 | fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz); |
| 334 | #endif |
| 335 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 336 | va_end(ap); |
| 337 | } |
| 338 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 339 | /* |
| 340 | ** Convert an SQL-style quoted string into a normal string by removing |
| 341 | ** the quote characters. The conversion is done in-place. If the |
| 342 | ** input does not begin with a quote character, then this routine |
| 343 | ** is a no-op. |
| 344 | */ |
| 345 | void sqliteDequote(char *z){ |
| 346 | int quote; |
| 347 | int i, j; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 348 | if( z==0 ) return; |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 349 | quote = z[0]; |
| 350 | if( quote!='\'' && quote!='"' ) return; |
| 351 | for(i=1, j=0; z[i]; i++){ |
| 352 | if( z[i]==quote ){ |
| 353 | if( z[i+1]==quote ){ |
| 354 | z[j++] = quote; |
| 355 | i++; |
| 356 | }else{ |
| 357 | z[j++] = 0; |
| 358 | break; |
| 359 | } |
| 360 | }else{ |
| 361 | z[j++] = z[i]; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 366 | /* An array to map all upper-case characters into their corresponding |
| 367 | ** lower-case character. |
| 368 | */ |
| 369 | static unsigned char UpperToLower[] = { |
| 370 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, |
| 371 | 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, |
| 372 | 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, |
| 373 | 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103, |
| 374 | 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121, |
| 375 | 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107, |
| 376 | 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, |
| 377 | 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, |
| 378 | 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161, |
| 379 | 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179, |
| 380 | 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197, |
| 381 | 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215, |
| 382 | 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233, |
| 383 | 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251, |
| 384 | 252,253,254,255 |
| 385 | }; |
| 386 | |
| 387 | /* |
| 388 | ** This function computes a hash on the name of a keyword. |
| 389 | ** Case is not significant. |
| 390 | */ |
| 391 | int sqliteHashNoCase(const char *z, int n){ |
| 392 | int h = 0; |
| 393 | int c; |
| 394 | if( n<=0 ) n = strlen(z); |
| 395 | while( n-- > 0 && (c = *z++)!=0 ){ |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 396 | h = (h<<3) ^ h ^ UpperToLower[c]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 397 | } |
| 398 | if( h<0 ) h = -h; |
| 399 | return h; |
| 400 | } |
| 401 | |
| 402 | /* |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 403 | ** Some systems have stricmp(). Others have strcasecmp(). Because |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 404 | ** there is no consistency, we will define our own. |
| 405 | */ |
| 406 | int sqliteStrICmp(const char *zLeft, const char *zRight){ |
| 407 | register unsigned char *a, *b; |
| 408 | a = (unsigned char *)zLeft; |
| 409 | b = (unsigned char *)zRight; |
| 410 | while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 411 | return *a - *b; |
| 412 | } |
| 413 | int sqliteStrNICmp(const char *zLeft, const char *zRight, int N){ |
| 414 | register unsigned char *a, *b; |
| 415 | a = (unsigned char *)zLeft; |
| 416 | b = (unsigned char *)zRight; |
| 417 | while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 418 | return N<0 ? 0 : *a - *b; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* Notes on string comparisions. |
| 422 | ** |
| 423 | ** We want the main string comparision function used for sorting to |
| 424 | ** sort both numbers and alphanumeric words into the correct sequence. |
| 425 | ** The same routine should do both without prior knowledge of which |
| 426 | ** type of text the input represents. It should even work for strings |
| 427 | ** which are a mixture of text and numbers. |
| 428 | ** |
| 429 | ** To accomplish this, we keep track of a state number while scanning |
| 430 | ** the two strings. The states are as follows: |
| 431 | ** |
| 432 | ** 1 Beginning of word |
| 433 | ** 2 Arbitrary text |
| 434 | ** 3 Integer |
| 435 | ** 4 Negative integer |
| 436 | ** 5 Real number |
| 437 | ** 6 Negative real |
| 438 | ** |
| 439 | ** The scan begins in state 1, beginning of word. Transitions to other |
| 440 | ** states are determined by characters seen, as shown in the following |
| 441 | ** chart: |
| 442 | ** |
| 443 | ** Current State Character Seen New State |
| 444 | ** -------------------- -------------- ------------------- |
| 445 | ** 0 Beginning of word "-" 3 Negative integer |
| 446 | ** digit 2 Integer |
| 447 | ** space 0 Beginning of word |
| 448 | ** otherwise 1 Arbitrary text |
| 449 | ** |
| 450 | ** 1 Arbitrary text space 0 Beginning of word |
| 451 | ** digit 2 Integer |
| 452 | ** otherwise 1 Arbitrary text |
| 453 | ** |
| 454 | ** 2 Integer space 0 Beginning of word |
| 455 | ** "." 4 Real number |
| 456 | ** digit 2 Integer |
| 457 | ** otherwise 1 Arbitrary text |
| 458 | ** |
| 459 | ** 3 Negative integer space 0 Beginning of word |
| 460 | ** "." 5 Negative Real num |
| 461 | ** digit 3 Negative integer |
| 462 | ** otherwise 1 Arbitrary text |
| 463 | ** |
| 464 | ** 4 Real number space 0 Beginning of word |
| 465 | ** digit 4 Real number |
| 466 | ** otherwise 1 Arbitrary text |
| 467 | ** |
| 468 | ** 5 Negative real num space 0 Beginning of word |
| 469 | ** digit 5 Negative real num |
| 470 | ** otherwise 1 Arbitrary text |
| 471 | ** |
| 472 | ** To implement this state machine, we first classify each character |
| 473 | ** into on of the following categories: |
| 474 | ** |
| 475 | ** 0 Text |
| 476 | ** 1 Space |
| 477 | ** 2 Digit |
| 478 | ** 3 "-" |
| 479 | ** 4 "." |
| 480 | ** |
| 481 | ** Given an arbitrary character, the array charClass[] maps that character |
| 482 | ** into one of the atove categories. |
| 483 | */ |
| 484 | static const unsigned char charClass[] = { |
| 485 | /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ |
| 486 | /* 0x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, |
| 487 | /* 1x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 488 | /* 2x */ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0, |
| 489 | /* 3x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, |
| 490 | /* 4x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 491 | /* 5x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 492 | /* 6x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 493 | /* 7x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 494 | /* 8x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 495 | /* 9x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 496 | /* Ax */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 497 | /* Bx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 498 | /* Cx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 499 | /* Dx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 500 | /* Ex */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 501 | /* Fx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 502 | }; |
| 503 | #define N_CHAR_CLASS 5 |
| 504 | |
| 505 | /* |
| 506 | ** Given the current state number (0 thru 5), this array figures |
| 507 | ** the new state number given the character class. |
| 508 | */ |
| 509 | static const unsigned char stateMachine[] = { |
| 510 | /* Text, Space, Digit, "-", "." */ |
| 511 | 1, 0, 2, 3, 1, /* State 0: Beginning of word */ |
| 512 | 1, 0, 2, 1, 1, /* State 1: Arbitrary text */ |
| 513 | 1, 0, 2, 1, 4, /* State 2: Integer */ |
| 514 | 1, 0, 3, 1, 5, /* State 3: Negative integer */ |
| 515 | 1, 0, 4, 1, 1, /* State 4: Real number */ |
| 516 | 1, 0, 5, 1, 1, /* State 5: Negative real num */ |
| 517 | }; |
| 518 | |
| 519 | /* This routine does a comparison of two strings. Case is used only |
| 520 | ** if useCase!=0. Numbers compare in numerical order. |
| 521 | */ |
| 522 | static int privateStrCmp(const char *atext, const char *btext, int useCase){ |
| 523 | register unsigned char *a, *b, *map, ca, cb; |
| 524 | int result; |
| 525 | register int cclass = 0; |
| 526 | |
| 527 | a = (unsigned char *)atext; |
| 528 | b = (unsigned char *)btext; |
| 529 | if( useCase ){ |
| 530 | do{ |
| 531 | if( (ca= *a++)!=(cb= *b++) ) break; |
| 532 | cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]]; |
| 533 | }while( ca!=0 ); |
| 534 | }else{ |
| 535 | map = UpperToLower; |
| 536 | do{ |
| 537 | if( (ca=map[*a++])!=(cb=map[*b++]) ) break; |
| 538 | cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]]; |
| 539 | }while( ca!=0 ); |
| 540 | } |
| 541 | switch( cclass ){ |
| 542 | case 0: |
| 543 | case 1: { |
| 544 | if( isdigit(ca) && isdigit(cb) ){ |
| 545 | cclass = 2; |
| 546 | } |
| 547 | break; |
| 548 | } |
| 549 | default: { |
| 550 | break; |
| 551 | } |
| 552 | } |
| 553 | switch( cclass ){ |
| 554 | case 2: |
| 555 | case 3: { |
| 556 | if( isdigit(ca) ){ |
| 557 | if( isdigit(cb) ){ |
| 558 | int acnt, bcnt; |
| 559 | acnt = bcnt = 0; |
| 560 | while( isdigit(*a++) ) acnt++; |
| 561 | while( isdigit(*b++) ) bcnt++; |
| 562 | result = acnt - bcnt; |
| 563 | if( result==0 ) result = ca-cb; |
| 564 | }else{ |
| 565 | result = 1; |
| 566 | } |
| 567 | }else if( isdigit(cb) ){ |
| 568 | result = -1; |
| 569 | }else if( ca=='.' ){ |
| 570 | result = 1; |
| 571 | }else if( cb=='.' ){ |
| 572 | result = -1; |
| 573 | }else{ |
| 574 | result = ca - cb; |
| 575 | cclass = 2; |
| 576 | } |
| 577 | if( cclass==3 ) result = -result; |
| 578 | break; |
| 579 | } |
| 580 | case 0: |
| 581 | case 1: |
| 582 | case 4: { |
| 583 | result = ca - cb; |
| 584 | break; |
| 585 | } |
| 586 | case 5: { |
| 587 | result = cb - ca; |
| 588 | }; |
| 589 | } |
| 590 | return result; |
| 591 | } |
| 592 | |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 593 | /* |
| 594 | ** Do a comparison of pure numerics. If either string is not a pure |
| 595 | ** numeric, then return 0. Otherwise return 1 and set *pResult to be |
| 596 | ** negative, zero or positive if the first string are numerially less than |
| 597 | ** equal to, or greater than the second. |
| 598 | */ |
| 599 | static int privateCompareNum(const char *a, const char *b, int *pResult){ |
| 600 | char *endPtr; |
| 601 | double rA, rB; |
| 602 | int isNumA, isNumB; |
| 603 | if( isdigit(*a) || ((*a=='-' || *a=='+') && isdigit(a[1])) ){ |
| 604 | rA = strtod(a, &endPtr); |
| 605 | isNumA = *endPtr==0; |
| 606 | }else{ |
| 607 | isNumA = 0; |
| 608 | } |
| 609 | if( isdigit(*b) || ((*b=='-' || *b=='+') && isdigit(b[1])) ){ |
| 610 | rB = strtod(b, &endPtr); |
| 611 | isNumB = *endPtr==0; |
| 612 | }else{ |
| 613 | isNumB = 0; |
| 614 | } |
| 615 | if( isNumB==0 && isNumA==0 ) return 0; |
| 616 | if( isNumA!=isNumB ){ |
| 617 | *pResult = isNumA - isNumB; |
| 618 | }else if( rA<rB ){ |
| 619 | *pResult = -1; |
| 620 | }else if( rA>rB ){ |
| 621 | *pResult = 1; |
| 622 | }else{ |
| 623 | *pResult = 0; |
| 624 | } |
| 625 | return 1; |
| 626 | } |
| 627 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 628 | /* This comparison routine is what we use for comparison operations |
| 629 | ** in an SQL expression. (Ex: name<'Hello' or value<5). Compare two |
| 630 | ** strings. Use case only as a tie-breaker. Numbers compare in |
| 631 | ** numerical order. |
| 632 | */ |
| 633 | int sqliteCompare(const char *atext, const char *btext){ |
| 634 | int result; |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 635 | if( !privateCompareNum(atext, btext, &result) || result==0 ){ |
| 636 | result = privateStrCmp(atext, btext, 0); |
| 637 | if( result==0 ) result = privateStrCmp(atext, btext, 1); |
| 638 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 639 | return result; |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | ** If you compile just this one file with the -DTEST_COMPARE=1 option, |
| 644 | ** it generates a program to test the comparisons routines. |
| 645 | */ |
| 646 | #ifdef TEST_COMPARE |
| 647 | #include <stdlib.h> |
| 648 | #include <stdio.h> |
| 649 | int sortCmp(const char **a, const char **b){ |
| 650 | return sqliteCompare(*a, *b); |
| 651 | } |
| 652 | int main(int argc, char **argv){ |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 653 | int i, j, k, n, cnt; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 654 | static char *azStr[] = { |
| 655 | "abc", "aBc", "abcd", "aBcd", |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 656 | "123", "124", "1234", "-123", "-124", "-1234", "+124", |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 657 | "123.45", "123.456", "123.46", "-123.45", "-123.46", "-123.456", |
| 658 | "x9", "x10", "x-9", "x-10", "X9", "X10", |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 659 | "1.234e+02", "+123", "1.23E2", "1.2345e+2", "-1.2345e2", "+w" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 660 | }; |
| 661 | n = sizeof(azStr)/sizeof(azStr[0]); |
| 662 | qsort(azStr, n, sizeof(azStr[0]), sortCmp); |
| 663 | for(i=0; i<n; i++){ |
| 664 | printf("%s\n", azStr[i]); |
| 665 | } |
| 666 | printf("Sanity1..."); |
| 667 | fflush(stdout); |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 668 | cnt = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 669 | for(i=0; i<n-1; i++){ |
| 670 | char *a = azStr[i]; |
| 671 | for(j=i+1; j<n; j++){ |
| 672 | char *b = azStr[j]; |
| 673 | if( sqliteCompare(a,b) != -sqliteCompare(b,a) ){ |
| 674 | printf("Failed! \"%s\" vs \"%s\"\n", a, b); |
| 675 | i = j = n; |
| 676 | } |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 677 | cnt++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | if( i<n ){ |
drh | a5c2ad0 | 2000-09-14 01:21:10 +0000 | [diff] [blame] | 681 | printf(" OK (%d)\n", cnt); |
| 682 | } |
| 683 | printf("Sanity2..."); |
| 684 | fflush(stdout); |
| 685 | cnt = 0; |
| 686 | for(i=0; i<n; i++){ |
| 687 | char *a = azStr[i]; |
| 688 | for(j=0; j<n; j++){ |
| 689 | char *b = azStr[j]; |
| 690 | for(k=0; k<n; k++){ |
| 691 | char *c = azStr[k]; |
| 692 | int x1, x2, x3, success; |
| 693 | x1 = sqliteCompare(a,b); |
| 694 | x2 = sqliteCompare(b,c); |
| 695 | x3 = sqliteCompare(a,c); |
| 696 | if( x1==0 ){ |
| 697 | success = x2==x3; |
| 698 | }else if( x1<0 ){ |
| 699 | success = (x2<=0 && x3<=0) || x2>0; |
| 700 | }else{ |
| 701 | success = (x2>=0 && x3>=0) || x2<0; |
| 702 | } |
| 703 | if( !success ){ |
| 704 | printf("Failed! \"%s\" vs \"%s\" vs \"%s\"\n", a, b, c); |
| 705 | i = j = k = n+1; |
| 706 | } |
| 707 | cnt++; |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | if( i<n+1 ){ |
| 712 | printf(" OK (%d)\n", cnt); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 713 | } |
| 714 | return 0; |
| 715 | } |
| 716 | #endif |
| 717 | |
| 718 | /* |
drh | 16e5955 | 2000-07-31 11:57:37 +0000 | [diff] [blame] | 719 | ** This routine is used for sorting. Each key is a list of one or more |
| 720 | ** null-terminated strings. The list is terminated by two nulls in |
| 721 | ** a row. For example, the following text is key with three strings: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 722 | ** |
| 723 | ** +one\000-two\000+three\000\000 |
| 724 | ** |
| 725 | ** Both arguments will have the same number of strings. This routine |
| 726 | ** returns negative, zero, or positive if the first argument is less |
| 727 | ** than, equal to, or greater than the first. (Result is a-b). |
| 728 | ** |
| 729 | ** Every string begins with either a "+" or "-" character. If the |
| 730 | ** character is "-" then the return value is negated. This is done |
| 731 | ** to implement a sort in descending order. |
| 732 | */ |
| 733 | int sqliteSortCompare(const char *a, const char *b){ |
| 734 | int len; |
| 735 | int res = 0; |
| 736 | |
| 737 | while( res==0 && *a && *b ){ |
| 738 | res = sqliteCompare(&a[1], &b[1]); |
| 739 | if( res==0 ){ |
| 740 | len = strlen(a) + 1; |
| 741 | a += len; |
| 742 | b += len; |
| 743 | } |
| 744 | } |
| 745 | if( *a=='-' ) res = -res; |
| 746 | return res; |
| 747 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 748 | |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 749 | #ifdef SQLITE_UTF8 |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 750 | /* |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 751 | ** X is a pointer to the first byte of a UTF-8 character. Increment |
| 752 | ** X so that it points to the next character. This only works right |
| 753 | ** if X points to a well-formed UTF-8 string. |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 754 | */ |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 755 | #define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){} |
| 756 | #define sqliteCharVal(X) sqlite_utf8_to_int(X) |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 757 | |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 758 | #else /* !defined(SQLITE_UTF8) */ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 759 | /* |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 760 | ** For iso8859 encoding, the next character is just the next byte. |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 761 | */ |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 762 | #define sqliteNextChar(X) (++(X)); |
| 763 | #define sqliteCharVal(X) ((int)*(X)) |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 764 | |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 765 | #endif /* defined(SQLITE_UTF8) */ |
| 766 | |
| 767 | |
| 768 | #ifdef SQLITE_UTF8 |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 769 | /* |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 770 | ** Convert the UTF-8 character to which z points into a 31-bit |
| 771 | ** UCS character. This only works right if z points to a well-formed |
| 772 | ** UTF-8 string. |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 773 | */ |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 774 | static int sqlite_utf8_to_int(const unsigned char *z){ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 775 | int c; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 776 | static const int initVal[] = { |
| 777 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| 778 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, |
| 779 | 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, |
| 780 | 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, |
| 781 | 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, |
| 782 | 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, |
| 783 | 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, |
| 784 | 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, |
| 785 | 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, |
| 786 | 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, |
| 787 | 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, |
| 788 | 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, |
| 789 | 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 0, 1, 2, |
| 790 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, |
| 791 | 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, |
| 792 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 793 | 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 254, |
| 794 | 255, |
| 795 | }; |
| 796 | c = initVal[*(z++)]; |
| 797 | while( (0xc0&*z)==0x80 ){ |
| 798 | c = (c<<6) | (0x3f&*(z++)); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 799 | } |
| 800 | return c; |
| 801 | } |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 802 | #endif |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 803 | |
| 804 | /* |
| 805 | ** Compare two UTF-8 strings for equality where the first string can |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 806 | ** potentially be a "glob" expression. Return true (1) if they |
| 807 | ** are the same and false (0) if they are different. |
| 808 | ** |
| 809 | ** Globbing rules: |
| 810 | ** |
| 811 | ** '*' Matches any sequence of zero or more characters. |
| 812 | ** |
| 813 | ** '?' Matches exactly one character. |
| 814 | ** |
| 815 | ** [...] Matches one character from the enclosed list of |
| 816 | ** characters. |
| 817 | ** |
| 818 | ** [^...] Matches one character not in the enclosed list. |
| 819 | ** |
| 820 | ** With the [...] and [^...] matching, a ']' character can be included |
| 821 | ** in the list by making it the first character after '[' or '^'. A |
| 822 | ** range of characters can be specified using '-'. Example: |
| 823 | ** "[a-z]" matches any single lower-case letter. To match a '-', make |
| 824 | ** it the last character in the list. |
| 825 | ** |
| 826 | ** This routine is usually quick, but can be N**2 in the worst case. |
| 827 | ** |
| 828 | ** Hints: to match '*' or '?', put them in "[]". Like this: |
| 829 | ** |
| 830 | ** abc[*]xyz Matches "abc*xyz" only |
| 831 | */ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 832 | int |
| 833 | sqliteGlobCompare(const unsigned char *zPattern, const unsigned char *zString){ |
| 834 | register int c; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 835 | int invert; |
| 836 | int seen; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 837 | int c2; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 838 | |
| 839 | while( (c = *zPattern)!=0 ){ |
| 840 | switch( c ){ |
| 841 | case '*': |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 842 | while( (c=zPattern[1]) == '*' || c == '?' ){ |
| 843 | if( c=='?' ){ |
| 844 | if( *zString==0 ) return 0; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 845 | sqliteNextChar(zString); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 846 | } |
| 847 | zPattern++; |
| 848 | } |
| 849 | if( c==0 ) return 1; |
| 850 | c = UpperToLower[c]; |
| 851 | if( c=='[' ){ |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 852 | while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){ |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 853 | sqliteNextChar(zString); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 854 | } |
| 855 | return *zString!=0; |
| 856 | }else{ |
| 857 | while( (c2 = *zString)!=0 ){ |
| 858 | while( c2 != 0 && c2 != c ){ c2 = *++zString; } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 859 | if( c2==0 ) return 0; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 860 | if( sqliteGlobCompare(&zPattern[1],zString) ) return 1; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 861 | sqliteNextChar(zString); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 862 | } |
| 863 | return 0; |
| 864 | } |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 865 | case '?': { |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 866 | if( *zString==0 ) return 0; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 867 | sqliteNextChar(zString); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 868 | zPattern++; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 869 | break; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 870 | } |
| 871 | case '[': { |
| 872 | int prior_c = 0; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 873 | seen = 0; |
| 874 | invert = 0; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 875 | c = sqliteCharVal(zString); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 876 | if( c==0 ) return 0; |
| 877 | c2 = *++zPattern; |
| 878 | if( c2=='^' ){ invert = 1; c2 = *++zPattern; } |
| 879 | if( c2==']' ){ |
| 880 | if( c==']' ) seen = 1; |
| 881 | c2 = *++zPattern; |
| 882 | } |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 883 | while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 884 | if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){ |
| 885 | zPattern++; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 886 | c2 = sqliteCharVal(zPattern); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 887 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 888 | prior_c = 0; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 889 | }else if( c==c2 ){ |
| 890 | seen = 1; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 891 | prior_c = c2; |
| 892 | }else{ |
| 893 | prior_c = c2; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 894 | } |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 895 | sqliteNextChar(zPattern); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 896 | } |
| 897 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 898 | sqliteNextChar(zString); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 899 | zPattern++; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 900 | break; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 901 | } |
| 902 | default: { |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 903 | if( c != *zString ) return 0; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 904 | zPattern++; |
| 905 | zString++; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 906 | break; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 907 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 908 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 909 | } |
| 910 | return *zString==0; |
| 911 | } |
| 912 | |
| 913 | /* |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 914 | ** Compare two UTF-8 strings for equality using the "LIKE" operator of |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 915 | ** SQL. The '%' character matches any sequence of 0 or more |
| 916 | ** characters and '_' matches any single character. Case is |
| 917 | ** not significant. |
| 918 | ** |
| 919 | ** This routine is just an adaptation of the sqliteGlobCompare() |
| 920 | ** routine above. |
| 921 | */ |
| 922 | int |
| 923 | sqliteLikeCompare(const unsigned char *zPattern, const unsigned char *zString){ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 924 | register int c; |
| 925 | int c2; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 926 | |
| 927 | while( (c = UpperToLower[*zPattern])!=0 ){ |
| 928 | switch( c ){ |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 929 | case '%': { |
| 930 | while( (c=zPattern[1]) == '%' || c == '_' ){ |
| 931 | if( c=='_' ){ |
| 932 | if( *zString==0 ) return 0; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 933 | sqliteNextChar(zString); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 934 | } |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 935 | zPattern++; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 936 | } |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 937 | if( c==0 ) return 1; |
| 938 | c = UpperToLower[c]; |
| 939 | while( (c2=UpperToLower[*zString])!=0 ){ |
| 940 | while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; } |
| 941 | if( c2==0 ) return 0; |
| 942 | if( sqliteLikeCompare(&zPattern[1],zString) ) return 1; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 943 | sqliteNextChar(zString); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 944 | } |
| 945 | return 0; |
| 946 | } |
| 947 | case '_': { |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 948 | if( *zString==0 ) return 0; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 949 | sqliteNextChar(zString); |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 950 | zPattern++; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 951 | break; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 952 | } |
| 953 | default: { |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 954 | if( c != UpperToLower[*zString] ) return 0; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 955 | zPattern++; |
| 956 | zString++; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 957 | break; |
drh | e17a7e3 | 2001-04-04 21:10:18 +0000 | [diff] [blame] | 958 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 959 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 960 | } |
| 961 | return *zString==0; |
| 962 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 963 | |
| 964 | /* |
| 965 | ** Return a static string that describes the kind of error specified in the |
| 966 | ** argument. |
| 967 | */ |
| 968 | const char *sqliteErrStr(int rc){ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 969 | const char *z; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 970 | switch( rc ){ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 971 | case SQLITE_OK: z = "not an error"; break; |
| 972 | case SQLITE_ERROR: z = "SQL logic error or missing database"; break; |
| 973 | case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break; |
| 974 | case SQLITE_PERM: z = "access permission denied"; break; |
| 975 | case SQLITE_ABORT: z = "callback requested query abort"; break; |
| 976 | case SQLITE_BUSY: z = "database in use by another process"; break; |
| 977 | case SQLITE_NOMEM: z = "out of memory"; break; |
| 978 | case SQLITE_READONLY: z = "attempt to write a readonly database"; break; |
| 979 | case SQLITE_INTERRUPT: z = "interrupted"; break; |
| 980 | case SQLITE_IOERR: z = "disk I/O error"; break; |
| 981 | case SQLITE_CORRUPT: z = "database disk image is malformed"; break; |
| 982 | case SQLITE_NOTFOUND: z = "table or record not found"; break; |
| 983 | case SQLITE_FULL: z = "database is full"; break; |
| 984 | case SQLITE_CANTOPEN: z = "unable to open database file"; break; |
| 985 | case SQLITE_PROTOCOL: z = "database locking protocol failure"; break; |
| 986 | case SQLITE_EMPTY: z = "table contains no data"; break; |
| 987 | case SQLITE_SCHEMA: z = "database schema has changed"; break; |
drh | 092d035 | 2001-09-15 13:15:12 +0000 | [diff] [blame] | 988 | case SQLITE_TOOBIG: z = "too much data for one table row"; break; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 989 | default: z = "unknown error"; break; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 990 | } |
| 991 | return z; |
| 992 | } |