From 0fce5bca6498ce007dfcd7d537c7e7c4d54cf6f6 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sun, 18 Jan 2015 04:15:56 +0100 Subject: sds.c: Add two function prototypes is_hex_digit and hex_digit_to_int functions had no function prototypes. Silences two warnings. --- src/sds.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src') diff --git a/src/sds.c b/src/sds.c index f0515f1..f3396c9 100644 --- a/src/sds.c +++ b/src/sds.c @@ -33,6 +33,9 @@ #include "sds.h" +int is_hex_digit(char c); +int hex_digit_to_int(char c); + /* Create a new sds string with the content specified by the 'init' pointer * and 'initlen'. * If NULL is used for 'init' the string is initialized with zero bytes. -- cgit v1.2.3-70-g09d2 From 2bf92c344eb50a72d730eb27aaea208ef5539181 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sun, 18 Jan 2015 15:36:39 +0100 Subject: sds.c: Disable format-nonliteral warning around sdscatvprintf --- src/sds.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/sds.c b/src/sds.c index f3396c9..0e62f31 100644 --- a/src/sds.c +++ b/src/sds.c @@ -292,6 +292,7 @@ sds sdscpy(sds s, const char *t) { } /* Like sdscatpritf() but gets va_list instead of being variadic. */ +#pragma GCC diagnostic ignored "-Wformat-nonliteral" sds sdscatvprintf(sds s, const char *fmt, va_list ap) { va_list cpy; char *buf, *t; @@ -314,6 +315,7 @@ sds sdscatvprintf(sds s, const char *fmt, va_list ap) { free(buf); return t; } +#pragma GCC diagnostic warning "-Wformat-nonliteral" /* Append to the sds string 's' a string obtained using printf-alike format * specifier. -- cgit v1.2.3-70-g09d2 From a3d3b21dbdad912fcca74ba2263d007c764b9ea3 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sun, 18 Jan 2015 15:38:11 +0100 Subject: sds.h: Change len and free to size_t --- src/sds.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/sds.h b/src/sds.h index 489e623..86e2a8c 100644 --- a/src/sds.h +++ b/src/sds.h @@ -31,13 +31,14 @@ #define SDS_MAX_PREALLOC (1024*1024) #include +#include #include typedef char *sds; struct sdshdr { - int len; - int free; + size_t len; + size_t free; char buf[]; }; -- cgit v1.2.3-70-g09d2 From 459d2cb217e1af053f08d7e30a92b1b6bf61a90a Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sun, 18 Jan 2015 15:41:09 +0100 Subject: sdsrange: change start/end args to ptrdiff_t MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An sds string can hold a much bigger value than an int can hold, but since size_t is unsigned it can’t be used for this function. Using ptrdiff_t limits the function to work on roughly 1 Exabyte long strings, but if you need to work with longer ones you probably will have your custom string library already. --- src/sds.c | 3 ++- src/sds.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/sds.c b/src/sds.c index 0e62f31..96a6c4c 100644 --- a/src/sds.c +++ b/src/sds.c @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -388,7 +389,7 @@ void sdstrim(sds s, const char *cset) { * s = sdsnew("Hello World"); * sdsrange(s,1,-1); => "ello World" */ -void sdsrange(sds s, int start, int end) { +void sdsrange(sds s, ptrdiff_t start, ptrdiff_t end) { struct sdshdr *sh = sdsheader(s); size_t newlen, len = sdslen(s); diff --git a/src/sds.h b/src/sds.h index 86e2a8c..c0fe90e 100644 --- a/src/sds.h +++ b/src/sds.h @@ -77,7 +77,7 @@ sds sdscatprintf(sds s, const char *fmt, ...); #endif void sdstrim(sds s, const char *cset); -void sdsrange(sds s, int start, int end); +void sdsrange(sds s, ptrdiff_t start, ptrdiff_t end); void sdsupdatelen(sds s); void sdsclear(sds s); int sdscmp(const sds s1, const sds s2); -- cgit v1.2.3-70-g09d2 From 6b0750a000900d2ff775e6792797d64174e509c1 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sun, 18 Jan 2015 15:43:51 +0100 Subject: sdsIncrLen: change incr argument to size_t --- src/sds.c | 2 +- src/sds.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/sds.c b/src/sds.c index 96a6c4c..e68a401 100644 --- a/src/sds.c +++ b/src/sds.c @@ -200,7 +200,7 @@ size_t sdsAllocSize(sds s) { * ... check for nread <= 0 and handle it ... * sdsIncrLen(s, nread); */ -void sdsIncrLen(sds s, int incr) { +void sdsIncrLen(sds s, size_t incr) { struct sdshdr *sh = sdsheader(s); assert(sh->free >= incr); diff --git a/src/sds.h b/src/sds.h index c0fe90e..a46b06b 100644 --- a/src/sds.h +++ b/src/sds.h @@ -94,7 +94,7 @@ sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen); /* Low level functions exposed to the user API */ sds sdsMakeRoomFor(sds s, size_t addlen); -void sdsIncrLen(sds s, int incr); +void sdsIncrLen(sds s, size_t incr); sds sdsRemoveFreeSpace(sds s); size_t sdsAllocSize(sds s); -- cgit v1.2.3-70-g09d2 From 5079d22319930f47647d1ed9507ad07ec0b922d0 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sun, 18 Jan 2015 15:45:20 +0100 Subject: test.c: Make test_list static const, fix oldfree type --- src/test.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/test.c b/src/test.c index 3d71f6f..d2ad304 100644 --- a/src/test.c +++ b/src/test.c @@ -27,7 +27,7 @@ bool sdsIncrLen_content (void); bool sdsIncrLen_len (void); bool sdsIncrLen_free (void); -struct test test_list [] = { +static const struct test test_list [] = { { "create a string and obtain the length", check_string_length }, { "create a string with specified length", create_with_length }, { "string concatenation", string_concat }, @@ -216,7 +216,7 @@ sdsIncrLen_free (void) { _sds_cleanup_ sds x = sdsnew("0"); x = sdsMakeRoomFor(x, 1); struct sdshdr *sh = (void*) (x-(sizeof(struct sdshdr))); - int oldfree = sh->free; + size_t oldfree = sh->free; x[1] = '1'; sdsIncrLen(x, 1); return (sh->free == oldfree-1); -- cgit v1.2.3-70-g09d2 From 021b386ddac3384643d9342683cb448047f83a1a Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sun, 18 Jan 2015 15:46:39 +0100 Subject: sdsheader: cast pointer through void pointer Because a char* and struct sdshdr pointer have a different alignment you get the following warning when try to cast it directly: warning: cast from 'sds' (aka 'char *') to 'struct sdshdr *' increases required alignment from 1 to 8 [-Wcast-align] To silence the warning we cast it to a void pointer first. --- src/sds.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/sds.h b/src/sds.h index a46b06b..d0830fe 100644 --- a/src/sds.h +++ b/src/sds.h @@ -43,7 +43,9 @@ struct sdshdr { }; static inline struct sdshdr * sdsheader(const sds s) { - return s - (sizeof (struct sdshdr)); + /* The sdshdr pointer has a different alignment than the original char + * pointer, so cast it through a void pointer to silence the warning. */ + return (void *)(s - (sizeof (struct sdshdr))); } static inline size_t sdslen(const sds s) { -- cgit v1.2.3-70-g09d2 From 6aa8c1d8976ad3a609f6600966ad48582d024d5e Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sun, 18 Jan 2015 15:51:30 +0100 Subject: sdssplitlen: Change length arguments to size_t --- src/sds.c | 6 +++--- src/sds.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/sds.c b/src/sds.c index e68a401..aee013f 100644 --- a/src/sds.c +++ b/src/sds.c @@ -472,11 +472,11 @@ int sdscmp(const sds s1, const sds s2) { * requires length arguments. sdssplit() is just the * same function but for zero-terminated strings. */ -sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count) { - int elements = 0, slots = 5, start = 0, j; +sds *sdssplitlen(const char *s, size_t len, const char *sep, size_t seplen, size_t *count) { + size_t elements = 0, slots = 5, start = 0, j; sds *tokens; - if (seplen < 1 || len < 0) { return NULL; } + if (seplen < 1) { return NULL; } tokens = malloc(sizeof(sds)*slots); if (tokens == NULL) { return NULL; } diff --git a/src/sds.h b/src/sds.h index d0830fe..13c94de 100644 --- a/src/sds.h +++ b/src/sds.h @@ -83,7 +83,7 @@ void sdsrange(sds s, ptrdiff_t start, ptrdiff_t end); void sdsupdatelen(sds s); void sdsclear(sds s); int sdscmp(const sds s1, const sds s2); -sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count); +sds *sdssplitlen(const char *s, size_t len, const char *sep, size_t seplen, size_t *count); void sdsfreesplitres(sds *tokens, int count); void sdstolower(sds s); void sdstoupper(sds s); -- cgit v1.2.3-70-g09d2 From f97d7f2eb8c948eb1334036ff0c1e9fbefbc2e06 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sun, 18 Jan 2015 15:54:33 +0100 Subject: sds.c: Change spacing around arguments and operators --- src/sds.c | 162 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 81 insertions(+), 81 deletions(-) (limited to 'src') 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; } -- cgit v1.2.3-70-g09d2 From dd96070610efd6fd6bf887f7680b76dda37a5c1a Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sun, 18 Jan 2015 16:10:03 +0100 Subject: sds.c: Change remaining len vars to size_t --- src/sds.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/sds.c b/src/sds.c index 6b0a633..b7bf98f 100644 --- a/src/sds.c +++ b/src/sds.c @@ -106,7 +106,7 @@ void sdsfree(sds s) { * remains 6 bytes. */ void sdsupdatelen(sds s) { struct sdshdr *sh = sdsheader(s); - int reallen = strlen(s); + size_t reallen = strlen(s); sh->free += (sh->len - reallen); sh->len = reallen; } @@ -421,14 +421,14 @@ void sdsrange(sds s, ptrdiff_t start, ptrdiff_t end) { /* Apply tolower() to every character of the sds string 's'. */ void sdstolower(sds s) { - int len = sdslen(s), j; + size_t len = sdslen(s), j; for (j = 0; j < len; j++) s[j] = tolower(s[j]); } /* Apply toupper() to every character of the sds string 's'. */ void sdstoupper(sds s) { - int len = sdslen(s), j; + size_t len = sdslen(s), j; for (j = 0; j < len; j++) s[j] = toupper(s[j]); } @@ -513,7 +513,7 @@ sds *sdssplitlen(const char *s, size_t len, const char *sep, size_t seplen, size cleanup: { - int i; + size_t i; for (i = 0; i < elements; i++) sdsfree(tokens[i]); free(tokens); *count = 0; -- cgit v1.2.3-70-g09d2 From d2a12b1e97815ffebcfd1bc5355e8c374ab32adb Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sun, 18 Jan 2015 16:10:55 +0100 Subject: sds.c: Add explicit casts everywhere Silences a /lot/ of warnings. --- src/sds.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/sds.c b/src/sds.c index b7bf98f..13e4634 100644 --- a/src/sds.c +++ b/src/sds.c @@ -366,7 +366,7 @@ void sdstrim(sds s, const char *cset) { 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 = (size_t)((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); @@ -395,20 +395,20 @@ void sdsrange(sds s, ptrdiff_t start, ptrdiff_t end) { if (len == 0) { return; } if (start < 0) { - start = len + start; + start = (ptrdiff_t)len + start; if (start < 0) { start = 0; } } if (end < 0) { - end = len + end; + end = (ptrdiff_t)len + end; if (end < 0) { end = 0; } } - newlen = (start > end) ? 0 : (end - start) + 1; + newlen = (size_t)((start > end) ? 0 : (end - start) + 1); if (newlen != 0) { - if (start >= (signed)len) { + if ((size_t)start >= len) { newlen = 0; - } else if (end >= (signed)len) { - end = len - 1; - newlen = (start > end) ? 0 : (end - start) + 1; + } else if ((size_t)end >= len) { + end = (ptrdiff_t)len - 1; + newlen = (size_t)((start > end) ? 0 : (end - start) + 1); } } else { start = 0; @@ -423,14 +423,14 @@ void sdsrange(sds s, ptrdiff_t start, ptrdiff_t end) { void sdstolower(sds s) { size_t len = sdslen(s), j; - for (j = 0; j < len; j++) s[j] = tolower(s[j]); + for (j = 0; j < len; j++) s[j] = (char)tolower(s[j]); } /* Apply toupper() to every character of the sds string 's'. */ void sdstoupper(sds s) { size_t len = sdslen(s), j; - for (j = 0; j < len; j++) s[j] = toupper(s[j]); + for (j = 0; j < len; j++) s[j] = (char)toupper(s[j]); } /* Compare two sds strings s1 and s2 with memcmp(). @@ -452,7 +452,7 @@ int sdscmp(const sds s1, const sds s2) { l2 = sdslen(s2); minlen = (l1 < l2) ? l1 : l2; cmp = memcmp(s1, s2, minlen); - if (cmp == 0) { return (l1 - l2); } + if (cmp == 0) { return (int)(l1 - l2); } return cmp; } @@ -497,7 +497,7 @@ sds *sdssplitlen(const char *s, size_t len, const char *sep, size_t seplen, size } /* search the separator */ if ((seplen == 1 && *(s + j) == sep[0]) || (memcmp(s + j, sep, seplen) == 0)) { - tokens[elements] = sdsnewlen(s + start, j - start); + tokens[elements] = sdsnewlen(s + start, (size_t)(j - start)); if (tokens[elements] == NULL) { goto cleanup; } elements++; start = j + seplen; @@ -505,7 +505,7 @@ sds *sdssplitlen(const char *s, size_t len, const char *sep, size_t seplen, size } } /* Add the final element. We are sure there is room in the tokens array. */ - tokens[elements] = sdsnewlen(s+start,len-start); + tokens[elements] = sdsnewlen(s + start, (size_t)(len - start)); if (tokens[elements] == NULL) { goto cleanup; } elements++; *count = elements; @@ -546,7 +546,7 @@ sds sdsfromlonglong(long long value) { } while(v); if (value < 0) { *p-- = '-'; } p++; - return sdsnewlen(p,32-(p-buf)); + return sdsnewlen(p, (size_t)(32 - (p - buf))); } /* Append to the sds string "s" an escaped string representation where @@ -655,8 +655,8 @@ sds *sdssplitargs(const char *line, int *argc) { { unsigned char byte; - byte = ((hex_digit_to_int(*(p + 2))*16) + - hex_digit_to_int(*(p + 3))); + byte = (unsigned char)((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)) { @@ -721,7 +721,7 @@ sds *sdssplitargs(const char *line, int *argc) { if (*p) { p++; } } /* add the token to the vector */ - vector = realloc(vector, ((*argc) + 1) * (sizeof (char *))); + vector = realloc(vector, (unsigned long)((*argc) + 1) * (sizeof (char *))); vector[*argc] = current; (*argc)++; current = NULL; -- cgit v1.2.3-70-g09d2 From d7d703e33aa65a4818a05bc678fe62750390ed67 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sun, 18 Jan 2015 17:36:39 +0100 Subject: Move variables into for loop, remove unnevessary lens --- src/sds.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/sds.c b/src/sds.c index 13e4634..0179e60 100644 --- a/src/sds.c +++ b/src/sds.c @@ -421,16 +421,16 @@ void sdsrange(sds s, ptrdiff_t start, ptrdiff_t end) { /* Apply tolower() to every character of the sds string 's'. */ void sdstolower(sds s) { - size_t len = sdslen(s), j; - - for (j = 0; j < len; j++) s[j] = (char)tolower(s[j]); + for (size_t j = 0; j < sdslen(s); j++) { + s[j] = (char)tolower(s[j]); + } } /* Apply toupper() to every character of the sds string 's'. */ void sdstoupper(sds s) { - size_t len = sdslen(s), j; - - for (j = 0; j < len; j++) s[j] = (char)toupper(s[j]); + for (size_t j = 0; j < sdslen(s); j++) { + s[j] = (char)toupper(s[j]); + } } /* Compare two sds strings s1 and s2 with memcmp(). @@ -473,7 +473,7 @@ int sdscmp(const sds s1, const sds s2) { * same function but for zero-terminated strings. */ sds *sdssplitlen(const char *s, size_t len, const char *sep, size_t seplen, size_t *count) { - size_t elements = 0, slots = 5, start = 0, j; + size_t elements = 0, slots = 5, start = 0; sds *tokens; if (seplen < 1) { return NULL; } @@ -485,7 +485,7 @@ 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 (size_t j = 0; j < (len - (seplen - 1)); j++) { /* make sure there is room for the next element and the final one */ if (slots < elements + 2) { sds *newtokens; @@ -513,8 +513,7 @@ sds *sdssplitlen(const char *s, size_t len, const char *sep, size_t seplen, size cleanup: { - size_t i; - for (i = 0; i < elements; i++) sdsfree(tokens[i]); + for (size_t i = 0; i < elements; i++) sdsfree(tokens[i]); free(tokens); *count = 0; return NULL; @@ -752,10 +751,8 @@ err: * The function returns the sds string pointer, that is always the same * as the input pointer since no resize is needed. */ sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) { - size_t j, i, l = sdslen(s); - - for (j = 0; j < l; j++) { - for (i = 0; i < setlen; i++) { + for (size_t j = 0; j < sdslen(s); j++) { + for (size_t i = 0; i < setlen; i++) { if (s[j] == from[i]) { s[j] = to[i]; break; @@ -769,9 +766,8 @@ sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) { * Returns the result as an sds string. */ sds sdsjoin(char **argv, int argc, char *sep, size_t seplen) { sds join = sdsempty(); - int j; - for (j = 0; j < argc; j++) { + for (int j = 0; j < argc; j++) { join = sdscat(join, argv[j]); if (j != argc - 1) { join = sdscatlen(join, sep, seplen); } } @@ -781,9 +777,8 @@ sds sdsjoin(char **argv, int argc, char *sep, size_t seplen) { /* Like sdsjoin, but joins an array of SDS strings. */ sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) { sds join = sdsempty(); - int j; - for (j = 0; j < argc; j++) { + for (int j = 0; j < argc; j++) { join = sdscatsds(join, argv[j]); if (j != argc - 1) { join = sdscatlen(join, sep, seplen); } } -- cgit v1.2.3-70-g09d2 From 5a0205be790dbe6402c36ae36151b8688522e2f0 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sun, 18 Jan 2015 18:42:56 +0100 Subject: sdsfreesplitres: Change count arg to size_t This makes it match sdssplitlen --- src/sds.c | 2 +- src/sds.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/sds.c b/src/sds.c index 0179e60..b0e596e 100644 --- a/src/sds.c +++ b/src/sds.c @@ -521,7 +521,7 @@ cleanup: } /* Free the result returned by sdssplitlen(), or do nothing if 'tokens' is NULL. */ -void sdsfreesplitres(sds *tokens, int count) { +void sdsfreesplitres(sds *tokens, size_t count) { if (!tokens) { return; } while(count--) { sdsfree(tokens[count]); diff --git a/src/sds.h b/src/sds.h index 13c94de..3354096 100644 --- a/src/sds.h +++ b/src/sds.h @@ -84,7 +84,7 @@ void sdsupdatelen(sds s); void sdsclear(sds s); int sdscmp(const sds s1, const sds s2); sds *sdssplitlen(const char *s, size_t len, const char *sep, size_t seplen, size_t *count); -void sdsfreesplitres(sds *tokens, int count); +void sdsfreesplitres(sds *tokens, size_t count); void sdstolower(sds s); void sdstoupper(sds s); sds sdsfromlonglong(long long value); -- cgit v1.2.3-70-g09d2