aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/sds.c162
1 files changed, 81 insertions, 81 deletions
diff --git a/src/sds.c b/src/sds.c
index aee013f..6b0a633 100644
--- a/src/sds.c
+++ b/src/sds.c
@@ -44,7 +44,7 @@ int hex_digit_to_int(char c);
* The string is always null-termined (all the sds strings are, always) so
* even if you create an sds string with:
*
- * mystring = sdsnewlen("abc",3");
+ * mystring = sdsnewlen("abc", 3");
*
* You can print the string with printf() as there is an implicit \0 at the
* end of the string. However the string is binary safe and can contain
@@ -53,9 +53,9 @@ sds sdsnewlen(const void *init, size_t initlen) {
struct sdshdr *sh;
if (init) {
- sh = malloc(sizeof(struct sdshdr)+initlen+1);
+ sh = malloc(sizeof(struct sdshdr) + initlen + 1);
} else {
- sh = calloc(sizeof(struct sdshdr)+initlen+1,1);
+ sh = calloc(sizeof(struct sdshdr) + initlen + 1, 1);
}
if (sh == NULL) { return NULL; }
sh->len = initlen;
@@ -70,7 +70,7 @@ sds sdsnewlen(const void *init, size_t initlen) {
/* Create an empty (zero length) sds string. Even in this case the string
* always has an implicit null term. */
sds sdsempty(void) {
- return sdsnewlen("",0);
+ return sdsnewlen("", 0);
}
/* Create a new sds string starting from a null termined C string. */
@@ -107,7 +107,7 @@ void sdsfree(sds s) {
void sdsupdatelen(sds s) {
struct sdshdr *sh = sdsheader(s);
int reallen = strlen(s);
- sh->free += (sh->len-reallen);
+ sh->free += (sh->len - reallen);
sh->len = reallen;
}
@@ -136,13 +136,13 @@ sds sdsMakeRoomFor(sds s, size_t addlen) {
if (free >= addlen) { return s; }
len = sdslen(s);
sh = sdsheader(s);
- newlen = (len+addlen);
+ newlen = (len + addlen);
if (newlen < SDS_MAX_PREALLOC) {
newlen *= 2;
} else {
newlen += SDS_MAX_PREALLOC;
}
- newsh = realloc(sh, sizeof(struct sdshdr)+newlen+1);
+ newsh = realloc(sh, sizeof(struct sdshdr) + newlen + 1);
if (newsh == NULL) { return NULL; }
newsh->free = newlen - len;
@@ -159,7 +159,7 @@ sds sdsRemoveFreeSpace(sds s) {
struct sdshdr *sh;
sh = sdsheader(s);
- sh = realloc(sh, sizeof(struct sdshdr)+sh->len+1);
+ sh = realloc(sh, sizeof(struct sdshdr) + sh->len + 1);
sh->free = 0;
return sh->buf;
}
@@ -174,7 +174,7 @@ sds sdsRemoveFreeSpace(sds s) {
size_t sdsAllocSize(sds s) {
struct sdshdr *sh = sdsheader(s);
- return sizeof(*sh)+sh->len+sh->free+1;
+ return sizeof(*sh) + sh->len + sh->free + 1;
}
/* Increment the sds length and decrements the left free space at the
@@ -196,7 +196,7 @@ size_t sdsAllocSize(sds s) {
*
* oldlen = sdslen(s);
* s = sdsMakeRoomFor(s, BUFFER_SIZE);
- * nread = read(fd, s+oldlen, BUFFER_SIZE);
+ * nread = read(fd, s + oldlen, BUFFER_SIZE);
* ... check for nread <= 0 and handle it ...
* sdsIncrLen(s, nread);
*/
@@ -220,15 +220,15 @@ sds sdsgrowzero(sds s, size_t len) {
size_t totlen, curlen = sh->len;
if (len <= curlen) { return s; }
- s = sdsMakeRoomFor(s,len-curlen);
+ s = sdsMakeRoomFor(s, len - curlen);
if (s == NULL) { return NULL; }
/* Make sure added region doesn't contain garbage */
sh = sdsheader(s);
- memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */
- totlen = sh->len+sh->free;
+ 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;
+ sh->free = totlen - sh->len;
return s;
}
@@ -241,13 +241,13 @@ sds sdscatlen(sds s, const void *t, size_t len) {
struct sdshdr *sh;
size_t curlen = sdslen(s);
- s = sdsMakeRoomFor(s,len);
+ s = sdsMakeRoomFor(s, len);
if (s == NULL) { return NULL; }
sh = sdsheader(s);
- memcpy(s+curlen, t, len);
- sh->len = curlen+len;
- sh->free = sh->free-len;
- s[curlen+len] = '\0';
+ memcpy(s + curlen, t, len);
+ sh->len = curlen + len;
+ sh->free = sh->free - len;
+ s[curlen + len] = '\0';
return s;
}
@@ -271,18 +271,18 @@ sds sdscatsds(sds s, const sds t) {
* safe string pointed by 't' of length 'len' bytes. */
sds sdscpylen(sds s, const char *t, size_t len) {
struct sdshdr *sh = sdsheader(s);
- size_t totlen = sh->free+sh->len;
+ size_t totlen = sh->free + sh->len;
if (totlen < len) {
- s = sdsMakeRoomFor(s,len-sh->len);
+ s = sdsMakeRoomFor(s, len - sh->len);
if (s == NULL) { return NULL; }
sh = sdsheader(s);
- totlen = sh->free+sh->len;
+ totlen = sh->free + sh->len;
}
memcpy(s, t, len);
s[len] = '\0';
sh->len = len;
- sh->free = totlen-len;
+ sh->free = totlen - len;
return s;
}
@@ -302,10 +302,10 @@ sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
while(1) {
buf = malloc(buflen);
if (buf == NULL) { return NULL; }
- buf[buflen-2] = '\0';
- va_copy(cpy,ap);
+ buf[buflen - 2] = '\0';
+ va_copy(cpy, ap);
vsnprintf(buf, buflen, fmt, cpy);
- if (buf[buflen-2] != '\0') {
+ if (buf[buflen - 2] != '\0') {
free(buf);
buflen *= 2;
continue;
@@ -327,7 +327,7 @@ sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
* Example:
*
* s = sdsempty("Sum is: ");
- * s = sdscatprintf(s,"%d+%d = %d",a,b,a+b).
+ * s = sdscatprintf(s, "%d + %d = %d", a, b, a + b).
*
* Often you need to create a string from scratch with the printf-alike
* format. When this is the need, just use sdsempty() as the target string:
@@ -338,7 +338,7 @@ sds sdscatprintf(sds s, const char *fmt, ...) {
va_list ap;
char *t;
va_start(ap, fmt);
- t = sdscatvprintf(s,fmt,ap);
+ t = sdscatvprintf(s, fmt, ap);
va_end(ap);
return t;
}
@@ -352,7 +352,7 @@ sds sdscatprintf(sds s, const char *fmt, ...) {
* Example:
*
* s = sdsnew("AA...AA.a.aa.aHelloWorld :::");
- * s = sdstrim(s,"A. :");
+ * s = sdstrim(s, "A. :");
* printf("%s\n", s);
*
* Output will be just "Hello World".
@@ -363,13 +363,13 @@ void sdstrim(sds s, const char *cset) {
size_t len;
sp = start = s;
- ep = end = s+sdslen(s)-1;
+ ep = end = s + sdslen(s) - 1;
while(sp <= end && strchr(cset, *sp)) { sp++; }
while(ep > start && strchr(cset, *ep)) { ep--; }
- len = (sp > ep) ? 0 : ((ep-sp)+1);
+ len = (sp > ep) ? 0 : ((ep - sp) + 1);
if (sh->buf != sp) { memmove(sh->buf, sp, len); }
sh->buf[len] = '\0';
- sh->free = sh->free+(sh->len-len);
+ sh->free = sh->free + (sh->len - len);
sh->len = len;
}
@@ -387,7 +387,7 @@ void sdstrim(sds s, const char *cset) {
* Example:
*
* s = sdsnew("Hello World");
- * sdsrange(s,1,-1); => "ello World"
+ * sdsrange(s, 1, -1); => "ello World"
*/
void sdsrange(sds s, ptrdiff_t start, ptrdiff_t end) {
struct sdshdr *sh = sdsheader(s);
@@ -395,27 +395,27 @@ void sdsrange(sds s, ptrdiff_t start, ptrdiff_t end) {
if (len == 0) { return; }
if (start < 0) {
- start = len+start;
+ start = len + start;
if (start < 0) { start = 0; }
}
if (end < 0) {
- end = len+end;
+ end = len + end;
if (end < 0) { end = 0; }
}
- newlen = (start > end) ? 0 : (end-start)+1;
+ newlen = (start > end) ? 0 : (end - start) + 1;
if (newlen != 0) {
if (start >= (signed)len) {
newlen = 0;
} else if (end >= (signed)len) {
- end = len-1;
- newlen = (start > end) ? 0 : (end-start)+1;
+ end = len - 1;
+ newlen = (start > end) ? 0 : (end - start) + 1;
}
} else {
start = 0;
}
- if (start && newlen) { memmove(sh->buf, sh->buf+start, newlen); }
+ if (start && newlen) { memmove(sh->buf, sh->buf + start, newlen); }
sh->buf[newlen] = 0;
- sh->free = sh->free+(sh->len-newlen);
+ sh->free = sh->free + (sh->len - newlen);
sh->len = newlen;
}
@@ -451,8 +451,8 @@ int sdscmp(const sds s1, const sds s2) {
l1 = sdslen(s1);
l2 = sdslen(s2);
minlen = (l1 < l2) ? l1 : l2;
- cmp = memcmp(s1,s2,minlen);
- if (cmp == 0) { return l1-l2; }
+ cmp = memcmp(s1, s2, minlen);
+ if (cmp == 0) { return (l1 - l2); }
return cmp;
}
@@ -465,7 +465,7 @@ int sdscmp(const sds s1, const sds s2) {
*
* Note that 'sep' is able to split a string using
* a multi-character separator. For example
- * sdssplit("foo_-_bar","_-_"); will return two
+ * sdssplit("foo_-_bar", "_-_"); will return two
* elements "foo" and "bar".
*
* This version of the function is binary-safe but
@@ -485,23 +485,23 @@ sds *sdssplitlen(const char *s, size_t len, const char *sep, size_t seplen, size
*count = 0;
return tokens;
}
- for (j = 0; j < (len-(seplen-1)); j++) {
+ 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) {
+ if (slots < elements + 2) {
sds *newtokens;
slots *= 2;
- newtokens = realloc(tokens,sizeof(sds)*slots);
+ newtokens = realloc(tokens, sizeof(sds) * slots);
if (newtokens == NULL) { goto cleanup; }
tokens = newtokens;
}
/* search the separator */
- if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) {
- tokens[elements] = sdsnewlen(s+start,j-start);
+ 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++;
- start = j+seplen;
- j = j+seplen-1; /* skip the separator */
+ start = j + seplen;
+ j = j + seplen - 1; /* skip the separator */
}
}
/* Add the final element. We are sure there is room in the tokens array. */
@@ -532,16 +532,16 @@ void sdsfreesplitres(sds *tokens, int count) {
/* Create an sds string from a long long value. It is much faster than:
*
- * sdscatprintf(sdsempty(),"%lld\n", value);
+ * sdscatprintf(sdsempty(), "%lld\n", value);
*/
sds sdsfromlonglong(long long value) {
char buf[32], *p;
unsigned long long v;
v = (unsigned long long)((value < 0) ? -value : value);
- p = buf+31; /* point to the last character */
+ p = buf + 31; /* point to the last character */
do {
- *p-- = '0'+(v%10);
+ *p-- = '0' + (v%10);
v /= 10;
} while(v);
if (value < 0) { *p-- = '-'; }
@@ -556,29 +556,29 @@ sds sdsfromlonglong(long long value) {
* After the call, the modified sds string is no longer valid and all the
* references must be substituted with the new pointer returned by the call. */
sds sdscatrepr(sds s, const char *p, size_t len) {
- s = sdscatlen(s,"\"",1);
+ s = sdscatlen(s, "\"", 1);
while(len--) {
switch(*p) {
case '\\':
case '"':
- s = sdscatprintf(s,"\\%c",*p);
+ s = sdscatprintf(s, "\\%c", *p);
break;
- case '\n': s = sdscatlen(s,"\\n",2); break;
- case '\r': s = sdscatlen(s,"\\r",2); break;
- case '\t': s = sdscatlen(s,"\\t",2); break;
- case '\a': s = sdscatlen(s,"\\a",2); break;
- case '\b': s = sdscatlen(s,"\\b",2); break;
+ case '\n': s = sdscatlen(s, "\\n", 2); break;
+ case '\r': s = sdscatlen(s, "\\r", 2); break;
+ case '\t': s = sdscatlen(s, "\\t", 2); break;
+ case '\a': s = sdscatlen(s, "\\a", 2); break;
+ case '\b': s = sdscatlen(s, "\\b", 2); break;
default:
if (isprint(*p)) {
- s = sdscatprintf(s,"%c",*p);
+ s = sdscatprintf(s, "%c", *p);
} else {
- s = sdscatprintf(s,"\\x%02x",(unsigned char)*p);
+ s = sdscatprintf(s, "\\x%02x", (unsigned char)*p);
break;
}
}
p++;
}
- return sdscatlen(s,"\"",1);
+ return sdscatlen(s, "\"", 1);
}
/* Helper function for sdssplitargs() that returns non zero if 'c'
@@ -649,17 +649,17 @@ sds *sdssplitargs(const char *line, int *argc) {
if (current == NULL) { current = sdsempty(); }
while(!done) {
if (inq) {
- if (*p == '\\' && *(p+1) == 'x' &&
- is_hex_digit(*(p+2)) &&
- is_hex_digit(*(p+3)))
+ if (*p == '\\' && *(p + 1) == 'x' &&
+ is_hex_digit(*(p + 2)) &&
+ is_hex_digit(*(p + 3)))
{
unsigned char byte;
- byte = (hex_digit_to_int(*(p+2))*16)+
- hex_digit_to_int(*(p+3));
- current = sdscatlen(current,(char*)&byte,1);
+ byte = ((hex_digit_to_int(*(p + 2))*16) +
+ hex_digit_to_int(*(p + 3)));
+ current = sdscatlen(current, (char*)&byte, 1);
p += 3;
- } else if (*p == '\\' && *(p+1)) {
+ } else if (*p == '\\' && *(p + 1)) {
char c;
p++;
@@ -671,32 +671,32 @@ sds *sdssplitargs(const char *line, int *argc) {
case 'a': c = '\a'; break;
default: c = *p; break;
}
- current = sdscatlen(current,&c,1);
+ current = sdscatlen(current, &c, 1);
} else if (*p == '"') {
/* closing quote must be followed by a space or
* nothing at all. */
- if (*(p+1) && !isspace(*(p+1))) { goto err; }
+ if (*(p + 1) && !isspace(*(p + 1))) { goto err; }
done=1;
} else if (!*p) {
/* unterminated quotes */
goto err;
} else {
- current = sdscatlen(current,p,1);
+ current = sdscatlen(current, p, 1);
}
} else if (insq) {
- if (*p == '\\' && *(p+1) == '\'') {
+ if (*p == '\\' && *(p + 1) == '\'') {
p++;
- current = sdscatlen(current,"'",1);
+ current = sdscatlen(current, "'", 1);
} else if (*p == '\'') {
/* closing quote must be followed by a space or
* nothing at all. */
- if (*(p+1) && !isspace(*(p+1))) { goto err; }
+ if (*(p + 1) && !isspace(*(p + 1))) { goto err; }
done=1;
} else if (!*p) {
/* unterminated quotes */
goto err;
} else {
- current = sdscatlen(current,p,1);
+ current = sdscatlen(current, p, 1);
}
} else {
switch(*p) {
@@ -714,14 +714,14 @@ sds *sdssplitargs(const char *line, int *argc) {
insq=1;
break;
default:
- current = sdscatlen(current,p,1);
+ current = sdscatlen(current, p, 1);
break;
}
}
if (*p) { p++; }
}
/* add the token to the vector */
- vector = realloc(vector,((*argc)+1)*sizeof(char*));
+ vector = realloc(vector, ((*argc) + 1) * (sizeof (char *)));
vector[*argc] = current;
(*argc)++;
current = NULL;
@@ -773,7 +773,7 @@ sds sdsjoin(char **argv, int argc, char *sep, size_t seplen) {
for (j = 0; j < argc; j++) {
join = sdscat(join, argv[j]);
- if (j != argc-1) { join = sdscatlen(join,sep,seplen); }
+ if (j != argc - 1) { join = sdscatlen(join, sep, seplen); }
}
return join;
}
@@ -785,7 +785,7 @@ sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) {
for (j = 0; j < argc; j++) {
join = sdscatsds(join, argv[j]);
- if (j != argc-1) { join = sdscatlen(join,sep,seplen); }
+ if (j != argc - 1) { join = sdscatlen(join, sep, seplen); }
}
return join;
}