drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** The "printf" code that follows dates from the 1980's. It is in |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 3 | ** the public domain. |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 4 | ** |
| 5 | ************************************************************************** |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 6 | ** |
drh | ed1fddf | 2011-10-12 18:52:59 +0000 | [diff] [blame] | 7 | ** This file contains code for a set of "printf"-like routines. These |
| 8 | ** routines format strings much like the printf() from the standard C |
| 9 | ** library, though the implementation here has enhancements to support |
mistachkin | f5b5f9b | 2015-06-08 17:42:57 +0000 | [diff] [blame] | 10 | ** SQLite. |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 11 | */ |
| 12 | #include "sqliteInt.h" |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 13 | |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 14 | /* |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 15 | ** Conversion types fall into various categories as defined by the |
| 16 | ** following enumeration. |
| 17 | */ |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 18 | #define etRADIX 0 /* non-decimal integer types. %x %o */ |
drh | ad5a9d7 | 2016-05-05 11:53:12 +0000 | [diff] [blame] | 19 | #define etFLOAT 1 /* Floating point. %f */ |
| 20 | #define etEXP 2 /* Exponentional notation. %e and %E */ |
| 21 | #define etGENERIC 3 /* Floating or exponential, depending on exponent. %g */ |
| 22 | #define etSIZE 4 /* Return number of characters processed so far. %n */ |
| 23 | #define etSTRING 5 /* Strings. %s */ |
| 24 | #define etDYNSTRING 6 /* Dynamically allocated strings. %z */ |
| 25 | #define etPERCENT 7 /* Percent symbol. %% */ |
| 26 | #define etCHARX 8 /* Characters. %c */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 27 | /* The rest are extensions, not normally found in printf() */ |
drh | ad5a9d7 | 2016-05-05 11:53:12 +0000 | [diff] [blame] | 28 | #define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */ |
| 29 | #define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '', |
chw | 0cfcf3f | 2002-06-16 04:55:48 +0000 | [diff] [blame] | 30 | NULL pointers replaced by SQL NULL. %Q */ |
drh | ad5a9d7 | 2016-05-05 11:53:12 +0000 | [diff] [blame] | 31 | #define etTOKEN 11 /* a pointer to a Token structure */ |
| 32 | #define etSRCLIST 12 /* a pointer to a SrcList */ |
| 33 | #define etPOINTER 13 /* The %p conversion */ |
| 34 | #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */ |
| 35 | #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 36 | #define etDECIMAL 16 /* %d or %u, but not %x, %o */ |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 37 | |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 38 | #define etINVALID 17 /* Any unrecognized conversion type */ |
drh | 874ba04 | 2009-04-08 16:10:04 +0000 | [diff] [blame] | 39 | |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | ** An "etByte" is an 8-bit unsigned value. |
| 43 | */ |
| 44 | typedef unsigned char etByte; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | ** Each builtin conversion character (ex: the 'd' in "%d") is described |
| 48 | ** by an instance of the following structure |
| 49 | */ |
| 50 | typedef struct et_info { /* Information about each format field */ |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 51 | char fmttype; /* The format field code letter */ |
| 52 | etByte base; /* The base for radix conversion */ |
| 53 | etByte flags; /* One or more of FLAG_ constants below */ |
| 54 | etByte type; /* Conversion paradigm */ |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 55 | etByte charset; /* Offset into aDigits[] of the digits string */ |
| 56 | etByte prefix; /* Offset into aPrefix[] of the prefix string */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 57 | } et_info; |
| 58 | |
| 59 | /* |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 60 | ** Allowed values for et_info.flags |
| 61 | */ |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 62 | #define FLAG_SIGNED 1 /* True if the value to convert is signed */ |
| 63 | #define FLAG_STRING 4 /* Allow infinite precision */ |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | /* |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 67 | ** The following table is searched linearly, so it is good to put the |
| 68 | ** most frequently used conversion types first. |
| 69 | */ |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 70 | static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; |
| 71 | static const char aPrefix[] = "-x0\000X0"; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 72 | static const et_info fmtinfo[] = { |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 73 | { 'd', 10, 1, etDECIMAL, 0, 0 }, |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 74 | { 's', 0, 4, etSTRING, 0, 0 }, |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 75 | { 'g', 0, 1, etGENERIC, 30, 0 }, |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 76 | { 'z', 0, 4, etDYNSTRING, 0, 0 }, |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 77 | { 'q', 0, 4, etSQLESCAPE, 0, 0 }, |
| 78 | { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, |
danielk1977 | f3b863e | 2007-06-24 06:32:17 +0000 | [diff] [blame] | 79 | { 'w', 0, 4, etSQLESCAPE3, 0, 0 }, |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 80 | { 'c', 0, 0, etCHARX, 0, 0 }, |
| 81 | { 'o', 8, 0, etRADIX, 0, 2 }, |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 82 | { 'u', 10, 0, etDECIMAL, 0, 0 }, |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 83 | { 'x', 16, 0, etRADIX, 16, 1 }, |
| 84 | { 'X', 16, 0, etRADIX, 0, 4 }, |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame] | 85 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 86 | { 'f', 0, 1, etFLOAT, 0, 0 }, |
| 87 | { 'e', 0, 1, etEXP, 30, 0 }, |
| 88 | { 'E', 0, 1, etEXP, 14, 0 }, |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 89 | { 'G', 0, 1, etGENERIC, 14, 0 }, |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame] | 90 | #endif |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 91 | { 'i', 10, 1, etDECIMAL, 0, 0 }, |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 92 | { 'n', 0, 0, etSIZE, 0, 0 }, |
| 93 | { '%', 0, 0, etPERCENT, 0, 0 }, |
| 94 | { 'p', 16, 0, etPOINTER, 0, 1 }, |
drh | 7e3ff5d | 2009-04-08 11:49:42 +0000 | [diff] [blame] | 95 | |
drh | 8236f68 | 2017-01-04 00:26:28 +0000 | [diff] [blame] | 96 | /* All the rest are undocumented and are for internal use only */ |
| 97 | { 'T', 0, 0, etTOKEN, 0, 0 }, |
| 98 | { 'S', 0, 0, etSRCLIST, 0, 0 }, |
| 99 | { 'r', 10, 1, etORDINAL, 0, 0 }, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 100 | }; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 101 | |
drh | a0ed86b | 2019-05-24 22:58:16 +0000 | [diff] [blame] | 102 | /* Floating point constants used for rounding */ |
| 103 | static const double arRound[] = { |
| 104 | 5.0e-01, 5.0e-02, 5.0e-03, 5.0e-04, 5.0e-05, |
| 105 | 5.0e-06, 5.0e-07, 5.0e-08, 5.0e-09, 5.0e-10, |
| 106 | }; |
| 107 | |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 108 | /* |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame] | 109 | ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 110 | ** conversions will work. |
| 111 | */ |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame] | 112 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 113 | /* |
| 114 | ** "*val" is a double such that 0.1 <= *val < 10.0 |
| 115 | ** Return the ascii code for the leading digit of *val, then |
| 116 | ** multiply "*val" by 10.0 to renormalize. |
| 117 | ** |
| 118 | ** Example: |
| 119 | ** input: *val = 3.14159 |
| 120 | ** output: *val = 1.4159 function return = '3' |
| 121 | ** |
| 122 | ** The counter *cnt is incremented each time. After counter exceeds |
| 123 | ** 16 (the number of significant digits in a 64-bit float) '0' is |
| 124 | ** always returned. |
| 125 | */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 126 | static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 127 | int digit; |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 128 | LONGDOUBLE_TYPE d; |
drh | 72b3fbc | 2012-06-19 03:11:25 +0000 | [diff] [blame] | 129 | if( (*cnt)<=0 ) return '0'; |
| 130 | (*cnt)--; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 131 | digit = (int)*val; |
| 132 | d = digit; |
| 133 | digit += '0'; |
| 134 | *val = (*val - d)*10.0; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 135 | return (char)digit; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 136 | } |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame] | 137 | #endif /* SQLITE_OMIT_FLOATING_POINT */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 138 | |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 139 | /* |
drh | a6353a3 | 2013-12-09 19:03:26 +0000 | [diff] [blame] | 140 | ** Set the StrAccum object to an error mode. |
| 141 | */ |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 142 | static void setStrAccumError(StrAccum *p, u8 eError){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 143 | assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG ); |
drh | a6353a3 | 2013-12-09 19:03:26 +0000 | [diff] [blame] | 144 | p->accError = eError; |
drh | 255a81f | 2019-02-22 15:42:10 +0000 | [diff] [blame] | 145 | if( p->mxAlloc ) sqlite3_str_reset(p); |
drh | c3dcdba | 2019-04-09 21:32:46 +0000 | [diff] [blame] | 146 | if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError); |
drh | a6353a3 | 2013-12-09 19:03:26 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 150 | ** Extra argument values from a PrintfArguments object |
| 151 | */ |
| 152 | static sqlite3_int64 getIntArg(PrintfArguments *p){ |
| 153 | if( p->nArg<=p->nUsed ) return 0; |
| 154 | return sqlite3_value_int64(p->apArg[p->nUsed++]); |
| 155 | } |
| 156 | static double getDoubleArg(PrintfArguments *p){ |
| 157 | if( p->nArg<=p->nUsed ) return 0.0; |
| 158 | return sqlite3_value_double(p->apArg[p->nUsed++]); |
| 159 | } |
| 160 | static char *getTextArg(PrintfArguments *p){ |
| 161 | if( p->nArg<=p->nUsed ) return 0; |
| 162 | return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); |
| 163 | } |
| 164 | |
drh | 2964225 | 2019-02-01 20:29:04 +0000 | [diff] [blame] | 165 | /* |
| 166 | ** Allocate memory for a temporary buffer needed for printf rendering. |
| 167 | ** |
| 168 | ** If the requested size of the temp buffer is larger than the size |
| 169 | ** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error. |
| 170 | ** Do the size check before the memory allocation to prevent rogue |
| 171 | ** SQL from requesting large allocations using the precision or width |
| 172 | ** field of the printf() function. |
| 173 | */ |
| 174 | static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){ |
| 175 | char *z; |
drh | 255a81f | 2019-02-22 15:42:10 +0000 | [diff] [blame] | 176 | if( pAccum->accError ) return 0; |
drh | 2964225 | 2019-02-01 20:29:04 +0000 | [diff] [blame] | 177 | if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){ |
| 178 | setStrAccumError(pAccum, SQLITE_TOOBIG); |
| 179 | return 0; |
| 180 | } |
| 181 | z = sqlite3DbMallocRaw(pAccum->db, n); |
| 182 | if( z==0 ){ |
| 183 | setStrAccumError(pAccum, SQLITE_NOMEM); |
| 184 | } |
| 185 | return z; |
| 186 | } |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 187 | |
| 188 | /* |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 189 | ** On machines with a small stack size, you can redefine the |
drh | ed1fddf | 2011-10-12 18:52:59 +0000 | [diff] [blame] | 190 | ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired. |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 191 | */ |
| 192 | #ifndef SQLITE_PRINT_BUF_SIZE |
drh | 59eedf7 | 2011-10-11 17:54:54 +0000 | [diff] [blame] | 193 | # define SQLITE_PRINT_BUF_SIZE 70 |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 194 | #endif |
| 195 | #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 196 | |
| 197 | /* |
drh | ed1fddf | 2011-10-12 18:52:59 +0000 | [diff] [blame] | 198 | ** Render a string given by "fmt" into the StrAccum object. |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 199 | */ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 200 | void sqlite3_str_vappendf( |
| 201 | sqlite3_str *pAccum, /* Accumulate results here */ |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 202 | const char *fmt, /* Format string */ |
| 203 | va_list ap /* arguments */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 204 | ){ |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 205 | int c; /* Next character in the format string */ |
| 206 | char *bufpt; /* Pointer to the conversion buffer */ |
| 207 | int precision; /* Precision of the current field */ |
| 208 | int length; /* Length of the field */ |
| 209 | int idx; /* A general purpose loop counter */ |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 210 | int width; /* Width of the current field */ |
| 211 | etByte flag_leftjustify; /* True if "-" flag is present */ |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 212 | etByte flag_prefix; /* '+' or ' ' or 0 for prefix */ |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 213 | etByte flag_alternateform; /* True if "#" flag is present */ |
drh | 531fe87 | 2005-08-13 13:40:42 +0000 | [diff] [blame] | 214 | etByte flag_altform2; /* True if "!" flag is present */ |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 215 | etByte flag_zeropad; /* True if field width constant starts with zero */ |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 216 | etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */ |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 217 | etByte done; /* Loop termination flag */ |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 218 | etByte cThousand; /* Thousands separator for %d and %u */ |
drh | ad5a9d7 | 2016-05-05 11:53:12 +0000 | [diff] [blame] | 219 | etByte xtype = etINVALID; /* Conversion paradigm */ |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 220 | u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ |
drh | ed1fddf | 2011-10-12 18:52:59 +0000 | [diff] [blame] | 221 | char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ |
drh | 27436af | 2006-03-28 23:57:17 +0000 | [diff] [blame] | 222 | sqlite_uint64 longvalue; /* Value for integer types */ |
drh | 384eef3 | 2004-01-07 03:04:27 +0000 | [diff] [blame] | 223 | LONGDOUBLE_TYPE realvalue; /* Value for real types */ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 224 | const et_info *infop; /* Pointer to the appropriate info structure */ |
drh | 59eedf7 | 2011-10-11 17:54:54 +0000 | [diff] [blame] | 225 | char *zOut; /* Rendering buffer */ |
| 226 | int nOut; /* Size of the rendering buffer */ |
drh | af8f513 | 2014-10-29 18:20:18 +0000 | [diff] [blame] | 227 | char *zExtra = 0; /* Malloced memory used by some conversion */ |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame] | 228 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 229 | int exp, e2; /* exponent of real numbers */ |
drh | ed1fddf | 2011-10-12 18:52:59 +0000 | [diff] [blame] | 230 | int nsd; /* Number of significant digits returned */ |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 231 | double rounder; /* Used for rounding floating point values */ |
| 232 | etByte flag_dp; /* True if decimal point should be shown */ |
| 233 | etByte flag_rtz; /* True if trailing zeros should be removed */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 234 | #endif |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 235 | PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ |
drh | ed1fddf | 2011-10-12 18:52:59 +0000 | [diff] [blame] | 236 | char buf[etBUFSIZE]; /* Conversion buffer */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 237 | |
drh | cc39896 | 2018-02-20 15:23:37 +0000 | [diff] [blame] | 238 | /* pAccum never starts out with an empty buffer that was obtained from |
| 239 | ** malloc(). This precondition is required by the mprintf("%z...") |
| 240 | ** optimization. */ |
| 241 | assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); |
| 242 | |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 243 | bufpt = 0; |
drh | 8236f68 | 2017-01-04 00:26:28 +0000 | [diff] [blame] | 244 | if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){ |
| 245 | pArgList = va_arg(ap, PrintfArguments*); |
| 246 | bArgList = 1; |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 247 | }else{ |
drh | 8236f68 | 2017-01-04 00:26:28 +0000 | [diff] [blame] | 248 | bArgList = 0; |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 249 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 250 | for(; (c=(*fmt))!=0; ++fmt){ |
| 251 | if( c!='%' ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 252 | bufpt = (char *)fmt; |
drh | 760b159 | 2014-09-18 01:50:09 +0000 | [diff] [blame] | 253 | #if HAVE_STRCHRNUL |
| 254 | fmt = strchrnul(fmt, '%'); |
| 255 | #else |
| 256 | do{ fmt++; }while( *fmt && *fmt != '%' ); |
| 257 | #endif |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 258 | sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt)); |
drh | 760b159 | 2014-09-18 01:50:09 +0000 | [diff] [blame] | 259 | if( *fmt==0 ) break; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 260 | } |
| 261 | if( (c=(*++fmt))==0 ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 262 | sqlite3_str_append(pAccum, "%", 1); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | /* Find out what flags are present */ |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 266 | flag_leftjustify = flag_prefix = cThousand = |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 267 | flag_alternateform = flag_altform2 = flag_zeropad = 0; |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 268 | done = 0; |
drh | 9a6d01b | 2019-02-01 18:46:41 +0000 | [diff] [blame] | 269 | width = 0; |
| 270 | flag_long = 0; |
| 271 | precision = -1; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 272 | do{ |
| 273 | switch( c ){ |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 274 | case '-': flag_leftjustify = 1; break; |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 275 | case '+': flag_prefix = '+'; break; |
| 276 | case ' ': flag_prefix = ' '; break; |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 277 | case '#': flag_alternateform = 1; break; |
| 278 | case '!': flag_altform2 = 1; break; |
| 279 | case '0': flag_zeropad = 1; break; |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 280 | case ',': cThousand = ','; break; |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 281 | default: done = 1; break; |
drh | 9a6d01b | 2019-02-01 18:46:41 +0000 | [diff] [blame] | 282 | case 'l': { |
| 283 | flag_long = 1; |
| 284 | c = *++fmt; |
| 285 | if( c=='l' ){ |
| 286 | c = *++fmt; |
| 287 | flag_long = 2; |
| 288 | } |
| 289 | done = 1; |
| 290 | break; |
| 291 | } |
| 292 | case '1': case '2': case '3': case '4': case '5': |
| 293 | case '6': case '7': case '8': case '9': { |
| 294 | unsigned wx = c - '0'; |
| 295 | while( (c = *++fmt)>='0' && c<='9' ){ |
| 296 | wx = wx*10 + c - '0'; |
| 297 | } |
| 298 | testcase( wx>0x7fffffff ); |
| 299 | width = wx & 0x7fffffff; |
| 300 | #ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 301 | if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ |
| 302 | width = SQLITE_PRINTF_PRECISION_LIMIT; |
| 303 | } |
| 304 | #endif |
| 305 | if( c!='.' && c!='l' ){ |
| 306 | done = 1; |
| 307 | }else{ |
| 308 | fmt--; |
| 309 | } |
| 310 | break; |
| 311 | } |
| 312 | case '*': { |
| 313 | if( bArgList ){ |
| 314 | width = (int)getIntArg(pArgList); |
| 315 | }else{ |
| 316 | width = va_arg(ap,int); |
| 317 | } |
| 318 | if( width<0 ){ |
| 319 | flag_leftjustify = 1; |
| 320 | width = width >= -2147483647 ? -width : 0; |
| 321 | } |
| 322 | #ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 323 | if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ |
| 324 | width = SQLITE_PRINTF_PRECISION_LIMIT; |
| 325 | } |
| 326 | #endif |
| 327 | if( (c = fmt[1])!='.' && c!='l' ){ |
| 328 | c = *++fmt; |
| 329 | done = 1; |
| 330 | } |
| 331 | break; |
| 332 | } |
| 333 | case '.': { |
| 334 | c = *++fmt; |
| 335 | if( c=='*' ){ |
| 336 | if( bArgList ){ |
| 337 | precision = (int)getIntArg(pArgList); |
| 338 | }else{ |
| 339 | precision = va_arg(ap,int); |
| 340 | } |
| 341 | if( precision<0 ){ |
| 342 | precision = precision >= -2147483647 ? -precision : -1; |
| 343 | } |
| 344 | c = *++fmt; |
| 345 | }else{ |
| 346 | unsigned px = 0; |
| 347 | while( c>='0' && c<='9' ){ |
| 348 | px = px*10 + c - '0'; |
| 349 | c = *++fmt; |
| 350 | } |
| 351 | testcase( px>0x7fffffff ); |
| 352 | precision = px & 0x7fffffff; |
| 353 | } |
| 354 | #ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 355 | if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){ |
| 356 | precision = SQLITE_PRINTF_PRECISION_LIMIT; |
| 357 | } |
| 358 | #endif |
| 359 | if( c=='l' ){ |
| 360 | --fmt; |
| 361 | }else{ |
| 362 | done = 1; |
| 363 | } |
| 364 | break; |
| 365 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 366 | } |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 367 | }while( !done && (c=(*++fmt))!=0 ); |
dan | 8c06914 | 2015-04-07 14:38:57 +0000 | [diff] [blame] | 368 | |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 369 | /* Fetch the info entry for the field */ |
drh | 874ba04 | 2009-04-08 16:10:04 +0000 | [diff] [blame] | 370 | infop = &fmtinfo[0]; |
| 371 | xtype = etINVALID; |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 372 | for(idx=0; idx<ArraySize(fmtinfo); idx++){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 373 | if( c==fmtinfo[idx].fmttype ){ |
| 374 | infop = &fmtinfo[idx]; |
drh | 8236f68 | 2017-01-04 00:26:28 +0000 | [diff] [blame] | 375 | xtype = infop->type; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 376 | break; |
| 377 | } |
| 378 | } |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 379 | |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 380 | /* |
| 381 | ** At this point, variables are initialized as follows: |
| 382 | ** |
| 383 | ** flag_alternateform TRUE if a '#' is present. |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 384 | ** flag_altform2 TRUE if a '!' is present. |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 385 | ** flag_prefix '+' or ' ' or zero |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 386 | ** flag_leftjustify TRUE if a '-' is present or if the |
| 387 | ** field width was negative. |
| 388 | ** flag_zeropad TRUE if the width began with 0. |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 389 | ** flag_long 1 for "l", 2 for "ll" |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 390 | ** width The specified field width. This is |
| 391 | ** always non-negative. Zero is the default. |
| 392 | ** precision The specified precision. The default |
| 393 | ** is -1. |
| 394 | ** xtype The class of the conversion. |
| 395 | ** infop Pointer to the appropriate info struct. |
| 396 | */ |
| 397 | switch( xtype ){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 398 | case etPOINTER: |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 399 | flag_long = sizeof(char*)==sizeof(i64) ? 2 : |
| 400 | sizeof(char*)==sizeof(long int) ? 1 : 0; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 401 | /* Fall through into the next case */ |
drh | 9a99334 | 2007-12-13 02:45:31 +0000 | [diff] [blame] | 402 | case etORDINAL: |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 403 | case etRADIX: |
| 404 | cThousand = 0; |
| 405 | /* Fall through into the next case */ |
| 406 | case etDECIMAL: |
drh | e84a306 | 2004-02-02 12:29:25 +0000 | [diff] [blame] | 407 | if( infop->flags & FLAG_SIGNED ){ |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 408 | i64 v; |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 409 | if( bArgList ){ |
| 410 | v = getIntArg(pArgList); |
drh | eeb23a4 | 2009-05-04 20:20:16 +0000 | [diff] [blame] | 411 | }else if( flag_long ){ |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 412 | if( flag_long==2 ){ |
| 413 | v = va_arg(ap,i64) ; |
| 414 | }else{ |
| 415 | v = va_arg(ap,long int); |
| 416 | } |
drh | eeb23a4 | 2009-05-04 20:20:16 +0000 | [diff] [blame] | 417 | }else{ |
| 418 | v = va_arg(ap,int); |
| 419 | } |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 420 | if( v<0 ){ |
drh | 158b9cb | 2011-03-05 20:59:46 +0000 | [diff] [blame] | 421 | if( v==SMALLEST_INT64 ){ |
| 422 | longvalue = ((u64)1)<<63; |
| 423 | }else{ |
| 424 | longvalue = -v; |
| 425 | } |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 426 | prefix = '-'; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 427 | }else{ |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 428 | longvalue = v; |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 429 | prefix = flag_prefix; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 +0000 | [diff] [blame] | 430 | } |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 431 | }else{ |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 432 | if( bArgList ){ |
| 433 | longvalue = (u64)getIntArg(pArgList); |
drh | eeb23a4 | 2009-05-04 20:20:16 +0000 | [diff] [blame] | 434 | }else if( flag_long ){ |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 435 | if( flag_long==2 ){ |
| 436 | longvalue = va_arg(ap,u64); |
| 437 | }else{ |
| 438 | longvalue = va_arg(ap,unsigned long int); |
| 439 | } |
drh | eeb23a4 | 2009-05-04 20:20:16 +0000 | [diff] [blame] | 440 | }else{ |
| 441 | longvalue = va_arg(ap,unsigned int); |
| 442 | } |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 443 | prefix = 0; |
| 444 | } |
| 445 | if( longvalue==0 ) flag_alternateform = 0; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 446 | if( flag_zeropad && precision<width-(prefix!=0) ){ |
| 447 | precision = width-(prefix!=0); |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 448 | } |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 449 | if( precision<etBUFSIZE-10-etBUFSIZE/3 ){ |
drh | 59eedf7 | 2011-10-11 17:54:54 +0000 | [diff] [blame] | 450 | nOut = etBUFSIZE; |
| 451 | zOut = buf; |
| 452 | }else{ |
drh | 7ba03ea | 2019-02-01 21:08:27 +0000 | [diff] [blame] | 453 | u64 n; |
| 454 | n = (u64)precision + 10; |
| 455 | if( cThousand ) n += precision/3; |
drh | 2964225 | 2019-02-01 20:29:04 +0000 | [diff] [blame] | 456 | zOut = zExtra = printfTempBuf(pAccum, n); |
| 457 | if( zOut==0 ) return; |
drh | 5f42995 | 2017-03-20 16:34:18 +0000 | [diff] [blame] | 458 | nOut = (int)n; |
drh | 59eedf7 | 2011-10-11 17:54:54 +0000 | [diff] [blame] | 459 | } |
| 460 | bufpt = &zOut[nOut-1]; |
drh | 9a99334 | 2007-12-13 02:45:31 +0000 | [diff] [blame] | 461 | if( xtype==etORDINAL ){ |
drh | 43f6e06 | 2007-12-13 17:50:22 +0000 | [diff] [blame] | 462 | static const char zOrd[] = "thstndrd"; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 463 | int x = (int)(longvalue % 10); |
drh | 43f6e06 | 2007-12-13 17:50:22 +0000 | [diff] [blame] | 464 | if( x>=4 || (longvalue/10)%10==1 ){ |
| 465 | x = 0; |
| 466 | } |
drh | 59eedf7 | 2011-10-11 17:54:54 +0000 | [diff] [blame] | 467 | *(--bufpt) = zOrd[x*2+1]; |
| 468 | *(--bufpt) = zOrd[x*2]; |
drh | 9a99334 | 2007-12-13 02:45:31 +0000 | [diff] [blame] | 469 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 470 | { |
drh | 0e68209 | 2014-03-17 15:06:57 +0000 | [diff] [blame] | 471 | const char *cset = &aDigits[infop->charset]; |
| 472 | u8 base = infop->base; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 473 | do{ /* Convert to ascii */ |
| 474 | *(--bufpt) = cset[longvalue%base]; |
| 475 | longvalue = longvalue/base; |
| 476 | }while( longvalue>0 ); |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 477 | } |
drh | 59eedf7 | 2011-10-11 17:54:54 +0000 | [diff] [blame] | 478 | length = (int)(&zOut[nOut-1]-bufpt); |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 479 | while( precision>length ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 480 | *(--bufpt) = '0'; /* Zero pad */ |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 481 | length++; |
| 482 | } |
| 483 | if( cThousand ){ |
| 484 | int nn = (length - 1)/3; /* Number of "," to insert */ |
| 485 | int ix = (length - 1)%3 + 1; |
| 486 | bufpt -= nn; |
| 487 | for(idx=0; nn>0; idx++){ |
| 488 | bufpt[idx] = bufpt[idx+nn]; |
| 489 | ix--; |
| 490 | if( ix==0 ){ |
| 491 | bufpt[++idx] = cThousand; |
| 492 | nn--; |
| 493 | ix = 3; |
| 494 | } |
| 495 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 496 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 497 | if( prefix ) *(--bufpt) = prefix; /* Add sign */ |
| 498 | if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ |
drh | 76ff3a0 | 2004-09-24 22:32:30 +0000 | [diff] [blame] | 499 | const char *pre; |
| 500 | char x; |
| 501 | pre = &aPrefix[infop->prefix]; |
drh | af005fb | 2008-07-09 16:51:51 +0000 | [diff] [blame] | 502 | for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 503 | } |
drh | 59eedf7 | 2011-10-11 17:54:54 +0000 | [diff] [blame] | 504 | length = (int)(&zOut[nOut-1]-bufpt); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 505 | break; |
| 506 | case etFLOAT: |
| 507 | case etEXP: |
| 508 | case etGENERIC: |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 509 | if( bArgList ){ |
| 510 | realvalue = getDoubleArg(pArgList); |
| 511 | }else{ |
| 512 | realvalue = va_arg(ap,double); |
| 513 | } |
drh | 69ef703 | 2010-03-04 17:11:31 +0000 | [diff] [blame] | 514 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 515 | length = 0; |
| 516 | #else |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 517 | if( precision<0 ) precision = 6; /* Set default precision */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 518 | if( realvalue<0.0 ){ |
| 519 | realvalue = -realvalue; |
| 520 | prefix = '-'; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 521 | }else{ |
drh | 2c338a9 | 2017-02-10 19:38:36 +0000 | [diff] [blame] | 522 | prefix = flag_prefix; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 523 | } |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 524 | if( xtype==etGENERIC && precision>0 ) precision--; |
drh | a30d22a | 2015-04-07 13:28:41 +0000 | [diff] [blame] | 525 | testcase( precision>0xfff ); |
drh | a0ed86b | 2019-05-24 22:58:16 +0000 | [diff] [blame] | 526 | idx = precision & 0xfff; |
| 527 | rounder = arRound[idx%10]; |
| 528 | while( idx>=10 ){ rounder *= 1.0e-10; idx -= 10; } |
| 529 | if( xtype==etFLOAT ){ |
drh | ef7d518 | 2019-05-27 00:29:15 +0000 | [diff] [blame] | 530 | double rx = (double)realvalue; |
| 531 | sqlite3_uint64 u; |
| 532 | int ex; |
| 533 | memcpy(&u, &rx, sizeof(u)); |
| 534 | ex = -1023 + (int)((u>>52)&0x7ff); |
| 535 | if( precision+(ex/3) < 15 ) rounder += realvalue*3e-16; |
drh | a0ed86b | 2019-05-24 22:58:16 +0000 | [diff] [blame] | 536 | realvalue += rounder; |
| 537 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 538 | /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ |
| 539 | exp = 0; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 540 | if( sqlite3IsNaN((double)realvalue) ){ |
drh | 53c1402 | 2007-05-10 17:23:11 +0000 | [diff] [blame] | 541 | bufpt = "NaN"; |
| 542 | length = 3; |
| 543 | break; |
| 544 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 545 | if( realvalue>0.0 ){ |
drh | 72b3fbc | 2012-06-19 03:11:25 +0000 | [diff] [blame] | 546 | LONGDOUBLE_TYPE scale = 1.0; |
drh | 4ef9413 | 2012-06-19 03:35:05 +0000 | [diff] [blame] | 547 | while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;} |
drh | 2a8f671 | 2015-09-02 21:00:48 +0000 | [diff] [blame] | 548 | while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; } |
drh | 72b3fbc | 2012-06-19 03:11:25 +0000 | [diff] [blame] | 549 | while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } |
| 550 | realvalue /= scale; |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 551 | while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } |
| 552 | while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } |
drh | af005fb | 2008-07-09 16:51:51 +0000 | [diff] [blame] | 553 | if( exp>350 ){ |
drh | 2a8f671 | 2015-09-02 21:00:48 +0000 | [diff] [blame] | 554 | bufpt = buf; |
| 555 | buf[0] = prefix; |
| 556 | memcpy(buf+(prefix!=0),"Inf",4); |
| 557 | length = 3+(prefix!=0); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 558 | break; |
| 559 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 560 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 561 | bufpt = buf; |
| 562 | /* |
| 563 | ** If the field type is etGENERIC, then convert to either etEXP |
| 564 | ** or etFLOAT, as appropriate. |
| 565 | */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 566 | if( xtype!=etFLOAT ){ |
| 567 | realvalue += rounder; |
| 568 | if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } |
| 569 | } |
| 570 | if( xtype==etGENERIC ){ |
| 571 | flag_rtz = !flag_alternateform; |
| 572 | if( exp<-4 || exp>precision ){ |
| 573 | xtype = etEXP; |
| 574 | }else{ |
| 575 | precision = precision - exp; |
| 576 | xtype = etFLOAT; |
| 577 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 578 | }else{ |
drh | 72b3fbc | 2012-06-19 03:11:25 +0000 | [diff] [blame] | 579 | flag_rtz = flag_altform2; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 580 | } |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 581 | if( xtype==etEXP ){ |
| 582 | e2 = 0; |
| 583 | }else{ |
| 584 | e2 = exp; |
| 585 | } |
drh | 2964225 | 2019-02-01 20:29:04 +0000 | [diff] [blame] | 586 | { |
| 587 | i64 szBufNeeded; /* Size of a temporary buffer needed */ |
| 588 | szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15; |
| 589 | if( szBufNeeded > etBUFSIZE ){ |
| 590 | bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded); |
| 591 | if( bufpt==0 ) return; |
drh | 59eedf7 | 2011-10-11 17:54:54 +0000 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | zOut = bufpt; |
drh | 72b3fbc | 2012-06-19 03:11:25 +0000 | [diff] [blame] | 595 | nsd = 16 + flag_altform2*10; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 596 | flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 597 | /* The sign in front of the number */ |
| 598 | if( prefix ){ |
| 599 | *(bufpt++) = prefix; |
| 600 | } |
| 601 | /* Digits prior to the decimal point */ |
| 602 | if( e2<0 ){ |
| 603 | *(bufpt++) = '0'; |
| 604 | }else{ |
| 605 | for(; e2>=0; e2--){ |
| 606 | *(bufpt++) = et_getdigit(&realvalue,&nsd); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 607 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 608 | } |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 609 | /* The decimal point */ |
| 610 | if( flag_dp ){ |
| 611 | *(bufpt++) = '.'; |
| 612 | } |
| 613 | /* "0" digits after the decimal point but before the first |
| 614 | ** significant digit of the number */ |
drh | af005fb | 2008-07-09 16:51:51 +0000 | [diff] [blame] | 615 | for(e2++; e2<0; precision--, e2++){ |
| 616 | assert( precision>0 ); |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 617 | *(bufpt++) = '0'; |
| 618 | } |
| 619 | /* Significant digits after the decimal point */ |
| 620 | while( (precision--)>0 ){ |
| 621 | *(bufpt++) = et_getdigit(&realvalue,&nsd); |
| 622 | } |
| 623 | /* Remove trailing zeros and the "." if no digits follow the "." */ |
| 624 | if( flag_rtz && flag_dp ){ |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 625 | while( bufpt[-1]=='0' ) *(--bufpt) = 0; |
drh | 59eedf7 | 2011-10-11 17:54:54 +0000 | [diff] [blame] | 626 | assert( bufpt>zOut ); |
drh | 3e9aeec | 2005-08-13 13:39:02 +0000 | [diff] [blame] | 627 | if( bufpt[-1]=='.' ){ |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 628 | if( flag_altform2 ){ |
| 629 | *(bufpt++) = '0'; |
| 630 | }else{ |
| 631 | *(--bufpt) = 0; |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | /* Add the "eNNN" suffix */ |
drh | 59eedf7 | 2011-10-11 17:54:54 +0000 | [diff] [blame] | 636 | if( xtype==etEXP ){ |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 637 | *(bufpt++) = aDigits[infop->charset]; |
| 638 | if( exp<0 ){ |
| 639 | *(bufpt++) = '-'; exp = -exp; |
| 640 | }else{ |
| 641 | *(bufpt++) = '+'; |
| 642 | } |
| 643 | if( exp>=100 ){ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 644 | *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 645 | exp %= 100; |
| 646 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 647 | *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ |
| 648 | *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ |
drh | 557cc60 | 2005-08-13 12:59:14 +0000 | [diff] [blame] | 649 | } |
| 650 | *bufpt = 0; |
| 651 | |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 652 | /* The converted number is in buf[] and zero terminated. Output it. |
| 653 | ** Note that the number is in the usual order, not reversed as with |
| 654 | ** integer conversions. */ |
drh | 59eedf7 | 2011-10-11 17:54:54 +0000 | [diff] [blame] | 655 | length = (int)(bufpt-zOut); |
| 656 | bufpt = zOut; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 657 | |
| 658 | /* Special case: Add leading zeros if the flag_zeropad flag is |
| 659 | ** set and we are not left justified */ |
| 660 | if( flag_zeropad && !flag_leftjustify && length < width){ |
| 661 | int i; |
| 662 | int nPad = width - length; |
| 663 | for(i=width; i>=nPad; i--){ |
| 664 | bufpt[i] = bufpt[i-nPad]; |
| 665 | } |
| 666 | i = prefix!=0; |
| 667 | while( nPad-- ) bufpt[i++] = '0'; |
| 668 | length = width; |
| 669 | } |
drh | 69ef703 | 2010-03-04 17:11:31 +0000 | [diff] [blame] | 670 | #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 671 | break; |
| 672 | case etSIZE: |
drh | fc6ee9d | 2013-12-17 15:58:42 +0000 | [diff] [blame] | 673 | if( !bArgList ){ |
| 674 | *(va_arg(ap,int*)) = pAccum->nChar; |
| 675 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 676 | length = width = 0; |
| 677 | break; |
| 678 | case etPERCENT: |
| 679 | buf[0] = '%'; |
| 680 | bufpt = buf; |
| 681 | length = 1; |
| 682 | break; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 683 | case etCHARX: |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 684 | if( bArgList ){ |
drh | fc6ee9d | 2013-12-17 15:58:42 +0000 | [diff] [blame] | 685 | bufpt = getTextArg(pArgList); |
drh | a15a7c3 | 2018-02-19 21:58:16 +0000 | [diff] [blame] | 686 | length = 1; |
drh | 136102b | 2018-02-19 18:56:52 +0000 | [diff] [blame] | 687 | if( bufpt ){ |
| 688 | buf[0] = c = *(bufpt++); |
drh | 136102b | 2018-02-19 18:56:52 +0000 | [diff] [blame] | 689 | if( (c&0xc0)==0xc0 ){ |
| 690 | while( length<4 && (bufpt[0]&0xc0)==0x80 ){ |
| 691 | buf[length++] = *(bufpt++); |
| 692 | } |
| 693 | } |
drh | a15a7c3 | 2018-02-19 21:58:16 +0000 | [diff] [blame] | 694 | }else{ |
| 695 | buf[0] = 0; |
drh | 136102b | 2018-02-19 18:56:52 +0000 | [diff] [blame] | 696 | } |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 697 | }else{ |
drh | 136102b | 2018-02-19 18:56:52 +0000 | [diff] [blame] | 698 | unsigned int ch = va_arg(ap,unsigned int); |
| 699 | if( ch<0x00080 ){ |
| 700 | buf[0] = ch & 0xff; |
| 701 | length = 1; |
| 702 | }else if( ch<0x00800 ){ |
| 703 | buf[0] = 0xc0 + (u8)((ch>>6)&0x1f); |
| 704 | buf[1] = 0x80 + (u8)(ch & 0x3f); |
| 705 | length = 2; |
| 706 | }else if( ch<0x10000 ){ |
| 707 | buf[0] = 0xe0 + (u8)((ch>>12)&0x0f); |
| 708 | buf[1] = 0x80 + (u8)((ch>>6) & 0x3f); |
| 709 | buf[2] = 0x80 + (u8)(ch & 0x3f); |
| 710 | length = 3; |
| 711 | }else{ |
| 712 | buf[0] = 0xf0 + (u8)((ch>>18) & 0x07); |
| 713 | buf[1] = 0x80 + (u8)((ch>>12) & 0x3f); |
| 714 | buf[2] = 0x80 + (u8)((ch>>6) & 0x3f); |
| 715 | buf[3] = 0x80 + (u8)(ch & 0x3f); |
| 716 | length = 4; |
| 717 | } |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 718 | } |
drh | af8f513 | 2014-10-29 18:20:18 +0000 | [diff] [blame] | 719 | if( precision>1 ){ |
| 720 | width -= precision-1; |
| 721 | if( width>1 && !flag_leftjustify ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 722 | sqlite3_str_appendchar(pAccum, width-1, ' '); |
drh | af8f513 | 2014-10-29 18:20:18 +0000 | [diff] [blame] | 723 | width = 0; |
| 724 | } |
drh | 136102b | 2018-02-19 18:56:52 +0000 | [diff] [blame] | 725 | while( precision-- > 1 ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 726 | sqlite3_str_append(pAccum, buf, length); |
drh | 136102b | 2018-02-19 18:56:52 +0000 | [diff] [blame] | 727 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 728 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 729 | bufpt = buf; |
drh | cf7c837 | 2018-02-19 20:23:20 +0000 | [diff] [blame] | 730 | flag_altform2 = 1; |
| 731 | goto adjust_width_for_utf8; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 732 | case etSTRING: |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 733 | case etDYNSTRING: |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 734 | if( bArgList ){ |
| 735 | bufpt = getTextArg(pArgList); |
drh | 2a8f671 | 2015-09-02 21:00:48 +0000 | [diff] [blame] | 736 | xtype = etSTRING; |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 737 | }else{ |
| 738 | bufpt = va_arg(ap,char*); |
| 739 | } |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 740 | if( bufpt==0 ){ |
| 741 | bufpt = ""; |
drh | 2a8f671 | 2015-09-02 21:00:48 +0000 | [diff] [blame] | 742 | }else if( xtype==etDYNSTRING ){ |
drh | af524a6 | 2018-09-13 17:07:12 +0000 | [diff] [blame] | 743 | if( pAccum->nChar==0 |
| 744 | && pAccum->mxAlloc |
| 745 | && width==0 |
| 746 | && precision<0 |
| 747 | && pAccum->accError==0 |
| 748 | ){ |
drh | cc39896 | 2018-02-20 15:23:37 +0000 | [diff] [blame] | 749 | /* Special optimization for sqlite3_mprintf("%z..."): |
| 750 | ** Extend an existing memory allocation rather than creating |
| 751 | ** a new one. */ |
| 752 | assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); |
| 753 | pAccum->zText = bufpt; |
| 754 | pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt); |
| 755 | pAccum->nChar = 0x7fffffff & (int)strlen(bufpt); |
| 756 | pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED; |
| 757 | length = 0; |
| 758 | break; |
| 759 | } |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 760 | zExtra = bufpt; |
| 761 | } |
drh | e509094 | 2008-04-29 15:22:27 +0000 | [diff] [blame] | 762 | if( precision>=0 ){ |
drh | 6285646 | 2018-02-19 17:03:23 +0000 | [diff] [blame] | 763 | if( flag_altform2 ){ |
| 764 | /* Set length to the number of bytes needed in order to display |
| 765 | ** precision characters */ |
| 766 | unsigned char *z = (unsigned char*)bufpt; |
| 767 | while( precision-- > 0 && z[0] ){ |
| 768 | SQLITE_SKIP_UTF8(z); |
| 769 | } |
| 770 | length = (int)(z - (unsigned char*)bufpt); |
| 771 | }else{ |
| 772 | for(length=0; length<precision && bufpt[length]; length++){} |
| 773 | } |
drh | e509094 | 2008-04-29 15:22:27 +0000 | [diff] [blame] | 774 | }else{ |
drh | c84ddf1 | 2017-08-19 20:38:18 +0000 | [diff] [blame] | 775 | length = 0x7fffffff & (int)strlen(bufpt); |
drh | e509094 | 2008-04-29 15:22:27 +0000 | [diff] [blame] | 776 | } |
drh | 57e3ba7 | 2018-02-19 18:03:10 +0000 | [diff] [blame] | 777 | adjust_width_for_utf8: |
drh | 6285646 | 2018-02-19 17:03:23 +0000 | [diff] [blame] | 778 | if( flag_altform2 && width>0 ){ |
| 779 | /* Adjust width to account for extra bytes in UTF-8 characters */ |
| 780 | int ii = length - 1; |
| 781 | while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++; |
| 782 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 783 | break; |
drh | 57e3ba7 | 2018-02-19 18:03:10 +0000 | [diff] [blame] | 784 | case etSQLESCAPE: /* %q: Escape ' characters */ |
| 785 | case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */ |
| 786 | case etSQLESCAPE3: { /* %w: Escape " characters */ |
drh | 8965b50 | 2009-11-25 16:53:37 +0000 | [diff] [blame] | 787 | int i, j, k, n, isnull; |
drh | 5eba8c0 | 2005-08-19 02:26:27 +0000 | [diff] [blame] | 788 | int needQuote; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 789 | char ch; |
danielk1977 | f3b863e | 2007-06-24 06:32:17 +0000 | [diff] [blame] | 790 | char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 791 | char *escarg; |
| 792 | |
| 793 | if( bArgList ){ |
| 794 | escarg = getTextArg(pArgList); |
| 795 | }else{ |
| 796 | escarg = va_arg(ap,char*); |
| 797 | } |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 798 | isnull = escarg==0; |
| 799 | if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); |
drh | 57e3ba7 | 2018-02-19 18:03:10 +0000 | [diff] [blame] | 800 | /* For %q, %Q, and %w, the precision is the number of byte (or |
| 801 | ** characters if the ! flags is present) to use from the input. |
| 802 | ** Because of the extra quoting characters inserted, the number |
| 803 | ** of output characters may be larger than the precision. |
| 804 | */ |
drh | 8965b50 | 2009-11-25 16:53:37 +0000 | [diff] [blame] | 805 | k = precision; |
dan | 60d4a30 | 2010-03-04 17:58:45 +0000 | [diff] [blame] | 806 | for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ |
danielk1977 | f3b863e | 2007-06-24 06:32:17 +0000 | [diff] [blame] | 807 | if( ch==q ) n++; |
drh | 57e3ba7 | 2018-02-19 18:03:10 +0000 | [diff] [blame] | 808 | if( flag_altform2 && (ch&0xc0)==0xc0 ){ |
| 809 | while( (escarg[i+1]&0xc0)==0x80 ){ i++; } |
| 810 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 811 | } |
drh | 5eba8c0 | 2005-08-19 02:26:27 +0000 | [diff] [blame] | 812 | needQuote = !isnull && xtype==etSQLESCAPE2; |
drh | 2a8f671 | 2015-09-02 21:00:48 +0000 | [diff] [blame] | 813 | n += i + 3; |
drh | 5eba8c0 | 2005-08-19 02:26:27 +0000 | [diff] [blame] | 814 | if( n>etBUFSIZE ){ |
drh | 2964225 | 2019-02-01 20:29:04 +0000 | [diff] [blame] | 815 | bufpt = zExtra = printfTempBuf(pAccum, n); |
| 816 | if( bufpt==0 ) return; |
drh | 5eba8c0 | 2005-08-19 02:26:27 +0000 | [diff] [blame] | 817 | }else{ |
| 818 | bufpt = buf; |
| 819 | } |
| 820 | j = 0; |
danielk1977 | f3b863e | 2007-06-24 06:32:17 +0000 | [diff] [blame] | 821 | if( needQuote ) bufpt[j++] = q; |
drh | 8965b50 | 2009-11-25 16:53:37 +0000 | [diff] [blame] | 822 | k = i; |
| 823 | for(i=0; i<k; i++){ |
| 824 | bufpt[j++] = ch = escarg[i]; |
danielk1977 | f3b863e | 2007-06-24 06:32:17 +0000 | [diff] [blame] | 825 | if( ch==q ) bufpt[j++] = ch; |
drh | 5eba8c0 | 2005-08-19 02:26:27 +0000 | [diff] [blame] | 826 | } |
danielk1977 | f3b863e | 2007-06-24 06:32:17 +0000 | [diff] [blame] | 827 | if( needQuote ) bufpt[j++] = q; |
drh | 5eba8c0 | 2005-08-19 02:26:27 +0000 | [diff] [blame] | 828 | bufpt[j] = 0; |
| 829 | length = j; |
drh | 57e3ba7 | 2018-02-19 18:03:10 +0000 | [diff] [blame] | 830 | goto adjust_width_for_utf8; |
drh | 5eba8c0 | 2005-08-19 02:26:27 +0000 | [diff] [blame] | 831 | } |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 832 | case etTOKEN: { |
drh | 8236f68 | 2017-01-04 00:26:28 +0000 | [diff] [blame] | 833 | Token *pToken; |
| 834 | if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; |
| 835 | pToken = va_arg(ap, Token*); |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 836 | assert( bArgList==0 ); |
drh | a9ab481 | 2013-12-11 11:00:44 +0000 | [diff] [blame] | 837 | if( pToken && pToken->n ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 838 | sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n); |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 839 | } |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 840 | length = width = 0; |
| 841 | break; |
| 842 | } |
| 843 | case etSRCLIST: { |
drh | 8236f68 | 2017-01-04 00:26:28 +0000 | [diff] [blame] | 844 | SrcList *pSrc; |
| 845 | int k; |
| 846 | struct SrcList_item *pItem; |
| 847 | if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; |
| 848 | pSrc = va_arg(ap, SrcList*); |
| 849 | k = va_arg(ap, int); |
| 850 | pItem = &pSrc->a[k]; |
drh | a5c1416 | 2013-12-17 15:03:06 +0000 | [diff] [blame] | 851 | assert( bArgList==0 ); |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 852 | assert( k>=0 && k<pSrc->nSrc ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 853 | if( pItem->zDatabase ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 854 | sqlite3_str_appendall(pAccum, pItem->zDatabase); |
| 855 | sqlite3_str_append(pAccum, ".", 1); |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 856 | } |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 857 | sqlite3_str_appendall(pAccum, pItem->zName); |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 858 | length = width = 0; |
| 859 | break; |
| 860 | } |
drh | 874ba04 | 2009-04-08 16:10:04 +0000 | [diff] [blame] | 861 | default: { |
| 862 | assert( xtype==etINVALID ); |
| 863 | return; |
| 864 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 865 | }/* End switch over the format type */ |
| 866 | /* |
| 867 | ** The text of the conversion is pointed to by "bufpt" and is |
| 868 | ** "length" characters long. The field width is "width". Do |
drh | 6285646 | 2018-02-19 17:03:23 +0000 | [diff] [blame] | 869 | ** the output. Both length and width are in bytes, not characters, |
| 870 | ** at this point. If the "!" flag was present on string conversions |
| 871 | ** indicating that width and precision should be expressed in characters, |
| 872 | ** then the values have been translated prior to reaching this point. |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 873 | */ |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 874 | width -= length; |
drh | 8236f68 | 2017-01-04 00:26:28 +0000 | [diff] [blame] | 875 | if( width>0 ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 876 | if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' '); |
| 877 | sqlite3_str_append(pAccum, bufpt, length); |
| 878 | if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' '); |
drh | 8236f68 | 2017-01-04 00:26:28 +0000 | [diff] [blame] | 879 | }else{ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 880 | sqlite3_str_append(pAccum, bufpt, length); |
drh | 8236f68 | 2017-01-04 00:26:28 +0000 | [diff] [blame] | 881 | } |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 882 | |
drh | af8f513 | 2014-10-29 18:20:18 +0000 | [diff] [blame] | 883 | if( zExtra ){ |
drh | 96ceaf8 | 2015-11-14 22:04:22 +0000 | [diff] [blame] | 884 | sqlite3DbFree(pAccum->db, zExtra); |
drh | af8f513 | 2014-10-29 18:20:18 +0000 | [diff] [blame] | 885 | zExtra = 0; |
| 886 | } |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 887 | }/* End for loop over the format string */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 888 | } /* End of function */ |
| 889 | |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 890 | /* |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 891 | ** Enlarge the memory allocation on a StrAccum object so that it is |
| 892 | ** able to accept at least N more bytes of text. |
| 893 | ** |
| 894 | ** Return the number of bytes of text that StrAccum is able to accept |
| 895 | ** after the attempted enlargement. The value returned might be zero. |
| 896 | */ |
| 897 | static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ |
| 898 | char *zNew; |
drh | a30d22a | 2015-04-07 13:28:41 +0000 | [diff] [blame] | 899 | assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */ |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 900 | if( p->accError ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 901 | testcase(p->accError==SQLITE_TOOBIG); |
| 902 | testcase(p->accError==SQLITE_NOMEM); |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 903 | return 0; |
| 904 | } |
drh | c049057 | 2015-05-02 11:45:53 +0000 | [diff] [blame] | 905 | if( p->mxAlloc==0 ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 906 | setStrAccumError(p, SQLITE_TOOBIG); |
drh | 255a81f | 2019-02-22 15:42:10 +0000 | [diff] [blame] | 907 | return p->nAlloc - p->nChar - 1; |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 908 | }else{ |
drh | 5f4a686 | 2016-01-30 12:50:25 +0000 | [diff] [blame] | 909 | char *zOld = isMalloced(p) ? p->zText : 0; |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 910 | i64 szNew = p->nChar; |
| 911 | szNew += N + 1; |
drh | 7b4d780 | 2014-11-03 14:46:29 +0000 | [diff] [blame] | 912 | if( szNew+p->nChar<=p->mxAlloc ){ |
| 913 | /* Force exponential buffer size growth as long as it does not overflow, |
| 914 | ** to avoid having to call this routine too often */ |
| 915 | szNew += p->nChar; |
| 916 | } |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 917 | if( szNew > p->mxAlloc ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 918 | sqlite3_str_reset(p); |
| 919 | setStrAccumError(p, SQLITE_TOOBIG); |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 920 | return 0; |
| 921 | }else{ |
| 922 | p->nAlloc = (int)szNew; |
| 923 | } |
drh | c049057 | 2015-05-02 11:45:53 +0000 | [diff] [blame] | 924 | if( p->db ){ |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 925 | zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); |
| 926 | }else{ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 927 | zNew = sqlite3_realloc64(zOld, p->nAlloc); |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 928 | } |
| 929 | if( zNew ){ |
drh | 7ef4d1c | 2014-05-31 15:39:53 +0000 | [diff] [blame] | 930 | assert( p->zText!=0 || p->nChar==0 ); |
drh | 5f4a686 | 2016-01-30 12:50:25 +0000 | [diff] [blame] | 931 | if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 932 | p->zText = zNew; |
drh | 7f5a7ec | 2014-11-03 13:24:12 +0000 | [diff] [blame] | 933 | p->nAlloc = sqlite3DbMallocSize(p->db, zNew); |
drh | 5f4a686 | 2016-01-30 12:50:25 +0000 | [diff] [blame] | 934 | p->printfFlags |= SQLITE_PRINTF_MALLOCED; |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 935 | }else{ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 936 | sqlite3_str_reset(p); |
| 937 | setStrAccumError(p, SQLITE_NOMEM); |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 938 | return 0; |
| 939 | } |
| 940 | } |
| 941 | return N; |
| 942 | } |
| 943 | |
| 944 | /* |
drh | af8f513 | 2014-10-29 18:20:18 +0000 | [diff] [blame] | 945 | ** Append N copies of character c to the given string buffer. |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 946 | */ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 947 | void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){ |
drh | a30d22a | 2015-04-07 13:28:41 +0000 | [diff] [blame] | 948 | testcase( p->nChar + (i64)N > 0x7fffffff ); |
| 949 | if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ |
| 950 | return; |
| 951 | } |
drh | af8f513 | 2014-10-29 18:20:18 +0000 | [diff] [blame] | 952 | while( (N--)>0 ) p->zText[p->nChar++] = c; |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | /* |
| 956 | ** The StrAccum "p" is not large enough to accept N new bytes of z[]. |
| 957 | ** So enlarge if first, then do the append. |
| 958 | ** |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 959 | ** This is a helper routine to sqlite3_str_append() that does special-case |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 960 | ** work (enlarging the buffer) using tail recursion, so that the |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 961 | ** sqlite3_str_append() routine can use fast calling semantics. |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 962 | */ |
drh | 172087f | 2014-08-22 15:40:20 +0000 | [diff] [blame] | 963 | static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 964 | N = sqlite3StrAccumEnlarge(p, N); |
| 965 | if( N>0 ){ |
| 966 | memcpy(&p->zText[p->nChar], z, N); |
| 967 | p->nChar += N; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | /* |
| 972 | ** Append N bytes of text from z to the StrAccum object. Increase the |
| 973 | ** size of the memory allocation for StrAccum if necessary. |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 974 | */ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 975 | void sqlite3_str_append(sqlite3_str *p, const char *z, int N){ |
drh | 3457338 | 2015-04-15 05:38:35 +0000 | [diff] [blame] | 976 | assert( z!=0 || N==0 ); |
drh | a6353a3 | 2013-12-09 19:03:26 +0000 | [diff] [blame] | 977 | assert( p->zText!=0 || p->nChar==0 || p->accError ); |
| 978 | assert( N>=0 ); |
drh | 255a81f | 2019-02-22 15:42:10 +0000 | [diff] [blame] | 979 | assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 ); |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 980 | if( p->nChar+N >= p->nAlloc ){ |
drh | a70a073 | 2014-03-17 14:24:27 +0000 | [diff] [blame] | 981 | enlargeAndAppend(p,z,N); |
dan | 895decf | 2016-12-30 14:15:56 +0000 | [diff] [blame] | 982 | }else if( N ){ |
drh | 172087f | 2014-08-22 15:40:20 +0000 | [diff] [blame] | 983 | assert( p->zText ); |
| 984 | p->nChar += N; |
| 985 | memcpy(&p->zText[p->nChar-N], z, N); |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 986 | } |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /* |
drh | a6353a3 | 2013-12-09 19:03:26 +0000 | [diff] [blame] | 990 | ** Append the complete text of zero-terminated string z[] to the p string. |
| 991 | */ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 992 | void sqlite3_str_appendall(sqlite3_str *p, const char *z){ |
| 993 | sqlite3_str_append(p, z, sqlite3Strlen30(z)); |
drh | a6353a3 | 2013-12-09 19:03:26 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | |
| 997 | /* |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 998 | ** Finish off a string by making sure it is zero-terminated. |
| 999 | ** Return a pointer to the resulting string. Return a NULL |
| 1000 | ** pointer if any kind of error was encountered. |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 1001 | */ |
drh | 043e586 | 2016-11-25 15:11:26 +0000 | [diff] [blame] | 1002 | static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){ |
drh | 3f18e6d | 2017-08-12 02:01:55 +0000 | [diff] [blame] | 1003 | char *zText; |
drh | 043e586 | 2016-11-25 15:11:26 +0000 | [diff] [blame] | 1004 | assert( p->mxAlloc>0 && !isMalloced(p) ); |
drh | 3f18e6d | 2017-08-12 02:01:55 +0000 | [diff] [blame] | 1005 | zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); |
| 1006 | if( zText ){ |
| 1007 | memcpy(zText, p->zText, p->nChar+1); |
drh | 043e586 | 2016-11-25 15:11:26 +0000 | [diff] [blame] | 1008 | p->printfFlags |= SQLITE_PRINTF_MALLOCED; |
| 1009 | }else{ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1010 | setStrAccumError(p, SQLITE_NOMEM); |
drh | 043e586 | 2016-11-25 15:11:26 +0000 | [diff] [blame] | 1011 | } |
drh | 3f18e6d | 2017-08-12 02:01:55 +0000 | [diff] [blame] | 1012 | p->zText = zText; |
| 1013 | return zText; |
drh | 043e586 | 2016-11-25 15:11:26 +0000 | [diff] [blame] | 1014 | } |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1015 | char *sqlite3StrAccumFinish(StrAccum *p){ |
| 1016 | if( p->zText ){ |
| 1017 | p->zText[p->nChar] = 0; |
drh | 5f4a686 | 2016-01-30 12:50:25 +0000 | [diff] [blame] | 1018 | if( p->mxAlloc>0 && !isMalloced(p) ){ |
drh | 043e586 | 2016-11-25 15:11:26 +0000 | [diff] [blame] | 1019 | return strAccumFinishRealloc(p); |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | return p->zText; |
| 1023 | } |
| 1024 | |
drh | f80bba9 | 2018-05-16 15:35:03 +0000 | [diff] [blame] | 1025 | /* |
| 1026 | ** This singleton is an sqlite3_str object that is returned if |
| 1027 | ** sqlite3_malloc() fails to provide space for a real one. This |
| 1028 | ** sqlite3_str object accepts no new text and always returns |
| 1029 | ** an SQLITE_NOMEM error. |
| 1030 | */ |
| 1031 | static sqlite3_str sqlite3OomStr = { |
drh | 3e62ddb | 2018-05-30 00:59:09 +0000 | [diff] [blame] | 1032 | 0, 0, 0, 0, 0, SQLITE_NOMEM, 0 |
drh | f80bba9 | 2018-05-16 15:35:03 +0000 | [diff] [blame] | 1033 | }; |
| 1034 | |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1035 | /* Finalize a string created using sqlite3_str_new(). |
| 1036 | */ |
| 1037 | char *sqlite3_str_finish(sqlite3_str *p){ |
| 1038 | char *z; |
drh | f80bba9 | 2018-05-16 15:35:03 +0000 | [diff] [blame] | 1039 | if( p!=0 && p!=&sqlite3OomStr ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1040 | z = sqlite3StrAccumFinish(p); |
drh | 446135d | 2018-05-09 14:29:40 +0000 | [diff] [blame] | 1041 | sqlite3_free(p); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1042 | }else{ |
| 1043 | z = 0; |
| 1044 | } |
| 1045 | return z; |
| 1046 | } |
| 1047 | |
| 1048 | /* Return any error code associated with p */ |
| 1049 | int sqlite3_str_errcode(sqlite3_str *p){ |
| 1050 | return p ? p->accError : SQLITE_NOMEM; |
| 1051 | } |
| 1052 | |
| 1053 | /* Return the current length of p in bytes */ |
| 1054 | int sqlite3_str_length(sqlite3_str *p){ |
| 1055 | return p ? p->nChar : 0; |
| 1056 | } |
| 1057 | |
| 1058 | /* Return the current value for p */ |
| 1059 | char *sqlite3_str_value(sqlite3_str *p){ |
drh | 446135d | 2018-05-09 14:29:40 +0000 | [diff] [blame] | 1060 | if( p==0 || p->nChar==0 ) return 0; |
| 1061 | p->zText[p->nChar] = 0; |
| 1062 | return p->zText; |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1065 | /* |
| 1066 | ** Reset an StrAccum string. Reclaim all malloced memory. |
| 1067 | */ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1068 | void sqlite3_str_reset(StrAccum *p){ |
drh | 5f4a686 | 2016-01-30 12:50:25 +0000 | [diff] [blame] | 1069 | if( isMalloced(p) ){ |
drh | c049057 | 2015-05-02 11:45:53 +0000 | [diff] [blame] | 1070 | sqlite3DbFree(p->db, p->zText); |
drh | 5f4a686 | 2016-01-30 12:50:25 +0000 | [diff] [blame] | 1071 | p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1072 | } |
drh | 446135d | 2018-05-09 14:29:40 +0000 | [diff] [blame] | 1073 | p->nAlloc = 0; |
| 1074 | p->nChar = 0; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 1075 | p->zText = 0; |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | /* |
drh | c049057 | 2015-05-02 11:45:53 +0000 | [diff] [blame] | 1079 | ** Initialize a string accumulator. |
| 1080 | ** |
| 1081 | ** p: The accumulator to be initialized. |
| 1082 | ** db: Pointer to a database connection. May be NULL. Lookaside |
| 1083 | ** memory is used if not NULL. db->mallocFailed is set appropriately |
| 1084 | ** when not NULL. |
| 1085 | ** zBase: An initial buffer. May be NULL in which case the initial buffer |
| 1086 | ** is malloced. |
| 1087 | ** n: Size of zBase in bytes. If total space requirements never exceed |
| 1088 | ** n then no memory allocations ever occur. |
| 1089 | ** mx: Maximum number of bytes to accumulate. If mx==0 then no memory |
| 1090 | ** allocations will ever occur. |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1091 | */ |
drh | c049057 | 2015-05-02 11:45:53 +0000 | [diff] [blame] | 1092 | void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){ |
drh | 3f18e6d | 2017-08-12 02:01:55 +0000 | [diff] [blame] | 1093 | p->zText = zBase; |
drh | c049057 | 2015-05-02 11:45:53 +0000 | [diff] [blame] | 1094 | p->db = db; |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1095 | p->nAlloc = n; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1096 | p->mxAlloc = mx; |
drh | 3f18e6d | 2017-08-12 02:01:55 +0000 | [diff] [blame] | 1097 | p->nChar = 0; |
drh | b49bc86 | 2013-08-21 21:12:10 +0000 | [diff] [blame] | 1098 | p->accError = 0; |
drh | 5f4a686 | 2016-01-30 12:50:25 +0000 | [diff] [blame] | 1099 | p->printfFlags = 0; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1102 | /* Allocate and initialize a new dynamic string object */ |
| 1103 | sqlite3_str *sqlite3_str_new(sqlite3 *db){ |
drh | 446135d | 2018-05-09 14:29:40 +0000 | [diff] [blame] | 1104 | sqlite3_str *p = sqlite3_malloc64(sizeof(*p)); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1105 | if( p ){ |
drh | 446135d | 2018-05-09 14:29:40 +0000 | [diff] [blame] | 1106 | sqlite3StrAccumInit(p, 0, 0, 0, |
| 1107 | db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH); |
drh | f80bba9 | 2018-05-16 15:35:03 +0000 | [diff] [blame] | 1108 | }else{ |
| 1109 | p = &sqlite3OomStr; |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1110 | } |
| 1111 | return p; |
| 1112 | } |
| 1113 | |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 1114 | /* |
| 1115 | ** Print into memory obtained from sqliteMalloc(). Use the internal |
| 1116 | ** %-conversion extensions. |
| 1117 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1118 | char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ |
| 1119 | char *z; |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 1120 | char zBase[SQLITE_PRINT_BUF_SIZE]; |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1121 | StrAccum acc; |
drh | bc6160b | 2009-04-08 15:45:31 +0000 | [diff] [blame] | 1122 | assert( db!=0 ); |
drh | c049057 | 2015-05-02 11:45:53 +0000 | [diff] [blame] | 1123 | sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase), |
drh | bc6160b | 2009-04-08 15:45:31 +0000 | [diff] [blame] | 1124 | db->aLimit[SQLITE_LIMIT_LENGTH]); |
drh | 5f4a686 | 2016-01-30 12:50:25 +0000 | [diff] [blame] | 1125 | acc.printfFlags = SQLITE_PRINTF_INTERNAL; |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1126 | sqlite3_str_vappendf(&acc, zFormat, ap); |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1127 | z = sqlite3StrAccumFinish(&acc); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1128 | if( acc.accError==SQLITE_NOMEM ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 1129 | sqlite3OomFault(db); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1130 | } |
| 1131 | return z; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | /* |
| 1135 | ** Print into memory obtained from sqliteMalloc(). Use the internal |
| 1136 | ** %-conversion extensions. |
| 1137 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1138 | char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 1139 | va_list ap; |
| 1140 | char *z; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 1141 | va_start(ap, zFormat); |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1142 | z = sqlite3VMPrintf(db, zFormat, ap); |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 1143 | va_end(ap); |
| 1144 | return z; |
| 1145 | } |
| 1146 | |
| 1147 | /* |
drh | 28dd479 | 2006-06-26 21:35:44 +0000 | [diff] [blame] | 1148 | ** Print into memory obtained from sqlite3_malloc(). Omit the internal |
| 1149 | ** %-conversion extensions. |
| 1150 | */ |
| 1151 | char *sqlite3_vmprintf(const char *zFormat, va_list ap){ |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1152 | char *z; |
drh | 28dd479 | 2006-06-26 21:35:44 +0000 | [diff] [blame] | 1153 | char zBase[SQLITE_PRINT_BUF_SIZE]; |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1154 | StrAccum acc; |
drh | 9ca9573 | 2014-10-24 00:35:58 +0000 | [diff] [blame] | 1155 | |
| 1156 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1157 | if( zFormat==0 ){ |
| 1158 | (void)SQLITE_MISUSE_BKPT; |
| 1159 | return 0; |
| 1160 | } |
| 1161 | #endif |
drh | ff1590e | 2008-07-14 12:52:53 +0000 | [diff] [blame] | 1162 | #ifndef SQLITE_OMIT_AUTOINIT |
| 1163 | if( sqlite3_initialize() ) return 0; |
| 1164 | #endif |
drh | c049057 | 2015-05-02 11:45:53 +0000 | [diff] [blame] | 1165 | sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1166 | sqlite3_str_vappendf(&acc, zFormat, ap); |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1167 | z = sqlite3StrAccumFinish(&acc); |
| 1168 | return z; |
drh | 28dd479 | 2006-06-26 21:35:44 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | /* |
| 1172 | ** Print into memory obtained from sqlite3_malloc()(). Omit the internal |
| 1173 | ** %-conversion extensions. |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 1174 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1175 | char *sqlite3_mprintf(const char *zFormat, ...){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1176 | va_list ap; |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 1177 | char *z; |
drh | ff1590e | 2008-07-14 12:52:53 +0000 | [diff] [blame] | 1178 | #ifndef SQLITE_OMIT_AUTOINIT |
| 1179 | if( sqlite3_initialize() ) return 0; |
| 1180 | #endif |
drh | 28dd479 | 2006-06-26 21:35:44 +0000 | [diff] [blame] | 1181 | va_start(ap, zFormat); |
drh | b3738b6 | 2007-03-31 15:02:49 +0000 | [diff] [blame] | 1182 | z = sqlite3_vmprintf(zFormat, ap); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1183 | va_end(ap); |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 1184 | return z; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 1187 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1188 | ** sqlite3_snprintf() works like snprintf() except that it ignores the |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 1189 | ** current locale settings. This is important for SQLite because we |
| 1190 | ** are not able to use a "," as the decimal point in place of "." as |
| 1191 | ** specified by some locales. |
drh | db26d4c | 2011-01-05 12:20:09 +0000 | [diff] [blame] | 1192 | ** |
| 1193 | ** Oops: The first two arguments of sqlite3_snprintf() are backwards |
| 1194 | ** from the snprintf() standard. Unfortunately, it is too late to change |
| 1195 | ** this without breaking compatibility, so we just have to live with the |
| 1196 | ** mistake. |
| 1197 | ** |
| 1198 | ** sqlite3_vsnprintf() is the varargs version. |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 1199 | */ |
drh | db26d4c | 2011-01-05 12:20:09 +0000 | [diff] [blame] | 1200 | char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ |
| 1201 | StrAccum acc; |
| 1202 | if( n<=0 ) return zBuf; |
drh | 9ca9573 | 2014-10-24 00:35:58 +0000 | [diff] [blame] | 1203 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1204 | if( zBuf==0 || zFormat==0 ) { |
| 1205 | (void)SQLITE_MISUSE_BKPT; |
drh | 96c707a | 2015-02-13 16:36:14 +0000 | [diff] [blame] | 1206 | if( zBuf ) zBuf[0] = 0; |
drh | 9ca9573 | 2014-10-24 00:35:58 +0000 | [diff] [blame] | 1207 | return zBuf; |
| 1208 | } |
| 1209 | #endif |
drh | c049057 | 2015-05-02 11:45:53 +0000 | [diff] [blame] | 1210 | sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1211 | sqlite3_str_vappendf(&acc, zFormat, ap); |
drh | e9bb566 | 2016-11-25 15:47:53 +0000 | [diff] [blame] | 1212 | zBuf[acc.nChar] = 0; |
| 1213 | return zBuf; |
drh | db26d4c | 2011-01-05 12:20:09 +0000 | [diff] [blame] | 1214 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1215 | char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 1216 | char *z; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 1217 | va_list ap; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 1218 | va_start(ap,zFormat); |
drh | db26d4c | 2011-01-05 12:20:09 +0000 | [diff] [blame] | 1219 | z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 1220 | va_end(ap); |
drh | 5f96843 | 2004-02-21 19:02:30 +0000 | [diff] [blame] | 1221 | return z; |
drh | 93a5c6b | 2003-12-23 02:17:35 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
drh | 3f28070 | 2010-02-18 18:45:09 +0000 | [diff] [blame] | 1224 | /* |
drh | 7c0c460 | 2010-03-03 22:25:18 +0000 | [diff] [blame] | 1225 | ** This is the routine that actually formats the sqlite3_log() message. |
| 1226 | ** We house it in a separate routine from sqlite3_log() to avoid using |
| 1227 | ** stack space on small-stack systems when logging is disabled. |
| 1228 | ** |
| 1229 | ** sqlite3_log() must render into a static buffer. It cannot dynamically |
| 1230 | ** allocate memory because it might be called while the memory allocator |
| 1231 | ** mutex is held. |
drh | a8dbd52 | 2015-07-14 22:43:37 +0000 | [diff] [blame] | 1232 | ** |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1233 | ** sqlite3_str_vappendf() might ask for *temporary* memory allocations for |
drh | a8dbd52 | 2015-07-14 22:43:37 +0000 | [diff] [blame] | 1234 | ** certain format characters (%q) or for very large precisions or widths. |
| 1235 | ** Care must be taken that any sqlite3_log() calls that occur while the |
| 1236 | ** memory mutex is held do not use these mechanisms. |
drh | 7c0c460 | 2010-03-03 22:25:18 +0000 | [diff] [blame] | 1237 | */ |
| 1238 | static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ |
drh | a64fa91 | 2010-03-04 00:53:32 +0000 | [diff] [blame] | 1239 | StrAccum acc; /* String accumulator */ |
| 1240 | char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ |
drh | 7c0c460 | 2010-03-03 22:25:18 +0000 | [diff] [blame] | 1241 | |
drh | c049057 | 2015-05-02 11:45:53 +0000 | [diff] [blame] | 1242 | sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1243 | sqlite3_str_vappendf(&acc, zFormat, ap); |
drh | 7c0c460 | 2010-03-03 22:25:18 +0000 | [diff] [blame] | 1244 | sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, |
| 1245 | sqlite3StrAccumFinish(&acc)); |
| 1246 | } |
| 1247 | |
| 1248 | /* |
drh | 3f28070 | 2010-02-18 18:45:09 +0000 | [diff] [blame] | 1249 | ** Format and write a message to the log if logging is enabled. |
| 1250 | */ |
drh | a756466 | 2010-02-22 19:32:31 +0000 | [diff] [blame] | 1251 | void sqlite3_log(int iErrCode, const char *zFormat, ...){ |
drh | 3f28070 | 2010-02-18 18:45:09 +0000 | [diff] [blame] | 1252 | va_list ap; /* Vararg list */ |
drh | 7c0c460 | 2010-03-03 22:25:18 +0000 | [diff] [blame] | 1253 | if( sqlite3GlobalConfig.xLog ){ |
drh | 3f28070 | 2010-02-18 18:45:09 +0000 | [diff] [blame] | 1254 | va_start(ap, zFormat); |
drh | 7c0c460 | 2010-03-03 22:25:18 +0000 | [diff] [blame] | 1255 | renderLogMsg(iErrCode, zFormat, ap); |
drh | 3f28070 | 2010-02-18 18:45:09 +0000 | [diff] [blame] | 1256 | va_end(ap); |
drh | 3f28070 | 2010-02-18 18:45:09 +0000 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | |
mistachkin | 02b0e26 | 2015-04-16 03:37:19 +0000 | [diff] [blame] | 1260 | #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 1261 | /* |
| 1262 | ** A version of printf() that understands %lld. Used for debugging. |
| 1263 | ** The printf() built into some versions of windows does not understand %lld |
| 1264 | ** and segfaults if you give it a long long int. |
| 1265 | */ |
| 1266 | void sqlite3DebugPrintf(const char *zFormat, ...){ |
| 1267 | va_list ap; |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1268 | StrAccum acc; |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 1269 | char zBuf[500]; |
drh | c049057 | 2015-05-02 11:45:53 +0000 | [diff] [blame] | 1270 | sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 1271 | va_start(ap,zFormat); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1272 | sqlite3_str_vappendf(&acc, zFormat, ap); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 1273 | va_end(ap); |
drh | bed8e7e | 2007-12-08 17:55:35 +0000 | [diff] [blame] | 1274 | sqlite3StrAccumFinish(&acc); |
mistachkin | 4a9ff91 | 2017-11-09 17:29:04 +0000 | [diff] [blame] | 1275 | #ifdef SQLITE_OS_TRACE_PROC |
| 1276 | { |
| 1277 | extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf); |
| 1278 | SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf)); |
| 1279 | } |
| 1280 | #else |
drh | 485f003 | 2007-01-26 19:23:33 +0000 | [diff] [blame] | 1281 | fprintf(stdout,"%s", zBuf); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1282 | fflush(stdout); |
mistachkin | 4a9ff91 | 2017-11-09 17:29:04 +0000 | [diff] [blame] | 1283 | #endif |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 1284 | } |
| 1285 | #endif |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 1286 | |
drh | 4fa4a54 | 2014-09-30 12:33:33 +0000 | [diff] [blame] | 1287 | |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 1288 | /* |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1289 | ** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument |
drh | d37bea5 | 2015-09-02 15:37:50 +0000 | [diff] [blame] | 1290 | ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats. |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 1291 | */ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1292 | void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){ |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 1293 | va_list ap; |
| 1294 | va_start(ap,zFormat); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 1295 | sqlite3_str_vappendf(p, zFormat, ap); |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 1296 | va_end(ap); |
| 1297 | } |