ref: bc84f7d811717a7ec07d7ce36d1442b1022cf0c9
parent: 96cd1bdbc532f00c2b258bbe91735bf6ffc03875
author: cancel <cancel@cancel.fm>
date: Tue Jan 7 18:42:10 EST 2020
Rename gbs to sdd
--- a/thirdparty/gbstring.c
+++ b/thirdparty/gbstring.c
@@ -9,60 +9,59 @@
/* Examples: */
/* C example */
#if 0
-#include "gbstring.h"
+#include "sdd.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
- gbs str = gbs_new("Hello");
- gbs other_str = gbs_newlen(", ", 2);
- str = gbs_catgbs(str, other_str);
- str = gbs_cat(str, "world!");
+ sdd str = sdd_new("Hello");
+ sdd other_str = sdd_newlen(", ", 2);
+ str = sdd_catsdd(str, other_str);
+ str = sdd_cat(str, "world!");
printf("%s\n", str); // Hello, world!
- printf("str length = %d\n", gbs_len(str));
+ printf("str length = %d\n", sdd_len(str));
- str = gbs_cpy(str, "Potato soup");
+ str = sdd_cpy(str, "Potato soup");
printf("%s\n", str); // Potato soup
- str = gbs_cpy(str, "Hello");
- other_str = gbs_cpy(other_str, "Pizza");
- if (gbs_equal(str, other_str))
+ str = sdd_cpy(str, "Hello");
+ other_str = sdd_cpy(other_str, "Pizza");
+ if (sdd_equal(str, other_str))
printf("Not called\n");
else
printf("Called\n");
- str = gbs_cpy(str, "Ab.;!...AHello World ??");
- str = gbs_trim(str, "Ab.;!. ?");
+ str = sdd_cpy(str, "Ab.;!...AHello World ??");
+ str = sdd_trim(str, "Ab.;!. ?");
printf("%s\n", str); // "Hello World"
- gbs_free(str);
- gbs_free(other_str);
+ sdd_free(str);
+ sdd_free(other_str);
}
#endif
-typedef struct gbStringHeader {
+typedef struct sddtringHeader {
size_t len;
size_t cap;
-} gbStringHeader;
+} sddtringHeader;
-#define GB_STRING_HEADER(s) ((gbStringHeader *)s - 1)
+#define SDD_HDR(s) ((sddtringHeader *)s - 1)
#if defined(__GNUC__) || defined(__clang__)
-#define GB_NOINLINE __attribute__((noinline))
+#define SDD_NOINLINE __attribute__((noinline))
#elif defined(_MSC_VER)
-#define GB_NOINLINE __declspec(noinline)
+#define SDD_NOINLINE __declspec(noinline)
#else
-#define GB_NOINLINE
+#define SDD_NOINLINE
#endif
-static void gbs_setcap(gbs str, size_t cap) {
- GB_STRING_HEADER(str)->cap = cap;
-}
+static void sdd_setcap(sdd str, size_t cap) { SDD_HDR(str)->cap = cap; }
-static GB_NOINLINE gbs gbs_impl_catvprintf(gbs s, const char *fmt, va_list ap) {
+static SDD_NOINLINE sdd sdd_impl_catvprintf(sdd s, const char *fmt,
+ va_list ap) {
size_t old_len;
int required;
va_list cpy;
@@ -70,11 +69,11 @@
required = vsnprintf(NULL, 0, fmt, cpy);
va_end(cpy);
if (s) {
- old_len = GB_STRING_HEADER(s)->len;
- s = gbs_makeroomfor(s, (size_t)required);
+ old_len = SDD_HDR(s)->len;
+ s = sdd_makeroomfor(s, (size_t)required);
} else {
old_len = 0;
- s = gbs_newcap((size_t)required);
+ s = sdd_newcap((size_t)required);
}
if (s == NULL)
return NULL;
@@ -82,10 +81,10 @@
return s;
}
-gbs gbs_newcap(size_t cap) {
- gbStringHeader *header;
+sdd sdd_newcap(size_t cap) {
+ sddtringHeader *header;
char *str;
- header = (gbStringHeader *)malloc(sizeof(gbStringHeader) + cap + 1);
+ header = (sddtringHeader *)malloc(sizeof(sddtringHeader) + cap + 1);
if (!header)
return NULL;
header->len = 0;
@@ -95,10 +94,10 @@
return str;
}
-gbs gbs_newlen(void const *init_str, size_t len) {
- gbStringHeader *header;
+sdd sdd_newlen(void const *init_str, size_t len) {
+ sddtringHeader *header;
char *str;
- header = (gbStringHeader *)malloc(sizeof(gbStringHeader) + len + 1);
+ header = (sddtringHeader *)malloc(sizeof(sddtringHeader) + len + 1);
if (!header)
return NULL;
header->len = len;
@@ -110,111 +109,109 @@
return str;
}
-gbs gbs_new(char const *str) {
+sdd sdd_new(char const *str) {
size_t len = str ? strlen(str) : 0;
- return gbs_newlen(str, len);
+ return sdd_newlen(str, len);
}
-gbs gbs_newvprintf(const char *fmt, va_list ap) {
- return gbs_impl_catvprintf(NULL, fmt, ap);
+sdd sdd_newvprintf(const char *fmt, va_list ap) {
+ return sdd_impl_catvprintf(NULL, fmt, ap);
}
-gbs gbs_newprintf(char const *fmt, ...) {
- gbs s;
+sdd sdd_newprintf(char const *fmt, ...) {
+ sdd s;
va_list ap;
va_start(ap, fmt);
- s = gbs_impl_catvprintf(NULL, fmt, ap);
+ s = sdd_impl_catvprintf(NULL, fmt, ap);
va_end(ap);
return s;
}
-void gbs_free(gbs str) {
+void sdd_free(sdd str) {
if (str == NULL)
return;
- free((gbStringHeader *)str - 1);
+ free((sddtringHeader *)str - 1);
}
-gbs gbs_dup(gbs const str) {
- return gbs_newlen(str, GB_STRING_HEADER(str)->len);
-}
+sdd sdd_dup(sdd const str) { return sdd_newlen(str, SDD_HDR(str)->len); }
-size_t gbs_len(gbs const str) { return GB_STRING_HEADER(str)->len; }
-size_t gbs_cap(gbs const str) { return GB_STRING_HEADER(str)->cap; }
+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 gbs_avail(gbs const str) {
- gbStringHeader *h = GB_STRING_HEADER(str);
+size_t sdd_avail(sdd const str) {
+ sddtringHeader *h = SDD_HDR(str);
if (h->cap > h->len)
return h->cap - h->len;
return 0;
}
-void gbs_clear(gbs str) {
- GB_STRING_HEADER(str)->len = 0;
+void sdd_clear(sdd str) {
+ SDD_HDR(str)->len = 0;
str[0] = '\0';
}
-gbs gbs_catlen(gbs str, void const *other, size_t other_len) {
- size_t curr_len = GB_STRING_HEADER(str)->len;
- str = gbs_makeroomfor(str, other_len);
+sdd sdd_catlen(sdd str, void const *other, size_t other_len) {
+ size_t curr_len = SDD_HDR(str)->len;
+ str = sdd_makeroomfor(str, other_len);
if (str == NULL)
return NULL;
memcpy(str + curr_len, other, other_len);
str[curr_len + other_len] = '\0';
- GB_STRING_HEADER(str)->len = curr_len + other_len;
+ SDD_HDR(str)->len = curr_len + other_len;
return str;
}
-gbs gbs_catgbs(gbs str, gbs const other) {
- return gbs_catlen(str, other, GB_STRING_HEADER(other)->len);
+sdd sdd_catsdd(sdd str, sdd const other) {
+ return sdd_catlen(str, other, SDD_HDR(other)->len);
}
-gbs gbs_cat(gbs str, char const *other) {
- return gbs_catlen(str, other, strlen(other));
+sdd sdd_cat(sdd str, char const *other) {
+ return sdd_catlen(str, other, strlen(other));
}
-gbs gbs_cpylen(gbs str, char const *cstr, size_t len) {
- if (gbs_cap(str) < len) {
- str = gbs_makeroomfor(str, len - GB_STRING_HEADER(str)->len);
+sdd sdd_cpylen(sdd str, char const *cstr, size_t len) {
+ if (sdd_cap(str) < len) {
+ str = sdd_makeroomfor(str, len - SDD_HDR(str)->len);
if (str == NULL)
return NULL;
}
- GB_STRING_HEADER(str)->len = len;
+ SDD_HDR(str)->len = len;
memcpy(str, cstr, len);
str[len] = '\0';
return str;
}
-gbs gbs_cpy(gbs str, char const *cstr) {
- return gbs_cpylen(str, cstr, strlen(cstr));
+sdd sdd_cpy(sdd str, char const *cstr) {
+ return sdd_cpylen(str, cstr, strlen(cstr));
}
-gbs gbs_makeroomfor(gbs str, size_t add_len) {
- size_t len = GB_STRING_HEADER(str)->len;
+sdd sdd_makeroomfor(sdd str, size_t add_len) {
+ size_t len = SDD_HDR(str)->len;
size_t new_len = len + add_len; // TODO overflow check
void *ptr, *new_ptr;
size_t available, new_size;
- available = gbs_avail(str);
+ available = sdd_avail(str);
if (available >= add_len) /* Return if there is enough space left */
return str;
- ptr = (char *)str - sizeof(gbStringHeader);
- new_size = sizeof(gbStringHeader) + new_len + 1;
+ ptr = (char *)str - sizeof(sddtringHeader);
+ new_size = sizeof(sddtringHeader) + new_len + 1;
new_ptr = realloc(ptr, new_size);
if (new_ptr == NULL) {
free(ptr);
return NULL;
}
- str = (char *)new_ptr + sizeof(gbStringHeader);
- gbs_setcap(str, new_len);
+ str = (char *)new_ptr + sizeof(sddtringHeader);
+ sdd_setcap(str, new_len);
return str;
}
-void gbs_pokelen(gbs str, size_t len) { GB_STRING_HEADER(str)->len = len; }
+void sdd_pokelen(sdd str, size_t len) { SDD_HDR(str)->len = len; }
-size_t gbs_totalmemused(gbs const s) {
- size_t cap = gbs_cap(s);
- return sizeof(gbStringHeader) + cap;
+size_t sdd_totalmemused(sdd const s) {
+ size_t cap = sdd_cap(s);
+ return sizeof(sddtringHeader) + cap;
}
-bool gbs_equal(gbs const lhs, gbs const rhs) {
- size_t lhs_len = GB_STRING_HEADER(lhs)->len;
- size_t rhs_len = GB_STRING_HEADER(rhs)->len;
+bool sdd_equal(sdd const lhs, sdd const rhs) {
+ size_t lhs_len = SDD_HDR(lhs)->len;
+ size_t rhs_len = SDD_HDR(rhs)->len;
if (lhs_len != rhs_len)
return false;
for (size_t i = 0; i < lhs_len; i++) {
@@ -224,12 +221,12 @@
return true;
}
-gbs gbs_trim(gbs str, char const *cut_set) {
+sdd sdd_trim(sdd str, char const *cut_set) {
char *start, *end, *start_pos, *end_pos;
size_t len;
start_pos = start = str;
- end_pos = end = str + GB_STRING_HEADER(str)->len - 1;
+ end_pos = end = str + SDD_HDR(str)->len - 1;
while (start_pos <= end && strchr(cut_set, *start_pos))
start_pos++;
@@ -238,7 +235,7 @@
len = (start_pos > end_pos) ? 0 : ((size_t)(end_pos - start_pos) + 1);
- GB_STRING_HEADER(str)->len = len;
+ SDD_HDR(str)->len = len;
if (str != start_pos)
memmove(str, start_pos, len);
str[len] = '\0';
@@ -245,22 +242,22 @@
return str;
}
-gbs gbs_catvprintf(gbs s, const char *fmt, va_list ap) {
+sdd sdd_catvprintf(sdd s, const char *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.
assert(s != NULL);
- return gbs_impl_catvprintf(s, fmt, ap);
+ return sdd_impl_catvprintf(s, fmt, ap);
}
-gbs gbs_catprintf(gbs s, char const *fmt, ...) {
+sdd sdd_catprintf(sdd s, char const *fmt, ...) {
assert(s != NULL);
va_list ap;
va_start(ap, fmt);
- s = gbs_impl_catvprintf(s, fmt, ap);
+ s = sdd_impl_catvprintf(s, fmt, ap);
va_end(ap);
return s;
}
-#undef GB_STRING_HEADER
-#undef GB_NOINLINE
+#undef SDD_HDR
+#undef SDD_NOINLINE
--- a/thirdparty/gbstring.h
+++ b/thirdparty/gbstring.h
@@ -3,52 +3,52 @@
#include <stdbool.h>
#include <stddef.h>
-typedef char *gbs;
+typedef char *sdd;
-gbs gbs_new(char const *str);
+sdd sdd_new(char const *str);
// ^- Create new with copy of null-terminated cstring
-gbs gbs_newlen(void const *str, size_t len);
+sdd sdd_newlen(void const *str, size_t len);
// ^- Same, but without calling strlen().
// Resulting new string will be null terminated.
-gbs gbs_newcap(size_t cap);
+sdd sdd_newcap(size_t cap);
// ^- 'Raw' new with a specific capacity.
// Length will be set to 0, and '\0' written at position 0.
-gbs gbs_newvprintf(const char *fmt, va_list ap);
-gbs gbs_newprintf(char const *fmt, ...)
+sdd sdd_newvprintf(const char *fmt, va_list ap);
+sdd sdd_newprintf(char const *fmt, ...)
#ifdef __GNUC__
__attribute__((format(printf, 1, 2)))
#endif
;
-void gbs_free(gbs str);
+void sdd_free(sdd str);
-gbs gbs_dup(gbs const str);
-// ^- Same as gbs_newlen(str, gbs_len(str))
-gbs gbs_cpy(gbs str, char const *cstr);
+sdd sdd_dup(sdd const str);
+// ^- Same as sdd_newlen(str, sdd_len(str))
+sdd sdd_cpy(sdd str, char const *cstr);
// ^- Set `str` to contain the contents of `cstr`
-gbs gbs_cpylen(gbs str, char const *cstr, size_t len);
+sdd sdd_cpylen(sdd str, char const *cstr, size_t len);
-size_t gbs_len(gbs const str); // Bytes used by string (excl. null term)
-size_t gbs_cap(gbs const str); // Bytes allocated on heap (excl. null term)
-size_t gbs_avail(gbs const str); // cap - len
+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
-gbs gbs_cat(gbs str, char const *other);
-gbs gbs_catlen(gbs str, void const *other, size_t len);
-gbs gbs_catgbs(gbs str, gbs const other);
-gbs gbs_catvprintf(gbs str, const char *fmt, va_list ap);
-gbs gbs_catprintf(gbs str, char const *fmt, ...)
+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
;
-void gbs_clear(gbs str); // Set len to 0, write '\0' at pos 0
-gbs gbs_makeroomfor(gbs str, size_t add_len);
+void sdd_clear(sdd str); // Set len to 0, write '\0' at pos 0
+sdd sdd_makeroomfor(sdd str, size_t add_len);
// ^- Makes sure
-void gbs_pokelen(gbs str, size_t len);
+void sdd_pokelen(sdd str, size_t len);
// ^- Manually update length field. Doesn't do anything else for you.
-bool gbs_equal(gbs const lhs, gbs const rhs);
+bool sdd_equal(sdd const lhs, sdd const rhs);
-gbs gbs_trim(gbs str, char const *cut_set);
+sdd sdd_trim(sdd str, char const *cut_set);
-size_t gbs_totalmemused(gbs const str);
+size_t sdd_totalmemused(sdd const str);