From 7f49249674a15e6ead215e93eb84d57ace7e882d Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 6 Feb 2014 14:21:44 +0100 Subject: Join functions improved. The original implementation of sdsjoin did not allowed a binary separator nor was able to handle sds strings contaning bianry data. sdsjoin() was modified to get a spearator length. sdsjoinsds() was added to join arrays of SDS strings. --- sds.c | 16 ++++++++++++++-- sds.h | 3 ++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/sds.c b/sds.c index 5cdb152..c0d4a3e 100644 --- a/sds.c +++ b/sds.c @@ -759,13 +759,25 @@ sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) { /* Join an array of C strings using the specified separator (also a C string). * Returns the result as an sds string. */ -sds sdsjoin(char **argv, int argc, char *sep) { +sds sdsjoin(char **argv, int argc, char *sep, size_t seplen) { sds join = sdsempty(); int j; for (j = 0; j < argc; j++) { join = sdscat(join, argv[j]); - if (j != argc-1) join = sdscat(join,sep); + if (j != argc-1) join = sdscatlen(join,sep,seplen); + } + return join; +} + +/* Like sdsjoin, but joins an array of SDS strings. */ +sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) { + sds join = sdsempty(); + int j; + + for (j = 0; j < argc; j++) { + join = sdscatsds(join, argv[j]); + if (j != argc-1) join = sdscatlen(join,sep,seplen); } return join; } diff --git a/sds.h b/sds.h index a188fcc..54c47e0 100644 --- a/sds.h +++ b/sds.h @@ -89,7 +89,8 @@ sds sdsfromlonglong(long long value); sds sdscatrepr(sds s, const char *p, size_t len); sds *sdssplitargs(const char *line, int *argc); sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen); -sds sdsjoin(char **argv, int argc, char *sep); +sds sdsjoin(char **argv, int argc, char *sep, size_t seplen); +sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen); /* Low level functions exposed to the user API */ sds sdsMakeRoomFor(sds s, size_t addlen); -- cgit v1.2.3-70-g09d2