ref: 96cd1bdbc532f00c2b258bbe91735bf6ffc03875
parent: d0602319e882c94fa8eb0223cb53664664f17d7a
author: cancel <cancel@cancel.fm>
date: Tue Jan 7 18:38:30 EST 2020
Cleanup
--- a/thirdparty/gbstring.c
+++ b/thirdparty/gbstring.c
@@ -58,10 +58,6 @@
#define GB_NOINLINE
#endif
-static void gbs_setlen(gbs str, size_t len) {
- GB_STRING_HEADER(str)->len = len;
-}
-
static void gbs_setcap(gbs str, size_t cap) {
GB_STRING_HEADER(str)->cap = cap;
}
@@ -135,7 +131,9 @@
free((gbStringHeader *)str - 1);
}
-gbs gbs_dup(gbs const str) { return gbs_newlen(str, gbs_len(str)); }
+gbs gbs_dup(gbs const str) {
+ return gbs_newlen(str, GB_STRING_HEADER(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; }
@@ -148,23 +146,23 @@
}
void gbs_clear(gbs str) {
- gbs_setlen(str, 0);
+ GB_STRING_HEADER(str)->len = 0;
str[0] = '\0';
}
gbs gbs_catlen(gbs str, void const *other, size_t other_len) {
- size_t curr_len = gbs_len(str);
+ size_t curr_len = GB_STRING_HEADER(str)->len;
str = gbs_makeroomfor(str, other_len);
if (str == NULL)
return NULL;
memcpy(str + curr_len, other, other_len);
str[curr_len + other_len] = '\0';
- gbs_setlen(str, curr_len + other_len);
+ GB_STRING_HEADER(str)->len = curr_len + other_len;
return str;
}
gbs gbs_catgbs(gbs str, gbs const other) {
- return gbs_catlen(str, other, gbs_len(other));
+ return gbs_catlen(str, other, GB_STRING_HEADER(other)->len);
}
gbs gbs_cat(gbs str, char const *other) {
@@ -173,13 +171,13 @@
gbs gbs_cpylen(gbs str, char const *cstr, size_t len) {
if (gbs_cap(str) < len) {
- str = gbs_makeroomfor(str, len - gbs_len(str));
+ str = gbs_makeroomfor(str, len - GB_STRING_HEADER(str)->len);
if (str == NULL)
return NULL;
}
+ GB_STRING_HEADER(str)->len = len;
memcpy(str, cstr, len);
str[len] = '\0';
- gbs_setlen(str, len);
return str;
}
gbs gbs_cpy(gbs str, char const *cstr) {
@@ -187,7 +185,7 @@
}
gbs gbs_makeroomfor(gbs str, size_t add_len) {
- size_t len = gbs_len(str);
+ size_t len = GB_STRING_HEADER(str)->len;
size_t new_len = len + add_len; // TODO overflow check
void *ptr, *new_ptr;
size_t available, new_size;
@@ -207,6 +205,8 @@
return str;
}
+void gbs_pokelen(gbs str, size_t len) { GB_STRING_HEADER(str)->len = len; }
+
size_t gbs_totalmemused(gbs const s) {
size_t cap = gbs_cap(s);
return sizeof(gbStringHeader) + cap;
@@ -213,17 +213,14 @@
}
bool gbs_equal(gbs const lhs, gbs const rhs) {
- size_t lhs_len, rhs_len, i;
- lhs_len = gbs_len(lhs);
- rhs_len = gbs_len(rhs);
+ size_t lhs_len = GB_STRING_HEADER(lhs)->len;
+ size_t rhs_len = GB_STRING_HEADER(rhs)->len;
if (lhs_len != rhs_len)
return false;
-
- for (i = 0; i < lhs_len; i++) {
+ for (size_t i = 0; i < lhs_len; i++) {
if (lhs[i] != rhs[i])
return false;
}
-
return true;
}
@@ -232,7 +229,7 @@
size_t len;
start_pos = start = str;
- end_pos = end = str + gbs_len(str) - 1;
+ end_pos = end = str + GB_STRING_HEADER(str)->len - 1;
while (start_pos <= end && strchr(cut_set, *start_pos))
start_pos++;
@@ -241,12 +238,10 @@
len = (start_pos > end_pos) ? 0 : ((size_t)(end_pos - start_pos) + 1);
+ GB_STRING_HEADER(str)->len = len;
if (str != start_pos)
memmove(str, start_pos, len);
str[len] = '\0';
-
- gbs_setlen(str, len);
-
return str;
}
--- a/thirdparty/gbstring.h
+++ b/thirdparty/gbstring.h
@@ -5,9 +5,14 @@
typedef char *gbs;
-gbs gbs_newcap(size_t cap);
-gbs gbs_newlen(void const *str, size_t len);
gbs gbs_new(char const *str);
+// ^- Create new with copy of null-terminated cstring
+gbs gbs_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);
+// ^- '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, ...)
#ifdef __GNUC__
@@ -17,15 +22,17 @@
void gbs_free(gbs str);
gbs gbs_dup(gbs const str);
+// ^- Same as gbs_newlen(str, gbs_len(str))
+gbs gbs_cpy(gbs str, char const *cstr);
+// ^- Set `str` to contain the contents of `cstr`
+gbs gbs_cpylen(gbs str, char const *cstr, size_t len);
-size_t gbs_len(gbs const str);
-size_t gbs_cap(gbs const str);
-size_t gbs_avail(gbs const str);
+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
-void gbs_clear(gbs str);
-
-gbs gbs_catlen(gbs str, void const *other, size_t 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, ...)
@@ -34,10 +41,11 @@
#endif
;
-gbs gbs_cpylen(gbs str, char const *cstr, size_t len);
-gbs gbs_cpy(gbs str, char const *cstr);
-
+void gbs_clear(gbs str); // Set len to 0, write '\0' at pos 0
gbs gbs_makeroomfor(gbs str, size_t add_len);
+// ^- Makes sure
+void gbs_pokelen(gbs str, size_t len);
+// ^- Manually update length field. Doesn't do anything else for you.
bool gbs_equal(gbs const lhs, gbs const rhs);