MCPcopy Create free account
hub / github.com/antirez/botlib / sdscatvprintf

Function sdscatvprintf

sds.c:522–557  ·  view source on GitHub ↗

Like sdscatprintf() but gets va_list instead of being variadic. */

Source from the content-addressed store, hash-verified

520
521/* Like sdscatprintf() but gets va_list instead of being variadic. */
522sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
523 va_list cpy;
524 char staticbuf[1024], *buf = staticbuf, *t;
525 size_t buflen = strlen(fmt)*2;
526
527 /* We try to start using a static buffer for speed.
528 * If not possible we revert to heap allocation. */
529 if (buflen > sizeof(staticbuf)) {
530 buf = s_malloc(buflen);
531 if (buf == NULL) return NULL;
532 } else {
533 buflen = sizeof(staticbuf);
534 }
535
536 /* Try with buffers two times bigger every time we fail to
537 * fit the string in the current buffer size. */
538 while(1) {
539 buf[buflen-2] = '\0';
540 va_copy(cpy,ap);
541 vsnprintf(buf, buflen, fmt, cpy);
542 va_end(cpy);
543 if (buf[buflen-2] != '\0') {
544 if (buf != staticbuf) s_free(buf);
545 buflen *= 2;
546 buf = s_malloc(buflen);
547 if (buf == NULL) return NULL;
548 continue;
549 }
550 break;
551 }
552
553 /* Finally concat the obtained string to the SDS string and return it. */
554 t = sdscat(s, buf);
555 if (buf != staticbuf) s_free(buf);
556 return t;
557}
558
559/* Append to the sds string 's' a string obtained using printf-alike format
560 * specifier.

Callers 1

sdscatprintfFunction · 0.85

Calls 1

sdscatFunction · 0.85

Tested by

no test coverage detected