X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/a4d1ba9a73e459d68423c4da623f8cf1663c70ba..b84186ff2dadea9fa67f512a743ab97196390f56:/sds.c diff --git a/sds.c b/sds.c index d2b7543e..feb1a621 100644 --- a/sds.c +++ b/sds.c @@ -1,6 +1,6 @@ /* SDSLib, A C dynamic strings library * - * Copyright (c) 2006-2009, Salvatore Sanfilippo + * Copyright (c) 2006-2010, Salvatore Sanfilippo * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,6 +28,8 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#define SDS_ABORT_ON_OOM + #include "sds.h" #include #include @@ -138,7 +140,7 @@ sds sdscpylen(sds s, char *t, size_t len) { size_t totlen = sh->free+sh->len; if (totlen < len) { - s = sdsMakeRoomFor(s,len-totlen); + s = sdsMakeRoomFor(s,len-sh->len); if (s == NULL) return NULL; sh = (void*) (s-(sizeof(struct sdshdr))); totlen = sh->free+sh->len; @@ -157,7 +159,7 @@ sds sdscpy(sds s, char *t) { sds sdscatprintf(sds s, const char *fmt, ...) { va_list ap; char *buf, *t; - size_t buflen = 32; + size_t buflen = 16; while(1) { buf = zmalloc(buflen); @@ -275,6 +277,10 @@ sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count) { if (tokens == NULL) sdsOomAbort(); #endif if (seplen < 1 || len < 0 || tokens == NULL) return NULL; + if (len == 0) { + *count = 0; + return tokens; + } for (j = 0; j < (len-(seplen-1)); j++) { /* make sure there is room for the next element and the final one */ if (slots < elements+2) { @@ -329,3 +335,25 @@ cleanup: } #endif } + +void sdsfreesplitres(sds *tokens, int count) { + if (!tokens) return; + while(count--) + sdsfree(tokens[count]); + zfree(tokens); +} + +sds sdsfromlonglong(long long value) { + char buf[32], *p; + unsigned long long v; + + v = (value < 0) ? -value : value; + p = buf+31; /* point to the last character */ + do { + *p-- = '0'+(v%10); + v /= 10; + } while(v); + if (value < 0) *p-- = '-'; + p++; + return sdsnewlen(p,32-(p-buf)); +}