Implement read-only string literals

This commit is contained in:
2025-02-09 14:29:54 +00:00
parent 7657ad1b58
commit bbf38499ca
4 changed files with 48 additions and 15 deletions

View File

@@ -15,15 +15,20 @@ struct str8 {
c8 *buf;
};
typedef const Str8 Str8RO;
#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))})
#define wapp_str8_lit(STRING) ((Str8){.capacity = (sizeof(STRING) - 1) * 2, \
.size = sizeof(STRING) - 1, \
.buf = memcpy(&((c8 [sizeof(STRING) * 2]){0}), STRING, sizeof(STRING))})
#define wapp_str8_lit_ro(STRING) ((Str8RO){.capacity = sizeof(STRING) - 1, \
.size = sizeof(STRING) - 1, \
.buf = (c8 *)STRING})
c8 wapp_str8_get(const Str8 *str, u64 index);
c8 wapp_str8_get(Str8RO *str, u64 index);
void wapp_str8_set(Str8 *str, u64 index, c8 c);
#ifdef __cplusplus