drh | a18c568 | 2000-10-08 22:20:57 | [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 | [diff] [blame] | 3 | ** the public domain. |
drh | e84a306 | 2004-02-02 12:29:25 | [diff] [blame] | 4 | ** |
| 5 | ************************************************************************** |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 6 | ** |
drh | ed1fddf | 2011-10-12 18:52:59 | [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 | [diff] [blame] | 10 | ** SQLite. |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 11 | */ |
| 12 | #include "sqliteInt.h" |
drh | 7c68d60 | 2000-10-11 19:28:51 | [diff] [blame] | 13 | |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 14 | /* |
drh | a18c568 | 2000-10-08 22:20:57 | [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 | [diff] [blame] | 18 | #define etRADIX 0 /* non-decimal integer types. %x %o */ |
drh | ad5a9d7 | 2016-05-05 11:53:12 | [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 | [diff] [blame] | 27 | /* The rest are extensions, not normally found in printf() */ |
drh | a3283ec | 2025-02-23 11:48:07 | [diff] [blame] | 28 | #define etESCAPE_q 9 /* Strings with '\'' doubled. %q */ |
| 29 | #define etESCAPE_Q 10 /* Strings with '\'' doubled and enclosed in '', |
| 30 | NULL pointers replaced by SQL NULL. %Q */ |
| 31 | #define etTOKEN 11 /* a pointer to a Token structure */ |
| 32 | #define etSRCITEM 12 /* a pointer to a SrcItem */ |
| 33 | #define etPOINTER 13 /* The %p conversion */ |
| 34 | #define etESCAPE_w 14 /* %w -> Strings with '\"' doubled */ |
| 35 | #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ |
| 36 | #define etDECIMAL 16 /* %d or %u, but not %x, %o */ |
drh | e84a306 | 2004-02-02 12:29:25 | [diff] [blame] | 37 | |
drh | a3283ec | 2025-02-23 11:48:07 | [diff] [blame] | 38 | #define etINVALID 17 /* Any unrecognized conversion type */ |
drh | 874ba04 | 2009-04-08 16:10:04 | [diff] [blame] | 39 | |
drh | e84a306 | 2004-02-02 12:29:25 | [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 | [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 | [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 | [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 | [diff] [blame] | 57 | } et_info; |
| 58 | |
| 59 | /* |
drh | e84a306 | 2004-02-02 12:29:25 | [diff] [blame] | 60 | ** Allowed values for et_info.flags |
| 61 | */ |
drh | 2c338a9 | 2017-02-10 19:38:36 | [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 | [diff] [blame] | 64 | |
| 65 | |
| 66 | /* |
drh | a18c568 | 2000-10-08 22:20:57 | [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 | [diff] [blame] | 70 | static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; |
| 71 | static const char aPrefix[] = "-x0\000X0"; |
drh | 5719628 | 2004-10-06 15:41:16 | [diff] [blame] | 72 | static const et_info fmtinfo[] = { |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 73 | { 'd', 10, 1, etDECIMAL, 0, 0 }, |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 74 | { 's', 0, 4, etSTRING, 0, 0 }, |
drh | 557cc60 | 2005-08-13 12:59:14 | [diff] [blame] | 75 | { 'g', 0, 1, etGENERIC, 30, 0 }, |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 76 | { 'z', 0, 4, etDYNSTRING, 0, 0 }, |
drh | a3283ec | 2025-02-23 11:48:07 | [diff] [blame] | 77 | { 'q', 0, 4, etESCAPE_q, 0, 0 }, |
| 78 | { 'Q', 0, 4, etESCAPE_Q, 0, 0 }, |
| 79 | { 'w', 0, 4, etESCAPE_w, 0, 0 }, |
drh | 76ff3a0 | 2004-09-24 22:32:30 | [diff] [blame] | 80 | { 'c', 0, 0, etCHARX, 0, 0 }, |
| 81 | { 'o', 8, 0, etRADIX, 0, 2 }, |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 82 | { 'u', 10, 0, etDECIMAL, 0, 0 }, |
drh | 76ff3a0 | 2004-09-24 22:32:30 | [diff] [blame] | 83 | { 'x', 16, 0, etRADIX, 16, 1 }, |
| 84 | { 'X', 16, 0, etRADIX, 0, 4 }, |
drh | b37df7b | 2005-10-13 02:09:49 | [diff] [blame] | 85 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 76ff3a0 | 2004-09-24 22:32:30 | [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 | [diff] [blame] | 89 | { 'G', 0, 1, etGENERIC, 14, 0 }, |
drh | b37df7b | 2005-10-13 02:09:49 | [diff] [blame] | 90 | #endif |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 91 | { 'i', 10, 1, etDECIMAL, 0, 0 }, |
drh | 76ff3a0 | 2004-09-24 22:32:30 | [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 | [diff] [blame] | 95 | |
drh | 8236f68 | 2017-01-04 00:26:28 | [diff] [blame] | 96 | /* All the rest are undocumented and are for internal use only */ |
| 97 | { 'T', 0, 0, etTOKEN, 0, 0 }, |
drh | a979993 | 2021-03-19 13:00:28 | [diff] [blame] | 98 | { 'S', 0, 0, etSRCITEM, 0, 0 }, |
drh | 8236f68 | 2017-01-04 00:26:28 | [diff] [blame] | 99 | { 'r', 10, 1, etORDINAL, 0, 0 }, |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 100 | }; |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 101 | |
drh | a979993 | 2021-03-19 13:00:28 | [diff] [blame] | 102 | /* Notes: |
| 103 | ** |
| 104 | ** %S Takes a pointer to SrcItem. Shows name or database.name |
drh | 2f2091b | 2021-03-20 15:46:01 | [diff] [blame] | 105 | ** %!S Like %S but prefer the zName over the zAlias |
drh | a979993 | 2021-03-19 13:00:28 | [diff] [blame] | 106 | */ |
| 107 | |
drh | 79158e1 | 2005-09-06 21:40:45 | [diff] [blame] | 108 | /* |
drh | a6353a3 | 2013-12-09 19:03:26 | [diff] [blame] | 109 | ** Set the StrAccum object to an error mode. |
| 110 | */ |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 111 | void sqlite3StrAccumSetError(StrAccum *p, u8 eError){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 112 | assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG ); |
drh | a6353a3 | 2013-12-09 19:03:26 | [diff] [blame] | 113 | p->accError = eError; |
drh | 255a81f | 2019-02-22 15:42:10 | [diff] [blame] | 114 | if( p->mxAlloc ) sqlite3_str_reset(p); |
drh | c3dcdba | 2019-04-09 21:32:46 | [diff] [blame] | 115 | if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError); |
drh | a6353a3 | 2013-12-09 19:03:26 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 119 | ** Extra argument values from a PrintfArguments object |
| 120 | */ |
| 121 | static sqlite3_int64 getIntArg(PrintfArguments *p){ |
| 122 | if( p->nArg<=p->nUsed ) return 0; |
| 123 | return sqlite3_value_int64(p->apArg[p->nUsed++]); |
| 124 | } |
| 125 | static double getDoubleArg(PrintfArguments *p){ |
| 126 | if( p->nArg<=p->nUsed ) return 0.0; |
| 127 | return sqlite3_value_double(p->apArg[p->nUsed++]); |
| 128 | } |
| 129 | static char *getTextArg(PrintfArguments *p){ |
| 130 | if( p->nArg<=p->nUsed ) return 0; |
| 131 | return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); |
| 132 | } |
| 133 | |
drh | 2964225 | 2019-02-01 20:29:04 | [diff] [blame] | 134 | /* |
| 135 | ** Allocate memory for a temporary buffer needed for printf rendering. |
| 136 | ** |
| 137 | ** If the requested size of the temp buffer is larger than the size |
| 138 | ** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error. |
| 139 | ** Do the size check before the memory allocation to prevent rogue |
| 140 | ** SQL from requesting large allocations using the precision or width |
| 141 | ** field of the printf() function. |
| 142 | */ |
| 143 | static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){ |
| 144 | char *z; |
drh | 255a81f | 2019-02-22 15:42:10 | [diff] [blame] | 145 | if( pAccum->accError ) return 0; |
drh | 2964225 | 2019-02-01 20:29:04 | [diff] [blame] | 146 | if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){ |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 147 | sqlite3StrAccumSetError(pAccum, SQLITE_TOOBIG); |
drh | 2964225 | 2019-02-01 20:29:04 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | z = sqlite3DbMallocRaw(pAccum->db, n); |
| 151 | if( z==0 ){ |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 152 | sqlite3StrAccumSetError(pAccum, SQLITE_NOMEM); |
drh | 2964225 | 2019-02-01 20:29:04 | [diff] [blame] | 153 | } |
| 154 | return z; |
| 155 | } |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 156 | |
| 157 | /* |
drh | 79158e1 | 2005-09-06 21:40:45 | [diff] [blame] | 158 | ** On machines with a small stack size, you can redefine the |
drh | ed1fddf | 2011-10-12 18:52:59 | [diff] [blame] | 159 | ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired. |
drh | 79158e1 | 2005-09-06 21:40:45 | [diff] [blame] | 160 | */ |
| 161 | #ifndef SQLITE_PRINT_BUF_SIZE |
drh | 59eedf7 | 2011-10-11 17:54:54 | [diff] [blame] | 162 | # define SQLITE_PRINT_BUF_SIZE 70 |
drh | 79158e1 | 2005-09-06 21:40:45 | [diff] [blame] | 163 | #endif |
| 164 | #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 165 | |
| 166 | /* |
drh | dd6c33d | 2020-05-23 19:58:07 | [diff] [blame] | 167 | ** Hard limit on the precision of floating-point conversions. |
| 168 | */ |
| 169 | #ifndef SQLITE_PRINTF_PRECISION_LIMIT |
| 170 | # define SQLITE_FP_PRECISION_LIMIT 100000000 |
| 171 | #endif |
| 172 | |
| 173 | /* |
drh | ed1fddf | 2011-10-12 18:52:59 | [diff] [blame] | 174 | ** Render a string given by "fmt" into the StrAccum object. |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 175 | */ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 176 | void sqlite3_str_vappendf( |
| 177 | sqlite3_str *pAccum, /* Accumulate results here */ |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 178 | const char *fmt, /* Format string */ |
| 179 | va_list ap /* arguments */ |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 180 | ){ |
drh | e84a306 | 2004-02-02 12:29:25 | [diff] [blame] | 181 | int c; /* Next character in the format string */ |
| 182 | char *bufpt; /* Pointer to the conversion buffer */ |
| 183 | int precision; /* Precision of the current field */ |
| 184 | int length; /* Length of the field */ |
| 185 | int idx; /* A general purpose loop counter */ |
drh | e84a306 | 2004-02-02 12:29:25 | [diff] [blame] | 186 | int width; /* Width of the current field */ |
| 187 | etByte flag_leftjustify; /* True if "-" flag is present */ |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 188 | etByte flag_prefix; /* '+' or ' ' or 0 for prefix */ |
drh | e84a306 | 2004-02-02 12:29:25 | [diff] [blame] | 189 | etByte flag_alternateform; /* True if "#" flag is present */ |
drh | 531fe87 | 2005-08-13 13:40:42 | [diff] [blame] | 190 | etByte flag_altform2; /* True if "!" flag is present */ |
drh | e84a306 | 2004-02-02 12:29:25 | [diff] [blame] | 191 | etByte flag_zeropad; /* True if field width constant starts with zero */ |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 192 | etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */ |
drh | 3e9aeec | 2005-08-13 13:39:02 | [diff] [blame] | 193 | etByte done; /* Loop termination flag */ |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 194 | etByte cThousand; /* Thousands separator for %d and %u */ |
drh | ad5a9d7 | 2016-05-05 11:53:12 | [diff] [blame] | 195 | etByte xtype = etINVALID; /* Conversion paradigm */ |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 196 | u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ |
drh | ed1fddf | 2011-10-12 18:52:59 | [diff] [blame] | 197 | char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ |
drh | 27436af | 2006-03-28 23:57:17 | [diff] [blame] | 198 | sqlite_uint64 longvalue; /* Value for integer types */ |
drh | aebeaba | 2023-06-30 23:18:44 | [diff] [blame] | 199 | double realvalue; /* Value for real types */ |
drh | 5719628 | 2004-10-06 15:41:16 | [diff] [blame] | 200 | const et_info *infop; /* Pointer to the appropriate info structure */ |
drh | 59eedf7 | 2011-10-11 17:54:54 | [diff] [blame] | 201 | char *zOut; /* Rendering buffer */ |
| 202 | int nOut; /* Size of the rendering buffer */ |
drh | af8f513 | 2014-10-29 18:20:18 | [diff] [blame] | 203 | char *zExtra = 0; /* Malloced memory used by some conversion */ |
drh | aebeaba | 2023-06-30 23:18:44 | [diff] [blame] | 204 | int exp, e2; /* exponent of real numbers */ |
drh | e84a306 | 2004-02-02 12:29:25 | [diff] [blame] | 205 | etByte flag_dp; /* True if decimal point should be shown */ |
| 206 | etByte flag_rtz; /* True if trailing zeros should be removed */ |
drh | aebeaba | 2023-06-30 23:18:44 | [diff] [blame] | 207 | |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 208 | PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ |
drh | ed1fddf | 2011-10-12 18:52:59 | [diff] [blame] | 209 | char buf[etBUFSIZE]; /* Conversion buffer */ |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 210 | |
drh | cc39896 | 2018-02-20 15:23:37 | [diff] [blame] | 211 | /* pAccum never starts out with an empty buffer that was obtained from |
| 212 | ** malloc(). This precondition is required by the mprintf("%z...") |
| 213 | ** optimization. */ |
| 214 | assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); |
| 215 | |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 216 | bufpt = 0; |
drh | 8236f68 | 2017-01-04 00:26:28 | [diff] [blame] | 217 | if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){ |
| 218 | pArgList = va_arg(ap, PrintfArguments*); |
| 219 | bArgList = 1; |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 220 | }else{ |
drh | 8236f68 | 2017-01-04 00:26:28 | [diff] [blame] | 221 | bArgList = 0; |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 222 | } |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 223 | for(; (c=(*fmt))!=0; ++fmt){ |
| 224 | if( c!='%' ){ |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 225 | bufpt = (char *)fmt; |
drh | 760b159 | 2014-09-18 01:50:09 | [diff] [blame] | 226 | #if HAVE_STRCHRNUL |
| 227 | fmt = strchrnul(fmt, '%'); |
| 228 | #else |
| 229 | do{ fmt++; }while( *fmt && *fmt != '%' ); |
| 230 | #endif |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 231 | sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt)); |
drh | 760b159 | 2014-09-18 01:50:09 | [diff] [blame] | 232 | if( *fmt==0 ) break; |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 233 | } |
| 234 | if( (c=(*++fmt))==0 ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 235 | sqlite3_str_append(pAccum, "%", 1); |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 236 | break; |
| 237 | } |
| 238 | /* Find out what flags are present */ |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 239 | flag_leftjustify = flag_prefix = cThousand = |
drh | 557cc60 | 2005-08-13 12:59:14 | [diff] [blame] | 240 | flag_alternateform = flag_altform2 = flag_zeropad = 0; |
drh | 3e9aeec | 2005-08-13 13:39:02 | [diff] [blame] | 241 | done = 0; |
drh | 9a6d01b | 2019-02-01 18:46:41 | [diff] [blame] | 242 | width = 0; |
| 243 | flag_long = 0; |
| 244 | precision = -1; |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 245 | do{ |
| 246 | switch( c ){ |
drh | 3e9aeec | 2005-08-13 13:39:02 | [diff] [blame] | 247 | case '-': flag_leftjustify = 1; break; |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 248 | case '+': flag_prefix = '+'; break; |
| 249 | case ' ': flag_prefix = ' '; break; |
drh | 3e9aeec | 2005-08-13 13:39:02 | [diff] [blame] | 250 | case '#': flag_alternateform = 1; break; |
| 251 | case '!': flag_altform2 = 1; break; |
| 252 | case '0': flag_zeropad = 1; break; |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 253 | case ',': cThousand = ','; break; |
drh | 3e9aeec | 2005-08-13 13:39:02 | [diff] [blame] | 254 | default: done = 1; break; |
drh | 9a6d01b | 2019-02-01 18:46:41 | [diff] [blame] | 255 | case 'l': { |
| 256 | flag_long = 1; |
| 257 | c = *++fmt; |
| 258 | if( c=='l' ){ |
| 259 | c = *++fmt; |
| 260 | flag_long = 2; |
| 261 | } |
| 262 | done = 1; |
| 263 | break; |
| 264 | } |
| 265 | case '1': case '2': case '3': case '4': case '5': |
| 266 | case '6': case '7': case '8': case '9': { |
| 267 | unsigned wx = c - '0'; |
| 268 | while( (c = *++fmt)>='0' && c<='9' ){ |
| 269 | wx = wx*10 + c - '0'; |
| 270 | } |
| 271 | testcase( wx>0x7fffffff ); |
| 272 | width = wx & 0x7fffffff; |
| 273 | #ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 274 | if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ |
| 275 | width = SQLITE_PRINTF_PRECISION_LIMIT; |
| 276 | } |
| 277 | #endif |
| 278 | if( c!='.' && c!='l' ){ |
| 279 | done = 1; |
| 280 | }else{ |
| 281 | fmt--; |
| 282 | } |
| 283 | break; |
| 284 | } |
| 285 | case '*': { |
| 286 | if( bArgList ){ |
| 287 | width = (int)getIntArg(pArgList); |
| 288 | }else{ |
| 289 | width = va_arg(ap,int); |
| 290 | } |
| 291 | if( width<0 ){ |
| 292 | flag_leftjustify = 1; |
| 293 | width = width >= -2147483647 ? -width : 0; |
| 294 | } |
| 295 | #ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 296 | if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ |
| 297 | width = SQLITE_PRINTF_PRECISION_LIMIT; |
| 298 | } |
| 299 | #endif |
| 300 | if( (c = fmt[1])!='.' && c!='l' ){ |
| 301 | c = *++fmt; |
| 302 | done = 1; |
| 303 | } |
| 304 | break; |
| 305 | } |
| 306 | case '.': { |
| 307 | c = *++fmt; |
| 308 | if( c=='*' ){ |
| 309 | if( bArgList ){ |
| 310 | precision = (int)getIntArg(pArgList); |
| 311 | }else{ |
| 312 | precision = va_arg(ap,int); |
| 313 | } |
| 314 | if( precision<0 ){ |
| 315 | precision = precision >= -2147483647 ? -precision : -1; |
| 316 | } |
| 317 | c = *++fmt; |
| 318 | }else{ |
| 319 | unsigned px = 0; |
| 320 | while( c>='0' && c<='9' ){ |
| 321 | px = px*10 + c - '0'; |
| 322 | c = *++fmt; |
| 323 | } |
| 324 | testcase( px>0x7fffffff ); |
| 325 | precision = px & 0x7fffffff; |
| 326 | } |
| 327 | #ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 328 | if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){ |
| 329 | precision = SQLITE_PRINTF_PRECISION_LIMIT; |
| 330 | } |
| 331 | #endif |
| 332 | if( c=='l' ){ |
| 333 | --fmt; |
| 334 | }else{ |
| 335 | done = 1; |
| 336 | } |
| 337 | break; |
| 338 | } |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 339 | } |
drh | 3e9aeec | 2005-08-13 13:39:02 | [diff] [blame] | 340 | }while( !done && (c=(*++fmt))!=0 ); |
dan | 8c06914 | 2015-04-07 14:38:57 | [diff] [blame] | 341 | |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 342 | /* Fetch the info entry for the field */ |
drh | 874ba04 | 2009-04-08 16:10:04 | [diff] [blame] | 343 | infop = &fmtinfo[0]; |
| 344 | xtype = etINVALID; |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 | [diff] [blame] | 345 | for(idx=0; idx<ArraySize(fmtinfo); idx++){ |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 346 | if( c==fmtinfo[idx].fmttype ){ |
| 347 | infop = &fmtinfo[idx]; |
drh | 8236f68 | 2017-01-04 00:26:28 | [diff] [blame] | 348 | xtype = infop->type; |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 349 | break; |
| 350 | } |
| 351 | } |
drh | 43617e9 | 2006-03-06 20:55:46 | [diff] [blame] | 352 | |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 353 | /* |
| 354 | ** At this point, variables are initialized as follows: |
| 355 | ** |
| 356 | ** flag_alternateform TRUE if a '#' is present. |
drh | 3e9aeec | 2005-08-13 13:39:02 | [diff] [blame] | 357 | ** flag_altform2 TRUE if a '!' is present. |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 358 | ** flag_prefix '+' or ' ' or zero |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 359 | ** flag_leftjustify TRUE if a '-' is present or if the |
| 360 | ** field width was negative. |
| 361 | ** flag_zeropad TRUE if the width began with 0. |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 362 | ** flag_long 1 for "l", 2 for "ll" |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 363 | ** width The specified field width. This is |
| 364 | ** always non-negative. Zero is the default. |
| 365 | ** precision The specified precision. The default |
| 366 | ** is -1. |
| 367 | ** xtype The class of the conversion. |
| 368 | ** infop Pointer to the appropriate info struct. |
| 369 | */ |
drh | b6907e2 | 2020-05-25 15:41:03 | [diff] [blame] | 370 | assert( width>=0 ); |
| 371 | assert( precision>=(-1) ); |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 372 | switch( xtype ){ |
drh | fe63d1c | 2004-09-08 20:13:04 | [diff] [blame] | 373 | case etPOINTER: |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 374 | flag_long = sizeof(char*)==sizeof(i64) ? 2 : |
| 375 | sizeof(char*)==sizeof(long int) ? 1 : 0; |
drh | 08b9208 | 2020-08-10 14:18:00 | [diff] [blame] | 376 | /* no break */ deliberate_fall_through |
drh | 9a99334 | 2007-12-13 02:45:31 | [diff] [blame] | 377 | case etORDINAL: |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 378 | case etRADIX: |
| 379 | cThousand = 0; |
drh | 08b9208 | 2020-08-10 14:18:00 | [diff] [blame] | 380 | /* no break */ deliberate_fall_through |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 381 | case etDECIMAL: |
drh | e84a306 | 2004-02-02 12:29:25 | [diff] [blame] | 382 | if( infop->flags & FLAG_SIGNED ){ |
drh | e970767 | 2004-06-25 01:10:48 | [diff] [blame] | 383 | i64 v; |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 384 | if( bArgList ){ |
| 385 | v = getIntArg(pArgList); |
drh | eeb23a4 | 2009-05-04 20:20:16 | [diff] [blame] | 386 | }else if( flag_long ){ |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 387 | if( flag_long==2 ){ |
| 388 | v = va_arg(ap,i64) ; |
| 389 | }else{ |
| 390 | v = va_arg(ap,long int); |
| 391 | } |
drh | eeb23a4 | 2009-05-04 20:20:16 | [diff] [blame] | 392 | }else{ |
| 393 | v = va_arg(ap,int); |
| 394 | } |
drh | e970767 | 2004-06-25 01:10:48 | [diff] [blame] | 395 | if( v<0 ){ |
drh | e2678b9 | 2020-08-28 12:58:21 | [diff] [blame] | 396 | testcase( v==SMALLEST_INT64 ); |
| 397 | testcase( v==(-1) ); |
| 398 | longvalue = ~v; |
| 399 | longvalue++; |
drh | e970767 | 2004-06-25 01:10:48 | [diff] [blame] | 400 | prefix = '-'; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 | [diff] [blame] | 401 | }else{ |
drh | e970767 | 2004-06-25 01:10:48 | [diff] [blame] | 402 | longvalue = v; |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 403 | prefix = flag_prefix; |
danielk1977 | cfcdaef | 2004-05-12 07:33:33 | [diff] [blame] | 404 | } |
drh | e970767 | 2004-06-25 01:10:48 | [diff] [blame] | 405 | }else{ |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 406 | if( bArgList ){ |
| 407 | longvalue = (u64)getIntArg(pArgList); |
drh | eeb23a4 | 2009-05-04 20:20:16 | [diff] [blame] | 408 | }else if( flag_long ){ |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 409 | if( flag_long==2 ){ |
| 410 | longvalue = va_arg(ap,u64); |
| 411 | }else{ |
| 412 | longvalue = va_arg(ap,unsigned long int); |
| 413 | } |
drh | eeb23a4 | 2009-05-04 20:20:16 | [diff] [blame] | 414 | }else{ |
| 415 | longvalue = va_arg(ap,unsigned int); |
| 416 | } |
drh | e970767 | 2004-06-25 01:10:48 | [diff] [blame] | 417 | prefix = 0; |
| 418 | } |
drh | 67f7082 | 2025-06-16 13:51:09 | [diff] [blame] | 419 | |
drh | 6ed5aa4 | 2025-06-16 15:34:26 | [diff] [blame] | 420 | #if WHERETRACE_ENABLED |
| 421 | if( xtype==etPOINTER && sqlite3WhereTrace & 0x100000 ) longvalue = 0; |
| 422 | #endif |
| 423 | #if TREETRACE_ENABLED |
| 424 | if( xtype==etPOINTER && sqlite3TreeTrace & 0x100000 ) longvalue = 0; |
drh | 67f7082 | 2025-06-16 13:51:09 | [diff] [blame] | 425 | #endif |
| 426 | |
drh | e970767 | 2004-06-25 01:10:48 | [diff] [blame] | 427 | if( longvalue==0 ) flag_alternateform = 0; |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 428 | if( flag_zeropad && precision<width-(prefix!=0) ){ |
| 429 | precision = width-(prefix!=0); |
drh | 9adf9ac | 2002-05-15 11:44:13 | [diff] [blame] | 430 | } |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 431 | if( precision<etBUFSIZE-10-etBUFSIZE/3 ){ |
drh | 59eedf7 | 2011-10-11 17:54:54 | [diff] [blame] | 432 | nOut = etBUFSIZE; |
| 433 | zOut = buf; |
| 434 | }else{ |
drh | 7ba03ea | 2019-02-01 21:08:27 | [diff] [blame] | 435 | u64 n; |
| 436 | n = (u64)precision + 10; |
| 437 | if( cThousand ) n += precision/3; |
drh | 2964225 | 2019-02-01 20:29:04 | [diff] [blame] | 438 | zOut = zExtra = printfTempBuf(pAccum, n); |
| 439 | if( zOut==0 ) return; |
drh | 5f42995 | 2017-03-20 16:34:18 | [diff] [blame] | 440 | nOut = (int)n; |
drh | 59eedf7 | 2011-10-11 17:54:54 | [diff] [blame] | 441 | } |
| 442 | bufpt = &zOut[nOut-1]; |
drh | 9a99334 | 2007-12-13 02:45:31 | [diff] [blame] | 443 | if( xtype==etORDINAL ){ |
drh | 43f6e06 | 2007-12-13 17:50:22 | [diff] [blame] | 444 | static const char zOrd[] = "thstndrd"; |
drh | ea67883 | 2008-12-10 19:26:22 | [diff] [blame] | 445 | int x = (int)(longvalue % 10); |
drh | 43f6e06 | 2007-12-13 17:50:22 | [diff] [blame] | 446 | if( x>=4 || (longvalue/10)%10==1 ){ |
| 447 | x = 0; |
| 448 | } |
drh | 59eedf7 | 2011-10-11 17:54:54 | [diff] [blame] | 449 | *(--bufpt) = zOrd[x*2+1]; |
| 450 | *(--bufpt) = zOrd[x*2]; |
drh | 9a99334 | 2007-12-13 02:45:31 | [diff] [blame] | 451 | } |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 452 | { |
drh | 0e68209 | 2014-03-17 15:06:57 | [diff] [blame] | 453 | const char *cset = &aDigits[infop->charset]; |
| 454 | u8 base = infop->base; |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 455 | do{ /* Convert to ascii */ |
| 456 | *(--bufpt) = cset[longvalue%base]; |
| 457 | longvalue = longvalue/base; |
| 458 | }while( longvalue>0 ); |
drh | 9adf9ac | 2002-05-15 11:44:13 | [diff] [blame] | 459 | } |
drh | 59eedf7 | 2011-10-11 17:54:54 | [diff] [blame] | 460 | length = (int)(&zOut[nOut-1]-bufpt); |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 461 | while( precision>length ){ |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 462 | *(--bufpt) = '0'; /* Zero pad */ |
drh | 2c338a9 | 2017-02-10 19:38:36 | [diff] [blame] | 463 | length++; |
| 464 | } |
| 465 | if( cThousand ){ |
| 466 | int nn = (length - 1)/3; /* Number of "," to insert */ |
| 467 | int ix = (length - 1)%3 + 1; |
| 468 | bufpt -= nn; |
| 469 | for(idx=0; nn>0; idx++){ |
| 470 | bufpt[idx] = bufpt[idx+nn]; |
| 471 | ix--; |
| 472 | if( ix==0 ){ |
| 473 | bufpt[++idx] = cThousand; |
| 474 | nn--; |
| 475 | ix = 3; |
| 476 | } |
| 477 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 | [diff] [blame] | 478 | } |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 479 | if( prefix ) *(--bufpt) = prefix; /* Add sign */ |
| 480 | if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ |
drh | 76ff3a0 | 2004-09-24 22:32:30 | [diff] [blame] | 481 | const char *pre; |
| 482 | char x; |
| 483 | pre = &aPrefix[infop->prefix]; |
drh | af005fb | 2008-07-09 16:51:51 | [diff] [blame] | 484 | for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 485 | } |
drh | 59eedf7 | 2011-10-11 17:54:54 | [diff] [blame] | 486 | length = (int)(&zOut[nOut-1]-bufpt); |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 487 | break; |
| 488 | case etFLOAT: |
| 489 | case etEXP: |
drh | aebeaba | 2023-06-30 23:18:44 | [diff] [blame] | 490 | case etGENERIC: { |
| 491 | FpDecode s; |
drh | 9ee9444 | 2023-07-01 15:23:24 | [diff] [blame] | 492 | int iRound; |
drh | 453be55 | 2023-07-01 18:33:26 | [diff] [blame] | 493 | int j; |
drh | aebeaba | 2023-06-30 23:18:44 | [diff] [blame] | 494 | |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 495 | if( bArgList ){ |
| 496 | realvalue = getDoubleArg(pArgList); |
| 497 | }else{ |
| 498 | realvalue = va_arg(ap,double); |
| 499 | } |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 500 | if( precision<0 ) precision = 6; /* Set default precision */ |
drh | dd6c33d | 2020-05-23 19:58:07 | [diff] [blame] | 501 | #ifdef SQLITE_FP_PRECISION_LIMIT |
| 502 | if( precision>SQLITE_FP_PRECISION_LIMIT ){ |
| 503 | precision = SQLITE_FP_PRECISION_LIMIT; |
| 504 | } |
| 505 | #endif |
drh | 9ee9444 | 2023-07-01 15:23:24 | [diff] [blame] | 506 | if( xtype==etFLOAT ){ |
| 507 | iRound = -precision; |
| 508 | }else if( xtype==etGENERIC ){ |
drh | 6161cdd | 2024-02-17 03:32:31 | [diff] [blame] | 509 | if( precision==0 ) precision = 1; |
drh | 9ee9444 | 2023-07-01 15:23:24 | [diff] [blame] | 510 | iRound = precision; |
| 511 | }else{ |
| 512 | iRound = precision+1; |
drh | 42d042e | 2023-07-01 14:03:50 | [diff] [blame] | 513 | } |
drh | 17c20bb | 2023-07-01 17:56:00 | [diff] [blame] | 514 | sqlite3FpDecode(&s, realvalue, iRound, flag_altform2 ? 26 : 16); |
drh | 9ee9444 | 2023-07-01 15:23:24 | [diff] [blame] | 515 | if( s.isSpecial ){ |
| 516 | if( s.isSpecial==2 ){ |
| 517 | bufpt = flag_zeropad ? "null" : "NaN"; |
| 518 | length = sqlite3Strlen30(bufpt); |
| 519 | break; |
| 520 | }else if( flag_zeropad ){ |
| 521 | s.z[0] = '9'; |
| 522 | s.iDP = 1000; |
| 523 | s.n = 1; |
drh | aebeaba | 2023-06-30 23:18:44 | [diff] [blame] | 524 | }else{ |
drh | 9ee9444 | 2023-07-01 15:23:24 | [diff] [blame] | 525 | memcpy(buf, "-Inf", 5); |
| 526 | bufpt = buf; |
| 527 | if( s.sign=='-' ){ |
| 528 | /* no-op */ |
| 529 | }else if( flag_prefix ){ |
| 530 | buf[0] = flag_prefix; |
| 531 | }else{ |
| 532 | bufpt++; |
| 533 | } |
| 534 | length = sqlite3Strlen30(bufpt); |
| 535 | break; |
drh | aebeaba | 2023-06-30 23:18:44 | [diff] [blame] | 536 | } |
drh | aebeaba | 2023-06-30 23:18:44 | [diff] [blame] | 537 | } |
drh | 42d042e | 2023-07-01 14:03:50 | [diff] [blame] | 538 | if( s.sign=='-' ){ |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 539 | prefix = '-'; |
drh | 9adf9ac | 2002-05-15 11:44:13 | [diff] [blame] | 540 | }else{ |
drh | 42d042e | 2023-07-01 14:03:50 | [diff] [blame] | 541 | prefix = flag_prefix; |
drh | 9adf9ac | 2002-05-15 11:44:13 | [diff] [blame] | 542 | } |
drh | 4df23a3 | 2023-02-23 21:18:47 | [diff] [blame] | 543 | |
drh | aebeaba | 2023-06-30 23:18:44 | [diff] [blame] | 544 | exp = s.iDP-1; |
drh | 4df23a3 | 2023-02-23 21:18:47 | [diff] [blame] | 545 | |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 546 | /* |
| 547 | ** If the field type is etGENERIC, then convert to either etEXP |
| 548 | ** or etFLOAT, as appropriate. |
| 549 | */ |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 550 | if( xtype==etGENERIC ){ |
drh | b468e68 | 2024-02-20 13:11:27 | [diff] [blame] | 551 | assert( precision>0 ); |
| 552 | precision--; |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 553 | flag_rtz = !flag_alternateform; |
| 554 | if( exp<-4 || exp>precision ){ |
| 555 | xtype = etEXP; |
| 556 | }else{ |
| 557 | precision = precision - exp; |
| 558 | xtype = etFLOAT; |
| 559 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 | [diff] [blame] | 560 | }else{ |
drh | 72b3fbc | 2012-06-19 03:11:25 | [diff] [blame] | 561 | flag_rtz = flag_altform2; |
drh | 9adf9ac | 2002-05-15 11:44:13 | [diff] [blame] | 562 | } |
drh | 557cc60 | 2005-08-13 12:59:14 | [diff] [blame] | 563 | if( xtype==etEXP ){ |
| 564 | e2 = 0; |
| 565 | }else{ |
drh | aebeaba | 2023-06-30 23:18:44 | [diff] [blame] | 566 | e2 = s.iDP - 1; |
drh | 557cc60 | 2005-08-13 12:59:14 | [diff] [blame] | 567 | } |
drh | 4df23a3 | 2023-02-23 21:18:47 | [diff] [blame] | 568 | bufpt = buf; |
drh | 2964225 | 2019-02-01 20:29:04 | [diff] [blame] | 569 | { |
| 570 | i64 szBufNeeded; /* Size of a temporary buffer needed */ |
| 571 | szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15; |
dan | 77eb3e3 | 2023-05-05 19:36:13 | [diff] [blame] | 572 | if( cThousand && e2>0 ) szBufNeeded += (e2+2)/3; |
drh | 2964225 | 2019-02-01 20:29:04 | [diff] [blame] | 573 | if( szBufNeeded > etBUFSIZE ){ |
| 574 | bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded); |
| 575 | if( bufpt==0 ) return; |
drh | 59eedf7 | 2011-10-11 17:54:54 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | zOut = bufpt; |
drh | ea67883 | 2008-12-10 19:26:22 | [diff] [blame] | 579 | flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; |
drh | 557cc60 | 2005-08-13 12:59:14 | [diff] [blame] | 580 | /* The sign in front of the number */ |
| 581 | if( prefix ){ |
| 582 | *(bufpt++) = prefix; |
| 583 | } |
| 584 | /* Digits prior to the decimal point */ |
drh | aebeaba | 2023-06-30 23:18:44 | [diff] [blame] | 585 | j = 0; |
drh | 557cc60 | 2005-08-13 12:59:14 | [diff] [blame] | 586 | if( e2<0 ){ |
| 587 | *(bufpt++) = '0'; |
| 588 | }else{ |
| 589 | for(; e2>=0; e2--){ |
drh | 42d042e | 2023-07-01 14:03:50 | [diff] [blame] | 590 | *(bufpt++) = j<s.n ? s.z[j++] : '0'; |
drh | e846809 | 2023-05-04 13:07:49 | [diff] [blame] | 591 | if( cThousand && (e2%3)==0 && e2>1 ) *(bufpt++) = ','; |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 592 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 | [diff] [blame] | 593 | } |
drh | 557cc60 | 2005-08-13 12:59:14 | [diff] [blame] | 594 | /* The decimal point */ |
| 595 | if( flag_dp ){ |
| 596 | *(bufpt++) = '.'; |
| 597 | } |
| 598 | /* "0" digits after the decimal point but before the first |
| 599 | ** significant digit of the number */ |
drh | aebeaba | 2023-06-30 23:18:44 | [diff] [blame] | 600 | for(e2++; e2<0 && precision>0; precision--, e2++){ |
drh | 557cc60 | 2005-08-13 12:59:14 | [diff] [blame] | 601 | *(bufpt++) = '0'; |
| 602 | } |
| 603 | /* Significant digits after the decimal point */ |
drh | aebeaba | 2023-06-30 23:18:44 | [diff] [blame] | 604 | while( (precision--)>0 ){ |
| 605 | *(bufpt++) = j<s.n ? s.z[j++] : '0'; |
drh | 557cc60 | 2005-08-13 12:59:14 | [diff] [blame] | 606 | } |
| 607 | /* Remove trailing zeros and the "." if no digits follow the "." */ |
| 608 | if( flag_rtz && flag_dp ){ |
drh | 3e9aeec | 2005-08-13 13:39:02 | [diff] [blame] | 609 | while( bufpt[-1]=='0' ) *(--bufpt) = 0; |
drh | 59eedf7 | 2011-10-11 17:54:54 | [diff] [blame] | 610 | assert( bufpt>zOut ); |
drh | 3e9aeec | 2005-08-13 13:39:02 | [diff] [blame] | 611 | if( bufpt[-1]=='.' ){ |
drh | 557cc60 | 2005-08-13 12:59:14 | [diff] [blame] | 612 | if( flag_altform2 ){ |
| 613 | *(bufpt++) = '0'; |
| 614 | }else{ |
| 615 | *(--bufpt) = 0; |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | /* Add the "eNNN" suffix */ |
drh | 59eedf7 | 2011-10-11 17:54:54 | [diff] [blame] | 620 | if( xtype==etEXP ){ |
drh | aebeaba | 2023-06-30 23:18:44 | [diff] [blame] | 621 | exp = s.iDP - 1; |
drh | 557cc60 | 2005-08-13 12:59:14 | [diff] [blame] | 622 | *(bufpt++) = aDigits[infop->charset]; |
| 623 | if( exp<0 ){ |
| 624 | *(bufpt++) = '-'; exp = -exp; |
| 625 | }else{ |
| 626 | *(bufpt++) = '+'; |
| 627 | } |
| 628 | if( exp>=100 ){ |
drh | ea67883 | 2008-12-10 19:26:22 | [diff] [blame] | 629 | *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ |
drh | 557cc60 | 2005-08-13 12:59:14 | [diff] [blame] | 630 | exp %= 100; |
| 631 | } |
drh | ea67883 | 2008-12-10 19:26:22 | [diff] [blame] | 632 | *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ |
| 633 | *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ |
drh | 557cc60 | 2005-08-13 12:59:14 | [diff] [blame] | 634 | } |
| 635 | *bufpt = 0; |
| 636 | |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 637 | /* The converted number is in buf[] and zero terminated. Output it. |
| 638 | ** Note that the number is in the usual order, not reversed as with |
| 639 | ** integer conversions. */ |
drh | 59eedf7 | 2011-10-11 17:54:54 | [diff] [blame] | 640 | length = (int)(bufpt-zOut); |
| 641 | bufpt = zOut; |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 642 | |
| 643 | /* Special case: Add leading zeros if the flag_zeropad flag is |
| 644 | ** set and we are not left justified */ |
| 645 | if( flag_zeropad && !flag_leftjustify && length < width){ |
| 646 | int i; |
| 647 | int nPad = width - length; |
| 648 | for(i=width; i>=nPad; i--){ |
| 649 | bufpt[i] = bufpt[i-nPad]; |
| 650 | } |
| 651 | i = prefix!=0; |
| 652 | while( nPad-- ) bufpt[i++] = '0'; |
| 653 | length = width; |
| 654 | } |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 655 | break; |
drh | aebeaba | 2023-06-30 23:18:44 | [diff] [blame] | 656 | } |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 657 | case etSIZE: |
drh | fc6ee9d | 2013-12-17 15:58:42 | [diff] [blame] | 658 | if( !bArgList ){ |
| 659 | *(va_arg(ap,int*)) = pAccum->nChar; |
| 660 | } |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 661 | length = width = 0; |
| 662 | break; |
| 663 | case etPERCENT: |
| 664 | buf[0] = '%'; |
| 665 | bufpt = buf; |
| 666 | length = 1; |
| 667 | break; |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 668 | case etCHARX: |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 669 | if( bArgList ){ |
drh | fc6ee9d | 2013-12-17 15:58:42 | [diff] [blame] | 670 | bufpt = getTextArg(pArgList); |
drh | a15a7c3 | 2018-02-19 21:58:16 | [diff] [blame] | 671 | length = 1; |
drh | 136102b | 2018-02-19 18:56:52 | [diff] [blame] | 672 | if( bufpt ){ |
| 673 | buf[0] = c = *(bufpt++); |
drh | 136102b | 2018-02-19 18:56:52 | [diff] [blame] | 674 | if( (c&0xc0)==0xc0 ){ |
| 675 | while( length<4 && (bufpt[0]&0xc0)==0x80 ){ |
| 676 | buf[length++] = *(bufpt++); |
| 677 | } |
| 678 | } |
drh | a15a7c3 | 2018-02-19 21:58:16 | [diff] [blame] | 679 | }else{ |
| 680 | buf[0] = 0; |
drh | 136102b | 2018-02-19 18:56:52 | [diff] [blame] | 681 | } |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 682 | }else{ |
drh | 136102b | 2018-02-19 18:56:52 | [diff] [blame] | 683 | unsigned int ch = va_arg(ap,unsigned int); |
drh | a357a90 | 2025-02-25 11:47:34 | [diff] [blame] | 684 | length = sqlite3AppendOneUtf8Character(buf, ch); |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 685 | } |
drh | af8f513 | 2014-10-29 18:20:18 | [diff] [blame] | 686 | if( precision>1 ){ |
drh | 79b9bc4 | 2022-12-21 19:11:56 | [diff] [blame] | 687 | i64 nPrior = 1; |
drh | af8f513 | 2014-10-29 18:20:18 | [diff] [blame] | 688 | width -= precision-1; |
| 689 | if( width>1 && !flag_leftjustify ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 690 | sqlite3_str_appendchar(pAccum, width-1, ' '); |
drh | af8f513 | 2014-10-29 18:20:18 | [diff] [blame] | 691 | width = 0; |
| 692 | } |
drh | 79b9bc4 | 2022-12-21 19:11:56 | [diff] [blame] | 693 | sqlite3_str_append(pAccum, buf, length); |
| 694 | precision--; |
| 695 | while( precision > 1 ){ |
| 696 | i64 nCopyBytes; |
| 697 | if( nPrior > precision-1 ) nPrior = precision - 1; |
| 698 | nCopyBytes = length*nPrior; |
| 699 | if( nCopyBytes + pAccum->nChar >= pAccum->nAlloc ){ |
| 700 | sqlite3StrAccumEnlarge(pAccum, nCopyBytes); |
| 701 | } |
| 702 | if( pAccum->accError ) break; |
| 703 | sqlite3_str_append(pAccum, |
| 704 | &pAccum->zText[pAccum->nChar-nCopyBytes], nCopyBytes); |
| 705 | precision -= nPrior; |
| 706 | nPrior *= 2; |
drh | 136102b | 2018-02-19 18:56:52 | [diff] [blame] | 707 | } |
drh | 9adf9ac | 2002-05-15 11:44:13 | [diff] [blame] | 708 | } |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 709 | bufpt = buf; |
drh | cf7c837 | 2018-02-19 20:23:20 | [diff] [blame] | 710 | flag_altform2 = 1; |
| 711 | goto adjust_width_for_utf8; |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 712 | case etSTRING: |
drh | d93d8a8 | 2003-06-16 03:08:18 | [diff] [blame] | 713 | case etDYNSTRING: |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 714 | if( bArgList ){ |
| 715 | bufpt = getTextArg(pArgList); |
drh | 2a8f671 | 2015-09-02 21:00:48 | [diff] [blame] | 716 | xtype = etSTRING; |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 717 | }else{ |
| 718 | bufpt = va_arg(ap,char*); |
| 719 | } |
drh | d93d8a8 | 2003-06-16 03:08:18 | [diff] [blame] | 720 | if( bufpt==0 ){ |
| 721 | bufpt = ""; |
drh | 2a8f671 | 2015-09-02 21:00:48 | [diff] [blame] | 722 | }else if( xtype==etDYNSTRING ){ |
drh | af524a6 | 2018-09-13 17:07:12 | [diff] [blame] | 723 | if( pAccum->nChar==0 |
| 724 | && pAccum->mxAlloc |
| 725 | && width==0 |
| 726 | && precision<0 |
| 727 | && pAccum->accError==0 |
| 728 | ){ |
drh | cc39896 | 2018-02-20 15:23:37 | [diff] [blame] | 729 | /* Special optimization for sqlite3_mprintf("%z..."): |
| 730 | ** Extend an existing memory allocation rather than creating |
| 731 | ** a new one. */ |
| 732 | assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); |
| 733 | pAccum->zText = bufpt; |
| 734 | pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt); |
| 735 | pAccum->nChar = 0x7fffffff & (int)strlen(bufpt); |
| 736 | pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED; |
| 737 | length = 0; |
| 738 | break; |
| 739 | } |
drh | d93d8a8 | 2003-06-16 03:08:18 | [diff] [blame] | 740 | zExtra = bufpt; |
| 741 | } |
drh | e509094 | 2008-04-29 15:22:27 | [diff] [blame] | 742 | if( precision>=0 ){ |
drh | 6285646 | 2018-02-19 17:03:23 | [diff] [blame] | 743 | if( flag_altform2 ){ |
| 744 | /* Set length to the number of bytes needed in order to display |
| 745 | ** precision characters */ |
| 746 | unsigned char *z = (unsigned char*)bufpt; |
| 747 | while( precision-- > 0 && z[0] ){ |
| 748 | SQLITE_SKIP_UTF8(z); |
| 749 | } |
| 750 | length = (int)(z - (unsigned char*)bufpt); |
| 751 | }else{ |
| 752 | for(length=0; length<precision && bufpt[length]; length++){} |
| 753 | } |
drh | e509094 | 2008-04-29 15:22:27 | [diff] [blame] | 754 | }else{ |
drh | c84ddf1 | 2017-08-19 20:38:18 | [diff] [blame] | 755 | length = 0x7fffffff & (int)strlen(bufpt); |
drh | e509094 | 2008-04-29 15:22:27 | [diff] [blame] | 756 | } |
drh | 57e3ba7 | 2018-02-19 18:03:10 | [diff] [blame] | 757 | adjust_width_for_utf8: |
drh | 6285646 | 2018-02-19 17:03:23 | [diff] [blame] | 758 | if( flag_altform2 && width>0 ){ |
| 759 | /* Adjust width to account for extra bytes in UTF-8 characters */ |
| 760 | int ii = length - 1; |
| 761 | while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++; |
| 762 | } |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 763 | break; |
drh | a3283ec | 2025-02-23 11:48:07 | [diff] [blame] | 764 | case etESCAPE_q: /* %q: Escape ' characters */ |
| 765 | case etESCAPE_Q: /* %Q: Escape ' and enclose in '...' */ |
| 766 | case etESCAPE_w: { /* %w: Escape " characters */ |
drh | 077e17b | 2022-07-18 15:02:00 | [diff] [blame] | 767 | i64 i, j, k, n; |
drh | d4c686e | 2025-02-25 12:18:27 | [diff] [blame] | 768 | int needQuote = 0; |
drh | ea67883 | 2008-12-10 19:26:22 | [diff] [blame] | 769 | char ch; |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 770 | char *escarg; |
drh | a3283ec | 2025-02-23 11:48:07 | [diff] [blame] | 771 | char q; |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 772 | |
| 773 | if( bArgList ){ |
| 774 | escarg = getTextArg(pArgList); |
| 775 | }else{ |
| 776 | escarg = va_arg(ap,char*); |
| 777 | } |
drh | d4c686e | 2025-02-25 12:18:27 | [diff] [blame] | 778 | if( escarg==0 ){ |
| 779 | escarg = (xtype==etESCAPE_Q ? "NULL" : "(NULL)"); |
| 780 | }else if( xtype==etESCAPE_Q ){ |
| 781 | needQuote = 1; |
| 782 | } |
drh | a3283ec | 2025-02-23 11:48:07 | [diff] [blame] | 783 | if( xtype==etESCAPE_w ){ |
| 784 | q = '"'; |
| 785 | flag_alternateform = 0; |
| 786 | }else{ |
| 787 | q = '\''; |
| 788 | } |
drh | b6907e2 | 2020-05-25 15:41:03 | [diff] [blame] | 789 | /* For %q, %Q, and %w, the precision is the number of bytes (or |
drh | 57e3ba7 | 2018-02-19 18:03:10 | [diff] [blame] | 790 | ** characters if the ! flags is present) to use from the input. |
| 791 | ** Because of the extra quoting characters inserted, the number |
| 792 | ** of output characters may be larger than the precision. |
| 793 | */ |
drh | 8965b50 | 2009-11-25 16:53:37 | [diff] [blame] | 794 | k = precision; |
dan | 60d4a30 | 2010-03-04 17:58:45 | [diff] [blame] | 795 | for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ |
danielk1977 | f3b863e | 2007-06-24 06:32:17 | [diff] [blame] | 796 | if( ch==q ) n++; |
drh | 57e3ba7 | 2018-02-19 18:03:10 | [diff] [blame] | 797 | if( flag_altform2 && (ch&0xc0)==0xc0 ){ |
| 798 | while( (escarg[i+1]&0xc0)==0x80 ){ i++; } |
| 799 | } |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 800 | } |
drh | 07ce182 | 2025-02-23 00:09:24 | [diff] [blame] | 801 | if( flag_alternateform ){ |
drh | a3283ec | 2025-02-23 11:48:07 | [diff] [blame] | 802 | /* For %#q, do unistr()-style backslash escapes for |
| 803 | ** all control characters, and for backslash itself. |
| 804 | ** For %#Q, do the same but only if there is at least |
| 805 | ** one control character. */ |
| 806 | u32 nBack = 0; |
| 807 | u32 nCtrl = 0; |
drh | 07ce182 | 2025-02-23 00:09:24 | [diff] [blame] | 808 | for(k=0; k<i; k++){ |
| 809 | if( escarg[k]=='\\' ){ |
drh | a3283ec | 2025-02-23 11:48:07 | [diff] [blame] | 810 | nBack++; |
drh | c171cf1 | 2025-03-02 20:29:49 | [diff] [blame] | 811 | }else if( ((u8*)escarg)[k]<=0x1f ){ |
drh | a3283ec | 2025-02-23 11:48:07 | [diff] [blame] | 812 | nCtrl++; |
drh | 07ce182 | 2025-02-23 00:09:24 | [diff] [blame] | 813 | } |
| 814 | } |
drh | a3283ec | 2025-02-23 11:48:07 | [diff] [blame] | 815 | if( nCtrl || xtype==etESCAPE_q ){ |
| 816 | n += nBack + 5*nCtrl; |
| 817 | if( xtype==etESCAPE_Q ){ |
| 818 | n += 10; |
| 819 | needQuote = 2; |
| 820 | } |
| 821 | }else{ |
| 822 | flag_alternateform = 0; |
| 823 | } |
drh | 07ce182 | 2025-02-23 00:09:24 | [diff] [blame] | 824 | } |
drh | 2a8f671 | 2015-09-02 21:00:48 | [diff] [blame] | 825 | n += i + 3; |
drh | 5eba8c0 | 2005-08-19 02:26:27 | [diff] [blame] | 826 | if( n>etBUFSIZE ){ |
drh | 2964225 | 2019-02-01 20:29:04 | [diff] [blame] | 827 | bufpt = zExtra = printfTempBuf(pAccum, n); |
| 828 | if( bufpt==0 ) return; |
drh | 5eba8c0 | 2005-08-19 02:26:27 | [diff] [blame] | 829 | }else{ |
| 830 | bufpt = buf; |
| 831 | } |
| 832 | j = 0; |
drh | a3283ec | 2025-02-23 11:48:07 | [diff] [blame] | 833 | if( needQuote ){ |
| 834 | if( needQuote==2 ){ |
| 835 | memcpy(&bufpt[j], "unistr('", 8); |
| 836 | j += 8; |
| 837 | }else{ |
| 838 | bufpt[j++] = '\''; |
| 839 | } |
| 840 | } |
drh | 8965b50 | 2009-11-25 16:53:37 | [diff] [blame] | 841 | k = i; |
drh | d4c686e | 2025-02-25 12:18:27 | [diff] [blame] | 842 | if( flag_alternateform ){ |
| 843 | for(i=0; i<k; i++){ |
| 844 | bufpt[j++] = ch = escarg[i]; |
| 845 | if( ch==q ){ |
| 846 | bufpt[j++] = ch; |
| 847 | }else if( ch=='\\' ){ |
drh | 07ce182 | 2025-02-23 00:09:24 | [diff] [blame] | 848 | bufpt[j++] = '\\'; |
drh | c171cf1 | 2025-03-02 20:29:49 | [diff] [blame] | 849 | }else if( ((unsigned char)ch)<=0x1f ){ |
drh | 07ce182 | 2025-02-23 00:09:24 | [diff] [blame] | 850 | bufpt[j-1] = '\\'; |
drh | a3283ec | 2025-02-23 11:48:07 | [diff] [blame] | 851 | bufpt[j++] = 'u'; |
drh | 07ce182 | 2025-02-23 00:09:24 | [diff] [blame] | 852 | bufpt[j++] = '0'; |
| 853 | bufpt[j++] = '0'; |
| 854 | bufpt[j++] = ch>=0x10 ? '1' : '0'; |
| 855 | bufpt[j++] = "0123456789abcdef"[ch&0xf]; |
| 856 | } |
| 857 | } |
drh | d4c686e | 2025-02-25 12:18:27 | [diff] [blame] | 858 | }else{ |
| 859 | for(i=0; i<k; i++){ |
| 860 | bufpt[j++] = ch = escarg[i]; |
| 861 | if( ch==q ) bufpt[j++] = ch; |
| 862 | } |
drh | 5eba8c0 | 2005-08-19 02:26:27 | [diff] [blame] | 863 | } |
drh | a3283ec | 2025-02-23 11:48:07 | [diff] [blame] | 864 | if( needQuote ){ |
| 865 | bufpt[j++] = '\''; |
| 866 | if( needQuote==2 ) bufpt[j++] = ')'; |
| 867 | } |
drh | 5eba8c0 | 2005-08-19 02:26:27 | [diff] [blame] | 868 | bufpt[j] = 0; |
| 869 | length = j; |
drh | 57e3ba7 | 2018-02-19 18:03:10 | [diff] [blame] | 870 | goto adjust_width_for_utf8; |
drh | 5eba8c0 | 2005-08-19 02:26:27 | [diff] [blame] | 871 | } |
drh | 5f96843 | 2004-02-21 19:02:30 | [diff] [blame] | 872 | case etTOKEN: { |
drh | 8236f68 | 2017-01-04 00:26:28 | [diff] [blame] | 873 | if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 874 | if( flag_alternateform ){ |
| 875 | /* %#T means an Expr pointer that uses Expr.u.zToken */ |
| 876 | Expr *pExpr = va_arg(ap,Expr*); |
drh | 5d20a21 | 2022-02-06 23:54:41 | [diff] [blame] | 877 | if( ALWAYS(pExpr) && ALWAYS(!ExprHasProperty(pExpr,EP_IntValue)) ){ |
drh | 62fc069 | 2022-02-06 00:30:04 | [diff] [blame] | 878 | sqlite3_str_appendall(pAccum, (const char*)pExpr->u.zToken); |
| 879 | sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr); |
| 880 | } |
| 881 | }else{ |
| 882 | /* %T means a Token pointer */ |
| 883 | Token *pToken = va_arg(ap, Token*); |
| 884 | assert( bArgList==0 ); |
| 885 | if( pToken && pToken->n ){ |
| 886 | sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n); |
| 887 | sqlite3RecordErrorByteOffset(pAccum->db, pToken->z); |
| 888 | } |
drh | ad6d946 | 2004-09-19 02:15:24 | [diff] [blame] | 889 | } |
drh | 5f96843 | 2004-02-21 19:02:30 | [diff] [blame] | 890 | length = width = 0; |
| 891 | break; |
| 892 | } |
drh | a979993 | 2021-03-19 13:00:28 | [diff] [blame] | 893 | case etSRCITEM: { |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 894 | SrcItem *pItem; |
drh | 8236f68 | 2017-01-04 00:26:28 | [diff] [blame] | 895 | if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; |
drh | a979993 | 2021-03-19 13:00:28 | [diff] [blame] | 896 | pItem = va_arg(ap, SrcItem*); |
drh | a5c1416 | 2013-12-17 15:03:06 | [diff] [blame] | 897 | assert( bArgList==0 ); |
drh | 2f2091b | 2021-03-20 15:46:01 | [diff] [blame] | 898 | if( pItem->zAlias && !flag_altform2 ){ |
drh | 8210233 | 2021-03-20 15:11:29 | [diff] [blame] | 899 | sqlite3_str_appendall(pAccum, pItem->zAlias); |
| 900 | }else if( pItem->zName ){ |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 901 | if( pItem->fg.fixedSchema==0 |
| 902 | && pItem->fg.isSubquery==0 |
| 903 | && pItem->u4.zDatabase!=0 |
| 904 | ){ |
drh | 8797bd6 | 2024-08-17 19:46:49 | [diff] [blame] | 905 | sqlite3_str_appendall(pAccum, pItem->u4.zDatabase); |
drh | 8210233 | 2021-03-20 15:11:29 | [diff] [blame] | 906 | sqlite3_str_append(pAccum, ".", 1); |
| 907 | } |
drh | a979993 | 2021-03-19 13:00:28 | [diff] [blame] | 908 | sqlite3_str_appendall(pAccum, pItem->zName); |
drh | 2f2091b | 2021-03-20 15:46:01 | [diff] [blame] | 909 | }else if( pItem->zAlias ){ |
| 910 | sqlite3_str_appendall(pAccum, pItem->zAlias); |
drh | 0766cbf | 2024-08-20 20:01:21 | [diff] [blame] | 911 | }else if( ALWAYS(pItem->fg.isSubquery) ){/* Because of tag-20240424-1 */ |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 912 | Select *pSel = pItem->u4.pSubq->pSelect; |
| 913 | assert( pSel!=0 ); |
drh | da653b8 | 2022-04-22 17:36:10 | [diff] [blame] | 914 | if( pSel->selFlags & SF_NestedFrom ){ |
| 915 | sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId); |
drh | 27a5ee8 | 2024-03-18 12:49:30 | [diff] [blame] | 916 | }else if( pSel->selFlags & SF_MultiValue ){ |
drh | ac7c6f5 | 2024-03-18 13:31:24 | [diff] [blame] | 917 | assert( !pItem->fg.isTabFunc && !pItem->fg.isIndexedBy ); |
drh | 27a5ee8 | 2024-03-18 12:49:30 | [diff] [blame] | 918 | sqlite3_str_appendf(pAccum, "%u-ROW VALUES CLAUSE", |
| 919 | pItem->u1.nRow); |
drh | da653b8 | 2022-04-22 17:36:10 | [diff] [blame] | 920 | }else{ |
| 921 | sqlite3_str_appendf(pAccum, "(subquery-%u)", pSel->selId); |
| 922 | } |
drh | a979993 | 2021-03-19 13:00:28 | [diff] [blame] | 923 | } |
drh | 5f96843 | 2004-02-21 19:02:30 | [diff] [blame] | 924 | length = width = 0; |
| 925 | break; |
| 926 | } |
drh | 874ba04 | 2009-04-08 16:10:04 | [diff] [blame] | 927 | default: { |
| 928 | assert( xtype==etINVALID ); |
| 929 | return; |
| 930 | } |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 931 | }/* End switch over the format type */ |
| 932 | /* |
| 933 | ** The text of the conversion is pointed to by "bufpt" and is |
| 934 | ** "length" characters long. The field width is "width". Do |
drh | 6285646 | 2018-02-19 17:03:23 | [diff] [blame] | 935 | ** the output. Both length and width are in bytes, not characters, |
| 936 | ** at this point. If the "!" flag was present on string conversions |
| 937 | ** indicating that width and precision should be expressed in characters, |
| 938 | ** then the values have been translated prior to reaching this point. |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 939 | */ |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 940 | width -= length; |
drh | 8236f68 | 2017-01-04 00:26:28 | [diff] [blame] | 941 | if( width>0 ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 942 | if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' '); |
| 943 | sqlite3_str_append(pAccum, bufpt, length); |
| 944 | if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' '); |
drh | 8236f68 | 2017-01-04 00:26:28 | [diff] [blame] | 945 | }else{ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 946 | sqlite3_str_append(pAccum, bufpt, length); |
drh | 8236f68 | 2017-01-04 00:26:28 | [diff] [blame] | 947 | } |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 948 | |
drh | af8f513 | 2014-10-29 18:20:18 | [diff] [blame] | 949 | if( zExtra ){ |
drh | 96ceaf8 | 2015-11-14 22:04:22 | [diff] [blame] | 950 | sqlite3DbFree(pAccum->db, zExtra); |
drh | af8f513 | 2014-10-29 18:20:18 | [diff] [blame] | 951 | zExtra = 0; |
| 952 | } |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 953 | }/* End for loop over the format string */ |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 954 | } /* End of function */ |
| 955 | |
drh | f62641e | 2021-12-24 20:22:13 | [diff] [blame] | 956 | |
| 957 | /* |
| 958 | ** The z string points to the first character of a token that is |
| 959 | ** associated with an error. If db does not already have an error |
| 960 | ** byte offset recorded, try to compute the error byte offset for |
| 961 | ** z and set the error byte offset in db. |
| 962 | */ |
| 963 | void sqlite3RecordErrorByteOffset(sqlite3 *db, const char *z){ |
| 964 | const Parse *pParse; |
| 965 | const char *zText; |
| 966 | const char *zEnd; |
| 967 | assert( z!=0 ); |
| 968 | if( NEVER(db==0) ) return; |
| 969 | if( db->errByteOffset!=(-2) ) return; |
| 970 | pParse = db->pParse; |
| 971 | if( NEVER(pParse==0) ) return; |
| 972 | zText =pParse->zTail; |
| 973 | if( NEVER(zText==0) ) return; |
| 974 | zEnd = &zText[strlen(zText)]; |
| 975 | if( SQLITE_WITHIN(z,zText,zEnd) ){ |
| 976 | db->errByteOffset = (int)(z-zText); |
| 977 | } |
| 978 | } |
| 979 | |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 980 | /* |
drh | 4f77c92 | 2022-02-05 23:11:19 | [diff] [blame] | 981 | ** If pExpr has a byte offset for the start of a token, record that as |
| 982 | ** as the error offset. |
| 983 | */ |
| 984 | void sqlite3RecordErrorOffsetOfExpr(sqlite3 *db, const Expr *pExpr){ |
drh | a6e8ee1 | 2022-05-13 16:38:40 | [diff] [blame] | 985 | while( pExpr |
| 986 | && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0) |
| 987 | ){ |
drh | 4f77c92 | 2022-02-05 23:11:19 | [diff] [blame] | 988 | pExpr = pExpr->pLeft; |
| 989 | } |
| 990 | if( pExpr==0 ) return; |
drh | 9566982 | 2024-11-20 11:34:16 | [diff] [blame] | 991 | if( ExprHasProperty(pExpr, EP_FromDDL) ) return; |
drh | 4f77c92 | 2022-02-05 23:11:19 | [diff] [blame] | 992 | db->errByteOffset = pExpr->w.iOfst; |
| 993 | } |
| 994 | |
| 995 | /* |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 996 | ** Enlarge the memory allocation on a StrAccum object so that it is |
| 997 | ** able to accept at least N more bytes of text. |
| 998 | ** |
| 999 | ** Return the number of bytes of text that StrAccum is able to accept |
| 1000 | ** after the attempted enlargement. The value returned might be zero. |
| 1001 | */ |
drh | 79b9bc4 | 2022-12-21 19:11:56 | [diff] [blame] | 1002 | int sqlite3StrAccumEnlarge(StrAccum *p, i64 N){ |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1003 | char *zNew; |
drh | 79b9bc4 | 2022-12-21 19:11:56 | [diff] [blame] | 1004 | assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */ |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1005 | if( p->accError ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1006 | testcase(p->accError==SQLITE_TOOBIG); |
| 1007 | testcase(p->accError==SQLITE_NOMEM); |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1008 | return 0; |
| 1009 | } |
drh | c049057 | 2015-05-02 11:45:53 | [diff] [blame] | 1010 | if( p->mxAlloc==0 ){ |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 1011 | sqlite3StrAccumSetError(p, SQLITE_TOOBIG); |
drh | 255a81f | 2019-02-22 15:42:10 | [diff] [blame] | 1012 | return p->nAlloc - p->nChar - 1; |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1013 | }else{ |
drh | 5f4a686 | 2016-01-30 12:50:25 | [diff] [blame] | 1014 | char *zOld = isMalloced(p) ? p->zText : 0; |
drh | 79b9bc4 | 2022-12-21 19:11:56 | [diff] [blame] | 1015 | i64 szNew = p->nChar + N + 1; |
drh | 7b4d780 | 2014-11-03 14:46:29 | [diff] [blame] | 1016 | if( szNew+p->nChar<=p->mxAlloc ){ |
| 1017 | /* Force exponential buffer size growth as long as it does not overflow, |
| 1018 | ** to avoid having to call this routine too often */ |
| 1019 | szNew += p->nChar; |
| 1020 | } |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1021 | if( szNew > p->mxAlloc ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1022 | sqlite3_str_reset(p); |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 1023 | sqlite3StrAccumSetError(p, SQLITE_TOOBIG); |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1024 | return 0; |
| 1025 | }else{ |
| 1026 | p->nAlloc = (int)szNew; |
| 1027 | } |
drh | c049057 | 2015-05-02 11:45:53 | [diff] [blame] | 1028 | if( p->db ){ |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1029 | zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); |
| 1030 | }else{ |
drh | d924e7b | 2020-05-17 00:26:44 | [diff] [blame] | 1031 | zNew = sqlite3Realloc(zOld, p->nAlloc); |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1032 | } |
| 1033 | if( zNew ){ |
drh | 7ef4d1c | 2014-05-31 15:39:53 | [diff] [blame] | 1034 | assert( p->zText!=0 || p->nChar==0 ); |
drh | 5f4a686 | 2016-01-30 12:50:25 | [diff] [blame] | 1035 | if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1036 | p->zText = zNew; |
drh | 7f5a7ec | 2014-11-03 13:24:12 | [diff] [blame] | 1037 | p->nAlloc = sqlite3DbMallocSize(p->db, zNew); |
drh | 5f4a686 | 2016-01-30 12:50:25 | [diff] [blame] | 1038 | p->printfFlags |= SQLITE_PRINTF_MALLOCED; |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1039 | }else{ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1040 | sqlite3_str_reset(p); |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 1041 | sqlite3StrAccumSetError(p, SQLITE_NOMEM); |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1042 | return 0; |
| 1043 | } |
| 1044 | } |
drh | 79b9bc4 | 2022-12-21 19:11:56 | [diff] [blame] | 1045 | assert( N>=0 && N<=0x7fffffff ); |
| 1046 | return (int)N; |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | /* |
drh | af8f513 | 2014-10-29 18:20:18 | [diff] [blame] | 1050 | ** Append N copies of character c to the given string buffer. |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1051 | */ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1052 | void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){ |
drh | a30d22a | 2015-04-07 13:28:41 | [diff] [blame] | 1053 | testcase( p->nChar + (i64)N > 0x7fffffff ); |
| 1054 | if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ |
| 1055 | return; |
| 1056 | } |
drh | af8f513 | 2014-10-29 18:20:18 | [diff] [blame] | 1057 | while( (N--)>0 ) p->zText[p->nChar++] = c; |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | /* |
| 1061 | ** The StrAccum "p" is not large enough to accept N new bytes of z[]. |
| 1062 | ** So enlarge if first, then do the append. |
| 1063 | ** |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1064 | ** This is a helper routine to sqlite3_str_append() that does special-case |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1065 | ** work (enlarging the buffer) using tail recursion, so that the |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1066 | ** sqlite3_str_append() routine can use fast calling semantics. |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1067 | */ |
drh | 172087f | 2014-08-22 15:40:20 | [diff] [blame] | 1068 | static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1069 | N = sqlite3StrAccumEnlarge(p, N); |
| 1070 | if( N>0 ){ |
| 1071 | memcpy(&p->zText[p->nChar], z, N); |
| 1072 | p->nChar += N; |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | /* |
| 1077 | ** Append N bytes of text from z to the StrAccum object. Increase the |
| 1078 | ** size of the memory allocation for StrAccum if necessary. |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 1079 | */ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1080 | void sqlite3_str_append(sqlite3_str *p, const char *z, int N){ |
drh | 3457338 | 2015-04-15 05:38:35 | [diff] [blame] | 1081 | assert( z!=0 || N==0 ); |
drh | a6353a3 | 2013-12-09 19:03:26 | [diff] [blame] | 1082 | assert( p->zText!=0 || p->nChar==0 || p->accError ); |
| 1083 | assert( N>=0 ); |
drh | 255a81f | 2019-02-22 15:42:10 | [diff] [blame] | 1084 | assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 ); |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1085 | if( p->nChar+N >= p->nAlloc ){ |
drh | a70a073 | 2014-03-17 14:24:27 | [diff] [blame] | 1086 | enlargeAndAppend(p,z,N); |
dan | 895decf | 2016-12-30 14:15:56 | [diff] [blame] | 1087 | }else if( N ){ |
drh | 172087f | 2014-08-22 15:40:20 | [diff] [blame] | 1088 | assert( p->zText ); |
| 1089 | p->nChar += N; |
| 1090 | memcpy(&p->zText[p->nChar-N], z, N); |
drh | 5f96843 | 2004-02-21 19:02:30 | [diff] [blame] | 1091 | } |
drh | 483750b | 2003-01-29 18:46:51 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /* |
drh | a6353a3 | 2013-12-09 19:03:26 | [diff] [blame] | 1095 | ** Append the complete text of zero-terminated string z[] to the p string. |
| 1096 | */ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1097 | void sqlite3_str_appendall(sqlite3_str *p, const char *z){ |
| 1098 | sqlite3_str_append(p, z, sqlite3Strlen30(z)); |
drh | a6353a3 | 2013-12-09 19:03:26 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | |
| 1102 | /* |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1103 | ** Finish off a string by making sure it is zero-terminated. |
| 1104 | ** Return a pointer to the resulting string. Return a NULL |
| 1105 | ** pointer if any kind of error was encountered. |
drh | 5f96843 | 2004-02-21 19:02:30 | [diff] [blame] | 1106 | */ |
drh | 043e586 | 2016-11-25 15:11:26 | [diff] [blame] | 1107 | static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){ |
drh | 3f18e6d | 2017-08-12 02:01:55 | [diff] [blame] | 1108 | char *zText; |
drh | 043e586 | 2016-11-25 15:11:26 | [diff] [blame] | 1109 | assert( p->mxAlloc>0 && !isMalloced(p) ); |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 1110 | zText = sqlite3DbMallocRaw(p->db, 1+(u64)p->nChar ); |
drh | 3f18e6d | 2017-08-12 02:01:55 | [diff] [blame] | 1111 | if( zText ){ |
| 1112 | memcpy(zText, p->zText, p->nChar+1); |
drh | 043e586 | 2016-11-25 15:11:26 | [diff] [blame] | 1113 | p->printfFlags |= SQLITE_PRINTF_MALLOCED; |
| 1114 | }else{ |
drh | f06db3e | 2021-10-01 00:25:06 | [diff] [blame] | 1115 | sqlite3StrAccumSetError(p, SQLITE_NOMEM); |
drh | 043e586 | 2016-11-25 15:11:26 | [diff] [blame] | 1116 | } |
drh | 3f18e6d | 2017-08-12 02:01:55 | [diff] [blame] | 1117 | p->zText = zText; |
| 1118 | return zText; |
drh | 043e586 | 2016-11-25 15:11:26 | [diff] [blame] | 1119 | } |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1120 | char *sqlite3StrAccumFinish(StrAccum *p){ |
| 1121 | if( p->zText ){ |
| 1122 | p->zText[p->nChar] = 0; |
drh | 5f4a686 | 2016-01-30 12:50:25 | [diff] [blame] | 1123 | if( p->mxAlloc>0 && !isMalloced(p) ){ |
drh | 043e586 | 2016-11-25 15:11:26 | [diff] [blame] | 1124 | return strAccumFinishRealloc(p); |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | return p->zText; |
| 1128 | } |
| 1129 | |
drh | f80bba9 | 2018-05-16 15:35:03 | [diff] [blame] | 1130 | /* |
drh | 5bf4715 | 2021-10-03 00:12:43 | [diff] [blame] | 1131 | ** Use the content of the StrAccum passed as the second argument |
| 1132 | ** as the result of an SQL function. |
| 1133 | */ |
| 1134 | void sqlite3ResultStrAccum(sqlite3_context *pCtx, StrAccum *p){ |
| 1135 | if( p->accError ){ |
| 1136 | sqlite3_result_error_code(pCtx, p->accError); |
| 1137 | sqlite3_str_reset(p); |
| 1138 | }else if( isMalloced(p) ){ |
| 1139 | sqlite3_result_text(pCtx, p->zText, p->nChar, SQLITE_DYNAMIC); |
| 1140 | }else{ |
| 1141 | sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC); |
| 1142 | sqlite3_str_reset(p); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | /* |
drh | f80bba9 | 2018-05-16 15:35:03 | [diff] [blame] | 1147 | ** This singleton is an sqlite3_str object that is returned if |
| 1148 | ** sqlite3_malloc() fails to provide space for a real one. This |
| 1149 | ** sqlite3_str object accepts no new text and always returns |
| 1150 | ** an SQLITE_NOMEM error. |
| 1151 | */ |
| 1152 | static sqlite3_str sqlite3OomStr = { |
drh | 3e62ddb | 2018-05-30 00:59:09 | [diff] [blame] | 1153 | 0, 0, 0, 0, 0, SQLITE_NOMEM, 0 |
drh | f80bba9 | 2018-05-16 15:35:03 | [diff] [blame] | 1154 | }; |
| 1155 | |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1156 | /* Finalize a string created using sqlite3_str_new(). |
| 1157 | */ |
| 1158 | char *sqlite3_str_finish(sqlite3_str *p){ |
| 1159 | char *z; |
drh | f80bba9 | 2018-05-16 15:35:03 | [diff] [blame] | 1160 | if( p!=0 && p!=&sqlite3OomStr ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1161 | z = sqlite3StrAccumFinish(p); |
drh | 446135d | 2018-05-09 14:29:40 | [diff] [blame] | 1162 | sqlite3_free(p); |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1163 | }else{ |
| 1164 | z = 0; |
| 1165 | } |
| 1166 | return z; |
| 1167 | } |
| 1168 | |
| 1169 | /* Return any error code associated with p */ |
| 1170 | int sqlite3_str_errcode(sqlite3_str *p){ |
| 1171 | return p ? p->accError : SQLITE_NOMEM; |
| 1172 | } |
| 1173 | |
| 1174 | /* Return the current length of p in bytes */ |
| 1175 | int sqlite3_str_length(sqlite3_str *p){ |
| 1176 | return p ? p->nChar : 0; |
| 1177 | } |
| 1178 | |
| 1179 | /* Return the current value for p */ |
| 1180 | char *sqlite3_str_value(sqlite3_str *p){ |
drh | 446135d | 2018-05-09 14:29:40 | [diff] [blame] | 1181 | if( p==0 || p->nChar==0 ) return 0; |
| 1182 | p->zText[p->nChar] = 0; |
| 1183 | return p->zText; |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1184 | } |
| 1185 | |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1186 | /* |
| 1187 | ** Reset an StrAccum string. Reclaim all malloced memory. |
| 1188 | */ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1189 | void sqlite3_str_reset(StrAccum *p){ |
drh | 5f4a686 | 2016-01-30 12:50:25 | [diff] [blame] | 1190 | if( isMalloced(p) ){ |
drh | c049057 | 2015-05-02 11:45:53 | [diff] [blame] | 1191 | sqlite3DbFree(p->db, p->zText); |
drh | 5f4a686 | 2016-01-30 12:50:25 | [diff] [blame] | 1192 | p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1193 | } |
drh | 446135d | 2018-05-09 14:29:40 | [diff] [blame] | 1194 | p->nAlloc = 0; |
| 1195 | p->nChar = 0; |
drh | f089aa4 | 2008-07-08 19:34:06 | [diff] [blame] | 1196 | p->zText = 0; |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /* |
drh | c049057 | 2015-05-02 11:45:53 | [diff] [blame] | 1200 | ** Initialize a string accumulator. |
| 1201 | ** |
| 1202 | ** p: The accumulator to be initialized. |
| 1203 | ** db: Pointer to a database connection. May be NULL. Lookaside |
| 1204 | ** memory is used if not NULL. db->mallocFailed is set appropriately |
| 1205 | ** when not NULL. |
| 1206 | ** zBase: An initial buffer. May be NULL in which case the initial buffer |
| 1207 | ** is malloced. |
| 1208 | ** n: Size of zBase in bytes. If total space requirements never exceed |
| 1209 | ** n then no memory allocations ever occur. |
| 1210 | ** mx: Maximum number of bytes to accumulate. If mx==0 then no memory |
| 1211 | ** allocations will ever occur. |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1212 | */ |
drh | c049057 | 2015-05-02 11:45:53 | [diff] [blame] | 1213 | void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){ |
drh | 3f18e6d | 2017-08-12 02:01:55 | [diff] [blame] | 1214 | p->zText = zBase; |
drh | c049057 | 2015-05-02 11:45:53 | [diff] [blame] | 1215 | p->db = db; |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1216 | p->nAlloc = n; |
drh | bb4957f | 2008-03-20 14:03:29 | [diff] [blame] | 1217 | p->mxAlloc = mx; |
drh | 3f18e6d | 2017-08-12 02:01:55 | [diff] [blame] | 1218 | p->nChar = 0; |
drh | b49bc86 | 2013-08-21 21:12:10 | [diff] [blame] | 1219 | p->accError = 0; |
drh | 5f4a686 | 2016-01-30 12:50:25 | [diff] [blame] | 1220 | p->printfFlags = 0; |
drh | 5f96843 | 2004-02-21 19:02:30 | [diff] [blame] | 1221 | } |
| 1222 | |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1223 | /* Allocate and initialize a new dynamic string object */ |
| 1224 | sqlite3_str *sqlite3_str_new(sqlite3 *db){ |
drh | 446135d | 2018-05-09 14:29:40 | [diff] [blame] | 1225 | sqlite3_str *p = sqlite3_malloc64(sizeof(*p)); |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1226 | if( p ){ |
drh | 446135d | 2018-05-09 14:29:40 | [diff] [blame] | 1227 | sqlite3StrAccumInit(p, 0, 0, 0, |
| 1228 | db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH); |
drh | f80bba9 | 2018-05-16 15:35:03 | [diff] [blame] | 1229 | }else{ |
| 1230 | p = &sqlite3OomStr; |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1231 | } |
| 1232 | return p; |
| 1233 | } |
| 1234 | |
drh | 5f96843 | 2004-02-21 19:02:30 | [diff] [blame] | 1235 | /* |
| 1236 | ** Print into memory obtained from sqliteMalloc(). Use the internal |
| 1237 | ** %-conversion extensions. |
| 1238 | */ |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 1239 | char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ |
| 1240 | char *z; |
drh | 79158e1 | 2005-09-06 21:40:45 | [diff] [blame] | 1241 | char zBase[SQLITE_PRINT_BUF_SIZE]; |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1242 | StrAccum acc; |
drh | bc6160b | 2009-04-08 15:45:31 | [diff] [blame] | 1243 | assert( db!=0 ); |
drh | c049057 | 2015-05-02 11:45:53 | [diff] [blame] | 1244 | sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase), |
drh | bc6160b | 2009-04-08 15:45:31 | [diff] [blame] | 1245 | db->aLimit[SQLITE_LIMIT_LENGTH]); |
drh | 5f4a686 | 2016-01-30 12:50:25 | [diff] [blame] | 1246 | acc.printfFlags = SQLITE_PRINTF_INTERNAL; |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1247 | sqlite3_str_vappendf(&acc, zFormat, ap); |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1248 | z = sqlite3StrAccumFinish(&acc); |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1249 | if( acc.accError==SQLITE_NOMEM ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 | [diff] [blame] | 1250 | sqlite3OomFault(db); |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 1251 | } |
| 1252 | return z; |
drh | 5f96843 | 2004-02-21 19:02:30 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | /* |
| 1256 | ** Print into memory obtained from sqliteMalloc(). Use the internal |
| 1257 | ** %-conversion extensions. |
| 1258 | */ |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 1259 | char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ |
drh | 5f96843 | 2004-02-21 19:02:30 | [diff] [blame] | 1260 | va_list ap; |
| 1261 | char *z; |
drh | 5f96843 | 2004-02-21 19:02:30 | [diff] [blame] | 1262 | va_start(ap, zFormat); |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1263 | z = sqlite3VMPrintf(db, zFormat, ap); |
drh | 5f96843 | 2004-02-21 19:02:30 | [diff] [blame] | 1264 | va_end(ap); |
| 1265 | return z; |
| 1266 | } |
| 1267 | |
| 1268 | /* |
drh | 28dd479 | 2006-06-26 21:35:44 | [diff] [blame] | 1269 | ** Print into memory obtained from sqlite3_malloc(). Omit the internal |
| 1270 | ** %-conversion extensions. |
| 1271 | */ |
| 1272 | char *sqlite3_vmprintf(const char *zFormat, va_list ap){ |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1273 | char *z; |
drh | 28dd479 | 2006-06-26 21:35:44 | [diff] [blame] | 1274 | char zBase[SQLITE_PRINT_BUF_SIZE]; |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1275 | StrAccum acc; |
drh | 9ca9573 | 2014-10-24 00:35:58 | [diff] [blame] | 1276 | |
| 1277 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1278 | if( zFormat==0 ){ |
| 1279 | (void)SQLITE_MISUSE_BKPT; |
| 1280 | return 0; |
| 1281 | } |
| 1282 | #endif |
drh | ff1590e | 2008-07-14 12:52:53 | [diff] [blame] | 1283 | #ifndef SQLITE_OMIT_AUTOINIT |
| 1284 | if( sqlite3_initialize() ) return 0; |
| 1285 | #endif |
drh | c049057 | 2015-05-02 11:45:53 | [diff] [blame] | 1286 | sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1287 | sqlite3_str_vappendf(&acc, zFormat, ap); |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1288 | z = sqlite3StrAccumFinish(&acc); |
| 1289 | return z; |
drh | 28dd479 | 2006-06-26 21:35:44 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | /* |
| 1293 | ** Print into memory obtained from sqlite3_malloc()(). Omit the internal |
| 1294 | ** %-conversion extensions. |
drh | 483750b | 2003-01-29 18:46:51 | [diff] [blame] | 1295 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 | [diff] [blame] | 1296 | char *sqlite3_mprintf(const char *zFormat, ...){ |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 1297 | va_list ap; |
drh | 5f96843 | 2004-02-21 19:02:30 | [diff] [blame] | 1298 | char *z; |
drh | ff1590e | 2008-07-14 12:52:53 | [diff] [blame] | 1299 | #ifndef SQLITE_OMIT_AUTOINIT |
| 1300 | if( sqlite3_initialize() ) return 0; |
| 1301 | #endif |
drh | 28dd479 | 2006-06-26 21:35:44 | [diff] [blame] | 1302 | va_start(ap, zFormat); |
drh | b3738b6 | 2007-03-31 15:02:49 | [diff] [blame] | 1303 | z = sqlite3_vmprintf(zFormat, ap); |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 1304 | va_end(ap); |
drh | 5f96843 | 2004-02-21 19:02:30 | [diff] [blame] | 1305 | return z; |
drh | a18c568 | 2000-10-08 22:20:57 | [diff] [blame] | 1306 | } |
| 1307 | |
drh | 93a5c6b | 2003-12-23 02:17:35 | [diff] [blame] | 1308 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 | [diff] [blame] | 1309 | ** sqlite3_snprintf() works like snprintf() except that it ignores the |
drh | 93a5c6b | 2003-12-23 02:17:35 | [diff] [blame] | 1310 | ** current locale settings. This is important for SQLite because we |
| 1311 | ** are not able to use a "," as the decimal point in place of "." as |
| 1312 | ** specified by some locales. |
drh | db26d4c | 2011-01-05 12:20:09 | [diff] [blame] | 1313 | ** |
| 1314 | ** Oops: The first two arguments of sqlite3_snprintf() are backwards |
| 1315 | ** from the snprintf() standard. Unfortunately, it is too late to change |
| 1316 | ** this without breaking compatibility, so we just have to live with the |
| 1317 | ** mistake. |
| 1318 | ** |
| 1319 | ** sqlite3_vsnprintf() is the varargs version. |
drh | 93a5c6b | 2003-12-23 02:17:35 | [diff] [blame] | 1320 | */ |
drh | db26d4c | 2011-01-05 12:20:09 | [diff] [blame] | 1321 | char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ |
| 1322 | StrAccum acc; |
| 1323 | if( n<=0 ) return zBuf; |
drh | 9ca9573 | 2014-10-24 00:35:58 | [diff] [blame] | 1324 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1325 | if( zBuf==0 || zFormat==0 ) { |
| 1326 | (void)SQLITE_MISUSE_BKPT; |
drh | 96c707a | 2015-02-13 16:36:14 | [diff] [blame] | 1327 | if( zBuf ) zBuf[0] = 0; |
drh | 9ca9573 | 2014-10-24 00:35:58 | [diff] [blame] | 1328 | return zBuf; |
| 1329 | } |
| 1330 | #endif |
drh | c049057 | 2015-05-02 11:45:53 | [diff] [blame] | 1331 | sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1332 | sqlite3_str_vappendf(&acc, zFormat, ap); |
drh | e9bb566 | 2016-11-25 15:47:53 | [diff] [blame] | 1333 | zBuf[acc.nChar] = 0; |
| 1334 | return zBuf; |
drh | db26d4c | 2011-01-05 12:20:09 | [diff] [blame] | 1335 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 | [diff] [blame] | 1336 | char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ |
drh | 0f8aebb | 2023-02-25 12:50:54 | [diff] [blame] | 1337 | StrAccum acc; |
drh | 93a5c6b | 2003-12-23 02:17:35 | [diff] [blame] | 1338 | va_list ap; |
drh | 0f8aebb | 2023-02-25 12:50:54 | [diff] [blame] | 1339 | if( n<=0 ) return zBuf; |
| 1340 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1341 | if( zBuf==0 || zFormat==0 ) { |
| 1342 | (void)SQLITE_MISUSE_BKPT; |
| 1343 | if( zBuf ) zBuf[0] = 0; |
| 1344 | return zBuf; |
| 1345 | } |
| 1346 | #endif |
| 1347 | sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); |
drh | 93a5c6b | 2003-12-23 02:17:35 | [diff] [blame] | 1348 | va_start(ap,zFormat); |
drh | 0f8aebb | 2023-02-25 12:50:54 | [diff] [blame] | 1349 | sqlite3_str_vappendf(&acc, zFormat, ap); |
drh | 93a5c6b | 2003-12-23 02:17:35 | [diff] [blame] | 1350 | va_end(ap); |
drh | 0f8aebb | 2023-02-25 12:50:54 | [diff] [blame] | 1351 | zBuf[acc.nChar] = 0; |
| 1352 | return zBuf; |
drh | 93a5c6b | 2003-12-23 02:17:35 | [diff] [blame] | 1353 | } |
| 1354 | |
drh | 56747d1 | 2025-04-17 17:46:28 | [diff] [blame] | 1355 | /* Maximum size of an sqlite3_log() message. */ |
| 1356 | #if defined(SQLITE_MAX_LOG_MESSAGE) |
| 1357 | /* Leave the definition as supplied */ |
| 1358 | #elif SQLITE_PRINT_BUF_SIZE*10>10000 |
| 1359 | # define SQLITE_MAX_LOG_MESSAGE 10000 |
| 1360 | #else |
| 1361 | # define SQLITE_MAX_LOG_MESSAGE (SQLITE_PRINT_BUF_SIZE*10) |
| 1362 | #endif |
| 1363 | |
drh | 3f28070 | 2010-02-18 18:45:09 | [diff] [blame] | 1364 | /* |
drh | 7c0c460 | 2010-03-03 22:25:18 | [diff] [blame] | 1365 | ** This is the routine that actually formats the sqlite3_log() message. |
| 1366 | ** We house it in a separate routine from sqlite3_log() to avoid using |
| 1367 | ** stack space on small-stack systems when logging is disabled. |
| 1368 | ** |
| 1369 | ** sqlite3_log() must render into a static buffer. It cannot dynamically |
| 1370 | ** allocate memory because it might be called while the memory allocator |
| 1371 | ** mutex is held. |
drh | a8dbd52 | 2015-07-14 22:43:37 | [diff] [blame] | 1372 | ** |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1373 | ** sqlite3_str_vappendf() might ask for *temporary* memory allocations for |
drh | a8dbd52 | 2015-07-14 22:43:37 | [diff] [blame] | 1374 | ** certain format characters (%q) or for very large precisions or widths. |
| 1375 | ** Care must be taken that any sqlite3_log() calls that occur while the |
| 1376 | ** memory mutex is held do not use these mechanisms. |
drh | 7c0c460 | 2010-03-03 22:25:18 | [diff] [blame] | 1377 | */ |
| 1378 | static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ |
drh | a64fa91 | 2010-03-04 00:53:32 | [diff] [blame] | 1379 | StrAccum acc; /* String accumulator */ |
drh | 56747d1 | 2025-04-17 17:46:28 | [diff] [blame] | 1380 | char zMsg[SQLITE_MAX_LOG_MESSAGE]; /* Complete log message */ |
drh | 7c0c460 | 2010-03-03 22:25:18 | [diff] [blame] | 1381 | |
drh | c049057 | 2015-05-02 11:45:53 | [diff] [blame] | 1382 | sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0); |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1383 | sqlite3_str_vappendf(&acc, zFormat, ap); |
drh | 7c0c460 | 2010-03-03 22:25:18 | [diff] [blame] | 1384 | sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, |
| 1385 | sqlite3StrAccumFinish(&acc)); |
| 1386 | } |
| 1387 | |
| 1388 | /* |
drh | 3f28070 | 2010-02-18 18:45:09 | [diff] [blame] | 1389 | ** Format and write a message to the log if logging is enabled. |
| 1390 | */ |
drh | a756466 | 2010-02-22 19:32:31 | [diff] [blame] | 1391 | void sqlite3_log(int iErrCode, const char *zFormat, ...){ |
drh | 3f28070 | 2010-02-18 18:45:09 | [diff] [blame] | 1392 | va_list ap; /* Vararg list */ |
drh | 7c0c460 | 2010-03-03 22:25:18 | [diff] [blame] | 1393 | if( sqlite3GlobalConfig.xLog ){ |
drh | 3f28070 | 2010-02-18 18:45:09 | [diff] [blame] | 1394 | va_start(ap, zFormat); |
drh | 7c0c460 | 2010-03-03 22:25:18 | [diff] [blame] | 1395 | renderLogMsg(iErrCode, zFormat, ap); |
drh | 3f28070 | 2010-02-18 18:45:09 | [diff] [blame] | 1396 | va_end(ap); |
drh | 3f28070 | 2010-02-18 18:45:09 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | |
mistachkin | 02b0e26 | 2015-04-16 03:37:19 | [diff] [blame] | 1400 | #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
drh | e54ca3f | 2004-06-07 01:52:14 | [diff] [blame] | 1401 | /* |
| 1402 | ** A version of printf() that understands %lld. Used for debugging. |
| 1403 | ** The printf() built into some versions of windows does not understand %lld |
| 1404 | ** and segfaults if you give it a long long int. |
| 1405 | */ |
| 1406 | void sqlite3DebugPrintf(const char *zFormat, ...){ |
| 1407 | va_list ap; |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1408 | StrAccum acc; |
mistachkin | a8e41ec | 2020-05-15 01:18:07 | [diff] [blame] | 1409 | char zBuf[SQLITE_PRINT_BUF_SIZE*10]; |
drh | c049057 | 2015-05-02 11:45:53 | [diff] [blame] | 1410 | sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
drh | ade8648 | 2007-11-28 22:36:40 | [diff] [blame] | 1411 | va_start(ap,zFormat); |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1412 | sqlite3_str_vappendf(&acc, zFormat, ap); |
drh | e54ca3f | 2004-06-07 01:52:14 | [diff] [blame] | 1413 | va_end(ap); |
drh | bed8e7e | 2007-12-08 17:55:35 | [diff] [blame] | 1414 | sqlite3StrAccumFinish(&acc); |
mistachkin | 4a9ff91 | 2017-11-09 17:29:04 | [diff] [blame] | 1415 | #ifdef SQLITE_OS_TRACE_PROC |
| 1416 | { |
| 1417 | extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf); |
| 1418 | SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf)); |
| 1419 | } |
| 1420 | #else |
drh | 485f003 | 2007-01-26 19:23:33 | [diff] [blame] | 1421 | fprintf(stdout,"%s", zBuf); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 | [diff] [blame] | 1422 | fflush(stdout); |
mistachkin | 4a9ff91 | 2017-11-09 17:29:04 | [diff] [blame] | 1423 | #endif |
drh | e54ca3f | 2004-06-07 01:52:14 | [diff] [blame] | 1424 | } |
| 1425 | #endif |
drh | c7bc4fd | 2009-11-25 18:03:42 | [diff] [blame] | 1426 | |
drh | 4fa4a54 | 2014-09-30 12:33:33 | [diff] [blame] | 1427 | |
drh | c7bc4fd | 2009-11-25 18:03:42 | [diff] [blame] | 1428 | /* |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1429 | ** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument |
drh | d37bea5 | 2015-09-02 15:37:50 | [diff] [blame] | 1430 | ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats. |
drh | c7bc4fd | 2009-11-25 18:03:42 | [diff] [blame] | 1431 | */ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1432 | void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){ |
drh | c7bc4fd | 2009-11-25 18:03:42 | [diff] [blame] | 1433 | va_list ap; |
| 1434 | va_start(ap,zFormat); |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 1435 | sqlite3_str_vappendf(p, zFormat, ap); |
drh | c7bc4fd | 2009-11-25 18:03:42 | [diff] [blame] | 1436 | va_end(ap); |
| 1437 | } |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 1438 | |
| 1439 | |
| 1440 | /***************************************************************************** |
drh | ca1ce77 | 2023-12-01 12:57:12 | [diff] [blame] | 1441 | ** Reference counted string/blob storage |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 1442 | *****************************************************************************/ |
| 1443 | |
| 1444 | /* |
| 1445 | ** Increase the reference count of the string by one. |
| 1446 | ** |
| 1447 | ** The input parameter is returned. |
| 1448 | */ |
| 1449 | char *sqlite3RCStrRef(char *z){ |
| 1450 | RCStr *p = (RCStr*)z; |
| 1451 | assert( p!=0 ); |
| 1452 | p--; |
| 1453 | p->nRCRef++; |
| 1454 | return z; |
| 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | ** Decrease the reference count by one. Free the string when the |
| 1459 | ** reference count reaches zero. |
| 1460 | */ |
drh | 43dc31c | 2023-10-17 19:33:52 | [diff] [blame] | 1461 | void sqlite3RCStrUnref(void *z){ |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 1462 | RCStr *p = (RCStr*)z; |
| 1463 | assert( p!=0 ); |
| 1464 | p--; |
| 1465 | assert( p->nRCRef>0 ); |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 1466 | if( p->nRCRef>=2 ){ |
| 1467 | p->nRCRef--; |
| 1468 | }else{ |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 1469 | sqlite3_free(p); |
| 1470 | } |
| 1471 | } |
| 1472 | |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 1473 | /* |
| 1474 | ** Create a new string that is capable of holding N bytes of text, not counting |
| 1475 | ** the zero byte at the end. The string is uninitialized. |
| 1476 | ** |
| 1477 | ** The reference count is initially 1. Call sqlite3RCStrUnref() to free the |
| 1478 | ** newly allocated string. |
| 1479 | ** |
| 1480 | ** This routine returns 0 on an OOM. |
| 1481 | */ |
| 1482 | char *sqlite3RCStrNew(u64 N){ |
drh | 59b8e66 | 2023-07-25 20:26:47 | [diff] [blame] | 1483 | RCStr *p = sqlite3_malloc64( N + sizeof(*p) + 1 ); |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 1484 | if( p==0 ) return 0; |
| 1485 | p->nRCRef = 1; |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 1486 | return (char*)&p[1]; |
| 1487 | } |
| 1488 | |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 1489 | /* |
| 1490 | ** Change the size of the string so that it is able to hold N bytes. |
| 1491 | ** The string might be reallocated, so return the new allocation. |
| 1492 | */ |
| 1493 | char *sqlite3RCStrResize(char *z, u64 N){ |
| 1494 | RCStr *p = (RCStr*)z; |
| 1495 | RCStr *pNew; |
| 1496 | assert( p!=0 ); |
| 1497 | p--; |
| 1498 | assert( p->nRCRef==1 ); |
drh | f02cc9a | 2023-07-25 15:08:18 | [diff] [blame] | 1499 | pNew = sqlite3_realloc64(p, N+sizeof(RCStr)+1); |
| 1500 | if( pNew==0 ){ |
| 1501 | sqlite3_free(p); |
| 1502 | return 0; |
| 1503 | }else{ |
| 1504 | return (char*)&pNew[1]; |
| 1505 | } |
| 1506 | } |