Add sds function that can be called with va_list
This commit is contained in:
parent
36c19d03e0
commit
60361e5aac
19
src/sds.c
19
src/sds.c
@ -33,7 +33,6 @@
|
|||||||
#include "sds.h"
|
#include "sds.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdarg.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include "zmalloc.h"
|
#include "zmalloc.h"
|
||||||
@ -156,8 +155,8 @@ sds sdscpy(sds s, char *t) {
|
|||||||
return sdscpylen(s, t, strlen(t));
|
return sdscpylen(s, t, strlen(t));
|
||||||
}
|
}
|
||||||
|
|
||||||
sds sdscatprintf(sds s, const char *fmt, ...) {
|
sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
|
||||||
va_list ap;
|
va_list cpy;
|
||||||
char *buf, *t;
|
char *buf, *t;
|
||||||
size_t buflen = 16;
|
size_t buflen = 16;
|
||||||
|
|
||||||
@ -169,9 +168,8 @@ sds sdscatprintf(sds s, const char *fmt, ...) {
|
|||||||
if (buf == NULL) return NULL;
|
if (buf == NULL) return NULL;
|
||||||
#endif
|
#endif
|
||||||
buf[buflen-2] = '\0';
|
buf[buflen-2] = '\0';
|
||||||
va_start(ap, fmt);
|
va_copy(cpy,ap);
|
||||||
vsnprintf(buf, buflen, fmt, ap);
|
vsnprintf(buf, buflen, fmt, cpy);
|
||||||
va_end(ap);
|
|
||||||
if (buf[buflen-2] != '\0') {
|
if (buf[buflen-2] != '\0') {
|
||||||
zfree(buf);
|
zfree(buf);
|
||||||
buflen *= 2;
|
buflen *= 2;
|
||||||
@ -184,6 +182,15 @@ sds sdscatprintf(sds s, const char *fmt, ...) {
|
|||||||
return t;
|
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) {
|
sds sdstrim(sds s, const char *cset) {
|
||||||
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
|
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
|
||||||
char *start, *end, *sp, *ep;
|
char *start, *end, *sp, *ep;
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
#define __SDS_H
|
#define __SDS_H
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
typedef char *sds;
|
typedef char *sds;
|
||||||
|
|
||||||
@ -53,6 +54,7 @@ sds sdscat(sds s, char *t);
|
|||||||
sds sdscpylen(sds s, char *t, size_t len);
|
sds sdscpylen(sds s, char *t, size_t len);
|
||||||
sds sdscpy(sds s, char *t);
|
sds sdscpy(sds s, char *t);
|
||||||
|
|
||||||
|
sds sdscatvprintf(sds s, const char *fmt, va_list ap);
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
sds sdscatprintf(sds s, const char *fmt, ...)
|
sds sdscatprintf(sds s, const char *fmt, ...)
|
||||||
__attribute__((format(printf, 2, 3)));
|
__attribute__((format(printf, 2, 3)));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user