Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(433)

Side by Side Diff: third_party/sqlite/sqlite-src-3080704/src/printf.c

Issue 883353008: [sql] Import reference version of SQLite 3.8.7.4. (Closed) Base URL: http://chromium.googlesource.com/chromium/src.git@master
Patch Set: Hold back encoding change which is messing up patch. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 ** The "printf" code that follows dates from the 1980's. It is in 2 ** The "printf" code that follows dates from the 1980's. It is in
3 ** the public domain. The original comments are included here for 3 ** the public domain. The original comments are included here for
4 ** completeness. They are very out-of-date but might be useful as 4 ** completeness. They are very out-of-date but might be useful as
5 ** an historical reference. Most of the "enhancements" have been backed 5 ** an historical reference. Most of the "enhancements" have been backed
6 ** out so that the functionality is now the same as standard printf(). 6 ** out so that the functionality is now the same as standard printf().
7 ** 7 **
8 ************************************************************************** 8 **************************************************************************
9 ** 9 **
10 ** The following modules is an enhanced replacement for the "printf" subroutines 10 ** This file contains code for a set of "printf"-like routines. These
11 ** found in the standard C library. The following enhancements are 11 ** routines format strings much like the printf() from the standard C
12 ** supported: 12 ** library, though the implementation here has enhancements to support
13 ** 13 ** SQLlite.
14 ** + Additional functions. The standard set of "printf" functions
15 ** includes printf, fprintf, sprintf, vprintf, vfprintf, and
16 ** vsprintf. This module adds the following:
17 **
18 ** * snprintf -- Works like sprintf, but has an extra argument
19 ** which is the size of the buffer written to.
20 **
21 ** * mprintf -- Similar to sprintf. Writes output to memory
22 ** obtained from malloc.
23 **
24 ** * xprintf -- Calls a function to dispose of output.
25 **
26 ** * nprintf -- No output, but returns the number of characters
27 ** that would have been output by printf.
28 **
29 ** * A v- version (ex: vsnprintf) of every function is also
30 ** supplied.
31 **
32 ** + A few extensions to the formatting notation are supported:
33 **
34 ** * The "=" flag (similar to "-") causes the output to be
35 ** be centered in the appropriately sized field.
36 **
37 ** * The %b field outputs an integer in binary notation.
38 **
39 ** * The %c field now accepts a precision. The character output
40 ** is repeated by the number of times the precision specifies.
41 **
42 ** * The %' field works like %c, but takes as its character the
43 ** next character of the format string, instead of the next
44 ** argument. For example, printf("%.78'-") prints 78 minus
45 ** signs, the same as printf("%.78c",'-').
46 **
47 ** + When compiled using GCC on a SPARC, this version of printf is
48 ** faster than the library printf for SUN OS 4.1.
49 **
50 ** + All functions are fully reentrant.
51 **
52 */ 14 */
53 #include "sqliteInt.h" 15 #include "sqliteInt.h"
54 16
55 /* 17 /*
18 ** If the strchrnul() library function is available, then set
19 ** HAVE_STRCHRNUL. If that routine is not available, this module
20 ** will supply its own. The built-in version is slower than
21 ** the glibc version so the glibc version is definitely preferred.
22 */
23 #if !defined(HAVE_STRCHRNUL)
24 # define HAVE_STRCHRNUL 0
25 #endif
26
27
28 /*
56 ** Conversion types fall into various categories as defined by the 29 ** Conversion types fall into various categories as defined by the
57 ** following enumeration. 30 ** following enumeration.
58 */ 31 */
59 #define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */ 32 #define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */
60 #define etFLOAT 2 /* Floating point. %f */ 33 #define etFLOAT 2 /* Floating point. %f */
61 #define etEXP 3 /* Exponentional notation. %e and %E */ 34 #define etEXP 3 /* Exponentional notation. %e and %E */
62 #define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */ 35 #define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */
63 #define etSIZE 5 /* Return number of characters processed so far. %n */ 36 #define etSIZE 5 /* Return number of characters processed so far. %n */
64 #define etSTRING 6 /* Strings. %s */ 37 #define etSTRING 6 /* Strings. %s */
65 #define etDYNSTRING 7 /* Dynamically allocated strings. %z */ 38 #define etDYNSTRING 7 /* Dynamically allocated strings. %z */
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 ** input: *val = 3.14159 128 ** input: *val = 3.14159
156 ** output: *val = 1.4159 function return = '3' 129 ** output: *val = 1.4159 function return = '3'
157 ** 130 **
158 ** The counter *cnt is incremented each time. After counter exceeds 131 ** The counter *cnt is incremented each time. After counter exceeds
159 ** 16 (the number of significant digits in a 64-bit float) '0' is 132 ** 16 (the number of significant digits in a 64-bit float) '0' is
160 ** always returned. 133 ** always returned.
161 */ 134 */
162 static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ 135 static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
163 int digit; 136 int digit;
164 LONGDOUBLE_TYPE d; 137 LONGDOUBLE_TYPE d;
165 if( (*cnt)++ >= 16 ) return '0'; 138 if( (*cnt)<=0 ) return '0';
139 (*cnt)--;
166 digit = (int)*val; 140 digit = (int)*val;
167 d = digit; 141 d = digit;
168 digit += '0'; 142 digit += '0';
169 *val = (*val - d)*10.0; 143 *val = (*val - d)*10.0;
170 return (char)digit; 144 return (char)digit;
171 } 145 }
172 #endif /* SQLITE_OMIT_FLOATING_POINT */ 146 #endif /* SQLITE_OMIT_FLOATING_POINT */
173 147
174 /* 148 /*
175 ** Append N space characters to the given string buffer. 149 ** Set the StrAccum object to an error mode.
176 */ 150 */
177 static void appendSpace(StrAccum *pAccum, int N){ 151 static void setStrAccumError(StrAccum *p, u8 eError){
178 static const char zSpaces[] = " "; 152 p->accError = eError;
179 while( N>=(int)sizeof(zSpaces)-1 ){ 153 p->nAlloc = 0;
180 sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
181 N -= sizeof(zSpaces)-1;
182 }
183 if( N>0 ){
184 sqlite3StrAccumAppend(pAccum, zSpaces, N);
185 }
186 } 154 }
187 155
188 /* 156 /*
157 ** Extra argument values from a PrintfArguments object
158 */
159 static sqlite3_int64 getIntArg(PrintfArguments *p){
160 if( p->nArg<=p->nUsed ) return 0;
161 return sqlite3_value_int64(p->apArg[p->nUsed++]);
162 }
163 static double getDoubleArg(PrintfArguments *p){
164 if( p->nArg<=p->nUsed ) return 0.0;
165 return sqlite3_value_double(p->apArg[p->nUsed++]);
166 }
167 static char *getTextArg(PrintfArguments *p){
168 if( p->nArg<=p->nUsed ) return 0;
169 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
170 }
171
172
173 /*
189 ** On machines with a small stack size, you can redefine the 174 ** On machines with a small stack size, you can redefine the
190 ** SQLITE_PRINT_BUF_SIZE to be less than 350. 175 ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
191 */ 176 */
192 #ifndef SQLITE_PRINT_BUF_SIZE 177 #ifndef SQLITE_PRINT_BUF_SIZE
193 # if defined(SQLITE_SMALL_STACK) 178 # define SQLITE_PRINT_BUF_SIZE 70
194 # define SQLITE_PRINT_BUF_SIZE 50
195 # else
196 # define SQLITE_PRINT_BUF_SIZE 350
197 # endif
198 #endif 179 #endif
199 #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ 180 #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
200 181
201 /* 182 /*
202 ** The root program. All variations call this core. 183 ** Render a string given by "fmt" into the StrAccum object.
203 **
204 ** INPUTS:
205 ** func This is a pointer to a function taking three arguments
206 ** 1. A pointer to anything. Same as the "arg" parameter.
207 ** 2. A pointer to the list of characters to be output
208 ** (Note, this list is NOT null terminated.)
209 ** 3. An integer number of characters to be output.
210 ** (Note: This number might be zero.)
211 **
212 ** arg This is the pointer to anything which will be passed as the
213 ** first argument to "func". Use it for whatever you like.
214 **
215 ** fmt This is the format string, as in the usual print.
216 **
217 ** ap This is a pointer to a list of arguments. Same as in
218 ** vfprint.
219 **
220 ** OUTPUTS:
221 ** The return value is the total number of characters sent to
222 ** the function "func". Returns -1 on a error.
223 **
224 ** Note that the order in which automatic variables are declared below
225 ** seems to make a big difference in determining how fast this beast
226 ** will run.
227 */ 184 */
228 void sqlite3VXPrintf( 185 void sqlite3VXPrintf(
229 StrAccum *pAccum, /* Accumulate results here */ 186 StrAccum *pAccum, /* Accumulate results here */
230 int useExtended, /* Allow extended %-conversions */ 187 u32 bFlags, /* SQLITE_PRINTF_* flags */
231 const char *fmt, /* Format string */ 188 const char *fmt, /* Format string */
232 va_list ap /* arguments */ 189 va_list ap /* arguments */
233 ){ 190 ){
234 int c; /* Next character in the format string */ 191 int c; /* Next character in the format string */
235 char *bufpt; /* Pointer to the conversion buffer */ 192 char *bufpt; /* Pointer to the conversion buffer */
236 int precision; /* Precision of the current field */ 193 int precision; /* Precision of the current field */
237 int length; /* Length of the field */ 194 int length; /* Length of the field */
238 int idx; /* A general purpose loop counter */ 195 int idx; /* A general purpose loop counter */
239 int width; /* Width of the current field */ 196 int width; /* Width of the current field */
240 etByte flag_leftjustify; /* True if "-" flag is present */ 197 etByte flag_leftjustify; /* True if "-" flag is present */
241 etByte flag_plussign; /* True if "+" flag is present */ 198 etByte flag_plussign; /* True if "+" flag is present */
242 etByte flag_blanksign; /* True if " " flag is present */ 199 etByte flag_blanksign; /* True if " " flag is present */
243 etByte flag_alternateform; /* True if "#" flag is present */ 200 etByte flag_alternateform; /* True if "#" flag is present */
244 etByte flag_altform2; /* True if "!" flag is present */ 201 etByte flag_altform2; /* True if "!" flag is present */
245 etByte flag_zeropad; /* True if field width constant starts with zero */ 202 etByte flag_zeropad; /* True if field width constant starts with zero */
246 etByte flag_long; /* True if "l" flag is present */ 203 etByte flag_long; /* True if "l" flag is present */
247 etByte flag_longlong; /* True if the "ll" flag is present */ 204 etByte flag_longlong; /* True if the "ll" flag is present */
248 etByte done; /* Loop termination flag */ 205 etByte done; /* Loop termination flag */
206 etByte xtype = 0; /* Conversion paradigm */
207 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
208 u8 useIntern; /* Ok to use internal conversions (ex: %T) */
209 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
249 sqlite_uint64 longvalue; /* Value for integer types */ 210 sqlite_uint64 longvalue; /* Value for integer types */
250 LONGDOUBLE_TYPE realvalue; /* Value for real types */ 211 LONGDOUBLE_TYPE realvalue; /* Value for real types */
251 const et_info *infop; /* Pointer to the appropriate info structure */ 212 const et_info *infop; /* Pointer to the appropriate info structure */
252 char buf[etBUFSIZE]; /* Conversion buffer */ 213 char *zOut; /* Rendering buffer */
253 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ 214 int nOut; /* Size of the rendering buffer */
254 etByte xtype = 0; /* Conversion paradigm */ 215 char *zExtra = 0; /* Malloced memory used by some conversion */
255 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
256 #ifndef SQLITE_OMIT_FLOATING_POINT 216 #ifndef SQLITE_OMIT_FLOATING_POINT
257 int exp, e2; /* exponent of real numbers */ 217 int exp, e2; /* exponent of real numbers */
218 int nsd; /* Number of significant digits returned */
258 double rounder; /* Used for rounding floating point values */ 219 double rounder; /* Used for rounding floating point values */
259 etByte flag_dp; /* True if decimal point should be shown */ 220 etByte flag_dp; /* True if decimal point should be shown */
260 etByte flag_rtz; /* True if trailing zeros should be removed */ 221 etByte flag_rtz; /* True if trailing zeros should be removed */
261 etByte flag_exp; /* True to force display of the exponent */
262 int nsd; /* Number of significant digits returned */
263 #endif 222 #endif
223 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
224 char buf[etBUFSIZE]; /* Conversion buffer */
264 225
265 length = 0;
266 bufpt = 0; 226 bufpt = 0;
227 if( bFlags ){
228 if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
229 pArgList = va_arg(ap, PrintfArguments*);
230 }
231 useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
232 }else{
233 bArgList = useIntern = 0;
234 }
267 for(; (c=(*fmt))!=0; ++fmt){ 235 for(; (c=(*fmt))!=0; ++fmt){
268 if( c!='%' ){ 236 if( c!='%' ){
269 int amt;
270 bufpt = (char *)fmt; 237 bufpt = (char *)fmt;
271 amt = 1; 238 #if HAVE_STRCHRNUL
272 while( (c=(*++fmt))!='%' && c!=0 ) amt++; 239 fmt = strchrnul(fmt, '%');
273 sqlite3StrAccumAppend(pAccum, bufpt, amt); 240 #else
274 if( c==0 ) break; 241 do{ fmt++; }while( *fmt && *fmt != '%' );
242 #endif
243 sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt));
244 if( *fmt==0 ) break;
275 } 245 }
276 if( (c=(*++fmt))==0 ){ 246 if( (c=(*++fmt))==0 ){
277 sqlite3StrAccumAppend(pAccum, "%", 1); 247 sqlite3StrAccumAppend(pAccum, "%", 1);
278 break; 248 break;
279 } 249 }
280 /* Find out what flags are present */ 250 /* Find out what flags are present */
281 flag_leftjustify = flag_plussign = flag_blanksign = 251 flag_leftjustify = flag_plussign = flag_blanksign =
282 flag_alternateform = flag_altform2 = flag_zeropad = 0; 252 flag_alternateform = flag_altform2 = flag_zeropad = 0;
283 done = 0; 253 done = 0;
284 do{ 254 do{
285 switch( c ){ 255 switch( c ){
286 case '-': flag_leftjustify = 1; break; 256 case '-': flag_leftjustify = 1; break;
287 case '+': flag_plussign = 1; break; 257 case '+': flag_plussign = 1; break;
288 case ' ': flag_blanksign = 1; break; 258 case ' ': flag_blanksign = 1; break;
289 case '#': flag_alternateform = 1; break; 259 case '#': flag_alternateform = 1; break;
290 case '!': flag_altform2 = 1; break; 260 case '!': flag_altform2 = 1; break;
291 case '0': flag_zeropad = 1; break; 261 case '0': flag_zeropad = 1; break;
292 default: done = 1; break; 262 default: done = 1; break;
293 } 263 }
294 }while( !done && (c=(*++fmt))!=0 ); 264 }while( !done && (c=(*++fmt))!=0 );
295 /* Get the field width */ 265 /* Get the field width */
296 width = 0; 266 width = 0;
297 if( c=='*' ){ 267 if( c=='*' ){
298 width = va_arg(ap,int); 268 if( bArgList ){
269 width = (int)getIntArg(pArgList);
270 }else{
271 width = va_arg(ap,int);
272 }
299 if( width<0 ){ 273 if( width<0 ){
300 flag_leftjustify = 1; 274 flag_leftjustify = 1;
301 width = -width; 275 width = -width;
302 } 276 }
303 c = *++fmt; 277 c = *++fmt;
304 }else{ 278 }else{
305 while( c>='0' && c<='9' ){ 279 while( c>='0' && c<='9' ){
306 width = width*10 + c - '0'; 280 width = width*10 + c - '0';
307 c = *++fmt; 281 c = *++fmt;
308 } 282 }
309 } 283 }
310 if( width > etBUFSIZE-10 ){
311 width = etBUFSIZE-10;
312 }
313 /* Get the precision */ 284 /* Get the precision */
314 if( c=='.' ){ 285 if( c=='.' ){
315 precision = 0; 286 precision = 0;
316 c = *++fmt; 287 c = *++fmt;
317 if( c=='*' ){ 288 if( c=='*' ){
318 precision = va_arg(ap,int); 289 if( bArgList ){
290 precision = (int)getIntArg(pArgList);
291 }else{
292 precision = va_arg(ap,int);
293 }
319 if( precision<0 ) precision = -precision; 294 if( precision<0 ) precision = -precision;
320 c = *++fmt; 295 c = *++fmt;
321 }else{ 296 }else{
322 while( c>='0' && c<='9' ){ 297 while( c>='0' && c<='9' ){
323 precision = precision*10 + c - '0'; 298 precision = precision*10 + c - '0';
324 c = *++fmt; 299 c = *++fmt;
325 } 300 }
326 } 301 }
327 }else{ 302 }else{
328 precision = -1; 303 precision = -1;
(...skipping 10 matching lines...) Expand all
339 } 314 }
340 }else{ 315 }else{
341 flag_long = flag_longlong = 0; 316 flag_long = flag_longlong = 0;
342 } 317 }
343 /* Fetch the info entry for the field */ 318 /* Fetch the info entry for the field */
344 infop = &fmtinfo[0]; 319 infop = &fmtinfo[0];
345 xtype = etINVALID; 320 xtype = etINVALID;
346 for(idx=0; idx<ArraySize(fmtinfo); idx++){ 321 for(idx=0; idx<ArraySize(fmtinfo); idx++){
347 if( c==fmtinfo[idx].fmttype ){ 322 if( c==fmtinfo[idx].fmttype ){
348 infop = &fmtinfo[idx]; 323 infop = &fmtinfo[idx];
349 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){ 324 if( useIntern || (infop->flags & FLAG_INTERN)==0 ){
350 xtype = infop->type; 325 xtype = infop->type;
351 }else{ 326 }else{
352 return; 327 return;
353 } 328 }
354 break; 329 break;
355 } 330 }
356 } 331 }
357 zExtra = 0;
358
359
360 /* Limit the precision to prevent overflowing buf[] during conversion */
361 if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
362 precision = etBUFSIZE-40;
363 }
364 332
365 /* 333 /*
366 ** At this point, variables are initialized as follows: 334 ** At this point, variables are initialized as follows:
367 ** 335 **
368 ** flag_alternateform TRUE if a '#' is present. 336 ** flag_alternateform TRUE if a '#' is present.
369 ** flag_altform2 TRUE if a '!' is present. 337 ** flag_altform2 TRUE if a '!' is present.
370 ** flag_plussign TRUE if a '+' is present. 338 ** flag_plussign TRUE if a '+' is present.
371 ** flag_leftjustify TRUE if a '-' is present or if the 339 ** flag_leftjustify TRUE if a '-' is present or if the
372 ** field width was negative. 340 ** field width was negative.
373 ** flag_zeropad TRUE if the width began with 0. 341 ** flag_zeropad TRUE if the width began with 0.
(...skipping 11 matching lines...) Expand all
385 */ 353 */
386 switch( xtype ){ 354 switch( xtype ){
387 case etPOINTER: 355 case etPOINTER:
388 flag_longlong = sizeof(char*)==sizeof(i64); 356 flag_longlong = sizeof(char*)==sizeof(i64);
389 flag_long = sizeof(char*)==sizeof(long int); 357 flag_long = sizeof(char*)==sizeof(long int);
390 /* Fall through into the next case */ 358 /* Fall through into the next case */
391 case etORDINAL: 359 case etORDINAL:
392 case etRADIX: 360 case etRADIX:
393 if( infop->flags & FLAG_SIGNED ){ 361 if( infop->flags & FLAG_SIGNED ){
394 i64 v; 362 i64 v;
395 if( flag_longlong ){ 363 if( bArgList ){
364 v = getIntArg(pArgList);
365 }else if( flag_longlong ){
396 v = va_arg(ap,i64); 366 v = va_arg(ap,i64);
397 }else if( flag_long ){ 367 }else if( flag_long ){
398 v = va_arg(ap,long int); 368 v = va_arg(ap,long int);
399 }else{ 369 }else{
400 v = va_arg(ap,int); 370 v = va_arg(ap,int);
401 } 371 }
402 if( v<0 ){ 372 if( v<0 ){
403 if( v==SMALLEST_INT64 ){ 373 if( v==SMALLEST_INT64 ){
404 longvalue = ((u64)1)<<63; 374 longvalue = ((u64)1)<<63;
405 }else{ 375 }else{
406 longvalue = -v; 376 longvalue = -v;
407 } 377 }
408 prefix = '-'; 378 prefix = '-';
409 }else{ 379 }else{
410 longvalue = v; 380 longvalue = v;
411 if( flag_plussign ) prefix = '+'; 381 if( flag_plussign ) prefix = '+';
412 else if( flag_blanksign ) prefix = ' '; 382 else if( flag_blanksign ) prefix = ' ';
413 else prefix = 0; 383 else prefix = 0;
414 } 384 }
415 }else{ 385 }else{
416 if( flag_longlong ){ 386 if( bArgList ){
387 longvalue = (u64)getIntArg(pArgList);
388 }else if( flag_longlong ){
417 longvalue = va_arg(ap,u64); 389 longvalue = va_arg(ap,u64);
418 }else if( flag_long ){ 390 }else if( flag_long ){
419 longvalue = va_arg(ap,unsigned long int); 391 longvalue = va_arg(ap,unsigned long int);
420 }else{ 392 }else{
421 longvalue = va_arg(ap,unsigned int); 393 longvalue = va_arg(ap,unsigned int);
422 } 394 }
423 prefix = 0; 395 prefix = 0;
424 } 396 }
425 if( longvalue==0 ) flag_alternateform = 0; 397 if( longvalue==0 ) flag_alternateform = 0;
426 if( flag_zeropad && precision<width-(prefix!=0) ){ 398 if( flag_zeropad && precision<width-(prefix!=0) ){
427 precision = width-(prefix!=0); 399 precision = width-(prefix!=0);
428 } 400 }
429 bufpt = &buf[etBUFSIZE-1]; 401 if( precision<etBUFSIZE-10 ){
402 nOut = etBUFSIZE;
403 zOut = buf;
404 }else{
405 nOut = precision + 10;
406 zOut = zExtra = sqlite3Malloc( nOut );
407 if( zOut==0 ){
408 setStrAccumError(pAccum, STRACCUM_NOMEM);
409 return;
410 }
411 }
412 bufpt = &zOut[nOut-1];
430 if( xtype==etORDINAL ){ 413 if( xtype==etORDINAL ){
431 static const char zOrd[] = "thstndrd"; 414 static const char zOrd[] = "thstndrd";
432 int x = (int)(longvalue % 10); 415 int x = (int)(longvalue % 10);
433 if( x>=4 || (longvalue/10)%10==1 ){ 416 if( x>=4 || (longvalue/10)%10==1 ){
434 x = 0; 417 x = 0;
435 } 418 }
436 buf[etBUFSIZE-3] = zOrd[x*2]; 419 *(--bufpt) = zOrd[x*2+1];
437 buf[etBUFSIZE-2] = zOrd[x*2+1]; 420 *(--bufpt) = zOrd[x*2];
438 bufpt -= 2;
439 } 421 }
440 { 422 {
441 register const char *cset; /* Use registers for speed */ 423 const char *cset = &aDigits[infop->charset];
442 register int base; 424 u8 base = infop->base;
443 cset = &aDigits[infop->charset];
444 base = infop->base;
445 do{ /* Convert to ascii */ 425 do{ /* Convert to ascii */
446 *(--bufpt) = cset[longvalue%base]; 426 *(--bufpt) = cset[longvalue%base];
447 longvalue = longvalue/base; 427 longvalue = longvalue/base;
448 }while( longvalue>0 ); 428 }while( longvalue>0 );
449 } 429 }
450 length = (int)(&buf[etBUFSIZE-1]-bufpt); 430 length = (int)(&zOut[nOut-1]-bufpt);
451 for(idx=precision-length; idx>0; idx--){ 431 for(idx=precision-length; idx>0; idx--){
452 *(--bufpt) = '0'; /* Zero pad */ 432 *(--bufpt) = '0'; /* Zero pad */
453 } 433 }
454 if( prefix ) *(--bufpt) = prefix; /* Add sign */ 434 if( prefix ) *(--bufpt) = prefix; /* Add sign */
455 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ 435 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
456 const char *pre; 436 const char *pre;
457 char x; 437 char x;
458 pre = &aPrefix[infop->prefix]; 438 pre = &aPrefix[infop->prefix];
459 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; 439 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
460 } 440 }
461 length = (int)(&buf[etBUFSIZE-1]-bufpt); 441 length = (int)(&zOut[nOut-1]-bufpt);
462 break; 442 break;
463 case etFLOAT: 443 case etFLOAT:
464 case etEXP: 444 case etEXP:
465 case etGENERIC: 445 case etGENERIC:
466 realvalue = va_arg(ap,double); 446 if( bArgList ){
447 realvalue = getDoubleArg(pArgList);
448 }else{
449 realvalue = va_arg(ap,double);
450 }
467 #ifdef SQLITE_OMIT_FLOATING_POINT 451 #ifdef SQLITE_OMIT_FLOATING_POINT
468 length = 0; 452 length = 0;
469 #else 453 #else
470 if( precision<0 ) precision = 6; /* Set default precision */ 454 if( precision<0 ) precision = 6; /* Set default precision */
471 if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
472 if( realvalue<0.0 ){ 455 if( realvalue<0.0 ){
473 realvalue = -realvalue; 456 realvalue = -realvalue;
474 prefix = '-'; 457 prefix = '-';
475 }else{ 458 }else{
476 if( flag_plussign ) prefix = '+'; 459 if( flag_plussign ) prefix = '+';
477 else if( flag_blanksign ) prefix = ' '; 460 else if( flag_blanksign ) prefix = ' ';
478 else prefix = 0; 461 else prefix = 0;
479 } 462 }
480 if( xtype==etGENERIC && precision>0 ) precision--; 463 if( xtype==etGENERIC && precision>0 ) precision--;
481 #if 0
482 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
483 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
484 #else
485 /* It makes more sense to use 0.5 */
486 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){} 464 for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
487 #endif
488 if( xtype==etFLOAT ) realvalue += rounder; 465 if( xtype==etFLOAT ) realvalue += rounder;
489 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ 466 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
490 exp = 0; 467 exp = 0;
491 if( sqlite3IsNaN((double)realvalue) ){ 468 if( sqlite3IsNaN((double)realvalue) ){
492 bufpt = "NaN"; 469 bufpt = "NaN";
493 length = 3; 470 length = 3;
494 break; 471 break;
495 } 472 }
496 if( realvalue>0.0 ){ 473 if( realvalue>0.0 ){
497 while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; } 474 LONGDOUBLE_TYPE scale = 1.0;
498 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; } 475 while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
499 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; } 476 while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
477 while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
478 while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
479 realvalue /= scale;
500 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } 480 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
501 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } 481 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
502 if( exp>350 ){ 482 if( exp>350 ){
503 if( prefix=='-' ){ 483 if( prefix=='-' ){
504 bufpt = "-Inf"; 484 bufpt = "-Inf";
505 }else if( prefix=='+' ){ 485 }else if( prefix=='+' ){
506 bufpt = "+Inf"; 486 bufpt = "+Inf";
507 }else{ 487 }else{
508 bufpt = "Inf"; 488 bufpt = "Inf";
509 } 489 }
510 length = sqlite3Strlen30(bufpt); 490 length = sqlite3Strlen30(bufpt);
511 break; 491 break;
512 } 492 }
513 } 493 }
514 bufpt = buf; 494 bufpt = buf;
515 /* 495 /*
516 ** If the field type is etGENERIC, then convert to either etEXP 496 ** If the field type is etGENERIC, then convert to either etEXP
517 ** or etFLOAT, as appropriate. 497 ** or etFLOAT, as appropriate.
518 */ 498 */
519 flag_exp = xtype==etEXP;
520 if( xtype!=etFLOAT ){ 499 if( xtype!=etFLOAT ){
521 realvalue += rounder; 500 realvalue += rounder;
522 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } 501 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
523 } 502 }
524 if( xtype==etGENERIC ){ 503 if( xtype==etGENERIC ){
525 flag_rtz = !flag_alternateform; 504 flag_rtz = !flag_alternateform;
526 if( exp<-4 || exp>precision ){ 505 if( exp<-4 || exp>precision ){
527 xtype = etEXP; 506 xtype = etEXP;
528 }else{ 507 }else{
529 precision = precision - exp; 508 precision = precision - exp;
530 xtype = etFLOAT; 509 xtype = etFLOAT;
531 } 510 }
532 }else{ 511 }else{
533 flag_rtz = 0; 512 flag_rtz = flag_altform2;
534 } 513 }
535 if( xtype==etEXP ){ 514 if( xtype==etEXP ){
536 e2 = 0; 515 e2 = 0;
537 }else{ 516 }else{
538 e2 = exp; 517 e2 = exp;
539 } 518 }
540 nsd = 0; 519 if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
520 bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
521 if( bufpt==0 ){
522 setStrAccumError(pAccum, STRACCUM_NOMEM);
523 return;
524 }
525 }
526 zOut = bufpt;
527 nsd = 16 + flag_altform2*10;
541 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; 528 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
542 /* The sign in front of the number */ 529 /* The sign in front of the number */
543 if( prefix ){ 530 if( prefix ){
544 *(bufpt++) = prefix; 531 *(bufpt++) = prefix;
545 } 532 }
546 /* Digits prior to the decimal point */ 533 /* Digits prior to the decimal point */
547 if( e2<0 ){ 534 if( e2<0 ){
548 *(bufpt++) = '0'; 535 *(bufpt++) = '0';
549 }else{ 536 }else{
550 for(; e2>=0; e2--){ 537 for(; e2>=0; e2--){
(...skipping 10 matching lines...) Expand all
561 assert( precision>0 ); 548 assert( precision>0 );
562 *(bufpt++) = '0'; 549 *(bufpt++) = '0';
563 } 550 }
564 /* Significant digits after the decimal point */ 551 /* Significant digits after the decimal point */
565 while( (precision--)>0 ){ 552 while( (precision--)>0 ){
566 *(bufpt++) = et_getdigit(&realvalue,&nsd); 553 *(bufpt++) = et_getdigit(&realvalue,&nsd);
567 } 554 }
568 /* Remove trailing zeros and the "." if no digits follow the "." */ 555 /* Remove trailing zeros and the "." if no digits follow the "." */
569 if( flag_rtz && flag_dp ){ 556 if( flag_rtz && flag_dp ){
570 while( bufpt[-1]=='0' ) *(--bufpt) = 0; 557 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
571 assert( bufpt>buf ); 558 assert( bufpt>zOut );
572 if( bufpt[-1]=='.' ){ 559 if( bufpt[-1]=='.' ){
573 if( flag_altform2 ){ 560 if( flag_altform2 ){
574 *(bufpt++) = '0'; 561 *(bufpt++) = '0';
575 }else{ 562 }else{
576 *(--bufpt) = 0; 563 *(--bufpt) = 0;
577 } 564 }
578 } 565 }
579 } 566 }
580 /* Add the "eNNN" suffix */ 567 /* Add the "eNNN" suffix */
581 if( flag_exp || xtype==etEXP ){ 568 if( xtype==etEXP ){
582 *(bufpt++) = aDigits[infop->charset]; 569 *(bufpt++) = aDigits[infop->charset];
583 if( exp<0 ){ 570 if( exp<0 ){
584 *(bufpt++) = '-'; exp = -exp; 571 *(bufpt++) = '-'; exp = -exp;
585 }else{ 572 }else{
586 *(bufpt++) = '+'; 573 *(bufpt++) = '+';
587 } 574 }
588 if( exp>=100 ){ 575 if( exp>=100 ){
589 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ 576 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
590 exp %= 100; 577 exp %= 100;
591 } 578 }
592 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ 579 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
593 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ 580 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
594 } 581 }
595 *bufpt = 0; 582 *bufpt = 0;
596 583
597 /* The converted number is in buf[] and zero terminated. Output it. 584 /* The converted number is in buf[] and zero terminated. Output it.
598 ** Note that the number is in the usual order, not reversed as with 585 ** Note that the number is in the usual order, not reversed as with
599 ** integer conversions. */ 586 ** integer conversions. */
600 length = (int)(bufpt-buf); 587 length = (int)(bufpt-zOut);
601 bufpt = buf; 588 bufpt = zOut;
602 589
603 /* Special case: Add leading zeros if the flag_zeropad flag is 590 /* Special case: Add leading zeros if the flag_zeropad flag is
604 ** set and we are not left justified */ 591 ** set and we are not left justified */
605 if( flag_zeropad && !flag_leftjustify && length < width){ 592 if( flag_zeropad && !flag_leftjustify && length < width){
606 int i; 593 int i;
607 int nPad = width - length; 594 int nPad = width - length;
608 for(i=width; i>=nPad; i--){ 595 for(i=width; i>=nPad; i--){
609 bufpt[i] = bufpt[i-nPad]; 596 bufpt[i] = bufpt[i-nPad];
610 } 597 }
611 i = prefix!=0; 598 i = prefix!=0;
612 while( nPad-- ) bufpt[i++] = '0'; 599 while( nPad-- ) bufpt[i++] = '0';
613 length = width; 600 length = width;
614 } 601 }
615 #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ 602 #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
616 break; 603 break;
617 case etSIZE: 604 case etSIZE:
618 *(va_arg(ap,int*)) = pAccum->nChar; 605 if( !bArgList ){
606 *(va_arg(ap,int*)) = pAccum->nChar;
607 }
619 length = width = 0; 608 length = width = 0;
620 break; 609 break;
621 case etPERCENT: 610 case etPERCENT:
622 buf[0] = '%'; 611 buf[0] = '%';
623 bufpt = buf; 612 bufpt = buf;
624 length = 1; 613 length = 1;
625 break; 614 break;
626 case etCHARX: 615 case etCHARX:
627 c = va_arg(ap,int); 616 if( bArgList ){
628 buf[0] = (char)c; 617 bufpt = getTextArg(pArgList);
629 if( precision>=0 ){ 618 c = bufpt ? bufpt[0] : 0;
630 for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
631 length = precision;
632 }else{ 619 }else{
633 length =1; 620 c = va_arg(ap,int);
634 } 621 }
622 if( precision>1 ){
623 width -= precision-1;
624 if( width>1 && !flag_leftjustify ){
625 sqlite3AppendChar(pAccum, width-1, ' ');
626 width = 0;
627 }
628 sqlite3AppendChar(pAccum, precision-1, c);
629 }
630 length = 1;
631 buf[0] = c;
635 bufpt = buf; 632 bufpt = buf;
636 break; 633 break;
637 case etSTRING: 634 case etSTRING:
638 case etDYNSTRING: 635 case etDYNSTRING:
639 bufpt = va_arg(ap,char*); 636 if( bArgList ){
637 bufpt = getTextArg(pArgList);
638 }else{
639 bufpt = va_arg(ap,char*);
640 }
640 if( bufpt==0 ){ 641 if( bufpt==0 ){
641 bufpt = ""; 642 bufpt = "";
642 }else if( xtype==etDYNSTRING ){ 643 }else if( xtype==etDYNSTRING && !bArgList ){
643 zExtra = bufpt; 644 zExtra = bufpt;
644 } 645 }
645 if( precision>=0 ){ 646 if( precision>=0 ){
646 for(length=0; length<precision && bufpt[length]; length++){} 647 for(length=0; length<precision && bufpt[length]; length++){}
647 }else{ 648 }else{
648 length = sqlite3Strlen30(bufpt); 649 length = sqlite3Strlen30(bufpt);
649 } 650 }
650 break; 651 break;
651 case etSQLESCAPE: 652 case etSQLESCAPE:
652 case etSQLESCAPE2: 653 case etSQLESCAPE2:
653 case etSQLESCAPE3: { 654 case etSQLESCAPE3: {
654 int i, j, k, n, isnull; 655 int i, j, k, n, isnull;
655 int needQuote; 656 int needQuote;
656 char ch; 657 char ch;
657 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ 658 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
658 char *escarg = va_arg(ap,char*); 659 char *escarg;
660
661 if( bArgList ){
662 escarg = getTextArg(pArgList);
663 }else{
664 escarg = va_arg(ap,char*);
665 }
659 isnull = escarg==0; 666 isnull = escarg==0;
660 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 667 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
661 k = precision; 668 k = precision;
662 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ 669 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
663 if( ch==q ) n++; 670 if( ch==q ) n++;
664 } 671 }
665 needQuote = !isnull && xtype==etSQLESCAPE2; 672 needQuote = !isnull && xtype==etSQLESCAPE2;
666 n += i + 1 + needQuote*2; 673 n += i + 1 + needQuote*2;
667 if( n>etBUFSIZE ){ 674 if( n>etBUFSIZE ){
668 bufpt = zExtra = sqlite3Malloc( n ); 675 bufpt = zExtra = sqlite3Malloc( n );
669 if( bufpt==0 ){ 676 if( bufpt==0 ){
670 pAccum->mallocFailed = 1; 677 setStrAccumError(pAccum, STRACCUM_NOMEM);
671 return; 678 return;
672 } 679 }
673 }else{ 680 }else{
674 bufpt = buf; 681 bufpt = buf;
675 } 682 }
676 j = 0; 683 j = 0;
677 if( needQuote ) bufpt[j++] = q; 684 if( needQuote ) bufpt[j++] = q;
678 k = i; 685 k = i;
679 for(i=0; i<k; i++){ 686 for(i=0; i<k; i++){
680 bufpt[j++] = ch = escarg[i]; 687 bufpt[j++] = ch = escarg[i];
681 if( ch==q ) bufpt[j++] = ch; 688 if( ch==q ) bufpt[j++] = ch;
682 } 689 }
683 if( needQuote ) bufpt[j++] = q; 690 if( needQuote ) bufpt[j++] = q;
684 bufpt[j] = 0; 691 bufpt[j] = 0;
685 length = j; 692 length = j;
686 /* The precision in %q and %Q means how many input characters to 693 /* The precision in %q and %Q means how many input characters to
687 ** consume, not the length of the output... 694 ** consume, not the length of the output...
688 ** if( precision>=0 && precision<length ) length = precision; */ 695 ** if( precision>=0 && precision<length ) length = precision; */
689 break; 696 break;
690 } 697 }
691 case etTOKEN: { 698 case etTOKEN: {
692 Token *pToken = va_arg(ap, Token*); 699 Token *pToken = va_arg(ap, Token*);
693 if( pToken ){ 700 assert( bArgList==0 );
701 if( pToken && pToken->n ){
694 sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); 702 sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
695 } 703 }
696 length = width = 0; 704 length = width = 0;
697 break; 705 break;
698 } 706 }
699 case etSRCLIST: { 707 case etSRCLIST: {
700 SrcList *pSrc = va_arg(ap, SrcList*); 708 SrcList *pSrc = va_arg(ap, SrcList*);
701 int k = va_arg(ap, int); 709 int k = va_arg(ap, int);
702 struct SrcList_item *pItem = &pSrc->a[k]; 710 struct SrcList_item *pItem = &pSrc->a[k];
711 assert( bArgList==0 );
703 assert( k>=0 && k<pSrc->nSrc ); 712 assert( k>=0 && k<pSrc->nSrc );
704 if( pItem->zDatabase ){ 713 if( pItem->zDatabase ){
705 sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1); 714 sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
706 sqlite3StrAccumAppend(pAccum, ".", 1); 715 sqlite3StrAccumAppend(pAccum, ".", 1);
707 } 716 }
708 sqlite3StrAccumAppend(pAccum, pItem->zName, -1); 717 sqlite3StrAccumAppendAll(pAccum, pItem->zName);
709 length = width = 0; 718 length = width = 0;
710 break; 719 break;
711 } 720 }
712 default: { 721 default: {
713 assert( xtype==etINVALID ); 722 assert( xtype==etINVALID );
714 return; 723 return;
715 } 724 }
716 }/* End switch over the format type */ 725 }/* End switch over the format type */
717 /* 726 /*
718 ** The text of the conversion is pointed to by "bufpt" and is 727 ** The text of the conversion is pointed to by "bufpt" and is
719 ** "length" characters long. The field width is "width". Do 728 ** "length" characters long. The field width is "width". Do
720 ** the output. 729 ** the output.
721 */ 730 */
722 if( !flag_leftjustify ){ 731 width -= length;
723 register int nspace; 732 if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
724 nspace = width-length; 733 sqlite3StrAccumAppend(pAccum, bufpt, length);
725 if( nspace>0 ){ 734 if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
726 appendSpace(pAccum, nspace); 735
727 }
728 }
729 if( length>0 ){
730 sqlite3StrAccumAppend(pAccum, bufpt, length);
731 }
732 if( flag_leftjustify ){
733 register int nspace;
734 nspace = width-length;
735 if( nspace>0 ){
736 appendSpace(pAccum, nspace);
737 }
738 }
739 if( zExtra ){ 736 if( zExtra ){
740 sqlite3_free(zExtra); 737 sqlite3_free(zExtra);
738 zExtra = 0;
741 } 739 }
742 }/* End for loop over the format string */ 740 }/* End for loop over the format string */
743 } /* End of function */ 741 } /* End of function */
744 742
745 /* 743 /*
746 ** Append N bytes of text from z to the StrAccum object. 744 ** Enlarge the memory allocation on a StrAccum object so that it is
745 ** able to accept at least N more bytes of text.
746 **
747 ** Return the number of bytes of text that StrAccum is able to accept
748 ** after the attempted enlargement. The value returned might be zero.
749 */
750 static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
751 char *zNew;
752 assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */
753 if( p->accError ){
754 testcase(p->accError==STRACCUM_TOOBIG);
755 testcase(p->accError==STRACCUM_NOMEM);
756 return 0;
757 }
758 if( !p->useMalloc ){
759 N = p->nAlloc - p->nChar - 1;
760 setStrAccumError(p, STRACCUM_TOOBIG);
761 return N;
762 }else{
763 char *zOld = (p->zText==p->zBase ? 0 : p->zText);
764 i64 szNew = p->nChar;
765 szNew += N + 1;
766 if( szNew > p->mxAlloc ){
767 sqlite3StrAccumReset(p);
768 setStrAccumError(p, STRACCUM_TOOBIG);
769 return 0;
770 }else{
771 p->nAlloc = (int)szNew;
772 }
773 if( p->useMalloc==1 ){
774 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
775 }else{
776 zNew = sqlite3_realloc(zOld, p->nAlloc);
777 }
778 if( zNew ){
779 assert( p->zText!=0 || p->nChar==0 );
780 if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
781 p->zText = zNew;
782 }else{
783 sqlite3StrAccumReset(p);
784 setStrAccumError(p, STRACCUM_NOMEM);
785 return 0;
786 }
787 }
788 return N;
789 }
790
791 /*
792 ** Append N copies of character c to the given string buffer.
793 */
794 void sqlite3AppendChar(StrAccum *p, int N, char c){
795 if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ) return;
796 while( (N--)>0 ) p->zText[p->nChar++] = c;
797 }
798
799 /*
800 ** The StrAccum "p" is not large enough to accept N new bytes of z[].
801 ** So enlarge if first, then do the append.
802 **
803 ** This is a helper routine to sqlite3StrAccumAppend() that does special-case
804 ** work (enlarging the buffer) using tail recursion, so that the
805 ** sqlite3StrAccumAppend() routine can use fast calling semantics.
806 */
807 static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
808 N = sqlite3StrAccumEnlarge(p, N);
809 if( N>0 ){
810 memcpy(&p->zText[p->nChar], z, N);
811 p->nChar += N;
812 }
813 }
814
815 /*
816 ** Append N bytes of text from z to the StrAccum object. Increase the
817 ** size of the memory allocation for StrAccum if necessary.
747 */ 818 */
748 void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ 819 void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
749 assert( z!=0 || N==0 ); 820 assert( z!=0 );
750 if( p->tooBig | p->mallocFailed ){ 821 assert( p->zText!=0 || p->nChar==0 || p->accError );
751 testcase(p->tooBig); 822 assert( N>=0 );
752 testcase(p->mallocFailed); 823 assert( p->accError==0 || p->nAlloc==0 );
753 return; 824 if( p->nChar+N >= p->nAlloc ){
825 enlargeAndAppend(p,z,N);
826 }else{
827 assert( p->zText );
828 p->nChar += N;
829 memcpy(&p->zText[p->nChar-N], z, N);
754 } 830 }
755 if( N<0 ){
756 N = sqlite3Strlen30(z);
757 }
758 if( N==0 || NEVER(z==0) ){
759 return;
760 }
761 if( p->nChar+N >= p->nAlloc ){
762 char *zNew;
763 if( !p->useMalloc ){
764 p->tooBig = 1;
765 N = p->nAlloc - p->nChar - 1;
766 if( N<=0 ){
767 return;
768 }
769 }else{
770 char *zOld = (p->zText==p->zBase ? 0 : p->zText);
771 i64 szNew = p->nChar;
772 szNew += N + 1;
773 if( szNew > p->mxAlloc ){
774 sqlite3StrAccumReset(p);
775 p->tooBig = 1;
776 return;
777 }else{
778 p->nAlloc = (int)szNew;
779 }
780 if( p->useMalloc==1 ){
781 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
782 }else{
783 zNew = sqlite3_realloc(zOld, p->nAlloc);
784 }
785 if( zNew ){
786 if( zOld==0 ) memcpy(zNew, p->zText, p->nChar);
787 p->zText = zNew;
788 }else{
789 p->mallocFailed = 1;
790 sqlite3StrAccumReset(p);
791 return;
792 }
793 }
794 }
795 memcpy(&p->zText[p->nChar], z, N);
796 p->nChar += N;
797 } 831 }
798 832
799 /* 833 /*
834 ** Append the complete text of zero-terminated string z[] to the p string.
835 */
836 void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
837 sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));
838 }
839
840
841 /*
800 ** Finish off a string by making sure it is zero-terminated. 842 ** Finish off a string by making sure it is zero-terminated.
801 ** Return a pointer to the resulting string. Return a NULL 843 ** Return a pointer to the resulting string. Return a NULL
802 ** pointer if any kind of error was encountered. 844 ** pointer if any kind of error was encountered.
803 */ 845 */
804 char *sqlite3StrAccumFinish(StrAccum *p){ 846 char *sqlite3StrAccumFinish(StrAccum *p){
805 if( p->zText ){ 847 if( p->zText ){
806 p->zText[p->nChar] = 0; 848 p->zText[p->nChar] = 0;
807 if( p->useMalloc && p->zText==p->zBase ){ 849 if( p->useMalloc && p->zText==p->zBase ){
808 if( p->useMalloc==1 ){ 850 if( p->useMalloc==1 ){
809 p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); 851 p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
810 }else{ 852 }else{
811 p->zText = sqlite3_malloc(p->nChar+1); 853 p->zText = sqlite3_malloc(p->nChar+1);
812 } 854 }
813 if( p->zText ){ 855 if( p->zText ){
814 memcpy(p->zText, p->zBase, p->nChar+1); 856 memcpy(p->zText, p->zBase, p->nChar+1);
815 }else{ 857 }else{
816 p->mallocFailed = 1; 858 setStrAccumError(p, STRACCUM_NOMEM);
817 } 859 }
818 } 860 }
819 } 861 }
820 return p->zText; 862 return p->zText;
821 } 863 }
822 864
823 /* 865 /*
824 ** Reset an StrAccum string. Reclaim all malloced memory. 866 ** Reset an StrAccum string. Reclaim all malloced memory.
825 */ 867 */
826 void sqlite3StrAccumReset(StrAccum *p){ 868 void sqlite3StrAccumReset(StrAccum *p){
(...skipping 10 matching lines...) Expand all
837 /* 879 /*
838 ** Initialize a string accumulator 880 ** Initialize a string accumulator
839 */ 881 */
840 void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){ 882 void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
841 p->zText = p->zBase = zBase; 883 p->zText = p->zBase = zBase;
842 p->db = 0; 884 p->db = 0;
843 p->nChar = 0; 885 p->nChar = 0;
844 p->nAlloc = n; 886 p->nAlloc = n;
845 p->mxAlloc = mx; 887 p->mxAlloc = mx;
846 p->useMalloc = 1; 888 p->useMalloc = 1;
847 p->tooBig = 0; 889 p->accError = 0;
848 p->mallocFailed = 0;
849 } 890 }
850 891
851 /* 892 /*
852 ** Print into memory obtained from sqliteMalloc(). Use the internal 893 ** Print into memory obtained from sqliteMalloc(). Use the internal
853 ** %-conversion extensions. 894 ** %-conversion extensions.
854 */ 895 */
855 char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ 896 char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
856 char *z; 897 char *z;
857 char zBase[SQLITE_PRINT_BUF_SIZE]; 898 char zBase[SQLITE_PRINT_BUF_SIZE];
858 StrAccum acc; 899 StrAccum acc;
859 assert( db!=0 ); 900 assert( db!=0 );
860 sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), 901 sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
861 db->aLimit[SQLITE_LIMIT_LENGTH]); 902 db->aLimit[SQLITE_LIMIT_LENGTH]);
862 acc.db = db; 903 acc.db = db;
863 sqlite3VXPrintf(&acc, 1, zFormat, ap); 904 sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap);
864 z = sqlite3StrAccumFinish(&acc); 905 z = sqlite3StrAccumFinish(&acc);
865 if( acc.mallocFailed ){ 906 if( acc.accError==STRACCUM_NOMEM ){
866 db->mallocFailed = 1; 907 db->mallocFailed = 1;
867 } 908 }
868 return z; 909 return z;
869 } 910 }
870 911
871 /* 912 /*
872 ** Print into memory obtained from sqliteMalloc(). Use the internal 913 ** Print into memory obtained from sqliteMalloc(). Use the internal
873 ** %-conversion extensions. 914 ** %-conversion extensions.
874 */ 915 */
875 char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ 916 char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
876 va_list ap; 917 va_list ap;
877 char *z; 918 char *z;
878 va_start(ap, zFormat); 919 va_start(ap, zFormat);
879 z = sqlite3VMPrintf(db, zFormat, ap); 920 z = sqlite3VMPrintf(db, zFormat, ap);
880 va_end(ap); 921 va_end(ap);
881 return z; 922 return z;
882 } 923 }
883 924
884 /* 925 /*
885 ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting 926 ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
886 ** the string and before returnning. This routine is intended to be used 927 ** the string and before returning. This routine is intended to be used
887 ** to modify an existing string. For example: 928 ** to modify an existing string. For example:
888 ** 929 **
889 ** x = sqlite3MPrintf(db, x, "prefix %s suffix", x); 930 ** x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
890 ** 931 **
891 */ 932 */
892 char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){ 933 char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
893 va_list ap; 934 va_list ap;
894 char *z; 935 char *z;
895 va_start(ap, zFormat); 936 va_start(ap, zFormat);
896 z = sqlite3VMPrintf(db, zFormat, ap); 937 z = sqlite3VMPrintf(db, zFormat, ap);
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
1009 acc.useMalloc = 0; 1050 acc.useMalloc = 0;
1010 va_start(ap,zFormat); 1051 va_start(ap,zFormat);
1011 sqlite3VXPrintf(&acc, 0, zFormat, ap); 1052 sqlite3VXPrintf(&acc, 0, zFormat, ap);
1012 va_end(ap); 1053 va_end(ap);
1013 sqlite3StrAccumFinish(&acc); 1054 sqlite3StrAccumFinish(&acc);
1014 fprintf(stdout,"%s", zBuf); 1055 fprintf(stdout,"%s", zBuf);
1015 fflush(stdout); 1056 fflush(stdout);
1016 } 1057 }
1017 #endif 1058 #endif
1018 1059
1019 #ifndef SQLITE_OMIT_TRACE 1060 #ifdef SQLITE_DEBUG
1061 /*************************************************************************
1062 ** Routines for implementing the "TreeView" display of hierarchical
1063 ** data structures for debugging.
1064 **
1065 ** The main entry points (coded elsewhere) are:
1066 ** sqlite3TreeViewExpr(0, pExpr, 0);
1067 ** sqlite3TreeViewExprList(0, pList, 0, 0);
1068 ** sqlite3TreeViewSelect(0, pSelect, 0);
1069 ** Insert calls to those routines while debugging in order to display
1070 ** a diagram of Expr, ExprList, and Select objects.
1071 **
1072 */
1073 /* Add a new subitem to the tree. The moreToFollow flag indicates that this
1074 ** is not the last item in the tree. */
1075 TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
1076 if( p==0 ){
1077 p = sqlite3_malloc( sizeof(*p) );
1078 if( p==0 ) return 0;
1079 memset(p, 0, sizeof(*p));
1080 }else{
1081 p->iLevel++;
1082 }
1083 assert( moreToFollow==0 || moreToFollow==1 );
1084 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
1085 return p;
1086 }
1087 /* Finished with one layer of the tree */
1088 void sqlite3TreeViewPop(TreeView *p){
1089 if( p==0 ) return;
1090 p->iLevel--;
1091 if( p->iLevel<0 ) sqlite3_free(p);
1092 }
1093 /* Generate a single line of output for the tree, with a prefix that contains
1094 ** all the appropriate tree lines */
1095 void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
1096 va_list ap;
1097 int i;
1098 StrAccum acc;
1099 char zBuf[500];
1100 sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
1101 acc.useMalloc = 0;
1102 if( p ){
1103 for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
1104 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4);
1105 }
1106 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
1107 }
1108 va_start(ap, zFormat);
1109 sqlite3VXPrintf(&acc, 0, zFormat, ap);
1110 va_end(ap);
1111 if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
1112 sqlite3StrAccumFinish(&acc);
1113 fprintf(stdout,"%s", zBuf);
1114 fflush(stdout);
1115 }
1116 /* Shorthand for starting a new tree item that consists of a single label */
1117 void sqlite3TreeViewItem(TreeView *p, const char *zLabel, u8 moreToFollow){
1118 p = sqlite3TreeViewPush(p, moreToFollow);
1119 sqlite3TreeViewLine(p, "%s", zLabel);
1120 }
1121 #endif /* SQLITE_DEBUG */
1122
1020 /* 1123 /*
1021 ** variable-argument wrapper around sqlite3VXPrintf(). 1124 ** variable-argument wrapper around sqlite3VXPrintf().
1022 */ 1125 */
1023 void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){ 1126 void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
1024 va_list ap; 1127 va_list ap;
1025 va_start(ap,zFormat); 1128 va_start(ap,zFormat);
1026 sqlite3VXPrintf(p, 1, zFormat, ap); 1129 sqlite3VXPrintf(p, bFlags, zFormat, ap);
1027 va_end(ap); 1130 va_end(ap);
1028 } 1131 }
1029 #endif
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3080704/src/prepare.c ('k') | third_party/sqlite/sqlite-src-3080704/src/random.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698