From 63e699cb0a2681f094deba0c7cc8b809db948bd7 Mon Sep 17 00:00:00 2001 From: cancel Date: Wed, 8 Jan 2020 09:15:06 +0900 Subject: [PATCH] Cleanup --- thirdparty/sdd.c | 54 ++++++++++++++++++++++++------------------------ thirdparty/sdd.h | 25 +++++++++++----------- 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/thirdparty/sdd.c b/thirdparty/sdd.c index ed1fb51..da731b0 100644 --- a/thirdparty/sdd.c +++ b/thirdparty/sdd.c @@ -115,6 +115,33 @@ sdd *sdd_cpylen(sdd *restrict s, char const *restrict cstr, size_t len) { sdd *sdd_cpysdd(sdd *restrict s, sdd const *restrict other) { return sdd_cpylen(s, (char const *)other, SDD_HDR(other)->len); } +sdd *sdd_cat(sdd *restrict s, char const *restrict other) { + return sdd_catlen(s, other, strlen(other)); +} +SDD_NOINLINE +sdd *sdd_catlen(sdd *restrict s, char const *restrict other, size_t other_len) { + size_t curr_len = SDD_HDR(s)->len; + s = sdd_makeroomfor(s, other_len); + if (!s) + return NULL; + memcpy((char *)s + curr_len, other, other_len); + ((char *)s)[curr_len + other_len] = '\0'; + SDD_HDR(s)->len = curr_len + other_len; + return s; +} +sdd *sdd_catsdd(sdd *restrict s, sdd const *restrict other) { + return sdd_catlen(s, (char const *)other, SDD_HDR(other)->len); +} +sdd *sdd_catvprintf(sdd *restrict s, char const *fmt, va_list ap) { + return sdd_impl_catvprintf(s, fmt, ap); +} +sdd *sdd_catprintf(sdd *restrict s, char const *fmt, ...) { + va_list ap; + va_start(ap, fmt); + s = sdd_impl_catvprintf(s, fmt, ap); + va_end(ap); + return s; +} SDD_NOINLINE sdd *sdd_ensurecap(sdd *s, size_t new_cap) { sdd_header *hdr = SDD_HDR(s); @@ -145,33 +172,6 @@ size_t sdd_avail(sdd const *s) { sdd_header *h = SDD_HDR(s); return h->cap - h->len; } -sdd *sdd_cat(sdd *restrict s, char const *restrict other) { - return sdd_catlen(s, other, strlen(other)); -} -SDD_NOINLINE -sdd *sdd_catlen(sdd *restrict s, char const *restrict other, size_t other_len) { - size_t curr_len = SDD_HDR(s)->len; - s = sdd_makeroomfor(s, other_len); - if (!s) - return NULL; - memcpy((char *)s + curr_len, other, other_len); - ((char *)s)[curr_len + other_len] = '\0'; - SDD_HDR(s)->len = curr_len + other_len; - return s; -} -sdd *sdd_catsdd(sdd *restrict s, sdd const *restrict other) { - return sdd_catlen(s, (char const *)other, SDD_HDR(other)->len); -} -sdd *sdd_catvprintf(sdd *restrict s, char const *fmt, va_list ap) { - return sdd_impl_catvprintf(s, fmt, ap); -} -sdd *sdd_catprintf(sdd *restrict s, char const *fmt, ...) { - va_list ap; - va_start(ap, fmt); - s = sdd_impl_catvprintf(s, fmt, ap); - va_end(ap); - return s; -} void sdd_clear(sdd *s) { SDD_HDR(s)->len = 0; ((char *)s)[0] = '\0'; diff --git a/thirdparty/sdd.h b/thirdparty/sdd.h index bd0cc96..2b2c686 100644 --- a/thirdparty/sdd.h +++ b/thirdparty/sdd.h @@ -54,21 +54,15 @@ void sdd_free(sdd *s); // ^- Calling with null is allowed. sdd *sdd_cpy(sdd *restrict s, char const *restrict cstr) SDD_NONNULL() SDD_USED; -// ^- Set `s` to contain the contents of `cstr` +// ^- Set `s` to contain the contents of `cstr`. This is really more like +// "change into" rather than "copy". sdd *sdd_cpylen(sdd *restrict s, char const *restrict cstr, size_t len) SDD_NONNULL() SDD_USED; sdd *sdd_cpysdd(sdd *restrict s, sdd const *restrict other); -size_t sdd_len(sdd const *s) SDD_NONNULL(); -// ^- Bytes used by string (excl. null term) -size_t sdd_cap(sdd const *s) SDD_NONNULL(); -// ^- Bytes allocated on heap (excl. null term) -size_t sdd_avail(sdd const *s) SDD_NONNULL(); -// ^- sdd_cap(s) - sdd_len(s) - sdd *sdd_cat(sdd *restrict s, char const *restrict other) SDD_NONNULL() SDD_USED; -// ^- Appends contents. The two strings must not overlap. +// ^- Appends contents. The two strings must not overlap in memory. sdd *sdd_catlen(sdd *restrict s, char const *restrict other, size_t len) SDD_NONNULL() SDD_USED; sdd *sdd_catsdd(sdd *restrict s, sdd const *restrict other) @@ -79,6 +73,13 @@ sdd *sdd_catprintf(sdd *restrict s, char const *fmt, ...) SDD_NONNULL((1, 2)) SDD_PRINTF(2, 3) SDD_USED; // ^- Appends by using printf. +size_t sdd_len(sdd const *s) SDD_NONNULL(); +// ^- Bytes used by string (excluding '\0' terminator) +size_t sdd_cap(sdd const *s) SDD_NONNULL(); +// ^- Bytes allocated on heap (excluding '\0' terminator) +size_t sdd_avail(sdd const *s) SDD_NONNULL(); +// ^- sdd_cap(s) - sdd_len(s) + void sdd_clear(sdd *s) SDD_NONNULL(); // ^- Set len to 0, write '\0' at pos 0. Leaves allocated memory in place. void sdd_pokelen(sdd *s, size_t len) SDD_NONNULL(); @@ -92,9 +93,9 @@ sdd *sdd_ensurecap(sdd *s, size_t cap) SDD_NONNULL() SDD_USED; // only the backing memory allocation. sdd *sdd_makeroomfor(sdd *s, size_t add_len) SDD_NONNULL() SDD_USED; // ^- Ensure that s has enough allocated space after the valid, -// null-terminated characters to hold an additional add_len characters. -// It does not adjust the length, only the capacity, if necessary. -// Soon after you call sdd_makeroomfor(), you need to call sdd_pokelen(), +// null-terminated characters to hold an additional add_len characters. It +// does not adjust the length, only the capacity, if necessary. Soon after +// you call sdd_makeroomfor(), you probably will want to call sdd_pokelen(), // otherwise you're probably using it incorrectly. #undef SDD_PRINTF