blob: 65ccc1ab8105e7a661bb293eace1d2feb3086607 [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**
drhbec2bf42000-05-29 23:48:22 +000029** $Id: util.c,v 1.4 2000/05/29 23:48:23 drh Exp $
drh75897232000-05-29 14:26:00 +000030*/
31#include "sqliteInt.h"
32#include <stdarg.h>
33#include <ctype.h>
34
35/*
36** Allocate new memory and set it to zero. Return NULL if
37** no memory is available.
38*/
39void *sqliteMalloc(int n){
40 void *p = malloc(n);
drh305cea62000-05-29 17:44:25 +000041 /* printf("alloc 0x%x size: %d bytes\n", (int)p, n); */
drh75897232000-05-29 14:26:00 +000042 if( p==0 ) return 0;
43 memset(p, 0, n);
44 return p;
45}
46
47/*
48** Free memory previously obtained from sqliteMalloc()
49*/
50void sqliteFree(void *p){
drh305cea62000-05-29 17:44:25 +000051 if( p ){
52 /* printf("free 0x%x\n", (int)p); */
53 free(p);
54 }
drh75897232000-05-29 14:26:00 +000055}
56
57/*
58** Resize a prior allocation. If p==0, then this routine
59** works just like sqliteMalloc(). If n==0, then this routine
60** works just like sqliteFree().
61*/
62void *sqliteRealloc(void *p, int n){
63 if( p==0 ){
64 return sqliteMalloc(n);
65 }
66 if( n==0 ){
67 sqliteFree(p);
68 return 0;
69 }
drhb24fcbe2000-05-29 23:30:50 +000070 /* printf("realloc 0x%x size: %d bytes\n", (int)p, n); */
drh75897232000-05-29 14:26:00 +000071 return realloc(p, n);
72}
73
74/*
75** Create a string from the 2nd and subsequent arguments (up to the
76** first NULL argument), store the string in memory obtained from
77** sqliteMalloc() and make the pointer indicated by the 1st argument
78** point to that string.
79*/
80void sqliteSetString(char **pz, const char *zFirst, ...){
81 va_list ap;
82 int nByte;
83 const char *z;
84 char *zResult;
85
86 if( pz==0 ) return;
87 nByte = strlen(zFirst) + 1;
88 va_start(ap, zFirst);
89 while( (z = va_arg(ap, const char*))!=0 ){
90 nByte += strlen(z);
91 }
92 va_end(ap);
93 sqliteFree(*pz);
94 *pz = zResult = sqliteMalloc( nByte );
95 if( zResult==0 ) return;
96 strcpy(zResult, zFirst);
97 zResult += strlen(zResult);
98 va_start(ap, zFirst);
99 while( (z = va_arg(ap, const char*))!=0 ){
100 strcpy(zResult, z);
101 zResult += strlen(zResult);
102 }
103 va_end(ap);
104}
105
106/*
107** Works like sqliteSetString, but each string is now followed by
108** a length integer. -1 means use the whole string.
109*/
110void sqliteSetNString(char **pz, ...){
111 va_list ap;
112 int nByte;
113 const char *z;
114 char *zResult;
115 int n;
116
117 if( pz==0 ) return;
118 nByte = 0;
119 va_start(ap, pz);
120 while( (z = va_arg(ap, const char*))!=0 ){
121 n = va_arg(ap, int);
122 if( n<=0 ) n = strlen(z);
123 nByte += n;
124 }
125 va_end(ap);
126 sqliteFree(*pz);
127 *pz = zResult = sqliteMalloc( nByte + 1 );
128 if( zResult==0 ) return;
129 va_start(ap, pz);
130 while( (z = va_arg(ap, const char*))!=0 ){
131 n = va_arg(ap, int);
132 if( n<=0 ) n = strlen(z);
133 strncpy(zResult, z, n);
134 zResult += n;
135 }
136 *zResult = 0;
137 va_end(ap);
138}
139
140/* An array to map all upper-case characters into their corresponding
141** lower-case character.
142*/
143static unsigned char UpperToLower[] = {
144 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
145 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
146 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
147 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
148 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
149 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
150 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
151 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
152 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
153 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
154 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
155 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
156 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
157 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
158 252,253,254,255
159};
160
161/*
162** This function computes a hash on the name of a keyword.
163** Case is not significant.
164*/
165int sqliteHashNoCase(const char *z, int n){
166 int h = 0;
167 int c;
168 if( n<=0 ) n = strlen(z);
169 while( n-- > 0 && (c = *z++)!=0 ){
170 h = h<<3 ^ h ^ UpperToLower[c];
171 }
172 if( h<0 ) h = -h;
173 return h;
174}
175
176/*
177** Some system shave stricmp(). Others have strcasecmp(). Because
178** there is no consistency, we will define our own.
179*/
180int sqliteStrICmp(const char *zLeft, const char *zRight){
181 register unsigned char *a, *b;
182 a = (unsigned char *)zLeft;
183 b = (unsigned char *)zRight;
184 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
185 return *a - *b;
186}
187int sqliteStrNICmp(const char *zLeft, const char *zRight, int N){
188 register unsigned char *a, *b;
189 a = (unsigned char *)zLeft;
190 b = (unsigned char *)zRight;
191 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
drhbec2bf42000-05-29 23:48:22 +0000192 return N<0 ? 0 : *a - *b;
drh75897232000-05-29 14:26:00 +0000193}
194
195/* Notes on string comparisions.
196**
197** We want the main string comparision function used for sorting to
198** sort both numbers and alphanumeric words into the correct sequence.
199** The same routine should do both without prior knowledge of which
200** type of text the input represents. It should even work for strings
201** which are a mixture of text and numbers.
202**
203** To accomplish this, we keep track of a state number while scanning
204** the two strings. The states are as follows:
205**
206** 1 Beginning of word
207** 2 Arbitrary text
208** 3 Integer
209** 4 Negative integer
210** 5 Real number
211** 6 Negative real
212**
213** The scan begins in state 1, beginning of word. Transitions to other
214** states are determined by characters seen, as shown in the following
215** chart:
216**
217** Current State Character Seen New State
218** -------------------- -------------- -------------------
219** 0 Beginning of word "-" 3 Negative integer
220** digit 2 Integer
221** space 0 Beginning of word
222** otherwise 1 Arbitrary text
223**
224** 1 Arbitrary text space 0 Beginning of word
225** digit 2 Integer
226** otherwise 1 Arbitrary text
227**
228** 2 Integer space 0 Beginning of word
229** "." 4 Real number
230** digit 2 Integer
231** otherwise 1 Arbitrary text
232**
233** 3 Negative integer space 0 Beginning of word
234** "." 5 Negative Real num
235** digit 3 Negative integer
236** otherwise 1 Arbitrary text
237**
238** 4 Real number space 0 Beginning of word
239** digit 4 Real number
240** otherwise 1 Arbitrary text
241**
242** 5 Negative real num space 0 Beginning of word
243** digit 5 Negative real num
244** otherwise 1 Arbitrary text
245**
246** To implement this state machine, we first classify each character
247** into on of the following categories:
248**
249** 0 Text
250** 1 Space
251** 2 Digit
252** 3 "-"
253** 4 "."
254**
255** Given an arbitrary character, the array charClass[] maps that character
256** into one of the atove categories.
257*/
258static const unsigned char charClass[] = {
259 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
260/* 0x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
261/* 1x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
262/* 2x */ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0,
263/* 3x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0,
264/* 4x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
265/* 5x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
266/* 6x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
267/* 7x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
268/* 8x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
269/* 9x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
270/* Ax */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
271/* Bx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
272/* Cx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
273/* Dx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
274/* Ex */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
275/* Fx */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
276};
277#define N_CHAR_CLASS 5
278
279/*
280** Given the current state number (0 thru 5), this array figures
281** the new state number given the character class.
282*/
283static const unsigned char stateMachine[] = {
284 /* Text, Space, Digit, "-", "." */
285 1, 0, 2, 3, 1, /* State 0: Beginning of word */
286 1, 0, 2, 1, 1, /* State 1: Arbitrary text */
287 1, 0, 2, 1, 4, /* State 2: Integer */
288 1, 0, 3, 1, 5, /* State 3: Negative integer */
289 1, 0, 4, 1, 1, /* State 4: Real number */
290 1, 0, 5, 1, 1, /* State 5: Negative real num */
291};
292
293/* This routine does a comparison of two strings. Case is used only
294** if useCase!=0. Numbers compare in numerical order.
295*/
296static int privateStrCmp(const char *atext, const char *btext, int useCase){
297 register unsigned char *a, *b, *map, ca, cb;
298 int result;
299 register int cclass = 0;
300
301 a = (unsigned char *)atext;
302 b = (unsigned char *)btext;
303 if( useCase ){
304 do{
305 if( (ca= *a++)!=(cb= *b++) ) break;
306 cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]];
307 }while( ca!=0 );
308 }else{
309 map = UpperToLower;
310 do{
311 if( (ca=map[*a++])!=(cb=map[*b++]) ) break;
312 cclass = stateMachine[cclass*N_CHAR_CLASS + charClass[ca]];
313 }while( ca!=0 );
314 }
315 switch( cclass ){
316 case 0:
317 case 1: {
318 if( isdigit(ca) && isdigit(cb) ){
319 cclass = 2;
320 }
321 break;
322 }
323 default: {
324 break;
325 }
326 }
327 switch( cclass ){
328 case 2:
329 case 3: {
330 if( isdigit(ca) ){
331 if( isdigit(cb) ){
332 int acnt, bcnt;
333 acnt = bcnt = 0;
334 while( isdigit(*a++) ) acnt++;
335 while( isdigit(*b++) ) bcnt++;
336 result = acnt - bcnt;
337 if( result==0 ) result = ca-cb;
338 }else{
339 result = 1;
340 }
341 }else if( isdigit(cb) ){
342 result = -1;
343 }else if( ca=='.' ){
344 result = 1;
345 }else if( cb=='.' ){
346 result = -1;
347 }else{
348 result = ca - cb;
349 cclass = 2;
350 }
351 if( cclass==3 ) result = -result;
352 break;
353 }
354 case 0:
355 case 1:
356 case 4: {
357 result = ca - cb;
358 break;
359 }
360 case 5: {
361 result = cb - ca;
362 };
363 }
364 return result;
365}
366
367/* This comparison routine is what we use for comparison operations
368** in an SQL expression. (Ex: name<'Hello' or value<5). Compare two
369** strings. Use case only as a tie-breaker. Numbers compare in
370** numerical order.
371*/
372int sqliteCompare(const char *atext, const char *btext){
373 int result;
374 result = privateStrCmp(atext, btext, 0);
375 if( result==0 ) result = privateStrCmp(atext, btext, 1);
376 return result;
377}
378
379/*
380** If you compile just this one file with the -DTEST_COMPARE=1 option,
381** it generates a program to test the comparisons routines.
382*/
383#ifdef TEST_COMPARE
384#include <stdlib.h>
385#include <stdio.h>
386int sortCmp(const char **a, const char **b){
387 return sqliteCompare(*a, *b);
388}
389int main(int argc, char **argv){
390 int i, j, k, n;
391 static char *azStr[] = {
392 "abc", "aBc", "abcd", "aBcd",
393 "123", "124", "1234", "-123", "-124", "-1234",
394 "123.45", "123.456", "123.46", "-123.45", "-123.46", "-123.456",
395 "x9", "x10", "x-9", "x-10", "X9", "X10",
396 };
397 n = sizeof(azStr)/sizeof(azStr[0]);
398 qsort(azStr, n, sizeof(azStr[0]), sortCmp);
399 for(i=0; i<n; i++){
400 printf("%s\n", azStr[i]);
401 }
402 printf("Sanity1...");
403 fflush(stdout);
404 for(i=0; i<n-1; i++){
405 char *a = azStr[i];
406 for(j=i+1; j<n; j++){
407 char *b = azStr[j];
408 if( sqliteCompare(a,b) != -sqliteCompare(b,a) ){
409 printf("Failed! \"%s\" vs \"%s\"\n", a, b);
410 i = j = n;
411 }
412 }
413 }
414 if( i<n ){
415 printf(" OK\n");
416 }
417 return 0;
418}
419#endif
420
421/*
422** This routine is used for sorting. Each key is a list one or more
423** null-terminated strings. The list is terminated by two null in
424** a row. For example, the following text is strings:
425**
426** +one\000-two\000+three\000\000
427**
428** Both arguments will have the same number of strings. This routine
429** returns negative, zero, or positive if the first argument is less
430** than, equal to, or greater than the first. (Result is a-b).
431**
432** Every string begins with either a "+" or "-" character. If the
433** character is "-" then the return value is negated. This is done
434** to implement a sort in descending order.
435*/
436int sqliteSortCompare(const char *a, const char *b){
437 int len;
438 int res = 0;
439
440 while( res==0 && *a && *b ){
441 res = sqliteCompare(&a[1], &b[1]);
442 if( res==0 ){
443 len = strlen(a) + 1;
444 a += len;
445 b += len;
446 }
447 }
448 if( *a=='-' ) res = -res;
449 return res;
450}