📄 util.c
字号:
/*** 2001 September 15**** The author disclaims copyright to this source code. In place of** a legal notice, here is a blessing:**** May you do good and not evil.** May you find forgiveness for yourself and forgive others.** May you share freely, never taking more than you give.***************************************************************************** Utility functions used throughout sqlite.**** This file contains functions for allocating memory, comparing** strings, and stuff like that.**** $Id: util.c,v 1.207 2007/06/26 00:37:28 drh Exp $*/#include "sqliteInt.h"#include "os.h"#include <stdarg.h>#include <ctype.h>/*** Set the most recent error code and error string for the sqlite** handle "db". The error code is set to "err_code".**** If it is not NULL, string zFormat specifies the format of the** error string in the style of the printf functions: The following** format characters are allowed:**** %s Insert a string** %z A string that should be freed after use** %d Insert an integer** %T Insert a token** %S Insert the first element of a SrcList**** zFormat and any string tokens that follow it are assumed to be** encoded in UTF-8.**** To clear the most recent error for sqlite handle "db", sqlite3Error** should be called with err_code set to SQLITE_OK and zFormat set** to NULL.*/void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){ if( db && (db->pErr || (db->pErr = sqlite3ValueNew())!=0) ){ db->errCode = err_code; if( zFormat ){ char *z; va_list ap; va_start(ap, zFormat); z = sqlite3VMPrintf(zFormat, ap); va_end(ap); sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3FreeX); }else{ sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC); } }}/*** Add an error message to pParse->zErrMsg and increment pParse->nErr.** The following formatting characters are allowed:**** %s Insert a string** %z A string that should be freed after use** %d Insert an integer** %T Insert a token** %S Insert the first element of a SrcList**** This function should be used to report any error that occurs whilst** compiling an SQL statement (i.e. within sqlite3_prepare()). The** last thing the sqlite3_prepare() function does is copy the error** stored by this function into the database handle using sqlite3Error().** Function sqlite3Error() should be used during statement execution** (sqlite3_step() etc.).*/void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ va_list ap; pParse->nErr++; sqliteFree(pParse->zErrMsg); va_start(ap, zFormat); pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap); va_end(ap); if( pParse->rc==SQLITE_OK ){ pParse->rc = SQLITE_ERROR; }}/*** Clear the error message in pParse, if any*/void sqlite3ErrorClear(Parse *pParse){ sqliteFree(pParse->zErrMsg); pParse->zErrMsg = 0; pParse->nErr = 0;}/*** Convert an SQL-style quoted string into a normal string by removing** the quote characters. The conversion is done in-place. If the** input does not begin with a quote character, then this routine** is a no-op.**** 2002-Feb-14: This routine is extended to remove MS-Access style** brackets from around identifers. For example: "[a-b-c]" becomes** "a-b-c".*/void sqlite3Dequote(char *z){ int quote; int i, j; if( z==0 ) return; quote = z[0]; switch( quote ){ case '\'': break; case '"': break; case '`': break; /* For MySQL compatibility */ case '[': quote = ']'; break; /* For MS SqlServer compatibility */ default: return; } for(i=1, j=0; z[i]; i++){ if( z[i]==quote ){ if( z[i+1]==quote ){ z[j++] = quote; i++; }else{ z[j++] = 0; break; } }else{ z[j++] = z[i]; } }}/* An array to map all upper-case characters into their corresponding** lower-case character. */const unsigned char sqlite3UpperToLower[] = {#ifdef SQLITE_ASCII 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103, 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121, 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107, 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161, 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179, 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197, 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215, 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233, 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251, 252,253,254,255#endif#ifdef SQLITE_EBCDIC 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */ 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */ 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */ 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */ 96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */ 112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */ 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */ 144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */ 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */ 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */ 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */ 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */ 224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */ 239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */#endif};#define UpperToLower sqlite3UpperToLower/*** Some systems have stricmp(). Others have strcasecmp(). Because** there is no consistency, we will define our own.*/int sqlite3StrICmp(const char *zLeft, const char *zRight){ register unsigned char *a, *b; a = (unsigned char *)zLeft; b = (unsigned char *)zRight; while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } return UpperToLower[*a] - UpperToLower[*b];}int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){ register unsigned char *a, *b; a = (unsigned char *)zLeft; b = (unsigned char *)zRight; while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];}/*** Return TRUE if z is a pure numeric string. Return FALSE if the** string contains any character which is not part of a number. If** the string is numeric and contains the '.' character, set *realnum** to TRUE (otherwise FALSE).**** An empty string is considered non-numeric.*/int sqlite3IsNumber(const char *z, int *realnum, u8 enc){ int incr = (enc==SQLITE_UTF8?1:2); if( enc==SQLITE_UTF16BE ) z++; if( *z=='-' || *z=='+' ) z += incr; if( !isdigit(*(u8*)z) ){ return 0; } z += incr; if( realnum ) *realnum = 0; while( isdigit(*(u8*)z) ){ z += incr; } if( *z=='.' ){ z += incr; if( !isdigit(*(u8*)z) ) return 0; while( isdigit(*(u8*)z) ){ z += incr; } if( realnum ) *realnum = 1; } if( *z=='e' || *z=='E' ){ z += incr; if( *z=='+' || *z=='-' ) z += incr; if( !isdigit(*(u8*)z) ) return 0; while( isdigit(*(u8*)z) ){ z += incr; } if( realnum ) *realnum = 1; } return *z==0;}/*** The string z[] is an ascii representation of a real number.** Convert this string to a double.**** This routine assumes that z[] really is a valid number. If it** is not, the result is undefined.**** This routine is used instead of the library atof() function because** the library atof() might want to use "," as the decimal point instead** of "." depending on how locale is set. But that would cause problems** for SQL. So this routine always uses "." regardless of locale.*/int sqlite3AtoF(const char *z, double *pResult){#ifndef SQLITE_OMIT_FLOATING_POINT int sign = 1; const char *zBegin = z; LONGDOUBLE_TYPE v1 = 0.0; while( isspace(*(u8*)z) ) z++; if( *z=='-' ){ sign = -1; z++; }else if( *z=='+' ){ z++; } while( isdigit(*(u8*)z) ){ v1 = v1*10.0 + (*z - '0'); z++; } if( *z=='.' ){ LONGDOUBLE_TYPE divisor = 1.0; z++; while( isdigit(*(u8*)z) ){ v1 = v1*10.0 + (*z - '0'); divisor *= 10.0; z++; } v1 /= divisor; } if( *z=='e' || *z=='E' ){ int esign = 1; int eval = 0; LONGDOUBLE_TYPE scale = 1.0; z++; if( *z=='-' ){ esign = -1; z++; }else if( *z=='+' ){ z++; } while( isdigit(*(u8*)z) ){ eval = eval*10 + *z - '0'; z++; } while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; } while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; } while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; } while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; } if( esign<0 ){ v1 /= scale; }else{ v1 *= scale; } } *pResult = sign<0 ? -v1 : v1; return z - zBegin;#else return sqlite3Atoi64(z, pResult);#endif /* SQLITE_OMIT_FLOATING_POINT */}/*** Compare the 19-character string zNum against the text representation** value 2^63: 9223372036854775808. Return negative, zero, or positive** if zNum is less than, equal to, or greater than the string.**** Unlike memcmp() this routine is guaranteed to return the difference** in the values of the last digit if the only difference is in the** last digit. So, for example,**** compare2pow63("9223372036854775800")**** will return -8.*/static int compare2pow63(const char *zNum){ int c; c = memcmp(zNum,"922337203685477580",18); if( c==0 ){ c = zNum[18] - '8'; } return c;}/*** Return TRUE if zNum is a 64-bit signed integer and write** the value of the integer into *pNum. If zNum is not an integer** or is an integer that is too large to be expressed with 64 bits,** then return false.**** When this routine was originally written it dealt with only** 32-bit numbers. At that time, it was much faster than the** atoi() library routine in RedHat 7.2.*/int sqlite3Atoi64(const char *zNum, i64 *pNum){ i64 v = 0; int neg; int i, c; while( isspace(*(u8*)zNum) ) zNum++; if( *zNum=='-' ){ neg = 1; zNum++; }else if( *zNum=='+' ){ neg = 0; zNum++; }else{ neg = 0; } while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */ for(i=0; (c=zNum[i])>='0' && c<='9'; i++){ v = v*10 + c - '0'; } *pNum = neg ? -v : v; if( c!=0 || i==0 || i>19 ){ /* zNum is empty or contains non-numeric text or is longer ** than 19 digits (thus guaranting that it is too large) */ return 0; }else if( i<19 ){ /* Less than 19 digits, so we know that it fits in 64 bits */ return 1; }else{ /* 19-digit numbers must be no larger than 9223372036854775807 if positive ** or 9223372036854775808 if negative. Note that 9223372036854665808 ** is 2^63. */ return compare2pow63(zNum)<neg; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -