aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2014-02-06 14:21:44 +0100
committerantirez <antirez@gmail.com>2014-02-06 14:52:22 +0100
commit7f49249674a15e6ead215e93eb84d57ace7e882d (patch)
tree9640d9f65e84e9a19baeea6a3e85effd40d3c6ce
parent33bb9cdecb836ef36009600f9e60d9f2c764b23b (diff)
downloadsds-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.c16
-rw-r--r--sds.h3
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);