aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/sds.c19
-rw-r--r--src/sds.h43
2 files changed, 33 insertions, 29 deletions
diff --git a/src/sds.c b/src/sds.c
index 57a2dc1..3f86e13 100644
--- a/src/sds.c
+++ b/src/sds.c
@@ -49,7 +49,7 @@
* 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
* \0 characters in the middle, as the length is stored in the sds header. */
-sds sdsnewlen(const void *init, size_t initlen) {
+sds sdsnew(const void *init, size_t initlen) {
struct sdshdr *sh;
if (init) {
@@ -58,6 +58,7 @@ sds sdsnewlen(const void *init, size_t initlen) {
sh = calloc(sizeof(struct sdshdr) + initlen + 1, 1);
}
if (sh == NULL) { return NULL; }
+
sh->len = initlen;
sh->free = 0;
if (initlen && init) {
@@ -67,21 +68,15 @@ sds sdsnewlen(const void *init, size_t initlen) {
return (char*)sh->buf;
}
-/* Create a new sds string starting from a null termined C string. */
-sds sdsnew(const char *init) {
- size_t initlen = (init == NULL) ? 0 : strlen(init);
- return sdsnewlen(init, initlen);
-}
-
/* Duplicate an sds string. */
sds sdsdup(const sds s) {
- return sdsnewlen(s, sdslen(s));
+ return sdsnew(s, sdslen(s));
}
/* 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 sdsnew("", 0);
}
/* Create an sds string from a long long value. It is much faster than:
@@ -100,7 +95,7 @@ sds sdsfromlonglong(long long value) {
} while(v);
if (value < 0) { *p-- = '-'; }
p++;
- return sdsnewlen(p, (size_t)(32 - (p - buf)));
+ return sdsnew(p, (size_t)(32 - (p - buf)));
}
@@ -516,7 +511,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, (size_t)(j - start));
+ tokens[elements] = sdsnew(s + start, (size_t)(j - start));
if (tokens[elements] == NULL) { goto cleanup; }
elements++;
start = j + seplen;
@@ -524,7 +519,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, (size_t)(len - start));
+ tokens[elements] = sdsnew(s + start, (size_t)(len - start));
if (tokens[elements] == NULL) { goto cleanup; }
elements++;
*count = elements;
diff --git a/src/sds.h b/src/sds.h
index 501ac53..13bdcbf 100644
--- a/src/sds.h
+++ b/src/sds.h
@@ -42,35 +42,19 @@ struct sdshdr {
char buf[];
};
-static inline struct sdshdr * sdsheader(const sds s) {
- /* 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) {
- return sdsheader(s)->len;
-}
-
-static inline size_t sdsavail(const sds s) {
- return sdsheader(s)->free;
-}
/**
* User API function prototypes
*/
/// Initialization
-sds sdsnewlen(const void *init, size_t initlen);
-sds sdsnew(const char *init);
+sds sdsnew(const void *init, size_t initlen);
sds sdsdup(const sds s);
sds sdsempty(void);
sds sdsfromlonglong(long long value);
/// Querying
-size_t sdsavail(const sds s);
-size_t sdslen(const sds s);
int sdscmp(const sds s1, const sds s2);
@@ -115,8 +99,33 @@ void sdsIncrLen(sds s, size_t incr);
sds sdsMakeRoomFor(sds s, size_t addlen);
sds sdsRemoveFreeSpace(sds s);
+
/// Low-level helper functions
int is_hex_digit(char c);
int hex_digit_to_int(char c);
+
+/**
+ * Inline functions
+ */
+
+static inline struct sdshdr *sdsheader(const sds s) {
+ /* 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 sds sdsauto(const char *s) {
+ return sdsnew(s, s ? strlen(s) : 0);
+}
+
+static inline size_t sdsavail(const sds s) {
+ return sdsheader(s)->free;
+}
+
+static inline size_t sdslen(const sds s) {
+ return sdsheader(s)->len;
+}
+
+
#endif