From def88074e514b2084e4926851d330e95faeee1d7 Mon Sep 17 00:00:00 2001 From: Michel Martens Date: Wed, 18 Apr 2018 09:38:47 +0200 Subject: [PATCH] Update deps/sds --- deps/sds/Makefile | 21 +++++---------------- deps/sds/sds.c | 40 ++++++++++++++++++++++++---------------- deps/sds/sds.h | 6 +++--- 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/deps/sds/Makefile b/deps/sds/Makefile index 91aef75..045fa88 100644 --- a/deps/sds/Makefile +++ b/deps/sds/Makefile @@ -1,19 +1,8 @@ -STD= -WARN= -Wall -OPT= -Os +all: sds-test -R_CFLAGS= $(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS) -R_LDFLAGS= $(LDFLAGS) -DEBUG= -g - -R_CC=$(CC) $(R_CFLAGS) -R_LD=$(CC) $(R_LDFLAGS) - -sds.o: sds.h sds.c - -.c.o: - $(R_CC) -c $< +sds-test: sds.c sds.h testhelp.h + $(CC) -o sds-test sds.c -Wall -std=c99 -pedantic -O2 -DSDS_TEST_MAIN + @echo ">>> Type ./sds-test to run the sds.c unit tests." clean: - rm -f *.o - + rm -f sds-test diff --git a/deps/sds/sds.c b/deps/sds/sds.c index eafa13c..603e36a 100644 --- a/deps/sds/sds.c +++ b/deps/sds/sds.c @@ -175,7 +175,7 @@ void sdsfree(sds s) { * the output will be "6" as the string was modified but the logical length * remains 6 bytes. */ void sdsupdatelen(sds s) { - int reallen = strlen(s); + size_t reallen = strlen(s); sdssetlen(s, reallen); } @@ -248,16 +248,23 @@ sds sdsMakeRoomFor(sds s, size_t addlen) { sds sdsRemoveFreeSpace(sds s) { void *sh, *newsh; char type, oldtype = s[-1] & SDS_TYPE_MASK; - int hdrlen; + int hdrlen, oldhdrlen = sdsHdrSize(oldtype); size_t len = sdslen(s); - sh = (char*)s-sdsHdrSize(oldtype); + sh = (char*)s-oldhdrlen; + /* Check what would be the minimum SDS header that is just good enough to + * fit this string. */ type = sdsReqType(len); hdrlen = sdsHdrSize(type); - if (oldtype==type) { - newsh = s_realloc(sh, hdrlen+len+1); + + /* If the type is the same, or at least a large enough type is still + * required, we just realloc(), letting the allocator to do the copy + * only if really needed. Otherwise if the change is huge, we manually + * reallocate the string to use the different header type. */ + if (oldtype==type || type > SDS_TYPE_8) { + newsh = s_realloc(sh, oldhdrlen+len+1); if (newsh == NULL) return NULL; - s = (char*)newsh+hdrlen; + s = (char*)newsh+oldhdrlen; } else { newsh = s_malloc(hdrlen+len+1); if (newsh == NULL) return NULL; @@ -312,7 +319,7 @@ void *sdsAllocPtr(sds s) { * ... check for nread <= 0 and handle it ... * sdsIncrLen(s, nread); */ -void sdsIncrLen(sds s, int incr) { +void sdsIncrLen(sds s, ssize_t incr) { unsigned char flags = s[-1]; size_t len; switch(flags&SDS_TYPE_MASK) { @@ -582,7 +589,7 @@ sds sdscatprintf(sds s, const char *fmt, ...) { sds sdscatfmt(sds s, char const *fmt, ...) { size_t initlen = sdslen(s); const char *f = fmt; - int i; + long i; va_list ap; va_start(ap,fmt); @@ -714,7 +721,7 @@ sds 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, ssize_t start, ssize_t end) { size_t newlen, len = sdslen(s); if (len == 0) return; @@ -728,9 +735,9 @@ void sdsrange(sds s, int start, int end) { } newlen = (start > end) ? 0 : (end-start)+1; if (newlen != 0) { - if (start >= (signed)len) { + if (start >= (ssize_t)len) { newlen = 0; - } else if (end >= (signed)len) { + } else if (end >= (ssize_t)len) { end = len-1; newlen = (start > end) ? 0 : (end-start)+1; } @@ -744,14 +751,14 @@ void sdsrange(sds s, int start, int 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]); } @@ -775,7 +782,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 l1>l2? 1: (l1