OLD | NEW |
1 /* | 1 /* |
2 ** 2001 September 15 | 2 ** 2001 September 15 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
(...skipping 24 matching lines...) Expand all Loading... |
35 ** Give a callback to the test harness that can be used to simulate faults | 35 ** Give a callback to the test harness that can be used to simulate faults |
36 ** in places where it is difficult or expensive to do so purely by means | 36 ** in places where it is difficult or expensive to do so purely by means |
37 ** of inputs. | 37 ** of inputs. |
38 ** | 38 ** |
39 ** The intent of the integer argument is to let the fault simulator know | 39 ** The intent of the integer argument is to let the fault simulator know |
40 ** which of multiple sqlite3FaultSim() calls has been hit. | 40 ** which of multiple sqlite3FaultSim() calls has been hit. |
41 ** | 41 ** |
42 ** Return whatever integer value the test callback returns, or return | 42 ** Return whatever integer value the test callback returns, or return |
43 ** SQLITE_OK if no test callback is installed. | 43 ** SQLITE_OK if no test callback is installed. |
44 */ | 44 */ |
45 #ifndef SQLITE_OMIT_BUILTIN_TEST | 45 #ifndef SQLITE_UNTESTABLE |
46 int sqlite3FaultSim(int iTest){ | 46 int sqlite3FaultSim(int iTest){ |
47 int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback; | 47 int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback; |
48 return xCallback ? xCallback(iTest) : SQLITE_OK; | 48 return xCallback ? xCallback(iTest) : SQLITE_OK; |
49 } | 49 } |
50 #endif | 50 #endif |
51 | 51 |
52 #ifndef SQLITE_OMIT_FLOATING_POINT | 52 #ifndef SQLITE_OMIT_FLOATING_POINT |
53 /* | 53 /* |
54 ** Return true if the floating point value is Not a Number (NaN). | 54 ** Return true if the floating point value is Not a Number (NaN). |
55 ** | 55 ** |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 ** The value returned will never be negative. Nor will it ever be greater | 103 ** The value returned will never be negative. Nor will it ever be greater |
104 ** than the actual length of the string. For very long strings (greater | 104 ** than the actual length of the string. For very long strings (greater |
105 ** than 1GiB) the value returned might be less than the true string length. | 105 ** than 1GiB) the value returned might be less than the true string length. |
106 */ | 106 */ |
107 int sqlite3Strlen30(const char *z){ | 107 int sqlite3Strlen30(const char *z){ |
108 if( z==0 ) return 0; | 108 if( z==0 ) return 0; |
109 return 0x3fffffff & (int)strlen(z); | 109 return 0x3fffffff & (int)strlen(z); |
110 } | 110 } |
111 | 111 |
112 /* | 112 /* |
| 113 ** Return the declared type of a column. Or return zDflt if the column |
| 114 ** has no declared type. |
| 115 ** |
| 116 ** The column type is an extra string stored after the zero-terminator on |
| 117 ** the column name if and only if the COLFLAG_HASTYPE flag is set. |
| 118 */ |
| 119 char *sqlite3ColumnType(Column *pCol, char *zDflt){ |
| 120 if( (pCol->colFlags & COLFLAG_HASTYPE)==0 ) return zDflt; |
| 121 return pCol->zName + strlen(pCol->zName) + 1; |
| 122 } |
| 123 |
| 124 /* |
| 125 ** Helper function for sqlite3Error() - called rarely. Broken out into |
| 126 ** a separate routine to avoid unnecessary register saves on entry to |
| 127 ** sqlite3Error(). |
| 128 */ |
| 129 static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){ |
| 130 if( db->pErr ) sqlite3ValueSetNull(db->pErr); |
| 131 sqlite3SystemError(db, err_code); |
| 132 } |
| 133 |
| 134 /* |
113 ** Set the current error code to err_code and clear any prior error message. | 135 ** Set the current error code to err_code and clear any prior error message. |
| 136 ** Also set iSysErrno (by calling sqlite3System) if the err_code indicates |
| 137 ** that would be appropriate. |
114 */ | 138 */ |
115 void sqlite3Error(sqlite3 *db, int err_code){ | 139 void sqlite3Error(sqlite3 *db, int err_code){ |
116 assert( db!=0 ); | 140 assert( db!=0 ); |
117 db->errCode = err_code; | 141 db->errCode = err_code; |
118 if( db->pErr ) sqlite3ValueSetNull(db->pErr); | 142 if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code); |
119 } | 143 } |
120 | 144 |
121 /* | 145 /* |
| 146 ** Load the sqlite3.iSysErrno field if that is an appropriate thing |
| 147 ** to do based on the SQLite error code in rc. |
| 148 */ |
| 149 void sqlite3SystemError(sqlite3 *db, int rc){ |
| 150 if( rc==SQLITE_IOERR_NOMEM ) return; |
| 151 rc &= 0xff; |
| 152 if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){ |
| 153 db->iSysErrno = sqlite3OsGetLastError(db->pVfs); |
| 154 } |
| 155 } |
| 156 |
| 157 /* |
122 ** Set the most recent error code and error string for the sqlite | 158 ** Set the most recent error code and error string for the sqlite |
123 ** handle "db". The error code is set to "err_code". | 159 ** handle "db". The error code is set to "err_code". |
124 ** | 160 ** |
125 ** If it is not NULL, string zFormat specifies the format of the | 161 ** If it is not NULL, string zFormat specifies the format of the |
126 ** error string in the style of the printf functions: The following | 162 ** error string in the style of the printf functions: The following |
127 ** format characters are allowed: | 163 ** format characters are allowed: |
128 ** | 164 ** |
129 ** %s Insert a string | 165 ** %s Insert a string |
130 ** %z A string that should be freed after use | 166 ** %z A string that should be freed after use |
131 ** %d Insert an integer | 167 ** %d Insert an integer |
132 ** %T Insert a token | 168 ** %T Insert a token |
133 ** %S Insert the first element of a SrcList | 169 ** %S Insert the first element of a SrcList |
134 ** | 170 ** |
135 ** zFormat and any string tokens that follow it are assumed to be | 171 ** zFormat and any string tokens that follow it are assumed to be |
136 ** encoded in UTF-8. | 172 ** encoded in UTF-8. |
137 ** | 173 ** |
138 ** To clear the most recent error for sqlite handle "db", sqlite3Error | 174 ** To clear the most recent error for sqlite handle "db", sqlite3Error |
139 ** should be called with err_code set to SQLITE_OK and zFormat set | 175 ** should be called with err_code set to SQLITE_OK and zFormat set |
140 ** to NULL. | 176 ** to NULL. |
141 */ | 177 */ |
142 void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){ | 178 void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){ |
143 assert( db!=0 ); | 179 assert( db!=0 ); |
144 db->errCode = err_code; | 180 db->errCode = err_code; |
| 181 sqlite3SystemError(db, err_code); |
145 if( zFormat==0 ){ | 182 if( zFormat==0 ){ |
146 sqlite3Error(db, err_code); | 183 sqlite3Error(db, err_code); |
147 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){ | 184 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){ |
148 char *z; | 185 char *z; |
149 va_list ap; | 186 va_list ap; |
150 va_start(ap, zFormat); | 187 va_start(ap, zFormat); |
151 z = sqlite3VMPrintf(db, zFormat, ap); | 188 z = sqlite3VMPrintf(db, zFormat, ap); |
152 va_end(ap); | 189 va_end(ap); |
153 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC); | 190 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC); |
154 } | 191 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 ** is added to the dequoted string. | 235 ** is added to the dequoted string. |
199 ** | 236 ** |
200 ** The return value is -1 if no dequoting occurs or the length of the | 237 ** The return value is -1 if no dequoting occurs or the length of the |
201 ** dequoted string, exclusive of the zero terminator, if dequoting does | 238 ** dequoted string, exclusive of the zero terminator, if dequoting does |
202 ** occur. | 239 ** occur. |
203 ** | 240 ** |
204 ** 2002-Feb-14: This routine is extended to remove MS-Access style | 241 ** 2002-Feb-14: This routine is extended to remove MS-Access style |
205 ** brackets from around identifiers. For example: "[a-b-c]" becomes | 242 ** brackets from around identifiers. For example: "[a-b-c]" becomes |
206 ** "a-b-c". | 243 ** "a-b-c". |
207 */ | 244 */ |
208 int sqlite3Dequote(char *z){ | 245 void sqlite3Dequote(char *z){ |
209 char quote; | 246 char quote; |
210 int i, j; | 247 int i, j; |
211 if( z==0 ) return -1; | 248 if( z==0 ) return; |
212 quote = z[0]; | 249 quote = z[0]; |
213 switch( quote ){ | 250 if( !sqlite3Isquote(quote) ) return; |
214 case '\'': break; | 251 if( quote=='[' ) quote = ']'; |
215 case '"': break; | |
216 case '`': break; /* For MySQL compatibility */ | |
217 case '[': quote = ']'; break; /* For MS SqlServer compatibility */ | |
218 default: return -1; | |
219 } | |
220 for(i=1, j=0;; i++){ | 252 for(i=1, j=0;; i++){ |
221 assert( z[i] ); | 253 assert( z[i] ); |
222 if( z[i]==quote ){ | 254 if( z[i]==quote ){ |
223 if( z[i+1]==quote ){ | 255 if( z[i+1]==quote ){ |
224 z[j++] = quote; | 256 z[j++] = quote; |
225 i++; | 257 i++; |
226 }else{ | 258 }else{ |
227 break; | 259 break; |
228 } | 260 } |
229 }else{ | 261 }else{ |
230 z[j++] = z[i]; | 262 z[j++] = z[i]; |
231 } | 263 } |
232 } | 264 } |
233 z[j] = 0; | 265 z[j] = 0; |
234 return j; | 266 } |
| 267 |
| 268 /* |
| 269 ** Generate a Token object from a string |
| 270 */ |
| 271 void sqlite3TokenInit(Token *p, char *z){ |
| 272 p->z = z; |
| 273 p->n = sqlite3Strlen30(z); |
235 } | 274 } |
236 | 275 |
237 /* Convenient short-hand */ | 276 /* Convenient short-hand */ |
238 #define UpperToLower sqlite3UpperToLower | 277 #define UpperToLower sqlite3UpperToLower |
239 | 278 |
240 /* | 279 /* |
241 ** Some systems have stricmp(). Others have strcasecmp(). Because | 280 ** Some systems have stricmp(). Others have strcasecmp(). Because |
242 ** there is no consistency, we will define our own. | 281 ** there is no consistency, we will define our own. |
243 ** | 282 ** |
244 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and | 283 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and |
245 ** sqlite3_strnicmp() APIs allow applications and extensions to compare | 284 ** sqlite3_strnicmp() APIs allow applications and extensions to compare |
246 ** the contents of two buffers containing UTF-8 strings in a | 285 ** the contents of two buffers containing UTF-8 strings in a |
247 ** case-independent fashion, using the same definition of "case | 286 ** case-independent fashion, using the same definition of "case |
248 ** independence" that SQLite uses internally when comparing identifiers. | 287 ** independence" that SQLite uses internally when comparing identifiers. |
249 */ | 288 */ |
250 int sqlite3_stricmp(const char *zLeft, const char *zRight){ | 289 int sqlite3_stricmp(const char *zLeft, const char *zRight){ |
251 register unsigned char *a, *b; | |
252 if( zLeft==0 ){ | 290 if( zLeft==0 ){ |
253 return zRight ? -1 : 0; | 291 return zRight ? -1 : 0; |
254 }else if( zRight==0 ){ | 292 }else if( zRight==0 ){ |
255 return 1; | 293 return 1; |
256 } | 294 } |
| 295 return sqlite3StrICmp(zLeft, zRight); |
| 296 } |
| 297 int sqlite3StrICmp(const char *zLeft, const char *zRight){ |
| 298 unsigned char *a, *b; |
| 299 int c; |
257 a = (unsigned char *)zLeft; | 300 a = (unsigned char *)zLeft; |
258 b = (unsigned char *)zRight; | 301 b = (unsigned char *)zRight; |
259 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } | 302 for(;;){ |
260 return UpperToLower[*a] - UpperToLower[*b]; | 303 c = (int)UpperToLower[*a] - (int)UpperToLower[*b]; |
| 304 if( c || *a==0 ) break; |
| 305 a++; |
| 306 b++; |
| 307 } |
| 308 return c; |
261 } | 309 } |
262 int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ | 310 int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ |
263 register unsigned char *a, *b; | 311 register unsigned char *a, *b; |
264 if( zLeft==0 ){ | 312 if( zLeft==0 ){ |
265 return zRight ? -1 : 0; | 313 return zRight ? -1 : 0; |
266 }else if( zRight==0 ){ | 314 }else if( zRight==0 ){ |
267 return 1; | 315 return 1; |
268 } | 316 } |
269 a = (unsigned char *)zLeft; | 317 a = (unsigned char *)zLeft; |
270 b = (unsigned char *)zRight; | 318 b = (unsigned char *)zRight; |
(...skipping 29 matching lines...) Expand all Loading... |
300 const char *zEnd = z + length; | 348 const char *zEnd = z + length; |
301 /* sign * significand * (10 ^ (esign * exponent)) */ | 349 /* sign * significand * (10 ^ (esign * exponent)) */ |
302 int sign = 1; /* sign of significand */ | 350 int sign = 1; /* sign of significand */ |
303 i64 s = 0; /* significand */ | 351 i64 s = 0; /* significand */ |
304 int d = 0; /* adjust exponent for shifting decimal point */ | 352 int d = 0; /* adjust exponent for shifting decimal point */ |
305 int esign = 1; /* sign of exponent */ | 353 int esign = 1; /* sign of exponent */ |
306 int e = 0; /* exponent */ | 354 int e = 0; /* exponent */ |
307 int eValid = 1; /* True exponent is either not used or is well-formed */ | 355 int eValid = 1; /* True exponent is either not used or is well-formed */ |
308 double result; | 356 double result; |
309 int nDigits = 0; | 357 int nDigits = 0; |
310 int nonNum = 0; | 358 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ |
311 | 359 |
312 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); | 360 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); |
313 *pResult = 0.0; /* Default return value, in case of an error */ | 361 *pResult = 0.0; /* Default return value, in case of an error */ |
314 | 362 |
315 if( enc==SQLITE_UTF8 ){ | 363 if( enc==SQLITE_UTF8 ){ |
316 incr = 1; | 364 incr = 1; |
317 }else{ | 365 }else{ |
318 int i; | 366 int i; |
319 incr = 2; | 367 incr = 2; |
320 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); | 368 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); |
321 for(i=3-enc; i<length && z[i]==0; i+=2){} | 369 for(i=3-enc; i<length && z[i]==0; i+=2){} |
322 nonNum = i<length; | 370 nonNum = i<length; |
323 zEnd = z+i+enc-3; | 371 zEnd = &z[i^1]; |
324 z += (enc&1); | 372 z += (enc&1); |
325 } | 373 } |
326 | 374 |
327 /* skip leading spaces */ | 375 /* skip leading spaces */ |
328 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; | 376 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; |
329 if( z>=zEnd ) return 0; | 377 if( z>=zEnd ) return 0; |
330 | 378 |
331 /* get sign of significand */ | 379 /* get sign of significand */ |
332 if( *z=='-' ){ | 380 if( *z=='-' ){ |
333 sign = -1; | 381 sign = -1; |
334 z+=incr; | 382 z+=incr; |
335 }else if( *z=='+' ){ | 383 }else if( *z=='+' ){ |
336 z+=incr; | 384 z+=incr; |
337 } | 385 } |
338 | 386 |
339 /* skip leading zeroes */ | |
340 while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++; | |
341 | |
342 /* copy max significant digits to significand */ | 387 /* copy max significant digits to significand */ |
343 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){ | 388 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){ |
344 s = s*10 + (*z - '0'); | 389 s = s*10 + (*z - '0'); |
345 z+=incr, nDigits++; | 390 z+=incr, nDigits++; |
346 } | 391 } |
347 | 392 |
348 /* skip non-significant significand digits | 393 /* skip non-significant significand digits |
349 ** (increase exponent by d to shift decimal left) */ | 394 ** (increase exponent by d to shift decimal left) */ |
350 while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++; | 395 while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++; |
351 if( z>=zEnd ) goto do_atof_calc; | 396 if( z>=zEnd ) goto do_atof_calc; |
352 | 397 |
353 /* if decimal point is present */ | 398 /* if decimal point is present */ |
354 if( *z=='.' ){ | 399 if( *z=='.' ){ |
355 z+=incr; | 400 z+=incr; |
356 /* copy digits from after decimal to significand | 401 /* copy digits from after decimal to significand |
357 ** (decrease exponent by d to shift decimal right) */ | 402 ** (decrease exponent by d to shift decimal right) */ |
358 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){ | 403 while( z<zEnd && sqlite3Isdigit(*z) ){ |
359 s = s*10 + (*z - '0'); | 404 if( s<((LARGEST_INT64-9)/10) ){ |
360 z+=incr, nDigits++, d--; | 405 s = s*10 + (*z - '0'); |
| 406 d--; |
| 407 } |
| 408 z+=incr, nDigits++; |
361 } | 409 } |
362 /* skip non-significant digits */ | |
363 while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++; | |
364 } | 410 } |
365 if( z>=zEnd ) goto do_atof_calc; | 411 if( z>=zEnd ) goto do_atof_calc; |
366 | 412 |
367 /* if exponent is present */ | 413 /* if exponent is present */ |
368 if( *z=='e' || *z=='E' ){ | 414 if( *z=='e' || *z=='E' ){ |
369 z+=incr; | 415 z+=incr; |
370 eValid = 0; | 416 eValid = 0; |
371 if( z>=zEnd ) goto do_atof_calc; | 417 |
| 418 /* This branch is needed to avoid a (harmless) buffer overread. The |
| 419 ** special comment alerts the mutation tester that the correct answer |
| 420 ** is obtained even if the branch is omitted */ |
| 421 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/ |
| 422 |
372 /* get sign of exponent */ | 423 /* get sign of exponent */ |
373 if( *z=='-' ){ | 424 if( *z=='-' ){ |
374 esign = -1; | 425 esign = -1; |
375 z+=incr; | 426 z+=incr; |
376 }else if( *z=='+' ){ | 427 }else if( *z=='+' ){ |
377 z+=incr; | 428 z+=incr; |
378 } | 429 } |
379 /* copy digits to exponent */ | 430 /* copy digits to exponent */ |
380 while( z<zEnd && sqlite3Isdigit(*z) ){ | 431 while( z<zEnd && sqlite3Isdigit(*z) ){ |
381 e = e<10000 ? (e*10 + (*z - '0')) : 10000; | 432 e = e<10000 ? (e*10 + (*z - '0')) : 10000; |
382 z+=incr; | 433 z+=incr; |
383 eValid = 1; | 434 eValid = 1; |
384 } | 435 } |
385 } | 436 } |
386 | 437 |
387 /* skip trailing spaces */ | 438 /* skip trailing spaces */ |
388 if( nDigits && eValid ){ | 439 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; |
389 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; | |
390 } | |
391 | 440 |
392 do_atof_calc: | 441 do_atof_calc: |
393 /* adjust exponent by d, and update sign */ | 442 /* adjust exponent by d, and update sign */ |
394 e = (e*esign) + d; | 443 e = (e*esign) + d; |
395 if( e<0 ) { | 444 if( e<0 ) { |
396 esign = -1; | 445 esign = -1; |
397 e *= -1; | 446 e *= -1; |
398 } else { | 447 } else { |
399 esign = 1; | 448 esign = 1; |
400 } | 449 } |
401 | 450 |
402 /* if 0 significand */ | 451 if( s==0 ) { |
403 if( !s ) { | 452 /* In the IEEE 754 standard, zero is signed. */ |
404 /* In the IEEE 754 standard, zero is signed. | 453 result = sign<0 ? -(double)0 : (double)0; |
405 ** Add the sign if we've seen at least one digit */ | |
406 result = (sign<0 && nDigits) ? -(double)0 : (double)0; | |
407 } else { | 454 } else { |
408 /* attempt to reduce exponent */ | 455 /* Attempt to reduce exponent. |
409 if( esign>0 ){ | 456 ** |
410 while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10; | 457 ** Branches that are not required for the correct answer but which only |
411 }else{ | 458 ** help to obtain the correct answer faster are marked with special |
412 while( !(s%10) && e>0 ) e--,s/=10; | 459 ** comments, as a hint to the mutation tester. |
| 460 */ |
| 461 while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/ |
| 462 if( esign>0 ){ |
| 463 if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/ |
| 464 s *= 10; |
| 465 }else{ |
| 466 if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/ |
| 467 s /= 10; |
| 468 } |
| 469 e--; |
413 } | 470 } |
414 | 471 |
415 /* adjust the sign of significand */ | 472 /* adjust the sign of significand */ |
416 s = sign<0 ? -s : s; | 473 s = sign<0 ? -s : s; |
417 | 474 |
418 /* if exponent, scale significand as appropriate | 475 if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/ |
419 ** and store in result. */ | 476 result = (double)s; |
420 if( e ){ | 477 }else{ |
421 LONGDOUBLE_TYPE scale = 1.0; | 478 LONGDOUBLE_TYPE scale = 1.0; |
422 /* attempt to handle extremely small/large numbers better */ | 479 /* attempt to handle extremely small/large numbers better */ |
423 if( e>307 && e<342 ){ | 480 if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/ |
424 while( e%308 ) { scale *= 1.0e+1; e -= 1; } | 481 if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/ |
425 if( esign<0 ){ | 482 while( e%308 ) { scale *= 1.0e+1; e -= 1; } |
426 result = s / scale; | 483 if( esign<0 ){ |
427 result /= 1.0e+308; | 484 result = s / scale; |
428 }else{ | 485 result /= 1.0e+308; |
429 result = s * scale; | 486 }else{ |
430 result *= 1.0e+308; | 487 result = s * scale; |
431 } | 488 result *= 1.0e+308; |
432 }else if( e>=342 ){ | 489 } |
433 if( esign<0 ){ | 490 }else{ assert( e>=342 ); |
434 result = 0.0*s; | 491 if( esign<0 ){ |
435 }else{ | 492 result = 0.0*s; |
436 result = 1e308*1e308*s; /* Infinity */ | 493 }else{ |
| 494 result = 1e308*1e308*s; /* Infinity */ |
| 495 } |
437 } | 496 } |
438 }else{ | 497 }else{ |
439 /* 1.0e+22 is the largest power of 10 than can be | 498 /* 1.0e+22 is the largest power of 10 than can be |
440 ** represented exactly. */ | 499 ** represented exactly. */ |
441 while( e%22 ) { scale *= 1.0e+1; e -= 1; } | 500 while( e%22 ) { scale *= 1.0e+1; e -= 1; } |
442 while( e>0 ) { scale *= 1.0e+22; e -= 22; } | 501 while( e>0 ) { scale *= 1.0e+22; e -= 22; } |
443 if( esign<0 ){ | 502 if( esign<0 ){ |
444 result = s / scale; | 503 result = s / scale; |
445 }else{ | 504 }else{ |
446 result = s * scale; | 505 result = s * scale; |
447 } | 506 } |
448 } | 507 } |
449 } else { | |
450 result = (double)s; | |
451 } | 508 } |
452 } | 509 } |
453 | 510 |
454 /* store the result */ | 511 /* store the result */ |
455 *pResult = result; | 512 *pResult = result; |
456 | 513 |
457 /* return true if number and no extra non-whitespace chracters after */ | 514 /* return true if number and no extra non-whitespace chracters after */ |
458 return z>=zEnd && nDigits>0 && eValid && nonNum==0; | 515 return z==zEnd && nDigits>0 && eValid && nonNum==0; |
459 #else | 516 #else |
460 return !sqlite3Atoi64(z, pResult, length, enc); | 517 return !sqlite3Atoi64(z, pResult, length, enc); |
461 #endif /* SQLITE_OMIT_FLOATING_POINT */ | 518 #endif /* SQLITE_OMIT_FLOATING_POINT */ |
462 } | 519 } |
463 | 520 |
464 /* | 521 /* |
465 ** Compare the 19-character string zNum against the text representation | 522 ** Compare the 19-character string zNum against the text representation |
466 ** value 2^63: 9223372036854775808. Return negative, zero, or positive | 523 ** value 2^63: 9223372036854775808. Return negative, zero, or positive |
467 ** if zNum is less than, equal to, or greater than the string. | 524 ** if zNum is less than, equal to, or greater than the string. |
468 ** Note that zNum must contain exactly 19 characters. | 525 ** Note that zNum must contain exactly 19 characters. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 ** length is the number of bytes in the string (bytes, not characters). | 567 ** length is the number of bytes in the string (bytes, not characters). |
511 ** The string is not necessarily zero-terminated. The encoding is | 568 ** The string is not necessarily zero-terminated. The encoding is |
512 ** given by enc. | 569 ** given by enc. |
513 */ | 570 */ |
514 int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ | 571 int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ |
515 int incr; | 572 int incr; |
516 u64 u = 0; | 573 u64 u = 0; |
517 int neg = 0; /* assume positive */ | 574 int neg = 0; /* assume positive */ |
518 int i; | 575 int i; |
519 int c = 0; | 576 int c = 0; |
520 int nonNum = 0; | 577 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ |
521 const char *zStart; | 578 const char *zStart; |
522 const char *zEnd = zNum + length; | 579 const char *zEnd = zNum + length; |
523 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); | 580 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); |
524 if( enc==SQLITE_UTF8 ){ | 581 if( enc==SQLITE_UTF8 ){ |
525 incr = 1; | 582 incr = 1; |
526 }else{ | 583 }else{ |
527 incr = 2; | 584 incr = 2; |
528 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); | 585 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); |
529 for(i=3-enc; i<length && zNum[i]==0; i+=2){} | 586 for(i=3-enc; i<length && zNum[i]==0; i+=2){} |
530 nonNum = i<length; | 587 nonNum = i<length; |
531 zEnd = zNum+i+enc-3; | 588 zEnd = &zNum[i^1]; |
532 zNum += (enc&1); | 589 zNum += (enc&1); |
533 } | 590 } |
534 while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr; | 591 while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr; |
535 if( zNum<zEnd ){ | 592 if( zNum<zEnd ){ |
536 if( *zNum=='-' ){ | 593 if( *zNum=='-' ){ |
537 neg = 1; | 594 neg = 1; |
538 zNum+=incr; | 595 zNum+=incr; |
539 }else if( *zNum=='+' ){ | 596 }else if( *zNum=='+' ){ |
540 zNum+=incr; | 597 zNum+=incr; |
541 } | 598 } |
542 } | 599 } |
543 zStart = zNum; | 600 zStart = zNum; |
544 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */ | 601 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */ |
545 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){ | 602 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){ |
546 u = u*10 + c - '0'; | 603 u = u*10 + c - '0'; |
547 } | 604 } |
548 if( u>LARGEST_INT64 ){ | 605 if( u>LARGEST_INT64 ){ |
549 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; | 606 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; |
550 }else if( neg ){ | 607 }else if( neg ){ |
551 *pNum = -(i64)u; | 608 *pNum = -(i64)u; |
552 }else{ | 609 }else{ |
553 *pNum = (i64)u; | 610 *pNum = (i64)u; |
554 } | 611 } |
555 testcase( i==18 ); | 612 testcase( i==18 ); |
556 testcase( i==19 ); | 613 testcase( i==19 ); |
557 testcase( i==20 ); | 614 testcase( i==20 ); |
558 if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) | 615 if( &zNum[i]<zEnd /* Extra bytes at the end */ |
559 || i>19*incr || nonNum ){ | 616 || (i==0 && zStart==zNum) /* No digits */ |
| 617 || i>19*incr /* Too many digits */ |
| 618 || nonNum /* UTF16 with high-order bytes non-zero */ |
| 619 ){ |
560 /* zNum is empty or contains non-numeric text or is longer | 620 /* zNum is empty or contains non-numeric text or is longer |
561 ** than 19 digits (thus guaranteeing that it is too large) */ | 621 ** than 19 digits (thus guaranteeing that it is too large) */ |
562 return 1; | 622 return 1; |
563 }else if( i<19*incr ){ | 623 }else if( i<19*incr ){ |
564 /* Less than 19 digits, so we know that it fits in 64 bits */ | 624 /* Less than 19 digits, so we know that it fits in 64 bits */ |
565 assert( u<=LARGEST_INT64 ); | 625 assert( u<=LARGEST_INT64 ); |
566 return 0; | 626 return 0; |
567 }else{ | 627 }else{ |
568 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ | 628 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ |
569 c = compare2pow63(zNum, incr); | 629 c = compare2pow63(zNum, incr); |
(...skipping 21 matching lines...) Expand all Loading... |
591 ** Returns: | 651 ** Returns: |
592 ** | 652 ** |
593 ** 0 Successful transformation. Fits in a 64-bit signed integer. | 653 ** 0 Successful transformation. Fits in a 64-bit signed integer. |
594 ** 1 Integer too large for a 64-bit signed integer or is malformed | 654 ** 1 Integer too large for a 64-bit signed integer or is malformed |
595 ** 2 Special case of 9223372036854775808 | 655 ** 2 Special case of 9223372036854775808 |
596 */ | 656 */ |
597 int sqlite3DecOrHexToI64(const char *z, i64 *pOut){ | 657 int sqlite3DecOrHexToI64(const char *z, i64 *pOut){ |
598 #ifndef SQLITE_OMIT_HEX_INTEGER | 658 #ifndef SQLITE_OMIT_HEX_INTEGER |
599 if( z[0]=='0' | 659 if( z[0]=='0' |
600 && (z[1]=='x' || z[1]=='X') | 660 && (z[1]=='x' || z[1]=='X') |
601 && sqlite3Isxdigit(z[2]) | |
602 ){ | 661 ){ |
603 u64 u = 0; | 662 u64 u = 0; |
604 int i, k; | 663 int i, k; |
605 for(i=2; z[i]=='0'; i++){} | 664 for(i=2; z[i]=='0'; i++){} |
606 for(k=i; sqlite3Isxdigit(z[k]); k++){ | 665 for(k=i; sqlite3Isxdigit(z[k]); k++){ |
607 u = u*16 + sqlite3HexToInt(z[k]); | 666 u = u*16 + sqlite3HexToInt(z[k]); |
608 } | 667 } |
609 memcpy(pOut, &u, 8); | 668 memcpy(pOut, &u, 8); |
610 return (z[k]==0 && k-i<=16) ? 0 : 1; | 669 return (z[k]==0 && k-i<=16) ? 0 : 1; |
611 }else | 670 }else |
(...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1061 } | 1120 } |
1062 #endif | 1121 #endif |
1063 } | 1122 } |
1064 | 1123 |
1065 /* | 1124 /* |
1066 ** Return the number of bytes that will be needed to store the given | 1125 ** Return the number of bytes that will be needed to store the given |
1067 ** 64-bit integer. | 1126 ** 64-bit integer. |
1068 */ | 1127 */ |
1069 int sqlite3VarintLen(u64 v){ | 1128 int sqlite3VarintLen(u64 v){ |
1070 int i; | 1129 int i; |
1071 for(i=1; (v >>= 7)!=0; i++){ assert( i<9 ); } | 1130 for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); } |
1072 return i; | 1131 return i; |
1073 } | 1132 } |
1074 | 1133 |
1075 | 1134 |
1076 /* | 1135 /* |
1077 ** Read or write a four-byte big-endian integer value. | 1136 ** Read or write a four-byte big-endian integer value. |
1078 */ | 1137 */ |
1079 u32 sqlite3Get4byte(const u8 *p){ | 1138 u32 sqlite3Get4byte(const u8 *p){ |
1080 #if SQLITE_BYTEORDER==4321 | 1139 #if SQLITE_BYTEORDER==4321 |
1081 u32 x; | 1140 u32 x; |
1082 memcpy(&x,p,4); | 1141 memcpy(&x,p,4); |
1083 return x; | 1142 return x; |
1084 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ | 1143 #elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000) |
1085 && defined(__GNUC__) && GCC_VERSION>=4003000 | |
1086 u32 x; | 1144 u32 x; |
1087 memcpy(&x,p,4); | 1145 memcpy(&x,p,4); |
1088 return __builtin_bswap32(x); | 1146 return __builtin_bswap32(x); |
1089 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ | 1147 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 |
1090 && defined(_MSC_VER) && _MSC_VER>=1300 | |
1091 u32 x; | 1148 u32 x; |
1092 memcpy(&x,p,4); | 1149 memcpy(&x,p,4); |
1093 return _byteswap_ulong(x); | 1150 return _byteswap_ulong(x); |
1094 #else | 1151 #else |
1095 testcase( p[0]&0x80 ); | 1152 testcase( p[0]&0x80 ); |
1096 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; | 1153 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
1097 #endif | 1154 #endif |
1098 } | 1155 } |
1099 void sqlite3Put4byte(unsigned char *p, u32 v){ | 1156 void sqlite3Put4byte(unsigned char *p, u32 v){ |
1100 #if SQLITE_BYTEORDER==4321 | 1157 #if SQLITE_BYTEORDER==4321 |
1101 memcpy(p,&v,4); | 1158 memcpy(p,&v,4); |
1102 #elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) && GCC_VERSION>=4003000 | 1159 #elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000) |
1103 u32 x = __builtin_bswap32(v); | 1160 u32 x = __builtin_bswap32(v); |
1104 memcpy(p,&x,4); | 1161 memcpy(p,&x,4); |
1105 #elif SQLITE_BYTEORDER==1234 && defined(_MSC_VER) && _MSC_VER>=1300 | 1162 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 |
1106 u32 x = _byteswap_ulong(v); | 1163 u32 x = _byteswap_ulong(v); |
1107 memcpy(p,&x,4); | 1164 memcpy(p,&x,4); |
1108 #else | 1165 #else |
1109 p[0] = (u8)(v>>24); | 1166 p[0] = (u8)(v>>24); |
1110 p[1] = (u8)(v>>16); | 1167 p[1] = (u8)(v>>16); |
1111 p[2] = (u8)(v>>8); | 1168 p[2] = (u8)(v>>8); |
1112 p[3] = (u8)v; | 1169 p[3] = (u8)v; |
1113 #endif | 1170 #endif |
1114 } | 1171 } |
1115 | 1172 |
(...skipping 19 matching lines...) Expand all Loading... |
1135 /* | 1192 /* |
1136 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary | 1193 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary |
1137 ** value. Return a pointer to its binary value. Space to hold the | 1194 ** value. Return a pointer to its binary value. Space to hold the |
1138 ** binary value has been obtained from malloc and must be freed by | 1195 ** binary value has been obtained from malloc and must be freed by |
1139 ** the calling routine. | 1196 ** the calling routine. |
1140 */ | 1197 */ |
1141 void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ | 1198 void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ |
1142 char *zBlob; | 1199 char *zBlob; |
1143 int i; | 1200 int i; |
1144 | 1201 |
1145 zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1); | 1202 zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1); |
1146 n--; | 1203 n--; |
1147 if( zBlob ){ | 1204 if( zBlob ){ |
1148 for(i=0; i<n; i+=2){ | 1205 for(i=0; i<n; i+=2){ |
1149 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]); | 1206 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]); |
1150 } | 1207 } |
1151 zBlob[i/2] = 0; | 1208 zBlob[i/2] = 0; |
1152 } | 1209 } |
1153 return zBlob; | 1210 return zBlob; |
1154 } | 1211 } |
1155 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */ | 1212 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */ |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1211 } | 1268 } |
1212 } | 1269 } |
1213 | 1270 |
1214 /* | 1271 /* |
1215 ** Attempt to add, substract, or multiply the 64-bit signed value iB against | 1272 ** Attempt to add, substract, or multiply the 64-bit signed value iB against |
1216 ** the other 64-bit signed integer at *pA and store the result in *pA. | 1273 ** the other 64-bit signed integer at *pA and store the result in *pA. |
1217 ** Return 0 on success. Or if the operation would have resulted in an | 1274 ** Return 0 on success. Or if the operation would have resulted in an |
1218 ** overflow, leave *pA unchanged and return 1. | 1275 ** overflow, leave *pA unchanged and return 1. |
1219 */ | 1276 */ |
1220 int sqlite3AddInt64(i64 *pA, i64 iB){ | 1277 int sqlite3AddInt64(i64 *pA, i64 iB){ |
| 1278 #if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000 |
| 1279 return __builtin_add_overflow(*pA, iB, pA); |
| 1280 #else |
1221 i64 iA = *pA; | 1281 i64 iA = *pA; |
1222 testcase( iA==0 ); testcase( iA==1 ); | 1282 testcase( iA==0 ); testcase( iA==1 ); |
1223 testcase( iB==-1 ); testcase( iB==0 ); | 1283 testcase( iB==-1 ); testcase( iB==0 ); |
1224 if( iB>=0 ){ | 1284 if( iB>=0 ){ |
1225 testcase( iA>0 && LARGEST_INT64 - iA == iB ); | 1285 testcase( iA>0 && LARGEST_INT64 - iA == iB ); |
1226 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 ); | 1286 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 ); |
1227 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1; | 1287 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1; |
1228 }else{ | 1288 }else{ |
1229 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 ); | 1289 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 ); |
1230 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); | 1290 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); |
1231 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; | 1291 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; |
1232 } | 1292 } |
1233 *pA += iB; | 1293 *pA += iB; |
1234 return 0; | 1294 return 0; |
| 1295 #endif |
1235 } | 1296 } |
1236 int sqlite3SubInt64(i64 *pA, i64 iB){ | 1297 int sqlite3SubInt64(i64 *pA, i64 iB){ |
| 1298 #if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000 |
| 1299 return __builtin_sub_overflow(*pA, iB, pA); |
| 1300 #else |
1237 testcase( iB==SMALLEST_INT64+1 ); | 1301 testcase( iB==SMALLEST_INT64+1 ); |
1238 if( iB==SMALLEST_INT64 ){ | 1302 if( iB==SMALLEST_INT64 ){ |
1239 testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); | 1303 testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); |
1240 if( (*pA)>=0 ) return 1; | 1304 if( (*pA)>=0 ) return 1; |
1241 *pA -= iB; | 1305 *pA -= iB; |
1242 return 0; | 1306 return 0; |
1243 }else{ | 1307 }else{ |
1244 return sqlite3AddInt64(pA, -iB); | 1308 return sqlite3AddInt64(pA, -iB); |
1245 } | 1309 } |
| 1310 #endif |
1246 } | 1311 } |
1247 int sqlite3MulInt64(i64 *pA, i64 iB){ | 1312 int sqlite3MulInt64(i64 *pA, i64 iB){ |
| 1313 /* TODO(shess): Chromium Android clang generates a link error: |
| 1314 ** undefined reference to '__mulodi4' |
| 1315 ** UPDATE(shess): Also, apparently, 32-bit Linux clang. |
| 1316 */ |
| 1317 #if GCC_VERSION>=5004000 || \ |
| 1318 (CLANG_VERSION>=4000000 && !defined(__ANDROID__) && \ |
| 1319 (!defined(__linux__) || !defined(__i386__))) |
| 1320 return __builtin_mul_overflow(*pA, iB, pA); |
| 1321 #else |
1248 i64 iA = *pA; | 1322 i64 iA = *pA; |
1249 if( iB>0 ){ | 1323 if( iB>0 ){ |
1250 if( iA>LARGEST_INT64/iB ) return 1; | 1324 if( iA>LARGEST_INT64/iB ) return 1; |
1251 if( iA<SMALLEST_INT64/iB ) return 1; | 1325 if( iA<SMALLEST_INT64/iB ) return 1; |
1252 }else if( iB<0 ){ | 1326 }else if( iB<0 ){ |
1253 if( iA>0 ){ | 1327 if( iA>0 ){ |
1254 if( iB<SMALLEST_INT64/iA ) return 1; | 1328 if( iB<SMALLEST_INT64/iA ) return 1; |
1255 }else if( iA<0 ){ | 1329 }else if( iA<0 ){ |
1256 if( iB==SMALLEST_INT64 ) return 1; | 1330 if( iB==SMALLEST_INT64 ) return 1; |
1257 if( iA==SMALLEST_INT64 ) return 1; | 1331 if( iA==SMALLEST_INT64 ) return 1; |
1258 if( -iA>LARGEST_INT64/-iB ) return 1; | 1332 if( -iA>LARGEST_INT64/-iB ) return 1; |
1259 } | 1333 } |
1260 } | 1334 } |
1261 *pA = iA*iB; | 1335 *pA = iA*iB; |
1262 return 0; | 1336 return 0; |
| 1337 #endif |
1263 } | 1338 } |
1264 | 1339 |
1265 /* | 1340 /* |
1266 ** Compute the absolute value of a 32-bit signed integer, of possible. Or | 1341 ** Compute the absolute value of a 32-bit signed integer, of possible. Or |
1267 ** if the integer has a value of -2147483648, return +2147483647 | 1342 ** if the integer has a value of -2147483648, return +2147483647 |
1268 */ | 1343 */ |
1269 int sqlite3AbsInt32(int x){ | 1344 int sqlite3AbsInt32(int x){ |
1270 if( x>=0 ) return x; | 1345 if( x>=0 ) return x; |
1271 if( x==(int)0x80000000 ) return 0x7fffffff; | 1346 if( x==(int)0x80000000 ) return 0x7fffffff; |
1272 return -x; | 1347 return -x; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1336 ** Convert an integer into a LogEst. In other words, compute an | 1411 ** Convert an integer into a LogEst. In other words, compute an |
1337 ** approximation for 10*log2(x). | 1412 ** approximation for 10*log2(x). |
1338 */ | 1413 */ |
1339 LogEst sqlite3LogEst(u64 x){ | 1414 LogEst sqlite3LogEst(u64 x){ |
1340 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 }; | 1415 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 }; |
1341 LogEst y = 40; | 1416 LogEst y = 40; |
1342 if( x<8 ){ | 1417 if( x<8 ){ |
1343 if( x<2 ) return 0; | 1418 if( x<2 ) return 0; |
1344 while( x<8 ){ y -= 10; x <<= 1; } | 1419 while( x<8 ){ y -= 10; x <<= 1; } |
1345 }else{ | 1420 }else{ |
1346 while( x>255 ){ y += 40; x >>= 4; } | 1421 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/ |
1347 while( x>15 ){ y += 10; x >>= 1; } | 1422 while( x>15 ){ y += 10; x >>= 1; } |
1348 } | 1423 } |
1349 return a[x&7] + y - 10; | 1424 return a[x&7] + y - 10; |
1350 } | 1425 } |
1351 | 1426 |
1352 #ifndef SQLITE_OMIT_VIRTUALTABLE | 1427 #ifndef SQLITE_OMIT_VIRTUALTABLE |
1353 /* | 1428 /* |
1354 ** Convert a double into a LogEst | 1429 ** Convert a double into a LogEst |
1355 ** In other words, compute an approximation for 10*log2(x). | 1430 ** In other words, compute an approximation for 10*log2(x). |
1356 */ | 1431 */ |
1357 LogEst sqlite3LogEstFromDouble(double x){ | 1432 LogEst sqlite3LogEstFromDouble(double x){ |
1358 u64 a; | 1433 u64 a; |
1359 LogEst e; | 1434 LogEst e; |
1360 assert( sizeof(x)==8 && sizeof(a)==8 ); | 1435 assert( sizeof(x)==8 && sizeof(a)==8 ); |
1361 if( x<=1 ) return 0; | 1436 if( x<=1 ) return 0; |
1362 if( x<=2000000000 ) return sqlite3LogEst((u64)x); | 1437 if( x<=2000000000 ) return sqlite3LogEst((u64)x); |
1363 memcpy(&a, &x, 8); | 1438 memcpy(&a, &x, 8); |
1364 e = (a>>52) - 1022; | 1439 e = (a>>52) - 1022; |
1365 return e*10; | 1440 return e*10; |
1366 } | 1441 } |
1367 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | 1442 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
1368 | 1443 |
| 1444 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \ |
| 1445 defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \ |
| 1446 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS) |
1369 /* | 1447 /* |
1370 ** Convert a LogEst into an integer. | 1448 ** Convert a LogEst into an integer. |
| 1449 ** |
| 1450 ** Note that this routine is only used when one or more of various |
| 1451 ** non-standard compile-time options is enabled. |
1371 */ | 1452 */ |
1372 u64 sqlite3LogEstToInt(LogEst x){ | 1453 u64 sqlite3LogEstToInt(LogEst x){ |
1373 u64 n; | 1454 u64 n; |
1374 if( x<10 ) return 1; | |
1375 n = x%10; | 1455 n = x%10; |
1376 x /= 10; | 1456 x /= 10; |
1377 if( n>=5 ) n -= 2; | 1457 if( n>=5 ) n -= 2; |
1378 else if( n>=1 ) n -= 1; | 1458 else if( n>=1 ) n -= 1; |
1379 if( x>=3 ){ | 1459 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \ |
1380 return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3); | 1460 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS) |
| 1461 if( x>60 ) return (u64)LARGEST_INT64; |
| 1462 #else |
| 1463 /* If only SQLITE_ENABLE_STAT3_OR_STAT4 is on, then the largest input |
| 1464 ** possible to this routine is 310, resulting in a maximum x of 31 */ |
| 1465 assert( x<=60 ); |
| 1466 #endif |
| 1467 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x); |
| 1468 } |
| 1469 #endif /* defined SCANSTAT or STAT4 or ESTIMATED_ROWS */ |
| 1470 |
| 1471 /* |
| 1472 ** Add a new name/number pair to a VList. This might require that the |
| 1473 ** VList object be reallocated, so return the new VList. If an OOM |
| 1474 ** error occurs, the original VList returned and the |
| 1475 ** db->mallocFailed flag is set. |
| 1476 ** |
| 1477 ** A VList is really just an array of integers. To destroy a VList, |
| 1478 ** simply pass it to sqlite3DbFree(). |
| 1479 ** |
| 1480 ** The first integer is the number of integers allocated for the whole |
| 1481 ** VList. The second integer is the number of integers actually used. |
| 1482 ** Each name/number pair is encoded by subsequent groups of 3 or more |
| 1483 ** integers. |
| 1484 ** |
| 1485 ** Each name/number pair starts with two integers which are the numeric |
| 1486 ** value for the pair and the size of the name/number pair, respectively. |
| 1487 ** The text name overlays one or more following integers. The text name |
| 1488 ** is always zero-terminated. |
| 1489 ** |
| 1490 ** Conceptually: |
| 1491 ** |
| 1492 ** struct VList { |
| 1493 ** int nAlloc; // Number of allocated slots |
| 1494 ** int nUsed; // Number of used slots |
| 1495 ** struct VListEntry { |
| 1496 ** int iValue; // Value for this entry |
| 1497 ** int nSlot; // Slots used by this entry |
| 1498 ** // ... variable name goes here |
| 1499 ** } a[0]; |
| 1500 ** } |
| 1501 ** |
| 1502 ** During code generation, pointers to the variable names within the |
| 1503 ** VList are taken. When that happens, nAlloc is set to zero as an |
| 1504 ** indication that the VList may never again be enlarged, since the |
| 1505 ** accompanying realloc() would invalidate the pointers. |
| 1506 */ |
| 1507 VList *sqlite3VListAdd( |
| 1508 sqlite3 *db, /* The database connection used for malloc() */ |
| 1509 VList *pIn, /* The input VList. Might be NULL */ |
| 1510 const char *zName, /* Name of symbol to add */ |
| 1511 int nName, /* Bytes of text in zName */ |
| 1512 int iVal /* Value to associate with zName */ |
| 1513 ){ |
| 1514 int nInt; /* number of sizeof(int) objects needed for zName */ |
| 1515 char *z; /* Pointer to where zName will be stored */ |
| 1516 int i; /* Index in pIn[] where zName is stored */ |
| 1517 |
| 1518 nInt = nName/4 + 3; |
| 1519 assert( pIn==0 || pIn[0]>=3 ); /* Verify ok to add new elements */ |
| 1520 if( pIn==0 || pIn[1]+nInt > pIn[0] ){ |
| 1521 /* Enlarge the allocation */ |
| 1522 int nAlloc = (pIn ? pIn[0]*2 : 10) + nInt; |
| 1523 VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int)); |
| 1524 if( pOut==0 ) return pIn; |
| 1525 if( pIn==0 ) pOut[1] = 2; |
| 1526 pIn = pOut; |
| 1527 pIn[0] = nAlloc; |
1381 } | 1528 } |
1382 return (n+8)>>(3-x); | 1529 i = pIn[1]; |
| 1530 pIn[i] = iVal; |
| 1531 pIn[i+1] = nInt; |
| 1532 z = (char*)&pIn[i+2]; |
| 1533 pIn[1] = i+nInt; |
| 1534 assert( pIn[1]<=pIn[0] ); |
| 1535 memcpy(z, zName, nName); |
| 1536 z[nName] = 0; |
| 1537 return pIn; |
1383 } | 1538 } |
| 1539 |
| 1540 /* |
| 1541 ** Return a pointer to the name of a variable in the given VList that |
| 1542 ** has the value iVal. Or return a NULL if there is no such variable in |
| 1543 ** the list |
| 1544 */ |
| 1545 const char *sqlite3VListNumToName(VList *pIn, int iVal){ |
| 1546 int i, mx; |
| 1547 if( pIn==0 ) return 0; |
| 1548 mx = pIn[1]; |
| 1549 i = 2; |
| 1550 do{ |
| 1551 if( pIn[i]==iVal ) return (char*)&pIn[i+2]; |
| 1552 i += pIn[i+1]; |
| 1553 }while( i<mx ); |
| 1554 return 0; |
| 1555 } |
| 1556 |
| 1557 /* |
| 1558 ** Return the number of the variable named zName, if it is in VList. |
| 1559 ** or return 0 if there is no such variable. |
| 1560 */ |
| 1561 int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){ |
| 1562 int i, mx; |
| 1563 if( pIn==0 ) return 0; |
| 1564 mx = pIn[1]; |
| 1565 i = 2; |
| 1566 do{ |
| 1567 const char *z = (const char*)&pIn[i+2]; |
| 1568 if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i]; |
| 1569 i += pIn[i+1]; |
| 1570 }while( i<mx ); |
| 1571 return 0; |
| 1572 } |
OLD | NEW |