diff options
author | Johannes Löthberg <johannes@kyriasis.com> | 2015-01-19 21:21:17 +0100 |
---|---|---|
committer | Johannes Löthberg <johannes@kyriasis.com> | 2015-01-19 21:21:17 +0100 |
commit | b656983022b945e0ea6d9fbb7f44b3b1735a60bf (patch) | |
tree | 560c9ae706f4680cd8acac76ba55eb5fa5b72ba4 /src/sds.h | |
parent | f4d49ce748c222768c703b8de82a2925b738cb79 (diff) | |
download | sds-b656983022b945e0ea6d9fbb7f44b3b1735a60bf.tar.xz |
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.
Diffstat (limited to 'src/sds.h')
-rw-r--r-- | src/sds.h | 43 |
1 files changed, 26 insertions, 17 deletions
@@ -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 |