X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/3c1bf4957e4c8aacd962a859e16cbcf2596f4edb..255eebe22167e00f74e359bc71718225d6bd70c8:/src/sds.c diff --git a/src/sds.c b/src/sds.c index ff477285..67e2d456 100644 --- a/src/sds.c +++ b/src/sds.c @@ -116,6 +116,25 @@ static sds sdsMakeRoomFor(sds s, size_t addlen) { return newsh->buf; } +/* Grow the sds to have the specified length. Bytes that were not part of + * the original length of the sds will be set to zero. */ +sds sdsgrowzero(sds s, size_t len) { + struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); + size_t totlen, curlen = sh->len; + + if (len <= curlen) return s; + s = sdsMakeRoomFor(s,len-curlen); + if (s == NULL) return NULL; + + /* Make sure added region doesn't contain garbage */ + sh = (void*)(s-(sizeof(struct sdshdr))); + memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */ + totlen = sh->len+sh->free; + sh->len = len; + sh->free = totlen-sh->len; + return s; +} + sds sdscatlen(sds s, void *t, size_t len) { struct sdshdr *sh; size_t curlen = sdslen(s); @@ -155,32 +174,6 @@ sds sdscpy(sds s, char *t) { return sdscpylen(s, t, strlen(t)); } -sds sdssetbit(sds s, size_t bit, int on) { - struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); - int byte = bit >> 3; - int reqlen = byte+1; - - if (reqlen > sh->len) { - size_t totlen; - - s = sdsMakeRoomFor(s,reqlen-sh->len); - if (s == NULL) return NULL; - sh = (void*)(s-(sizeof(struct sdshdr))); - - /* Make sure added region doesn't contain garbage */ - totlen = sh->len+sh->free; - memset(s+sh->len,0,sh->free+1); - sh->len = reqlen; - sh->free = totlen-sh->len; - } - - bit = 7 - (bit & 0x7); - on &= 0x1; - s[byte] |= on << bit; - s[byte] &= ~((!on) << bit); - return s; -} - sds sdscatvprintf(sds s, const char *fmt, va_list ap) { va_list cpy; char *buf, *t; @@ -312,7 +305,10 @@ sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count) { #ifdef SDS_ABORT_ON_OOM if (tokens == NULL) sdsOomAbort(); #endif - if (seplen < 1 || len < 0 || tokens == NULL) return NULL; + if (seplen < 1 || len < 0 || tokens == NULL) { + *count = 0; + return NULL; + } if (len == 0) { *count = 0; return tokens; @@ -367,6 +363,7 @@ cleanup: int i; for (i = 0; i < elements; i++) sdsfree(tokens[i]); zfree(tokens); + *count = 0; return NULL; } #endif