xref: /sqlite-3.40.0/src/printf.c (revision fc6ee9df)
1a18c5681Sdrh /*
2a18c5681Sdrh ** The "printf" code that follows dates from the 1980's.  It is in
3a18c5681Sdrh ** the public domain.  The original comments are included here for
4e84a306bSdrh ** completeness.  They are very out-of-date but might be useful as
5e84a306bSdrh ** an historical reference.  Most of the "enhancements" have been backed
6e84a306bSdrh ** out so that the functionality is now the same as standard printf().
7e84a306bSdrh **
8e84a306bSdrh **************************************************************************
9a18c5681Sdrh **
10ed1fddf4Sdrh ** This file contains code for a set of "printf"-like routines.  These
11ed1fddf4Sdrh ** routines format strings much like the printf() from the standard C
12ed1fddf4Sdrh ** library, though the implementation here has enhancements to support
13ed1fddf4Sdrh ** SQLlite.
14a18c5681Sdrh */
15a18c5681Sdrh #include "sqliteInt.h"
167c68d60bSdrh 
17a18c5681Sdrh /*
18a18c5681Sdrh ** Conversion types fall into various categories as defined by the
19a18c5681Sdrh ** following enumeration.
20a18c5681Sdrh */
21e84a306bSdrh #define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
22e84a306bSdrh #define etFLOAT       2 /* Floating point.  %f */
23e84a306bSdrh #define etEXP         3 /* Exponentional notation. %e and %E */
24e84a306bSdrh #define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
25e84a306bSdrh #define etSIZE        5 /* Return number of characters processed so far. %n */
26e84a306bSdrh #define etSTRING      6 /* Strings. %s */
27e84a306bSdrh #define etDYNSTRING   7 /* Dynamically allocated strings. %z */
28e84a306bSdrh #define etPERCENT     8 /* Percent symbol. %% */
29e84a306bSdrh #define etCHARX       9 /* Characters. %c */
30a18c5681Sdrh /* The rest are extensions, not normally found in printf() */
31af005fbcSdrh #define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
32af005fbcSdrh #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
330cfcf3fbSchw                           NULL pointers replaced by SQL NULL.  %Q */
34af005fbcSdrh #define etTOKEN      12 /* a pointer to a Token structure */
35af005fbcSdrh #define etSRCLIST    13 /* a pointer to a SrcList */
36af005fbcSdrh #define etPOINTER    14 /* The %p conversion */
37af005fbcSdrh #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
38af005fbcSdrh #define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
39e84a306bSdrh 
40874ba04cSdrh #define etINVALID     0 /* Any unrecognized conversion type */
41874ba04cSdrh 
42e84a306bSdrh 
43e84a306bSdrh /*
44e84a306bSdrh ** An "etByte" is an 8-bit unsigned value.
45e84a306bSdrh */
46e84a306bSdrh typedef unsigned char etByte;
47a18c5681Sdrh 
48a18c5681Sdrh /*
49a18c5681Sdrh ** Each builtin conversion character (ex: the 'd' in "%d") is described
50a18c5681Sdrh ** by an instance of the following structure
51a18c5681Sdrh */
52a18c5681Sdrh typedef struct et_info {   /* Information about each format field */
53e84a306bSdrh   char fmttype;            /* The format field code letter */
54e84a306bSdrh   etByte base;             /* The base for radix conversion */
55e84a306bSdrh   etByte flags;            /* One or more of FLAG_ constants below */
56e84a306bSdrh   etByte type;             /* Conversion paradigm */
5776ff3a0eSdrh   etByte charset;          /* Offset into aDigits[] of the digits string */
5876ff3a0eSdrh   etByte prefix;           /* Offset into aPrefix[] of the prefix string */
59a18c5681Sdrh } et_info;
60a18c5681Sdrh 
61a18c5681Sdrh /*
62e84a306bSdrh ** Allowed values for et_info.flags
63e84a306bSdrh */
64e84a306bSdrh #define FLAG_SIGNED  1     /* True if the value to convert is signed */
65e84a306bSdrh #define FLAG_INTERN  2     /* True if for internal use only */
664794f735Sdrh #define FLAG_STRING  4     /* Allow infinity precision */
67e84a306bSdrh 
68e84a306bSdrh 
69e84a306bSdrh /*
70a18c5681Sdrh ** The following table is searched linearly, so it is good to put the
71a18c5681Sdrh ** most frequently used conversion types first.
72a18c5681Sdrh */
7376ff3a0eSdrh static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
7476ff3a0eSdrh static const char aPrefix[] = "-x0\000X0";
755719628aSdrh static const et_info fmtinfo[] = {
7676ff3a0eSdrh   {  'd', 10, 1, etRADIX,      0,  0 },
774794f735Sdrh   {  's',  0, 4, etSTRING,     0,  0 },
78557cc60fSdrh   {  'g',  0, 1, etGENERIC,    30, 0 },
79153c62c4Sdrh   {  'z',  0, 4, etDYNSTRING,  0,  0 },
804794f735Sdrh   {  'q',  0, 4, etSQLESCAPE,  0,  0 },
814794f735Sdrh   {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
82f3b863edSdanielk1977   {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
83e84a306bSdrh   {  'c',  0, 0, etCHARX,      0,  0 },
8476ff3a0eSdrh   {  'o',  8, 0, etRADIX,      0,  2 },
8576ff3a0eSdrh   {  'u', 10, 0, etRADIX,      0,  0 },
8676ff3a0eSdrh   {  'x', 16, 0, etRADIX,      16, 1 },
8776ff3a0eSdrh   {  'X', 16, 0, etRADIX,      0,  4 },
88b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT
89e84a306bSdrh   {  'f',  0, 1, etFLOAT,      0,  0 },
9076ff3a0eSdrh   {  'e',  0, 1, etEXP,        30, 0 },
9176ff3a0eSdrh   {  'E',  0, 1, etEXP,        14, 0 },
9276ff3a0eSdrh   {  'G',  0, 1, etGENERIC,    14, 0 },
93b37df7b9Sdrh #endif
9476ff3a0eSdrh   {  'i', 10, 1, etRADIX,      0,  0 },
95e84a306bSdrh   {  'n',  0, 0, etSIZE,       0,  0 },
96e84a306bSdrh   {  '%',  0, 0, etPERCENT,    0,  0 },
9776ff3a0eSdrh   {  'p', 16, 0, etPOINTER,    0,  1 },
987e3ff5d8Sdrh 
997e3ff5d8Sdrh /* All the rest have the FLAG_INTERN bit set and are thus for internal
1007e3ff5d8Sdrh ** use only */
1015f968436Sdrh   {  'T',  0, 2, etTOKEN,      0,  0 },
1025f968436Sdrh   {  'S',  0, 2, etSRCLIST,    0,  0 },
1039a99334dSdrh   {  'r', 10, 3, etORDINAL,    0,  0 },
104a18c5681Sdrh };
105a18c5681Sdrh 
106a18c5681Sdrh /*
107b37df7b9Sdrh ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
108a18c5681Sdrh ** conversions will work.
109a18c5681Sdrh */
110b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT
111a18c5681Sdrh /*
112a18c5681Sdrh ** "*val" is a double such that 0.1 <= *val < 10.0
113a18c5681Sdrh ** Return the ascii code for the leading digit of *val, then
114a18c5681Sdrh ** multiply "*val" by 10.0 to renormalize.
115a18c5681Sdrh **
116a18c5681Sdrh ** Example:
117a18c5681Sdrh **     input:     *val = 3.14159
118a18c5681Sdrh **     output:    *val = 1.4159    function return = '3'
119a18c5681Sdrh **
120a18c5681Sdrh ** The counter *cnt is incremented each time.  After counter exceeds
121a18c5681Sdrh ** 16 (the number of significant digits in a 64-bit float) '0' is
122a18c5681Sdrh ** always returned.
123a18c5681Sdrh */
124ea678832Sdrh static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
125a18c5681Sdrh   int digit;
126384eef32Sdrh   LONGDOUBLE_TYPE d;
12772b3fbc7Sdrh   if( (*cnt)<=0 ) return '0';
12872b3fbc7Sdrh   (*cnt)--;
129a18c5681Sdrh   digit = (int)*val;
130a18c5681Sdrh   d = digit;
131a18c5681Sdrh   digit += '0';
132a18c5681Sdrh   *val = (*val - d)*10.0;
133ea678832Sdrh   return (char)digit;
134a18c5681Sdrh }
135b37df7b9Sdrh #endif /* SQLITE_OMIT_FLOATING_POINT */
136a18c5681Sdrh 
13779158e18Sdrh /*
138ade86483Sdrh ** Append N space characters to the given string buffer.
139ade86483Sdrh */
1407e02e5e6Sdrh void sqlite3AppendSpace(StrAccum *pAccum, int N){
141ade86483Sdrh   static const char zSpaces[] = "                             ";
14200e13613Sdanielk1977   while( N>=(int)sizeof(zSpaces)-1 ){
143ade86483Sdrh     sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
144ade86483Sdrh     N -= sizeof(zSpaces)-1;
145ade86483Sdrh   }
146ade86483Sdrh   if( N>0 ){
147ade86483Sdrh     sqlite3StrAccumAppend(pAccum, zSpaces, N);
148ade86483Sdrh   }
149ade86483Sdrh }
150ade86483Sdrh 
151ade86483Sdrh /*
152a6353a3fSdrh ** Set the StrAccum object to an error mode.
153a6353a3fSdrh */
154a5c1416dSdrh static void setStrAccumError(StrAccum *p, u8 eError){
155a6353a3fSdrh   p->accError = eError;
156a6353a3fSdrh   p->nAlloc = 0;
157a6353a3fSdrh }
158a6353a3fSdrh 
159a6353a3fSdrh /*
160a5c1416dSdrh ** Extra argument values from a PrintfArguments object
161a5c1416dSdrh */
162a5c1416dSdrh static sqlite3_int64 getIntArg(PrintfArguments *p){
163a5c1416dSdrh   if( p->nArg<=p->nUsed ) return 0;
164a5c1416dSdrh   return sqlite3_value_int64(p->apArg[p->nUsed++]);
165a5c1416dSdrh }
166a5c1416dSdrh static double getDoubleArg(PrintfArguments *p){
167a5c1416dSdrh   if( p->nArg<=p->nUsed ) return 0.0;
168a5c1416dSdrh   return sqlite3_value_double(p->apArg[p->nUsed++]);
169a5c1416dSdrh }
170a5c1416dSdrh static char *getTextArg(PrintfArguments *p){
171a5c1416dSdrh   if( p->nArg<=p->nUsed ) return 0;
172a5c1416dSdrh   return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
173a5c1416dSdrh }
174a5c1416dSdrh 
175a5c1416dSdrh 
176a5c1416dSdrh /*
17779158e18Sdrh ** On machines with a small stack size, you can redefine the
178ed1fddf4Sdrh ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
17979158e18Sdrh */
18079158e18Sdrh #ifndef SQLITE_PRINT_BUF_SIZE
18159eedf79Sdrh # define SQLITE_PRINT_BUF_SIZE 70
18250d654daSdrh #endif
18379158e18Sdrh #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
184a18c5681Sdrh 
185a18c5681Sdrh /*
186ed1fddf4Sdrh ** Render a string given by "fmt" into the StrAccum object.
187a18c5681Sdrh */
188f089aa45Sdrh void sqlite3VXPrintf(
189ade86483Sdrh   StrAccum *pAccum,          /* Accumulate results here */
190a5c1416dSdrh   u32 bFlags,                /* SQLITE_PRINTF_* flags */
1915f968436Sdrh   const char *fmt,           /* Format string */
1925f968436Sdrh   va_list ap                 /* arguments */
193a18c5681Sdrh ){
194e84a306bSdrh   int c;                     /* Next character in the format string */
195e84a306bSdrh   char *bufpt;               /* Pointer to the conversion buffer */
196e84a306bSdrh   int precision;             /* Precision of the current field */
197e84a306bSdrh   int length;                /* Length of the field */
198e84a306bSdrh   int idx;                   /* A general purpose loop counter */
199a18c5681Sdrh   int width;                 /* Width of the current field */
200e84a306bSdrh   etByte flag_leftjustify;   /* True if "-" flag is present */
201e84a306bSdrh   etByte flag_plussign;      /* True if "+" flag is present */
202e84a306bSdrh   etByte flag_blanksign;     /* True if " " flag is present */
203e84a306bSdrh   etByte flag_alternateform; /* True if "#" flag is present */
204531fe878Sdrh   etByte flag_altform2;      /* True if "!" flag is present */
205e84a306bSdrh   etByte flag_zeropad;       /* True if field width constant starts with zero */
206e84a306bSdrh   etByte flag_long;          /* True if "l" flag is present */
207a34b6764Sdrh   etByte flag_longlong;      /* True if the "ll" flag is present */
2083e9aeec0Sdrh   etByte done;               /* Loop termination flag */
209ed1fddf4Sdrh   etByte xtype = 0;          /* Conversion paradigm */
210a5c1416dSdrh   u8 bArgList;               /* True for SQLITE_PRINTF_SQLFUNC */
211a5c1416dSdrh   u8 useIntern;              /* Ok to use internal conversions (ex: %T) */
212ed1fddf4Sdrh   char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
21327436af7Sdrh   sqlite_uint64 longvalue;   /* Value for integer types */
214384eef32Sdrh   LONGDOUBLE_TYPE realvalue; /* Value for real types */
2155719628aSdrh   const et_info *infop;      /* Pointer to the appropriate info structure */
21659eedf79Sdrh   char *zOut;                /* Rendering buffer */
21759eedf79Sdrh   int nOut;                  /* Size of the rendering buffer */
218ed1fddf4Sdrh   char *zExtra;              /* Malloced memory used by some conversion */
219b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT
220557cc60fSdrh   int  exp, e2;              /* exponent of real numbers */
221ed1fddf4Sdrh   int nsd;                   /* Number of significant digits returned */
222a18c5681Sdrh   double rounder;            /* Used for rounding floating point values */
223e84a306bSdrh   etByte flag_dp;            /* True if decimal point should be shown */
224e84a306bSdrh   etByte flag_rtz;           /* True if trailing zeros should be removed */
225a18c5681Sdrh #endif
226a5c1416dSdrh   PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
227ed1fddf4Sdrh   char buf[etBUFSIZE];       /* Conversion buffer */
228a18c5681Sdrh 
229a18c5681Sdrh   bufpt = 0;
230a5c1416dSdrh   if( bFlags ){
231a5c1416dSdrh     if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
232a5c1416dSdrh       pArgList = va_arg(ap, PrintfArguments*);
233a5c1416dSdrh     }
234a5c1416dSdrh     useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
235a5c1416dSdrh   }else{
236a5c1416dSdrh     bArgList = useIntern = 0;
237a5c1416dSdrh   }
238a18c5681Sdrh   for(; (c=(*fmt))!=0; ++fmt){
239a18c5681Sdrh     if( c!='%' ){
240e84a306bSdrh       int amt;
241a18c5681Sdrh       bufpt = (char *)fmt;
242a18c5681Sdrh       amt = 1;
243a18c5681Sdrh       while( (c=(*++fmt))!='%' && c!=0 ) amt++;
244ade86483Sdrh       sqlite3StrAccumAppend(pAccum, bufpt, amt);
245a18c5681Sdrh       if( c==0 ) break;
246a18c5681Sdrh     }
247a18c5681Sdrh     if( (c=(*++fmt))==0 ){
248ade86483Sdrh       sqlite3StrAccumAppend(pAccum, "%", 1);
249a18c5681Sdrh       break;
250a18c5681Sdrh     }
251a18c5681Sdrh     /* Find out what flags are present */
252a18c5681Sdrh     flag_leftjustify = flag_plussign = flag_blanksign =
253557cc60fSdrh      flag_alternateform = flag_altform2 = flag_zeropad = 0;
2543e9aeec0Sdrh     done = 0;
255a18c5681Sdrh     do{
256a18c5681Sdrh       switch( c ){
2573e9aeec0Sdrh         case '-':   flag_leftjustify = 1;     break;
2583e9aeec0Sdrh         case '+':   flag_plussign = 1;        break;
2593e9aeec0Sdrh         case ' ':   flag_blanksign = 1;       break;
2603e9aeec0Sdrh         case '#':   flag_alternateform = 1;   break;
2613e9aeec0Sdrh         case '!':   flag_altform2 = 1;        break;
2623e9aeec0Sdrh         case '0':   flag_zeropad = 1;         break;
2633e9aeec0Sdrh         default:    done = 1;                 break;
264a18c5681Sdrh       }
2653e9aeec0Sdrh     }while( !done && (c=(*++fmt))!=0 );
266a18c5681Sdrh     /* Get the field width */
267a18c5681Sdrh     width = 0;
268a18c5681Sdrh     if( c=='*' ){
269a5c1416dSdrh       if( bArgList ){
270a5c1416dSdrh         width = (int)getIntArg(pArgList);
271a5c1416dSdrh       }else{
272a18c5681Sdrh         width = va_arg(ap,int);
273a5c1416dSdrh       }
274a18c5681Sdrh       if( width<0 ){
275a18c5681Sdrh         flag_leftjustify = 1;
276a18c5681Sdrh         width = -width;
277a18c5681Sdrh       }
278a18c5681Sdrh       c = *++fmt;
279a18c5681Sdrh     }else{
28017a68934Sdrh       while( c>='0' && c<='9' ){
281a18c5681Sdrh         width = width*10 + c - '0';
282a18c5681Sdrh         c = *++fmt;
283a18c5681Sdrh       }
284a18c5681Sdrh     }
285a18c5681Sdrh     /* Get the precision */
286a18c5681Sdrh     if( c=='.' ){
287a18c5681Sdrh       precision = 0;
288a18c5681Sdrh       c = *++fmt;
289a18c5681Sdrh       if( c=='*' ){
290a5c1416dSdrh         if( bArgList ){
291a5c1416dSdrh           precision = (int)getIntArg(pArgList);
292a5c1416dSdrh         }else{
293a18c5681Sdrh           precision = va_arg(ap,int);
294a5c1416dSdrh         }
295a18c5681Sdrh         if( precision<0 ) precision = -precision;
296a18c5681Sdrh         c = *++fmt;
297a18c5681Sdrh       }else{
29817a68934Sdrh         while( c>='0' && c<='9' ){
299a18c5681Sdrh           precision = precision*10 + c - '0';
300a18c5681Sdrh           c = *++fmt;
301a18c5681Sdrh         }
302a18c5681Sdrh       }
303a18c5681Sdrh     }else{
304a18c5681Sdrh       precision = -1;
305a18c5681Sdrh     }
306a18c5681Sdrh     /* Get the conversion type modifier */
307a18c5681Sdrh     if( c=='l' ){
308a18c5681Sdrh       flag_long = 1;
309a18c5681Sdrh       c = *++fmt;
310a34b6764Sdrh       if( c=='l' ){
311a34b6764Sdrh         flag_longlong = 1;
312a34b6764Sdrh         c = *++fmt;
313a18c5681Sdrh       }else{
314a34b6764Sdrh         flag_longlong = 0;
315a34b6764Sdrh       }
316a34b6764Sdrh     }else{
317a34b6764Sdrh       flag_long = flag_longlong = 0;
318a18c5681Sdrh     }
319a18c5681Sdrh     /* Fetch the info entry for the field */
320874ba04cSdrh     infop = &fmtinfo[0];
321874ba04cSdrh     xtype = etINVALID;
32200e13613Sdanielk1977     for(idx=0; idx<ArraySize(fmtinfo); idx++){
323a18c5681Sdrh       if( c==fmtinfo[idx].fmttype ){
324a18c5681Sdrh         infop = &fmtinfo[idx];
325a5c1416dSdrh         if( useIntern || (infop->flags & FLAG_INTERN)==0 ){
326e84a306bSdrh           xtype = infop->type;
327c4413565Sdrh         }else{
328ade86483Sdrh           return;
3295f968436Sdrh         }
330a18c5681Sdrh         break;
331a18c5681Sdrh       }
332a18c5681Sdrh     }
333a18c5681Sdrh     zExtra = 0;
33443617e9aSdrh 
335a18c5681Sdrh     /*
336a18c5681Sdrh     ** At this point, variables are initialized as follows:
337a18c5681Sdrh     **
338a18c5681Sdrh     **   flag_alternateform          TRUE if a '#' is present.
3393e9aeec0Sdrh     **   flag_altform2               TRUE if a '!' is present.
340a18c5681Sdrh     **   flag_plussign               TRUE if a '+' is present.
341a18c5681Sdrh     **   flag_leftjustify            TRUE if a '-' is present or if the
342a18c5681Sdrh     **                               field width was negative.
343a18c5681Sdrh     **   flag_zeropad                TRUE if the width began with 0.
344a18c5681Sdrh     **   flag_long                   TRUE if the letter 'l' (ell) prefixed
345a18c5681Sdrh     **                               the conversion character.
346a34b6764Sdrh     **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
347a34b6764Sdrh     **                               the conversion character.
348a18c5681Sdrh     **   flag_blanksign              TRUE if a ' ' is present.
349a18c5681Sdrh     **   width                       The specified field width.  This is
350a18c5681Sdrh     **                               always non-negative.  Zero is the default.
351a18c5681Sdrh     **   precision                   The specified precision.  The default
352a18c5681Sdrh     **                               is -1.
353a18c5681Sdrh     **   xtype                       The class of the conversion.
354a18c5681Sdrh     **   infop                       Pointer to the appropriate info struct.
355a18c5681Sdrh     */
356a18c5681Sdrh     switch( xtype ){
357fe63d1c9Sdrh       case etPOINTER:
358fe63d1c9Sdrh         flag_longlong = sizeof(char*)==sizeof(i64);
359fe63d1c9Sdrh         flag_long = sizeof(char*)==sizeof(long int);
360fe63d1c9Sdrh         /* Fall through into the next case */
3619a99334dSdrh       case etORDINAL:
362a18c5681Sdrh       case etRADIX:
363e84a306bSdrh         if( infop->flags & FLAG_SIGNED ){
364e9707671Sdrh           i64 v;
365a5c1416dSdrh           if( bArgList ){
366a5c1416dSdrh             v = getIntArg(pArgList);
367a5c1416dSdrh           }else if( flag_longlong ){
368eeb23a4cSdrh             v = va_arg(ap,i64);
369eeb23a4cSdrh           }else if( flag_long ){
370eeb23a4cSdrh             v = va_arg(ap,long int);
371eeb23a4cSdrh           }else{
372eeb23a4cSdrh             v = va_arg(ap,int);
373eeb23a4cSdrh           }
374e9707671Sdrh           if( v<0 ){
375158b9cb9Sdrh             if( v==SMALLEST_INT64 ){
376158b9cb9Sdrh               longvalue = ((u64)1)<<63;
377158b9cb9Sdrh             }else{
378158b9cb9Sdrh               longvalue = -v;
379158b9cb9Sdrh             }
380cfcdaefeSdanielk1977             prefix = '-';
381cfcdaefeSdanielk1977           }else{
382e9707671Sdrh             longvalue = v;
383e9707671Sdrh             if( flag_plussign )        prefix = '+';
384a18c5681Sdrh             else if( flag_blanksign )  prefix = ' ';
385a18c5681Sdrh             else                       prefix = 0;
386cfcdaefeSdanielk1977           }
387e9707671Sdrh         }else{
388a5c1416dSdrh           if( bArgList ){
389a5c1416dSdrh             longvalue = (u64)getIntArg(pArgList);
390a5c1416dSdrh           }else if( flag_longlong ){
391eeb23a4cSdrh             longvalue = va_arg(ap,u64);
392eeb23a4cSdrh           }else if( flag_long ){
393eeb23a4cSdrh             longvalue = va_arg(ap,unsigned long int);
394eeb23a4cSdrh           }else{
395eeb23a4cSdrh             longvalue = va_arg(ap,unsigned int);
396eeb23a4cSdrh           }
397e9707671Sdrh           prefix = 0;
398e9707671Sdrh         }
399e9707671Sdrh         if( longvalue==0 ) flag_alternateform = 0;
400a18c5681Sdrh         if( flag_zeropad && precision<width-(prefix!=0) ){
401a18c5681Sdrh           precision = width-(prefix!=0);
402a18c5681Sdrh         }
40359eedf79Sdrh         if( precision<etBUFSIZE-10 ){
40459eedf79Sdrh           nOut = etBUFSIZE;
40559eedf79Sdrh           zOut = buf;
40659eedf79Sdrh         }else{
40759eedf79Sdrh           nOut = precision + 10;
40859eedf79Sdrh           zOut = zExtra = sqlite3Malloc( nOut );
40959eedf79Sdrh           if( zOut==0 ){
410a6353a3fSdrh             setStrAccumError(pAccum, STRACCUM_NOMEM);
41159eedf79Sdrh             return;
41259eedf79Sdrh           }
41359eedf79Sdrh         }
41459eedf79Sdrh         bufpt = &zOut[nOut-1];
4159a99334dSdrh         if( xtype==etORDINAL ){
41643f6e064Sdrh           static const char zOrd[] = "thstndrd";
417ea678832Sdrh           int x = (int)(longvalue % 10);
41843f6e064Sdrh           if( x>=4 || (longvalue/10)%10==1 ){
41943f6e064Sdrh             x = 0;
42043f6e064Sdrh           }
42159eedf79Sdrh           *(--bufpt) = zOrd[x*2+1];
42259eedf79Sdrh           *(--bufpt) = zOrd[x*2];
4239a99334dSdrh         }
424a18c5681Sdrh         {
42576ff3a0eSdrh           register const char *cset;      /* Use registers for speed */
426a18c5681Sdrh           register int base;
42776ff3a0eSdrh           cset = &aDigits[infop->charset];
428a18c5681Sdrh           base = infop->base;
429a18c5681Sdrh           do{                                           /* Convert to ascii */
430a18c5681Sdrh             *(--bufpt) = cset[longvalue%base];
431a18c5681Sdrh             longvalue = longvalue/base;
432a18c5681Sdrh           }while( longvalue>0 );
433a18c5681Sdrh         }
43459eedf79Sdrh         length = (int)(&zOut[nOut-1]-bufpt);
435a18c5681Sdrh         for(idx=precision-length; idx>0; idx--){
436a18c5681Sdrh           *(--bufpt) = '0';                             /* Zero pad */
437a18c5681Sdrh         }
438a18c5681Sdrh         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
439a18c5681Sdrh         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
44076ff3a0eSdrh           const char *pre;
44176ff3a0eSdrh           char x;
44276ff3a0eSdrh           pre = &aPrefix[infop->prefix];
44376ff3a0eSdrh           for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
444a18c5681Sdrh         }
44559eedf79Sdrh         length = (int)(&zOut[nOut-1]-bufpt);
446a18c5681Sdrh         break;
447a18c5681Sdrh       case etFLOAT:
448a18c5681Sdrh       case etEXP:
449a18c5681Sdrh       case etGENERIC:
450a5c1416dSdrh         if( bArgList ){
451a5c1416dSdrh           realvalue = getDoubleArg(pArgList);
452a5c1416dSdrh         }else{
453a18c5681Sdrh           realvalue = va_arg(ap,double);
454a5c1416dSdrh         }
45569ef7036Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT
45669ef7036Sdrh         length = 0;
45769ef7036Sdrh #else
458a18c5681Sdrh         if( precision<0 ) precision = 6;         /* Set default precision */
459a18c5681Sdrh         if( realvalue<0.0 ){
460a18c5681Sdrh           realvalue = -realvalue;
461a18c5681Sdrh           prefix = '-';
462a18c5681Sdrh         }else{
463a18c5681Sdrh           if( flag_plussign )          prefix = '+';
464a18c5681Sdrh           else if( flag_blanksign )    prefix = ' ';
465a18c5681Sdrh           else                         prefix = 0;
466a18c5681Sdrh         }
4673e9aeec0Sdrh         if( xtype==etGENERIC && precision>0 ) precision--;
46874161705Sdrh         for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
4693e9aeec0Sdrh         if( xtype==etFLOAT ) realvalue += rounder;
470a18c5681Sdrh         /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
471a18c5681Sdrh         exp = 0;
472ea678832Sdrh         if( sqlite3IsNaN((double)realvalue) ){
47353c14021Sdrh           bufpt = "NaN";
47453c14021Sdrh           length = 3;
47553c14021Sdrh           break;
47653c14021Sdrh         }
477a18c5681Sdrh         if( realvalue>0.0 ){
47872b3fbc7Sdrh           LONGDOUBLE_TYPE scale = 1.0;
4794ef94130Sdrh           while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
48072b3fbc7Sdrh           while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
48172b3fbc7Sdrh           while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
48272b3fbc7Sdrh           while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
48372b3fbc7Sdrh           realvalue /= scale;
484af005fbcSdrh           while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
485af005fbcSdrh           while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
486af005fbcSdrh           if( exp>350 ){
48753c14021Sdrh             if( prefix=='-' ){
48853c14021Sdrh               bufpt = "-Inf";
48953c14021Sdrh             }else if( prefix=='+' ){
49053c14021Sdrh               bufpt = "+Inf";
49153c14021Sdrh             }else{
49253c14021Sdrh               bufpt = "Inf";
49353c14021Sdrh             }
494ea678832Sdrh             length = sqlite3Strlen30(bufpt);
495a18c5681Sdrh             break;
496a18c5681Sdrh           }
497a18c5681Sdrh         }
498a18c5681Sdrh         bufpt = buf;
499a18c5681Sdrh         /*
500a18c5681Sdrh         ** If the field type is etGENERIC, then convert to either etEXP
501a18c5681Sdrh         ** or etFLOAT, as appropriate.
502a18c5681Sdrh         */
503a18c5681Sdrh         if( xtype!=etFLOAT ){
504a18c5681Sdrh           realvalue += rounder;
505a18c5681Sdrh           if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
506a18c5681Sdrh         }
507a18c5681Sdrh         if( xtype==etGENERIC ){
508a18c5681Sdrh           flag_rtz = !flag_alternateform;
509a18c5681Sdrh           if( exp<-4 || exp>precision ){
510a18c5681Sdrh             xtype = etEXP;
511a18c5681Sdrh           }else{
512a18c5681Sdrh             precision = precision - exp;
513a18c5681Sdrh             xtype = etFLOAT;
514a18c5681Sdrh           }
515a18c5681Sdrh         }else{
51672b3fbc7Sdrh           flag_rtz = flag_altform2;
517a18c5681Sdrh         }
518557cc60fSdrh         if( xtype==etEXP ){
519557cc60fSdrh           e2 = 0;
520557cc60fSdrh         }else{
521557cc60fSdrh           e2 = exp;
522557cc60fSdrh         }
523e8c13bf2Sdrh         if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
524e8c13bf2Sdrh           bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
52559eedf79Sdrh           if( bufpt==0 ){
526a6353a3fSdrh             setStrAccumError(pAccum, STRACCUM_NOMEM);
52759eedf79Sdrh             return;
52859eedf79Sdrh           }
52959eedf79Sdrh         }
53059eedf79Sdrh         zOut = bufpt;
53172b3fbc7Sdrh         nsd = 16 + flag_altform2*10;
532ea678832Sdrh         flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
533557cc60fSdrh         /* The sign in front of the number */
534557cc60fSdrh         if( prefix ){
535557cc60fSdrh           *(bufpt++) = prefix;
536557cc60fSdrh         }
537557cc60fSdrh         /* Digits prior to the decimal point */
538557cc60fSdrh         if( e2<0 ){
539557cc60fSdrh           *(bufpt++) = '0';
540557cc60fSdrh         }else{
541557cc60fSdrh           for(; e2>=0; e2--){
542557cc60fSdrh             *(bufpt++) = et_getdigit(&realvalue,&nsd);
543557cc60fSdrh           }
544557cc60fSdrh         }
545557cc60fSdrh         /* The decimal point */
546557cc60fSdrh         if( flag_dp ){
547557cc60fSdrh           *(bufpt++) = '.';
548557cc60fSdrh         }
549557cc60fSdrh         /* "0" digits after the decimal point but before the first
550557cc60fSdrh         ** significant digit of the number */
551af005fbcSdrh         for(e2++; e2<0; precision--, e2++){
552af005fbcSdrh           assert( precision>0 );
553a18c5681Sdrh           *(bufpt++) = '0';
554a18c5681Sdrh         }
555557cc60fSdrh         /* Significant digits after the decimal point */
556557cc60fSdrh         while( (precision--)>0 ){
557557cc60fSdrh           *(bufpt++) = et_getdigit(&realvalue,&nsd);
558a18c5681Sdrh         }
559557cc60fSdrh         /* Remove trailing zeros and the "." if no digits follow the "." */
560557cc60fSdrh         if( flag_rtz && flag_dp ){
5613e9aeec0Sdrh           while( bufpt[-1]=='0' ) *(--bufpt) = 0;
56259eedf79Sdrh           assert( bufpt>zOut );
5633e9aeec0Sdrh           if( bufpt[-1]=='.' ){
564557cc60fSdrh             if( flag_altform2 ){
565557cc60fSdrh               *(bufpt++) = '0';
566557cc60fSdrh             }else{
567557cc60fSdrh               *(--bufpt) = 0;
568a18c5681Sdrh             }
569557cc60fSdrh           }
570557cc60fSdrh         }
571557cc60fSdrh         /* Add the "eNNN" suffix */
57259eedf79Sdrh         if( xtype==etEXP ){
57376ff3a0eSdrh           *(bufpt++) = aDigits[infop->charset];
574557cc60fSdrh           if( exp<0 ){
575557cc60fSdrh             *(bufpt++) = '-'; exp = -exp;
576557cc60fSdrh           }else{
577557cc60fSdrh             *(bufpt++) = '+';
578557cc60fSdrh           }
579a18c5681Sdrh           if( exp>=100 ){
580ea678832Sdrh             *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
581a18c5681Sdrh             exp %= 100;
582a18c5681Sdrh           }
583ea678832Sdrh           *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
584ea678832Sdrh           *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
585a18c5681Sdrh         }
586557cc60fSdrh         *bufpt = 0;
587557cc60fSdrh 
588a18c5681Sdrh         /* The converted number is in buf[] and zero terminated. Output it.
589a18c5681Sdrh         ** Note that the number is in the usual order, not reversed as with
590a18c5681Sdrh         ** integer conversions. */
59159eedf79Sdrh         length = (int)(bufpt-zOut);
59259eedf79Sdrh         bufpt = zOut;
593a18c5681Sdrh 
594a18c5681Sdrh         /* Special case:  Add leading zeros if the flag_zeropad flag is
595a18c5681Sdrh         ** set and we are not left justified */
596a18c5681Sdrh         if( flag_zeropad && !flag_leftjustify && length < width){
597a18c5681Sdrh           int i;
598a18c5681Sdrh           int nPad = width - length;
599a18c5681Sdrh           for(i=width; i>=nPad; i--){
600a18c5681Sdrh             bufpt[i] = bufpt[i-nPad];
601a18c5681Sdrh           }
602a18c5681Sdrh           i = prefix!=0;
603a18c5681Sdrh           while( nPad-- ) bufpt[i++] = '0';
604a18c5681Sdrh           length = width;
605a18c5681Sdrh         }
60669ef7036Sdrh #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
607a18c5681Sdrh         break;
608a18c5681Sdrh       case etSIZE:
609*fc6ee9dfSdrh         if( !bArgList ){
610*fc6ee9dfSdrh           *(va_arg(ap,int*)) = pAccum->nChar;
611*fc6ee9dfSdrh         }
612a18c5681Sdrh         length = width = 0;
613a18c5681Sdrh         break;
614a18c5681Sdrh       case etPERCENT:
615a18c5681Sdrh         buf[0] = '%';
616a18c5681Sdrh         bufpt = buf;
617a18c5681Sdrh         length = 1;
618a18c5681Sdrh         break;
619a18c5681Sdrh       case etCHARX:
620a5c1416dSdrh         if( bArgList ){
621*fc6ee9dfSdrh           bufpt = getTextArg(pArgList);
622*fc6ee9dfSdrh           c = bufpt ? bufpt[0] : 0;
623a5c1416dSdrh         }else{
624ea678832Sdrh           c = va_arg(ap,int);
625a5c1416dSdrh         }
626ea678832Sdrh         buf[0] = (char)c;
627a18c5681Sdrh         if( precision>=0 ){
628ea678832Sdrh           for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
629a18c5681Sdrh           length = precision;
630a18c5681Sdrh         }else{
631a18c5681Sdrh           length =1;
632a18c5681Sdrh         }
633a18c5681Sdrh         bufpt = buf;
634a18c5681Sdrh         break;
635a18c5681Sdrh       case etSTRING:
636d93d8a81Sdrh       case etDYNSTRING:
637a5c1416dSdrh         if( bArgList ){
638a5c1416dSdrh           bufpt = getTextArg(pArgList);
639a5c1416dSdrh         }else{
640cb485882Sdrh           bufpt = va_arg(ap,char*);
641a5c1416dSdrh         }
642d93d8a81Sdrh         if( bufpt==0 ){
643d93d8a81Sdrh           bufpt = "";
644a5c1416dSdrh         }else if( xtype==etDYNSTRING && !bArgList ){
645d93d8a81Sdrh           zExtra = bufpt;
646d93d8a81Sdrh         }
647e509094bSdrh         if( precision>=0 ){
648e509094bSdrh           for(length=0; length<precision && bufpt[length]; length++){}
649e509094bSdrh         }else{
650ea678832Sdrh           length = sqlite3Strlen30(bufpt);
651e509094bSdrh         }
652a18c5681Sdrh         break;
653a18c5681Sdrh       case etSQLESCAPE:
654f3b863edSdanielk1977       case etSQLESCAPE2:
655f3b863edSdanielk1977       case etSQLESCAPE3: {
6568965b50eSdrh         int i, j, k, n, isnull;
6574794f735Sdrh         int needQuote;
658ea678832Sdrh         char ch;
659f3b863edSdanielk1977         char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
660a5c1416dSdrh         char *escarg;
661a5c1416dSdrh 
662a5c1416dSdrh         if( bArgList ){
663a5c1416dSdrh           escarg = getTextArg(pArgList);
664a5c1416dSdrh         }else{
665a5c1416dSdrh           escarg = va_arg(ap,char*);
666a5c1416dSdrh         }
667f0113000Sdanielk1977         isnull = escarg==0;
668f0113000Sdanielk1977         if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
6698965b50eSdrh         k = precision;
67060d4a304Sdan         for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
671f3b863edSdanielk1977           if( ch==q )  n++;
672a18c5681Sdrh         }
6734794f735Sdrh         needQuote = !isnull && xtype==etSQLESCAPE2;
6744794f735Sdrh         n += i + 1 + needQuote*2;
675a18c5681Sdrh         if( n>etBUFSIZE ){
676e5ae5735Sdrh           bufpt = zExtra = sqlite3Malloc( n );
677d164fd34Sdrh           if( bufpt==0 ){
678a6353a3fSdrh             setStrAccumError(pAccum, STRACCUM_NOMEM);
679d164fd34Sdrh             return;
680d164fd34Sdrh           }
681a18c5681Sdrh         }else{
682a18c5681Sdrh           bufpt = buf;
683a18c5681Sdrh         }
6840cfcf3fbSchw         j = 0;
685f3b863edSdanielk1977         if( needQuote ) bufpt[j++] = q;
6868965b50eSdrh         k = i;
6878965b50eSdrh         for(i=0; i<k; i++){
6888965b50eSdrh           bufpt[j++] = ch = escarg[i];
689f3b863edSdanielk1977           if( ch==q ) bufpt[j++] = ch;
690a18c5681Sdrh         }
691f3b863edSdanielk1977         if( needQuote ) bufpt[j++] = q;
692a18c5681Sdrh         bufpt[j] = 0;
693a18c5681Sdrh         length = j;
6948965b50eSdrh         /* The precision in %q and %Q means how many input characters to
6958965b50eSdrh         ** consume, not the length of the output...
6968965b50eSdrh         ** if( precision>=0 && precision<length ) length = precision; */
697a18c5681Sdrh         break;
6985eba8c09Sdrh       }
6995f968436Sdrh       case etTOKEN: {
7005f968436Sdrh         Token *pToken = va_arg(ap, Token*);
701a5c1416dSdrh         assert( bArgList==0 );
702a9ab481fSdrh         if( pToken && pToken->n ){
703ade86483Sdrh           sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
704ad6d9460Sdrh         }
7055f968436Sdrh         length = width = 0;
7065f968436Sdrh         break;
7075f968436Sdrh       }
7085f968436Sdrh       case etSRCLIST: {
7095f968436Sdrh         SrcList *pSrc = va_arg(ap, SrcList*);
7105f968436Sdrh         int k = va_arg(ap, int);
7115f968436Sdrh         struct SrcList_item *pItem = &pSrc->a[k];
712a5c1416dSdrh         assert( bArgList==0 );
7135f968436Sdrh         assert( k>=0 && k<pSrc->nSrc );
71493a960a0Sdrh         if( pItem->zDatabase ){
715a6353a3fSdrh           sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
716ade86483Sdrh           sqlite3StrAccumAppend(pAccum, ".", 1);
7175f968436Sdrh         }
718a6353a3fSdrh         sqlite3StrAccumAppendAll(pAccum, pItem->zName);
7195f968436Sdrh         length = width = 0;
7205f968436Sdrh         break;
7215f968436Sdrh       }
722874ba04cSdrh       default: {
723874ba04cSdrh         assert( xtype==etINVALID );
724874ba04cSdrh         return;
725874ba04cSdrh       }
726a18c5681Sdrh     }/* End switch over the format type */
727a18c5681Sdrh     /*
728a18c5681Sdrh     ** The text of the conversion is pointed to by "bufpt" and is
729a18c5681Sdrh     ** "length" characters long.  The field width is "width".  Do
730a18c5681Sdrh     ** the output.
731a18c5681Sdrh     */
732a18c5681Sdrh     if( !flag_leftjustify ){
733a18c5681Sdrh       register int nspace;
734a18c5681Sdrh       nspace = width-length;
735a18c5681Sdrh       if( nspace>0 ){
7367e02e5e6Sdrh         sqlite3AppendSpace(pAccum, nspace);
737a18c5681Sdrh       }
738a18c5681Sdrh     }
739a18c5681Sdrh     if( length>0 ){
740ade86483Sdrh       sqlite3StrAccumAppend(pAccum, bufpt, length);
741a18c5681Sdrh     }
742a18c5681Sdrh     if( flag_leftjustify ){
743a18c5681Sdrh       register int nspace;
744a18c5681Sdrh       nspace = width-length;
745a18c5681Sdrh       if( nspace>0 ){
7467e02e5e6Sdrh         sqlite3AppendSpace(pAccum, nspace);
747a18c5681Sdrh       }
748a18c5681Sdrh     }
74940f22bedSdrh     if( zExtra ) sqlite3_free(zExtra);
750a18c5681Sdrh   }/* End for loop over the format string */
751a18c5681Sdrh } /* End of function */
752a18c5681Sdrh 
753a18c5681Sdrh /*
754ade86483Sdrh ** Append N bytes of text from z to the StrAccum object.
755a18c5681Sdrh */
756ade86483Sdrh void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
757a9ab481fSdrh   assert( z!=0 );
758a6353a3fSdrh   assert( p->zText!=0 || p->nChar==0 || p->accError );
759a6353a3fSdrh   assert( N>=0 );
760a6353a3fSdrh   assert( p->accError==0 || p->nAlloc==0 );
761a6353a3fSdrh   if( p->nChar+N >= p->nAlloc ){
762a6353a3fSdrh     char *zNew;
763b49bc86aSdrh     if( p->accError ){
764b49bc86aSdrh       testcase(p->accError==STRACCUM_TOOBIG);
765b49bc86aSdrh       testcase(p->accError==STRACCUM_NOMEM);
76617435752Sdrh       return;
767ade86483Sdrh     }
768ade86483Sdrh     if( !p->useMalloc ){
769ade86483Sdrh       N = p->nAlloc - p->nChar - 1;
770a6353a3fSdrh       setStrAccumError(p, STRACCUM_TOOBIG);
771ade86483Sdrh       if( N<=0 ){
772ade86483Sdrh         return;
7735f968436Sdrh       }
774a18c5681Sdrh     }else{
775a9ef7097Sdan       char *zOld = (p->zText==p->zBase ? 0 : p->zText);
77693a960a0Sdrh       i64 szNew = p->nChar;
777b1a6c3c1Sdrh       szNew += N + 1;
778b1a6c3c1Sdrh       if( szNew > p->mxAlloc ){
779ade86483Sdrh         sqlite3StrAccumReset(p);
780a6353a3fSdrh         setStrAccumError(p, STRACCUM_TOOBIG);
781eaad32b1Sdrh         return;
782b1a6c3c1Sdrh       }else{
783ea678832Sdrh         p->nAlloc = (int)szNew;
784a18c5681Sdrh       }
785b975598eSdrh       if( p->useMalloc==1 ){
786a9ef7097Sdan         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
787b975598eSdrh       }else{
788a9ef7097Sdan         zNew = sqlite3_realloc(zOld, p->nAlloc);
789b975598eSdrh       }
79053f733c7Sdrh       if( zNew ){
791b07028f7Sdrh         if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
792ade86483Sdrh         p->zText = zNew;
793ade86483Sdrh       }else{
794ade86483Sdrh         sqlite3StrAccumReset(p);
795a6353a3fSdrh         setStrAccumError(p, STRACCUM_NOMEM);
796ade86483Sdrh         return;
79753f733c7Sdrh       }
7985f968436Sdrh     }
7995f968436Sdrh   }
800b07028f7Sdrh   assert( p->zText );
801ade86483Sdrh   memcpy(&p->zText[p->nChar], z, N);
802ade86483Sdrh   p->nChar += N;
803483750baSdrh }
804483750baSdrh 
805483750baSdrh /*
806a6353a3fSdrh ** Append the complete text of zero-terminated string z[] to the p string.
807a6353a3fSdrh */
808a6353a3fSdrh void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
80953cd9646Smistachkin   sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));
810a6353a3fSdrh }
811a6353a3fSdrh 
812a6353a3fSdrh 
813a6353a3fSdrh /*
814ade86483Sdrh ** Finish off a string by making sure it is zero-terminated.
815ade86483Sdrh ** Return a pointer to the resulting string.  Return a NULL
816ade86483Sdrh ** pointer if any kind of error was encountered.
8175f968436Sdrh */
818ade86483Sdrh char *sqlite3StrAccumFinish(StrAccum *p){
819ade86483Sdrh   if( p->zText ){
820ade86483Sdrh     p->zText[p->nChar] = 0;
821ade86483Sdrh     if( p->useMalloc && p->zText==p->zBase ){
822b975598eSdrh       if( p->useMalloc==1 ){
823633e6d57Sdrh         p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
824b975598eSdrh       }else{
825b975598eSdrh         p->zText = sqlite3_malloc(p->nChar+1);
826b975598eSdrh       }
827ade86483Sdrh       if( p->zText ){
828ade86483Sdrh         memcpy(p->zText, p->zBase, p->nChar+1);
829ade86483Sdrh       }else{
830a6353a3fSdrh         setStrAccumError(p, STRACCUM_NOMEM);
831ade86483Sdrh       }
832ade86483Sdrh     }
833ade86483Sdrh   }
834ade86483Sdrh   return p->zText;
835ade86483Sdrh }
836ade86483Sdrh 
837ade86483Sdrh /*
838ade86483Sdrh ** Reset an StrAccum string.  Reclaim all malloced memory.
839ade86483Sdrh */
840ade86483Sdrh void sqlite3StrAccumReset(StrAccum *p){
841ade86483Sdrh   if( p->zText!=p->zBase ){
842b975598eSdrh     if( p->useMalloc==1 ){
843633e6d57Sdrh       sqlite3DbFree(p->db, p->zText);
844b975598eSdrh     }else{
845b975598eSdrh       sqlite3_free(p->zText);
846b975598eSdrh     }
847ade86483Sdrh   }
848f089aa45Sdrh   p->zText = 0;
849ade86483Sdrh }
850ade86483Sdrh 
851ade86483Sdrh /*
852ade86483Sdrh ** Initialize a string accumulator
853ade86483Sdrh */
854f089aa45Sdrh void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
855ade86483Sdrh   p->zText = p->zBase = zBase;
856633e6d57Sdrh   p->db = 0;
857ade86483Sdrh   p->nChar = 0;
858ade86483Sdrh   p->nAlloc = n;
859bb4957f8Sdrh   p->mxAlloc = mx;
860ade86483Sdrh   p->useMalloc = 1;
861b49bc86aSdrh   p->accError = 0;
8625f968436Sdrh }
8635f968436Sdrh 
8645f968436Sdrh /*
8655f968436Sdrh ** Print into memory obtained from sqliteMalloc().  Use the internal
8665f968436Sdrh ** %-conversion extensions.
8675f968436Sdrh */
86817435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
86917435752Sdrh   char *z;
87079158e18Sdrh   char zBase[SQLITE_PRINT_BUF_SIZE];
871ade86483Sdrh   StrAccum acc;
872bc6160b0Sdrh   assert( db!=0 );
873bb4957f8Sdrh   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
874bc6160b0Sdrh                       db->aLimit[SQLITE_LIMIT_LENGTH]);
875633e6d57Sdrh   acc.db = db;
876a5c1416dSdrh   sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap);
877ade86483Sdrh   z = sqlite3StrAccumFinish(&acc);
878b49bc86aSdrh   if( acc.accError==STRACCUM_NOMEM ){
87917435752Sdrh     db->mallocFailed = 1;
88017435752Sdrh   }
88117435752Sdrh   return z;
8825f968436Sdrh }
8835f968436Sdrh 
8845f968436Sdrh /*
8855f968436Sdrh ** Print into memory obtained from sqliteMalloc().  Use the internal
8865f968436Sdrh ** %-conversion extensions.
8875f968436Sdrh */
88817435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
8895f968436Sdrh   va_list ap;
8905f968436Sdrh   char *z;
8915f968436Sdrh   va_start(ap, zFormat);
892ade86483Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
8935f968436Sdrh   va_end(ap);
8945f968436Sdrh   return z;
8955f968436Sdrh }
8965f968436Sdrh 
8975f968436Sdrh /*
898633e6d57Sdrh ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
899633e6d57Sdrh ** the string and before returnning.  This routine is intended to be used
900633e6d57Sdrh ** to modify an existing string.  For example:
901633e6d57Sdrh **
902633e6d57Sdrh **       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
903633e6d57Sdrh **
904633e6d57Sdrh */
905633e6d57Sdrh char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
906633e6d57Sdrh   va_list ap;
907633e6d57Sdrh   char *z;
908633e6d57Sdrh   va_start(ap, zFormat);
909633e6d57Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
910633e6d57Sdrh   va_end(ap);
911633e6d57Sdrh   sqlite3DbFree(db, zStr);
912633e6d57Sdrh   return z;
913633e6d57Sdrh }
914633e6d57Sdrh 
915633e6d57Sdrh /*
91628dd479cSdrh ** Print into memory obtained from sqlite3_malloc().  Omit the internal
91728dd479cSdrh ** %-conversion extensions.
91828dd479cSdrh */
91928dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){
920ade86483Sdrh   char *z;
92128dd479cSdrh   char zBase[SQLITE_PRINT_BUF_SIZE];
922ade86483Sdrh   StrAccum acc;
923ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT
924ff1590eeSdrh   if( sqlite3_initialize() ) return 0;
925ff1590eeSdrh #endif
926bb4957f8Sdrh   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
927b975598eSdrh   acc.useMalloc = 2;
928f089aa45Sdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
929ade86483Sdrh   z = sqlite3StrAccumFinish(&acc);
930ade86483Sdrh   return z;
93128dd479cSdrh }
93228dd479cSdrh 
93328dd479cSdrh /*
93428dd479cSdrh ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
93528dd479cSdrh ** %-conversion extensions.
936483750baSdrh */
9376f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){
938a18c5681Sdrh   va_list ap;
9395f968436Sdrh   char *z;
940ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT
941ff1590eeSdrh   if( sqlite3_initialize() ) return 0;
942ff1590eeSdrh #endif
943a18c5681Sdrh   va_start(ap, zFormat);
944b3738b6cSdrh   z = sqlite3_vmprintf(zFormat, ap);
945a18c5681Sdrh   va_end(ap);
9465f968436Sdrh   return z;
947a18c5681Sdrh }
948a18c5681Sdrh 
949a18c5681Sdrh /*
9506f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the
95193a5c6bdSdrh ** current locale settings.  This is important for SQLite because we
95293a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as
95393a5c6bdSdrh ** specified by some locales.
954db26d4c9Sdrh **
955db26d4c9Sdrh ** Oops:  The first two arguments of sqlite3_snprintf() are backwards
956db26d4c9Sdrh ** from the snprintf() standard.  Unfortunately, it is too late to change
957db26d4c9Sdrh ** this without breaking compatibility, so we just have to live with the
958db26d4c9Sdrh ** mistake.
959db26d4c9Sdrh **
960db26d4c9Sdrh ** sqlite3_vsnprintf() is the varargs version.
96193a5c6bdSdrh */
962db26d4c9Sdrh char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
963db26d4c9Sdrh   StrAccum acc;
964db26d4c9Sdrh   if( n<=0 ) return zBuf;
965db26d4c9Sdrh   sqlite3StrAccumInit(&acc, zBuf, n, 0);
966db26d4c9Sdrh   acc.useMalloc = 0;
967db26d4c9Sdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
968db26d4c9Sdrh   return sqlite3StrAccumFinish(&acc);
969db26d4c9Sdrh }
9706f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
9715f968436Sdrh   char *z;
97293a5c6bdSdrh   va_list ap;
97393a5c6bdSdrh   va_start(ap,zFormat);
974db26d4c9Sdrh   z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
97593a5c6bdSdrh   va_end(ap);
9765f968436Sdrh   return z;
97793a5c6bdSdrh }
97893a5c6bdSdrh 
9793f280701Sdrh /*
9807c0c460fSdrh ** This is the routine that actually formats the sqlite3_log() message.
9817c0c460fSdrh ** We house it in a separate routine from sqlite3_log() to avoid using
9827c0c460fSdrh ** stack space on small-stack systems when logging is disabled.
9837c0c460fSdrh **
9847c0c460fSdrh ** sqlite3_log() must render into a static buffer.  It cannot dynamically
9857c0c460fSdrh ** allocate memory because it might be called while the memory allocator
9867c0c460fSdrh ** mutex is held.
9877c0c460fSdrh */
9887c0c460fSdrh static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
9897c0c460fSdrh   StrAccum acc;                          /* String accumulator */
990a64fa912Sdrh   char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
9917c0c460fSdrh 
9927c0c460fSdrh   sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
9937c0c460fSdrh   acc.useMalloc = 0;
9947c0c460fSdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
9957c0c460fSdrh   sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
9967c0c460fSdrh                            sqlite3StrAccumFinish(&acc));
9977c0c460fSdrh }
9987c0c460fSdrh 
9997c0c460fSdrh /*
10003f280701Sdrh ** Format and write a message to the log if logging is enabled.
10013f280701Sdrh */
1002a7564663Sdrh void sqlite3_log(int iErrCode, const char *zFormat, ...){
10033f280701Sdrh   va_list ap;                             /* Vararg list */
10047c0c460fSdrh   if( sqlite3GlobalConfig.xLog ){
10053f280701Sdrh     va_start(ap, zFormat);
10067c0c460fSdrh     renderLogMsg(iErrCode, zFormat, ap);
10073f280701Sdrh     va_end(ap);
10083f280701Sdrh   }
10093f280701Sdrh }
10103f280701Sdrh 
101185e9e22bSdrh #if defined(SQLITE_DEBUG)
1012e54ca3feSdrh /*
1013e54ca3feSdrh ** A version of printf() that understands %lld.  Used for debugging.
1014e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld
1015e54ca3feSdrh ** and segfaults if you give it a long long int.
1016e54ca3feSdrh */
1017e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){
1018e54ca3feSdrh   va_list ap;
1019ade86483Sdrh   StrAccum acc;
1020e54ca3feSdrh   char zBuf[500];
1021bb4957f8Sdrh   sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
1022ade86483Sdrh   acc.useMalloc = 0;
1023e54ca3feSdrh   va_start(ap,zFormat);
1024f089aa45Sdrh   sqlite3VXPrintf(&acc, 0, zFormat, ap);
1025e54ca3feSdrh   va_end(ap);
1026bed8e7e5Sdrh   sqlite3StrAccumFinish(&acc);
1027485f0039Sdrh   fprintf(stdout,"%s", zBuf);
10282ac3ee97Sdrh   fflush(stdout);
1029e54ca3feSdrh }
1030e54ca3feSdrh #endif
1031c7bc4fdeSdrh 
1032c7bc4fdeSdrh /*
1033c7bc4fdeSdrh ** variable-argument wrapper around sqlite3VXPrintf().
1034c7bc4fdeSdrh */
1035a5c1416dSdrh void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
1036c7bc4fdeSdrh   va_list ap;
1037c7bc4fdeSdrh   va_start(ap,zFormat);
1038a5c1416dSdrh   sqlite3VXPrintf(p, bFlags, zFormat, ap);
1039c7bc4fdeSdrh   va_end(ap);
1040c7bc4fdeSdrh }
1041