MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / strbuf_append_fmt_retry

Function strbuf_append_fmt_retry

deps/lua/src/strbuf.c:221–248  ·  view source on GitHub ↗

strbuf_append_fmt_retry() can be used when the there is no known * upper bound for the output string. */

Source from the content-addressed store, hash-verified

219/* strbuf_append_fmt_retry() can be used when the there is no known
220 * upper bound for the output string. */
221void strbuf_append_fmt_retry(strbuf_t *s, const char *fmt, ...)
222{
223 va_list arg;
224 int fmt_len, try;
225 int empty_len;
226
227 /* If the first attempt to append fails, resize the buffer appropriately
228 * and try again */
229 for (try = 0; ; try++) {
230 va_start(arg, fmt);
231 /* Append the new formatted string */
232 /* fmt_len is the length of the string required, excluding the
233 * trailing NULL */
234 empty_len = strbuf_empty_length(s);
235 /* Add 1 since there is also space to store the terminating NULL. */
236 fmt_len = vsnprintf(s->buf + s->length, empty_len + 1, fmt, arg);
237 va_end(arg);
238
239 if (fmt_len <= empty_len)
240 break; /* SUCCESS */
241 if (try > 0)
242 die("BUG: length of formatted string changed");
243
244 strbuf_resize(s, s->length + fmt_len);
245 }
246
247 s->length += fmt_len;
248}
249
250/* vi:ai et sw=4 ts=4:
251 */

Callers

nothing calls this directly

Calls 3

strbuf_empty_lengthFunction · 0.85
dieFunction · 0.85
strbuf_resizeFunction · 0.85

Tested by

no test coverage detected