From 67fb8df78a8ba6b944296b703e5c40b2323ed4b0 Mon Sep 17 00:00:00 2001 From: XiongKezhi Date: Tue, 19 Nov 2019 17:40:45 +0800 Subject: [PATCH 1/2] fixed the empty tokens problem caused by the location the separater --- sds.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/sds.c b/sds.c index ba93282..2b28706 100644 --- a/sds.c +++ b/sds.c @@ -828,7 +828,7 @@ sds *sdssplitlen(const char *s, ssize_t len, const char *sep, int seplen, int *c return tokens; } for (j = 0; j < (len-(seplen-1)); j++) { - /* make sure there is room for the next element and the final one */ + /* make sure there is room for the next element or the final one */ if (slots < elements+2) { sds *newtokens; @@ -839,17 +839,21 @@ sds *sdssplitlen(const char *s, ssize_t len, const char *sep, int seplen, int *c } /* search the separator */ if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) { - tokens[elements] = sdsnewlen(s+start,j-start); - if (tokens[elements] == NULL) goto cleanup; - elements++; + if (j != 0) { /* make sure the separator is not at the beginning */ + tokens[elements] = sdsnewlen(s+start,j-start); + if (tokens[elements] == NULL) goto cleanup; + elements++; + } start = j+seplen; j = j+seplen-1; /* skip the separator */ } } /* Add the final element. We are sure there is room in the tokens array. */ - tokens[elements] = sdsnewlen(s+start,len-start); - if (tokens[elements] == NULL) goto cleanup; - elements++; + if (len-start != 0) { /* make sure the string is not ended with the separator */ + tokens[elements] = sdsnewlen(s+start,len-start); + if (tokens[elements] == NULL) goto cleanup; + elements++; + } *count = elements; return tokens; From ae91345b1232fdaafe6c6d90a15eedf66a9563ca Mon Sep 17 00:00:00 2001 From: XiongKezhi Date: Tue, 19 Nov 2019 17:48:17 +0800 Subject: [PATCH 2/2] reset comment --- sds.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sds.c b/sds.c index 2b28706..a4acaf2 100644 --- a/sds.c +++ b/sds.c @@ -828,7 +828,7 @@ sds *sdssplitlen(const char *s, ssize_t len, const char *sep, int seplen, int *c return tokens; } for (j = 0; j < (len-(seplen-1)); j++) { - /* make sure there is room for the next element or the final one */ + /* make sure there is room for the next element and the final one */ if (slots < elements+2) { sds *newtokens;