diff --git a/sds.c b/sds.c index ba93282..a4acaf2 100644 --- a/sds.c +++ b/sds.c @@ -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;