From 3a2612e61b74f246f62585db1be23158f0243dd6 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 5 Oct 2024 11:47:44 -0700 Subject: Simplify. --- cstring/include/cstring.h | 199 ++++++++++++++++++++++++---------------------- 1 file changed, 105 insertions(+), 94 deletions(-) (limited to 'cstring/include') diff --git a/cstring/include/cstring.h b/cstring/include/cstring.h index 84976b1..b07dad6 100644 --- a/cstring/include/cstring.h +++ b/cstring/include/cstring.h @@ -17,100 +17,111 @@ /// A fixed-size string. /// The string is null-terminated so that it can be used with the usual C APIs. -#define DEF_STRING(STRING, SIZE) \ - typedef struct STRING { \ - size_t length; \ - char str[SIZE]; \ - } STRING; \ - \ - static const size_t STRING##_size = SIZE; \ - \ - static inline const char* STRING##_cstr(const STRING* str) { \ - return str->str; \ - } \ - \ - static inline size_t STRING##_length(STRING str) { return str.length; } \ - \ - static inline STRING STRING##_make(const char* cstr) { \ - if (!cstr) { \ - return (STRING){0}; \ - } else { \ - STRING str = (STRING){0}; \ - str.length = strlcpy(str.str, cstr, SIZE); \ - return str; \ - } \ - } \ - \ - static inline STRING STRING##_dirname(STRING path) { \ - STRING str = path; \ - for (int i = str.length - 1; i >= 0; --i) { \ - if (str.str[i] == '/' || str.str[i] == '\\') { \ - str.str[i] = 0; \ - str.length = i; \ - return str; \ - } else { \ - str.str[i] = 0; \ - } \ - } \ - str = (STRING){0}; \ - str.str[0] = '.'; \ - str.length = 1; \ - return str; \ - } \ - \ - static inline void STRING##_append_cstr(STRING* a, const char* b) { \ - size_t b_length = strlen(b); \ - ASSERT(a->length + b_length + 1 < SIZE); \ - strlcpy(a->str + a->length, b, SIZE); \ - a->length = a->length + b_length; \ - } \ - \ - static inline void STRING##_append(STRING* a, STRING b) { \ - ASSERT(a->length + b.length + 1 < SIZE); \ - strlcpy(a->str + a->length, b.str, SIZE); \ - a->length = a->length + b.length; \ - } \ - \ - static inline STRING STRING##_concat(STRING a, STRING b) { \ - ASSERT(a.length + b.length + 1 < SIZE); \ - STRING str = {0}; \ - strlcpy(str.str, a.str, SIZE); \ - strlcpy(str.str + a.length, b.str, SIZE); \ - str.length = a.length + b.length; \ - return str; \ - } \ - \ - static inline STRING STRING##_concat_cstr(STRING a, const char* b) { \ - return STRING##_concat(a, STRING##_make(b)); \ - } \ - \ - static inline STRING STRING##_concat_path(STRING a, STRING b) { \ - return STRING##_concat(STRING##_concat(a, STRING##_make("/")), b); \ - } \ - \ - static inline bool STRING##_eq(STRING a, STRING b) { \ - if (a.length != b.length) { \ - return false; \ - } \ - return strncmp(a.str, b.str, a.length) == 0; \ - } \ - \ - static inline bool STRING##_eq_cstr(STRING a, const char* b) { \ - return (a.length == strlen(b)) && strncmp(a.str, b, a.length) == 0; \ - } \ - \ - static inline bool STRING##_empty(STRING a) { return a.length == 0; } \ - \ - static inline STRING STRING##_itoa(int n) { \ - STRING str = (STRING){0}; \ - const int written = snprintf(str.str, SIZE, "%d", n); \ - ASSERT(written >= 0); \ - str.length = (size_t)written; \ - return str; \ - } \ - \ - static inline uint64_t STRING##_hash(STRING str) { \ - return cstring_hash(str.str); \ +#define DEF_STRING(STRING, SIZE) \ + typedef struct STRING { \ + size_t length; \ + char str[SIZE]; \ + } STRING; \ + \ + static const size_t STRING##_size = SIZE; \ + \ + static inline const char* STRING##_cstr(const STRING* str) { \ + return str->str; \ + } \ + \ + static inline size_t STRING##_length(const STRING* str) { \ + return str->length; \ + } \ + \ + static inline STRING STRING##_make(const char* cstr) { \ + if (!cstr) { \ + return (STRING){0}; \ + } else { \ + STRING str = (STRING){0}; \ + str.length = strlcpy(str.str, cstr, SIZE); \ + return str; \ + } \ + } \ + \ + static inline STRING STRING##_dirname(const STRING path) { \ + STRING str = path; \ + for (int i = str.length - 1; i >= 0; --i) { \ + if (str.str[i] == '/' || str.str[i] == '\\') { \ + str.str[i] = 0; \ + str.length = i; \ + return str; \ + } else { \ + str.str[i] = 0; \ + } \ + } \ + str = (STRING){0}; \ + str.str[0] = '.'; \ + str.length = 1; \ + return str; \ + } \ + \ + static inline void STRING##_append_cstr_len( \ + STRING* a, const char* b, const size_t b_length) { \ + ASSERT(a->length + b_length + 1 <= SIZE); \ + strlcpy(a->str + a->length, b, SIZE - a->length); \ + a->length += b_length; \ + } \ + \ + static inline void STRING##_append_cstr(STRING* a, const char* b) { \ + STRING##_append_cstr_len(a, b, strlen(b)); \ + } \ + \ + static inline void STRING##_append(STRING* a, const STRING b) { \ + STRING##_append_cstr_len(a, b.str, b.length); \ + } \ + \ + static inline STRING STRING##_concat_cstr_len( \ + const STRING a, const char* b, const size_t b_length) { \ + ASSERT(a.length + b_length + 1 <= SIZE); \ + STRING str = {0}; \ + strlcpy(str.str, a.str, SIZE); \ + strlcpy(str.str + a.length, b, SIZE - a.length); \ + str.length = a.length + b_length; \ + return str; \ + } \ + \ + static inline STRING STRING##_concat_cstr(const STRING a, const char* b) { \ + return STRING##_concat_cstr_len(a, b, strlen(b)); \ + } \ + \ + static inline STRING STRING##_concat(const STRING a, const STRING b) { \ + return STRING##_concat_cstr_len(a, b.str, b.length); \ + } \ + \ + static inline STRING STRING##_concat_path(const STRING a, const STRING b) { \ + return STRING##_concat(STRING##_concat_cstr(a, "/"), b); \ + } \ + \ + static inline bool STRING##_eq_cstr_len( \ + const STRING a, const char* b, size_t b_length) { \ + return (a.length == b_length) && strncmp(a.str, b, a.length) == 0; \ + } \ + \ + static inline bool STRING##_eq_cstr(const STRING a, const char* b) { \ + return STRING##_eq_cstr_len(a, b, strlen(b)); \ + } \ + \ + static inline bool STRING##_eq(const STRING a, const STRING b) { \ + return STRING##_eq_cstr_len(a, b.str, b.length); \ + } \ + \ + static inline bool STRING##_empty(const STRING a) { return a.length == 0; } \ + \ + static inline STRING STRING##_itoa(int n) { \ + STRING str = (STRING){0}; \ + const int written = snprintf(str.str, SIZE, "%d", n); \ + ASSERT(written >= 0); \ + str.length = (size_t)written; \ + return str; \ + } \ + \ + static inline uint64_t STRING##_hash(const STRING str) { \ + return cstring_hash(str.str); \ } /// Return a hash of the given string. -- cgit v1.2.3