aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md93
-rw-r--r--src/sds.c217
-rw-r--r--src/sds.h17
-rw-r--r--src/test.c4
4 files changed, 167 insertions, 164 deletions
diff --git a/README.md b/README.md
index a99c5ea..8d2db6b 100644
--- a/README.md
+++ b/README.md
@@ -67,7 +67,7 @@ one:
API calls look something like this:
```c
- s = sdscat(s,"Some more data");
+ s = sdscat(s, "Some more data");
```
As you can see `s` is used as input for `sdscat` but is also set to the
@@ -177,20 +177,17 @@ There are many ways to create SDS strings:
buf[0] = 'A';
buf[1] = 'B';
buf[2] = 'C';
- mystring = sdsnewlen(buf,3);
- printf("%s of len %d\n", mystring, (int) sdslen(mystring));
+ mystring = sdsnewlen(buf, 3);
+ printf("%s of len %zu\n", mystring, sdslen(mystring));
output> ABC of len 3
```
- Note: `sdslen` return value is casted to `int` because it returns a `size_t`
-type. You can use the right `printf` specifier instead of casting.
-
* The `sdsempty()` function creates an empty zero-length string:
```c
sds mystring = sdsempty();
- printf("%d\n", (int) sdslen(mystring));
+ printf("%zu\n", sdslen(mystring));
output> 0
```
@@ -225,8 +222,8 @@ As an example of the binary safeness of SDS strings, we can run the following
code:
```c
-sds s = sdsnewlen("A\0\0B",4);
-printf("%d\n", (int) sdslen(s));
+sds s = sdsnewlen("A\0\0B", 4);
+printf("%zu\n", sdslen(s));
output> 4
```
@@ -294,7 +291,7 @@ Usage is straightforward:
```c
sds s1 = sdsnew("aaa");
sds s2 = sdsnew("bbb");
-s1 = sdscatsds(s1,s2);
+s1 = sdscatsds(s1, s2);
sdsfree(s2);
printf("%s\n", s1);
@@ -315,7 +312,7 @@ it with zero bytes.
```c
sds s = sdsnew("Hello");
-s = sdsgrowzero(s,6);
+s = sdsgrowzero(s, 6);
s[5] = '!'; /* We are sure this is safe because of sdsgrowzero() */
printf("%s\n", s);
@@ -338,7 +335,7 @@ Example:
sds s;
int a = 10, b = 20;
s = sdsnew("The 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 SDS string directly from `printf` format specifiers.
@@ -357,7 +354,7 @@ You can use `sdscatprintf` in order to convert numbers into SDS strings:
```c
int some_integer = 100;
-sds num = sdscatprintf(sdsempty(),"%d\n", some_integer);
+sds num = sdscatprintf(sdsempty(), "%d\n", some_integer);
```
However this is slow and we have a special function to make it efficient.
@@ -377,7 +374,7 @@ Use it like this:
```c
sds s = sdsfromlonglong(10000);
-printf("%d\n", (int) sdslen(s));
+printf("%zu\n", sdslen(s));
output> 5
```
@@ -391,7 +388,7 @@ on strings is to just take a range out of a larger string.
```c
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);
```
SDS provides both the operations with the `sdstrim` and `sdsrange` functions.
@@ -408,8 +405,8 @@ from an SDS strings:
```c
sds s = sdsnew(" my string\n\n ");
-sdstrim(s," \n");
-printf("-%s-\n",s);
+sdstrim(s, " \n");
+printf("-%s-\n", s);
output> -my string-
```
@@ -426,8 +423,8 @@ indexes inside the string, to obtain the range that will be retained.
```c
sds s = sdsnew("Hello World!");
-sdsrange(s,1,4);
-printf("-%s-\n",s);
+sdsrange(s, 1, 4);
+printf("-%s-\n", s);
output> -ello-
```
@@ -437,10 +434,10 @@ string, so that `-1` means the last character, `-2` the penultimate, and so fort
```c
sds s = sdsnew("Hello World!");
-sdsrange(s,6,-1);
-printf("-%s-\n",s);
-sdsrange(s,0,-2);
-printf("-%s-\n",s);
+sdsrange(s, 6, -1);
+printf("-%s-\n", s);
+sdsrange(s, 0, -2);
+printf("-%s-\n", s);
output> -World!-
output> -World-
@@ -458,7 +455,7 @@ void clusterWriteHandler(..., int fd, void *privdata, ...) {
if (nwritten <= 0) {
/* Error handling... */
}
- sdsrange(link->sndbuf,nwritten,-1);
+ sdsrange(link->sndbuf, nwritten, -1);
... more code here ...
}
```
@@ -498,7 +495,7 @@ The string copy function of SDS is called `sdscpylen` and works like this:
```c
s = sdsnew("Hello World!");
-s = sdscpylen(s,"Hello Superman!",15);
+s = sdscpylen(s, "Hello Superman!", 15);
```
As you can see the function receives as input the SDS string `s`, but also
@@ -558,7 +555,7 @@ sds s2 = sdsempty();
s1[1] = 1;
s1[2] = 2;
s1[3] = '\n';
-s2 = sdscatrepr(s2,s1,sdslen(s1));
+s2 = sdscatrepr(s2, s1, sdslen(s1));
printf("%s\n", s2);
output> "a\x01\x02\n"
@@ -597,8 +594,8 @@ strings it is composed of, so SDS provides a function that returns an
array of SDS strings given a string and a separator.
```c
-sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count);
-void sdsfreesplitres(sds *tokens, int count);
+sds *sdssplitlen(const char *s, size_t len, const char *sep, size_t seplen, size_t *count);
+void sdsfreesplitres(sds *tokens, size_t count);
```
As usual the function can work with either SDS strings or normal C strings.
@@ -611,14 +608,14 @@ The return value is a heap allocated array of SDS strings.
```c
sds *tokens;
-int count, j;
+size_t count;
sds line = sdsnew("Hello World!");
-tokens = sdssplitlen(line,sdslen(line)," ",1,&count);
+tokens = sdssplitlen(line, sdslen(line), " ", 1, &count);
-for (j = 0; j < count; j++)
+for (size_t j = 0; j < count; j++)
printf("%s\n", tokens[j]);
-sdsfreesplitres(tokens,count);
+sdsfreesplitres(tokens, count);
output> Hello
output> World!
@@ -690,8 +687,8 @@ in the array to be SDS strings. Because of this only `sdsjoinsds` is
able to deal with binary data.
```c
-char *tokens[3] = {"foo","bar","zap"};
-sds s = sdsjoin(tokens,3,"|",1);
+char *tokens[3] = {"foo", "bar", "zap"};
+sds s = sdsjoin(tokens, 3, "|", 1);
printf("%s\n", s);
output> foo|bar|zap
@@ -718,8 +715,8 @@ structure implementing it:
```c
struct sdshdr {
- int len;
- int free;
+ size_t len;
+ size_t free;
char buf[];
};
```
@@ -727,7 +724,7 @@ struct sdshdr {
As you can see, the structure may resemble the one of a conventional string
library, however the `buf` field of the structure is different since it is
not a pointer but an array without any length declared, so `buf` actually
-points at the first byte just after the `free` integer. So in order to create
+points at the first byte just after the `free` size_t. So in order to create
an SDS string we just allocate a piece of memory that is as large as the
`sdshdr` structure plus the length of our string, plus an additional byte
for the mandatory null term that every SDS string has.
@@ -753,9 +750,9 @@ will create extra free space at the end, like in the following program:
```c
s = sdsempty();
-s = sdscat(s,"foo");
-s = sdscat(s,"bar");
-s = sdscat(s,"123");
+s = sdscat(s, "foo");
+s = sdscat(s, "bar");
+s = sdscat(s, "123");
```
Since SDS tries to be efficient it can't afford to reallocate the string every
@@ -796,10 +793,10 @@ total allocation for a given string, and is called `sdsAllocSize`.
```c
sds s = sdsnew("Ladies and gentlemen");
-s = sdscat(s,"... welcome to the C language.");
-printf("%d\n", (int) sdsAllocSize(s));
+s = sdscat(s, "... welcome to the C language.");
+printf("%zu\n", sdsAllocSize(s));
s = sdsRemoveFreeSpace(s);
-printf("%d\n", (int) sdsAllocSize(s));
+printf("%zu\n", sdsAllocSize(s));
output> 109
output> 59
@@ -823,9 +820,9 @@ information for the specified string to the length obtained via `strlen`.
```c
sds s = sdsnew("foobar");
s[2] = '\0';
-printf("%d\n", sdslen(s));
+printf("%zu\n", sdslen(s));
sdsupdatelen(s);
-printf("%d\n", sdslen(s));
+printf("%zu\n", sdslen(s));
output> 6
output> 2
@@ -886,7 +883,7 @@ sds string without copying into an intermediate buffer:
```c
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);
```
@@ -903,4 +900,6 @@ it without issues.
## Credits and license
-SDS was created by Salvatore Sanfilippo and is released under the BSD two clause license. See the LICENSE file in this source distribution for more information.
+SDS was created by Salvatore Sanfilippo and is released under the BSD two
+clause license. See the LICENSE file in this source distribution for more
+information.
diff --git a/src/sds.c b/src/sds.c
index f0515f1..b0e596e 100644
--- a/src/sds.c
+++ b/src/sds.c
@@ -27,12 +27,16 @@
#include <assert.h>
#include <ctype.h>
+#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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.
@@ -40,7 +44,7 @@
* 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
@@ -49,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;
@@ -66,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. */
@@ -102,8 +106,8 @@ void sdsfree(sds s) {
* remains 6 bytes. */
void sdsupdatelen(sds s) {
struct sdshdr *sh = sdsheader(s);
- int reallen = strlen(s);
- sh->free += (sh->len-reallen);
+ size_t reallen = strlen(s);
+ sh->free += (sh->len - reallen);
sh->len = reallen;
}
@@ -132,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;
@@ -155,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;
}
@@ -170,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
@@ -192,11 +196,11 @@ 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);
*/
-void sdsIncrLen(sds s, int incr) {
+void sdsIncrLen(sds s, size_t incr) {
struct sdshdr *sh = sdsheader(s);
assert(sh->free >= incr);
@@ -216,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;
}
@@ -237,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;
}
@@ -267,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;
}
@@ -289,6 +293,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;
@@ -297,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;
@@ -311,6 +316,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.
@@ -321,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:
@@ -332,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;
}
@@ -346,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".
@@ -357,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 = (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);
+ sh->free = sh->free + (sh->len - len);
sh->len = len;
}
@@ -381,50 +387,50 @@ 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, 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);
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;
}
- 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;
}
/* Apply tolower() to every character of the sds string 's'. */
void sdstolower(sds s) {
- int len = sdslen(s), j;
-
- for (j = 0; j < len; j++) s[j] = 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) {
- int len = sdslen(s), j;
-
- for (j = 0; j < len; j++) s[j] = 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().
@@ -445,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 (int)(l1 - l2); }
return cmp;
}
@@ -459,18 +465,18 @@ 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
* 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;
sds *tokens;
- if (seplen < 1 || len < 0) { return NULL; }
+ if (seplen < 1) { return NULL; }
tokens = malloc(sizeof(sds)*slots);
if (tokens == NULL) { return NULL; }
@@ -479,27 +485,27 @@ sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count
*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) {
+ 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, (size_t)(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. */
- 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;
@@ -507,8 +513,7 @@ sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count
cleanup:
{
- int 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;
@@ -516,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]);
@@ -526,21 +531,21 @@ 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-- = '-'; }
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
@@ -550,29 +555,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'
@@ -643,17 +648,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 = (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)) {
+ } else if (*p == '\\' && *(p + 1)) {
char c;
p++;
@@ -665,32 +670,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) {
@@ -708,14 +713,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, (unsigned long)((*argc) + 1) * (sizeof (char *)));
vector[*argc] = current;
(*argc)++;
current = NULL;
@@ -746,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;
@@ -763,11 +766,10 @@ 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); }
+ if (j != argc - 1) { join = sdscatlen(join, sep, seplen); }
}
return join;
}
@@ -775,11 +777,10 @@ 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); }
+ if (j != argc - 1) { join = sdscatlen(join, sep, seplen); }
}
return join;
}
diff --git a/src/sds.h b/src/sds.h
index 489e623..3354096 100644
--- a/src/sds.h
+++ b/src/sds.h
@@ -31,18 +31,21 @@
#define SDS_MAX_PREALLOC (1024*1024)
#include <stdarg.h>
+#include <stddef.h>
#include <sys/types.h>
typedef char *sds;
struct sdshdr {
- int len;
- int free;
+ size_t len;
+ size_t free;
char buf[];
};
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) {
@@ -76,12 +79,12 @@ 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);
-sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count);
-void sdsfreesplitres(sds *tokens, int count);
+sds *sdssplitlen(const char *s, size_t len, const char *sep, size_t seplen, size_t *count);
+void sdsfreesplitres(sds *tokens, size_t count);
void sdstolower(sds s);
void sdstoupper(sds s);
sds sdsfromlonglong(long long value);
@@ -93,7 +96,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);
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);