ref: a5bea69b725259e74e806ae8d3b464c23cb073bf
parent: 485bc17e6c33fe29694e196848000da77d1b3521
author: cancel <cancel@cancel.fm>
date: Tue Jan 7 20:29:06 EST 2020
Cleanup
--- a/thirdparty/sdd.c
+++ b/thirdparty/sdd.c
@@ -60,7 +60,7 @@
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 @@
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 @@
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 @@
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 @@
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.
--- a/thirdparty/sdd.h
+++ b/thirdparty/sdd.h
@@ -3,52 +3,64 @@
#include <stdbool.h>
#include <stddef.h>
+#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