]> git.saurik.com Git - redis.git/commitdiff
Add sds function that can be called with va_list
authorPieter Noordhuis <pcnoordhuis@gmail.com>
Thu, 2 Sep 2010 17:35:07 +0000 (19:35 +0200)
committerPieter Noordhuis <pcnoordhuis@gmail.com>
Thu, 2 Sep 2010 19:00:15 +0000 (21:00 +0200)
src/sds.c
src/sds.h

index a0ebb0591ee17ac9c181a5a89db1a920753227b0..2f3ffedcbf1d3ff8915ca033923e3fdbfa7269d2 100644 (file)
--- a/src/sds.c
+++ b/src/sds.c
@@ -33,7 +33,6 @@
 #include "sds.h"
 #include <stdio.h>
 #include <stdlib.h>
-#include <stdarg.h>
 #include <string.h>
 #include <ctype.h>
 #include "zmalloc.h"
@@ -156,8 +155,8 @@ sds sdscpy(sds s, char *t) {
     return sdscpylen(s, t, strlen(t));
 }
 
-sds sdscatprintf(sds s, const char *fmt, ...) {
-    va_list ap;
+sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
+    va_list cpy;
     char *buf, *t;
     size_t buflen = 16;
 
@@ -169,9 +168,8 @@ sds sdscatprintf(sds s, const char *fmt, ...) {
         if (buf == NULL) return NULL;
 #endif
         buf[buflen-2] = '\0';
-        va_start(ap, fmt);
-        vsnprintf(buf, buflen, fmt, ap);
-        va_end(ap);
+        va_copy(cpy,ap);
+        vsnprintf(buf, buflen, fmt, cpy);
         if (buf[buflen-2] != '\0') {
             zfree(buf);
             buflen *= 2;
@@ -184,6 +182,15 @@ sds sdscatprintf(sds s, const char *fmt, ...) {
     return t;
 }
 
+sds sdscatprintf(sds s, const char *fmt, ...) {
+    va_list ap;
+    char *t;
+    va_start(ap, fmt);
+    t = sdscatvprintf(s,fmt,ap);
+    va_end(ap);
+    return t;
+}
+
 sds sdstrim(sds s, const char *cset) {
     struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
     char *start, *end, *sp, *ep;
index a0e224f5ada8cc805478e8f863e4dc9dad5519c6..ae0f84fb5c4ec239698d09b9e167ccfce6dbc946 100644 (file)
--- a/src/sds.h
+++ b/src/sds.h
@@ -32,6 +32,7 @@
 #define __SDS_H
 
 #include <sys/types.h>
+#include <stdarg.h>
 
 typedef char *sds;
 
@@ -53,6 +54,7 @@ sds sdscat(sds s, char *t);
 sds sdscpylen(sds s, char *t, size_t len);
 sds sdscpy(sds s, char *t);
 
+sds sdscatvprintf(sds s, const char *fmt, va_list ap);
 #ifdef __GNUC__
 sds sdscatprintf(sds s, const char *fmt, ...)
     __attribute__((format(printf, 2, 3)));