From a5bea69b725259e74e806ae8d3b464c23cb073bf Mon Sep 17 00:00:00 2001 From: cancel Date: Wed, 8 Jan 2020 01:29:06 +0900 Subject: [PATCH] Cleanup --- thirdparty/sdd.c | 31 +++++++++++++------- thirdparty/sdd.h | 74 ++++++++++++++++++++++++++++-------------------- 2 files changed, 64 insertions(+), 41 deletions(-) diff --git a/thirdparty/sdd.c b/thirdparty/sdd.c index a16a813..f69b2af 100644 --- a/thirdparty/sdd.c +++ b/thirdparty/sdd.c @@ -60,7 +60,7 @@ typedef struct sdd_header { static void sdd_setcap(sdd str, size_t cap) { SDD_HDR(str)->cap = cap; } -static SDD_NOINLINE sdd sdd_impl_catvprintf(sdd s, const char *fmt, +static SDD_NOINLINE sdd sdd_impl_catvprintf(sdd s, char const *fmt, va_list ap) { size_t old_len; int required; @@ -78,6 +78,7 @@ static SDD_NOINLINE sdd sdd_impl_catvprintf(sdd s, const char *fmt, if (s == NULL) return NULL; vsnprintf(s + old_len, (size_t)required + 1, fmt, ap); + SDD_HDR(s)->len = old_len + (size_t)required; return s; } @@ -113,7 +114,7 @@ sdd sdd_new(char const *str) { size_t len = str ? strlen(str) : 0; return sdd_newlen(str, len); } -sdd sdd_newvprintf(const char *fmt, va_list ap) { +sdd sdd_newvprintf(char const *fmt, va_list ap) { return sdd_impl_catvprintf(NULL, fmt, ap); } sdd sdd_newprintf(char const *fmt, ...) { @@ -130,24 +131,34 @@ void sdd_free(sdd str) { free((sdd_header *)str - 1); } -sdd sdd_dup(sdd const str) { return sdd_newlen(str, SDD_HDR(str)->len); } +sdd sdd_dup(sdd const str) { + assert(str); + return sdd_newlen(str, SDD_HDR(str)->len); +} -size_t sdd_len(sdd const str) { return SDD_HDR(str)->len; } -size_t sdd_cap(sdd const str) { return SDD_HDR(str)->cap; } +size_t sdd_len(sdd const str) { + assert(str); + return SDD_HDR(str)->len; +} +size_t sdd_cap(sdd const str) { + assert(str); + return SDD_HDR(str)->cap; +} size_t sdd_avail(sdd const str) { + assert(str); sdd_header *h = SDD_HDR(str); - if (h->cap > h->len) - return h->cap - h->len; - return 0; + return h->cap - h->len; } void sdd_clear(sdd str) { + assert(str); SDD_HDR(str)->len = 0; str[0] = '\0'; } -sdd sdd_catlen(sdd str, void const *other, size_t other_len) { +sdd sdd_catlen(sdd str, char const *other, size_t other_len) { + assert(str); size_t curr_len = SDD_HDR(str)->len; str = sdd_makeroomfor(str, other_len); if (str == NULL) @@ -242,7 +253,7 @@ sdd sdd_trim(sdd str, char const *cut_set) { return str; } -sdd sdd_catvprintf(sdd s, const char *fmt, va_list ap) { +sdd sdd_catvprintf(sdd s, char const *fmt, va_list ap) { // not sure if we should make exception for cat_* functions to allow cat'ing // to null pointer. we should see if it ends up being useful in code, or if // we should just match the existing behavior of sds/gb_string. diff --git a/thirdparty/sdd.h b/thirdparty/sdd.h index f7006cc..79b78c4 100644 --- a/thirdparty/sdd.h +++ b/thirdparty/sdd.h @@ -3,52 +3,64 @@ #include #include +#ifdef __GNUC__ +#define SDD_PRINTF(n1, n2) __attribute__((format(printf, n1, n2))) +#define SDD_NONNULL(...) __attribute__((nonnull __VA_ARGS__)) +#define SDD_ALLOCS __attribute__((malloc, warn_unused_result)) +#define SDD_RESULT __attribute__((warn_unused_result)) +#else +#define SDD_PRINTF(n1, n2) +#define SDD_NONNULL +#define SDD_ALLOCS +#define SDD_RESULT +#endif + typedef char *sdd; -sdd sdd_new(char const *str); -// ^- Create new with copy of null-terminated cstring -sdd sdd_newlen(void const *str, size_t len); +sdd sdd_new(char const *str) SDD_ALLOCS; +// ^- Create new with copy of null-terminated cstring. +sdd sdd_newlen(void const *str, size_t len) SDD_ALLOCS; // ^- Same, but without calling strlen(). // Resulting new string will be null terminated. -sdd sdd_newcap(size_t cap); +sdd sdd_newcap(size_t cap) SDD_ALLOCS; // ^- 'Raw' new with a specific capacity. // Length will be set to 0, and '\0' written at position 0. -sdd sdd_newvprintf(const char *fmt, va_list ap); -sdd sdd_newprintf(char const *fmt, ...) -#ifdef __GNUC__ - __attribute__((format(printf, 1, 2))) -#endif - ; +sdd sdd_dup(sdd const str) SDD_ALLOCS; +// ^- Same as sdd_newlen(str, sdd_len(str)) +sdd sdd_newvprintf(char const *fmt, va_list ap) SDD_ALLOCS; +sdd sdd_newprintf(char const *fmt, ...) SDD_PRINTF(1, 2) SDD_ALLOCS; void sdd_free(sdd str); -sdd sdd_dup(sdd const str); -// ^- Same as sdd_newlen(str, sdd_len(str)) -sdd sdd_cpy(sdd str, char const *cstr); +sdd sdd_cpy(sdd str, char const *cstr) SDD_RESULT; // ^- Set `str` to contain the contents of `cstr` -sdd sdd_cpylen(sdd str, char const *cstr, size_t len); +sdd sdd_cpylen(sdd str, char const *cstr, size_t len) SDD_RESULT; -size_t sdd_len(sdd const str); // Bytes used by string (excl. null term) -size_t sdd_cap(sdd const str); // Bytes allocated on heap (excl. null term) -size_t sdd_avail(sdd const str); // cap - len +size_t sdd_len(sdd const str) SDD_NONNULL(); +// ^- Bytes used by string (excl. null term) +size_t sdd_cap(sdd const str) SDD_NONNULL(); +// ^- Bytes allocated on heap (excl. null term) +size_t sdd_avail(sdd const str) SDD_NONNULL(); +// ^- cap - len -sdd sdd_cat(sdd str, char const *other); -sdd sdd_catlen(sdd str, void const *other, size_t len); -sdd sdd_catsdd(sdd str, sdd const other); -sdd sdd_catvprintf(sdd str, const char *fmt, va_list ap); -sdd sdd_catprintf(sdd str, char const *fmt, ...) -#ifdef __GNUC__ - __attribute__((format(printf, 2, 3))) -#endif - ; +sdd sdd_cat(sdd str, char const *other) SDD_NONNULL() SDD_RESULT; +sdd sdd_catlen(sdd str, char const *other, size_t len) SDD_RESULT; +sdd sdd_catsdd(sdd str, sdd const other) SDD_RESULT; +sdd sdd_catvprintf(sdd str, char const *fmt, va_list ap) SDD_RESULT; +sdd sdd_catprintf(sdd str, char const *fmt, ...) SDD_PRINTF(2, 3) SDD_RESULT; -void sdd_clear(sdd str); // Set len to 0, write '\0' at pos 0 -sdd sdd_makeroomfor(sdd str, size_t add_len); +void sdd_clear(sdd str) SDD_NONNULL(); // Set len to 0, write '\0' at pos 0 +sdd sdd_makeroomfor(sdd str, size_t add_len) SDD_NONNULL() SDD_RESULT; // ^- Makes sure -void sdd_pokelen(sdd str, size_t len); +void sdd_pokelen(sdd str, size_t len) SDD_NONNULL(); // ^- Manually update length field. Doesn't do anything else for you. -bool sdd_equal(sdd const lhs, sdd const rhs); +bool sdd_equal(sdd const lhs, sdd const rhs) SDD_NONULL(); -sdd sdd_trim(sdd str, char const *cut_set); +sdd sdd_trim(sdd str, char const *cut_set) SDD_RESULT SDD_NONULL(); size_t sdd_totalmemused(sdd const str); + +#undef SDD_PRINTF +#undef SDD_NONNULL +#undef SDD_ALLOCS +#undef SDD_RESULT