Browse Source

Cleanup

master
cancel 5 years ago
parent
commit
a5bea69b72
  1. 29
      thirdparty/sdd.c
  2. 74
      thirdparty/sdd.h

29
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 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) { va_list ap) {
size_t old_len; size_t old_len;
int required; int required;
@ -78,6 +78,7 @@ static SDD_NOINLINE sdd sdd_impl_catvprintf(sdd s, const char *fmt,
if (s == NULL) if (s == NULL)
return NULL; return NULL;
vsnprintf(s + old_len, (size_t)required + 1, fmt, ap); vsnprintf(s + old_len, (size_t)required + 1, fmt, ap);
SDD_HDR(s)->len = old_len + (size_t)required;
return s; return s;
} }
@ -113,7 +114,7 @@ sdd sdd_new(char const *str) {
size_t len = str ? strlen(str) : 0; size_t len = str ? strlen(str) : 0;
return sdd_newlen(str, len); 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); return sdd_impl_catvprintf(NULL, fmt, ap);
} }
sdd sdd_newprintf(char const *fmt, ...) { sdd sdd_newprintf(char const *fmt, ...) {
@ -130,24 +131,34 @@ void sdd_free(sdd str) {
free((sdd_header *)str - 1); 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_len(sdd const str) {
size_t sdd_cap(sdd const str) { return SDD_HDR(str)->cap; } 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) { size_t sdd_avail(sdd const str) {
assert(str);
sdd_header *h = SDD_HDR(str); sdd_header *h = SDD_HDR(str);
if (h->cap > h->len)
return h->cap - h->len; return h->cap - h->len;
return 0;
} }
void sdd_clear(sdd str) { void sdd_clear(sdd str) {
assert(str);
SDD_HDR(str)->len = 0; SDD_HDR(str)->len = 0;
str[0] = '\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; size_t curr_len = SDD_HDR(str)->len;
str = sdd_makeroomfor(str, other_len); str = sdd_makeroomfor(str, other_len);
if (str == NULL) if (str == NULL)
@ -242,7 +253,7 @@ sdd sdd_trim(sdd str, char const *cut_set) {
return str; 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 // 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 // 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. // we should just match the existing behavior of sds/gb_string.

74
thirdparty/sdd.h

@ -3,52 +3,64 @@
#include <stdbool.h> #include <stdbool.h>
#include <stddef.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; typedef char *sdd;
sdd sdd_new(char const *str); sdd sdd_new(char const *str) SDD_ALLOCS;
// ^- Create new with copy of null-terminated cstring // ^- Create new with copy of null-terminated cstring.
sdd sdd_newlen(void const *str, size_t len); sdd sdd_newlen(void const *str, size_t len) SDD_ALLOCS;
// ^- Same, but without calling strlen(). // ^- Same, but without calling strlen().
// Resulting new string will be null terminated. // 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. // ^- 'Raw' new with a specific capacity.
// Length will be set to 0, and '\0' written at position 0. // Length will be set to 0, and '\0' written at position 0.
sdd sdd_newvprintf(const char *fmt, va_list ap); sdd sdd_dup(sdd const str) SDD_ALLOCS;
sdd sdd_newprintf(char const *fmt, ...) // ^- Same as sdd_newlen(str, sdd_len(str))
#ifdef __GNUC__ sdd sdd_newvprintf(char const *fmt, va_list ap) SDD_ALLOCS;
__attribute__((format(printf, 1, 2))) sdd sdd_newprintf(char const *fmt, ...) SDD_PRINTF(1, 2) SDD_ALLOCS;
#endif
;
void sdd_free(sdd str); void sdd_free(sdd str);
sdd sdd_dup(sdd const str); sdd sdd_cpy(sdd str, char const *cstr) SDD_RESULT;
// ^- Same as sdd_newlen(str, sdd_len(str))
sdd sdd_cpy(sdd str, char const *cstr);
// ^- Set `str` to contain the contents of `cstr` // ^- 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_len(sdd const str) SDD_NONNULL();
size_t sdd_cap(sdd const str); // Bytes allocated on heap (excl. null term) // ^- Bytes used by string (excl. null term)
size_t sdd_avail(sdd const str); // cap - len 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_cat(sdd str, char const *other) SDD_NONNULL() SDD_RESULT;
sdd sdd_catlen(sdd str, void const *other, size_t len); sdd sdd_catlen(sdd str, char const *other, size_t len) SDD_RESULT;
sdd sdd_catsdd(sdd str, sdd const other); sdd sdd_catsdd(sdd str, sdd const other) SDD_RESULT;
sdd sdd_catvprintf(sdd str, const char *fmt, va_list ap); sdd sdd_catvprintf(sdd str, char const *fmt, va_list ap) SDD_RESULT;
sdd sdd_catprintf(sdd str, char const *fmt, ...) sdd sdd_catprintf(sdd str, char const *fmt, ...) SDD_PRINTF(2, 3) SDD_RESULT;
#ifdef __GNUC__
__attribute__((format(printf, 2, 3)))
#endif
;
void sdd_clear(sdd str); // Set len to 0, write '\0' at pos 0 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 sdd_makeroomfor(sdd str, size_t add_len) SDD_NONNULL() SDD_RESULT;
// ^- Makes sure // ^- 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. // ^- 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); size_t sdd_totalmemused(sdd const str);
#undef SDD_PRINTF
#undef SDD_NONNULL
#undef SDD_ALLOCS
#undef SDD_RESULT

Loading…
Cancel
Save