1 /*-------------------------------------------------------------------------
5 * StringInfo provides an indefinitely-extensible string data type.
6 * It can be used to buffer either ordinary C strings (null-terminated text)
7 * or arbitrary binary data. All storage is allocated with palloc().
9 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
12 * src/backend/lib/stringinfo.c
14 *-------------------------------------------------------------------------
18 #include "lib/stringinfo.h"
19 #include "utils/memutils.h"
25 * Create an empty 'StringInfoData' & return a pointer to it.
32 res = (StringInfo) palloc(sizeof(StringInfoData));
42 * Initialize a StringInfoData struct (with previously undefined contents)
43 * to describe an empty string.
46 initStringInfo(StringInfo str)
48 int size = 1024; /* initial default buffer size */
50 str->data = (char *) palloc(size);
58 * Reset the StringInfo: the data buffer remains valid, but its
59 * previous content, if any, is cleared.
62 resetStringInfo(StringInfo str)
72 * Format text data under the control of fmt (an sprintf-style format string)
73 * and append it to whatever is already in str. More space is allocated
74 * to str if necessary. This is sort of like a combination of sprintf and
78 appendStringInfo(StringInfo str, const char *fmt,...)
85 /* Try to format the data. */
87 success = appendStringInfoVA(str, fmt, args);
93 /* Double the buffer size and try again. */
94 enlargeStringInfo(str, str->maxlen);
101 * Attempt to format text data under the control of fmt (an sprintf-style
102 * format string) and append it to whatever is already in str. If successful
103 * return true; if not (because there's not enough space), return false
104 * without modifying str. Typically the caller would enlarge str and retry
105 * on false return --- see appendStringInfo for standard usage pattern.
107 * XXX This API is ugly, but there seems no alternative given the C spec's
108 * restrictions on what can portably be done with va_list arguments: you have
109 * to redo va_start before you can rescan the argument list, and we can't do
113 appendStringInfoVA(StringInfo str, const char *fmt, va_list args)
121 * If there's hardly any space, don't bother trying, just fail to make the
122 * caller enlarge the buffer first.
124 avail = str->maxlen - str->len - 1;
129 * Assert check here is to catch buggy vsnprintf that overruns the
130 * specified buffer length. Solaris 7 in 64-bit mode is an example of a
131 * platform with such a bug.
133 #ifdef USE_ASSERT_CHECKING
134 str->data[str->maxlen - 1] = '\0';
137 nprinted = vsnprintf(str->data + str->len, avail, fmt, args);
139 Assert(str->data[str->maxlen - 1] == '\0');
142 * Note: some versions of vsnprintf return the number of chars actually
143 * stored, but at least one returns -1 on failure. Be conservative about
144 * believing whether the print worked.
146 if (nprinted >= 0 && nprinted < avail - 1)
148 /* Success. Note nprinted does not include trailing null. */
149 str->len += nprinted;
153 /* Restore the trailing null so that str is unmodified. */
154 str->data[str->len] = '\0';
159 * appendStringInfoString
161 * Append a null-terminated string to str.
162 * Like appendStringInfo(str, "%s", s) but faster.
165 appendStringInfoString(StringInfo str, const char *s)
167 appendBinaryStringInfo(str, s, strlen(s));
171 * appendStringInfoChar
173 * Append a single byte to str.
174 * Like appendStringInfo(str, "%c", ch) but much faster.
177 appendStringInfoChar(StringInfo str, char ch)
179 /* Make more room if needed */
180 if (str->len + 1 >= str->maxlen)
181 enlargeStringInfo(str, 1);
183 /* OK, append the character */
184 str->data[str->len] = ch;
186 str->data[str->len] = '\0';
190 * appendStringInfoSpaces
192 * Append the specified number of spaces to a buffer.
195 appendStringInfoSpaces(StringInfo str, int count)
199 /* Make more room if needed */
200 enlargeStringInfo(str, count);
202 /* OK, append the spaces */
204 str->data[str->len++] = ' ';
205 str->data[str->len] = '\0';
210 * appendBinaryStringInfo
212 * Append arbitrary binary data to a StringInfo, allocating more space
216 appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
220 /* Make more room if needed */
221 enlargeStringInfo(str, datalen);
223 /* OK, append the data */
224 memcpy(str->data + str->len, data, datalen);
228 * Keep a trailing null in place, even though it's probably useless for
229 * binary data. (Some callers are dealing with text but call this because
230 * their input isn't null-terminated.)
232 str->data[str->len] = '\0';
238 * Make sure there is enough space for 'needed' more bytes
239 * ('needed' does not include the terminating null).
241 * External callers usually need not concern themselves with this, since
242 * all stringinfo.c routines do it automatically. However, if a caller
243 * knows that a StringInfo will eventually become X bytes large, it
244 * can save some palloc overhead by enlarging the buffer before starting
245 * to store data in it.
247 * NB: because we use repalloc() to enlarge the buffer, the string buffer
248 * will remain allocated in the same memory context that was current when
249 * initStringInfo was called, even if another context is now current.
250 * This is the desired and indeed critical behavior!
253 enlargeStringInfo(StringInfo str, int needed)
258 * Guard against out-of-range "needed" values. Without this, we can get
259 * an overflow or infinite loop in the following.
261 if (needed < 0) /* should not happen */
262 elog(ERROR, "invalid string enlargement request size: %d", needed);
263 if (((Size) needed) >= (MaxAllocSize - (Size) str->len))
265 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
266 errmsg("out of memory"),
267 errdetail("Cannot enlarge string buffer containing %d bytes by %d more bytes.",
270 needed += str->len + 1; /* total space required now */
272 /* Because of the above test, we now have needed <= MaxAllocSize */
274 if (needed <= str->maxlen)
275 return; /* got enough space already */
278 * We don't want to allocate just a little more space with each append;
279 * for efficiency, double the buffer size each time it overflows.
280 * Actually, we might need to more than double it if 'needed' is big...
282 newlen = 2 * str->maxlen;
283 while (needed > newlen)
287 * Clamp to MaxAllocSize in case we went past it. Note we are assuming
288 * here that MaxAllocSize <= INT_MAX/2, else the above loop could
289 * overflow. We will still have newlen >= needed.
291 if (newlen > (int) MaxAllocSize)
292 newlen = (int) MaxAllocSize;
294 str->data = (char *) repalloc(str->data, newlen);
296 str->maxlen = newlen;