From b656983022b945e0ea6d9fbb7f44b3b1735a60bf Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Mon, 19 Jan 2015 21:21:17 +0100 Subject: Rename sdsnewlen to sdsnew, add inline sdsauto Warning: this commit introduces a breaking change to the public API. The job of the old sdsnew function is replaced with the sdsauto inline function. --- src/sds.h | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) (limited to 'src/sds.h') 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 -- cgit v1.2.3-70-g09d2