diff options
author | antirez <antirez@gmail.com> | 2014-02-06 14:21:44 +0100 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2014-02-06 14:52:22 +0100 |
commit | 7f49249674a15e6ead215e93eb84d57ace7e882d (patch) | |
tree | 9640d9f65e84e9a19baeea6a3e85effd40d3c6ce | |
parent | 33bb9cdecb836ef36009600f9e60d9f2c764b23b (diff) | |
download | sds-7f49249674a15e6ead215e93eb84d57ace7e882d.tar.xz |
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.
-rw-r--r-- | sds.c | 16 | ||||
-rw-r--r-- | sds.h | 3 |
2 files changed, 16 insertions, 3 deletions
@@ -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; } @@ -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); |