diff --git a/src/core/strings/str8/str8.c b/src/core/strings/str8/str8.c index faa4355..9b221a7 100644 --- a/src/core/strings/str8/str8.c +++ b/src/core/strings/str8/str8.c @@ -1,14 +1,4 @@ #include "str8.h" -#include - -Str8 wapp_str8_lit(char *str) { - if (!str) { - return (Str8){.capacity = 0, .size = 0, .buf = (u8 *)""}; - } - - u64 size = strlen(str); - return (Str8){.capacity = size, .size = size, .buf = (u8 *)str}; -} char wapp_str8_get(const Str8 *str, u64 index) { if (index >= str->size) { @@ -23,5 +13,5 @@ void wapp_str8_set(Str8 *str, u64 index, char c) { return; } - str->buf[index] = (u8)c; + str->buf[index] = (c8)c; } diff --git a/src/core/strings/str8/str8.h b/src/core/strings/str8/str8.h index c497dcb..b72763d 100644 --- a/src/core/strings/str8/str8.h +++ b/src/core/strings/str8/str8.h @@ -2,6 +2,7 @@ #define STR8_H #include "aliases.h" +#include #ifdef __cplusplus BEGIN_C_LINKAGE @@ -11,12 +12,17 @@ typedef struct str8 Str8; struct str8 { u64 capacity; u64 size; - u8 *buf; + c8 *buf; }; -#define wapp_str8_buf(CAPACITY) ((Str8){.capacity = CAPACITY, .size = 0, .buf = (u8[CAPACITY]){0}}) +#define wapp_str8_buf(CAPACITY) ((Str8){.capacity = CAPACITY, .size = 0, .buf = (c8[CAPACITY]){0}}) + +// Utilises the fact that memcpy returns pointer to dest buffer and that getting +// address of compound literals is valid in C to create a string on the stack +#define wapp_str8_lit(STRING) ((Str8){.capacity = sizeof(STRING) - 1, \ + .size = sizeof(STRING) - 1, \ + .buf = memcpy(&((c8 [sizeof(STRING)]){0}), STRING, sizeof(STRING))}) -Str8 wapp_str8_lit(char *str); char wapp_str8_get(const Str8 *str, u64 index); void wapp_str8_set(Str8 *str, u64 index, char c);