diff options
Diffstat (limited to 'src/std')
-rw-r--r-- | src/std/containers/darray.h | 12 | ||||
-rw-r--r-- | src/std/str.c | 2 | ||||
-rw-r--r-- | src/std/str.h | 14 |
3 files changed, 25 insertions, 3 deletions
diff --git a/src/std/containers/darray.h b/src/std/containers/darray.h index 729b4cf..25bf846 100644 --- a/src/std/containers/darray.h +++ b/src/std/containers/darray.h @@ -33,14 +33,22 @@ #define PREFIX static +/* if (arena != NULL) {\ */ +/* d = arena_alloc(arena, sizeof(T##_darray));\ */ +/* data = arena_alloc(arena, starting_capacity * sizeof(T));\ */ +/* } else {\ */ +/* }\ */ + #define KITC_DECL_TYPED_ARRAY(T) \ typedef typed_array(T) T##_darray; \ typedef typed_array_iterator(T) T##_darray_iter; \ \ /* Create a new one growable array */ \ PREFIX T##_darray *T##_darray_new(size_t starting_capacity) { \ - T##_darray *d = malloc(sizeof(T##_darray)); \ - T *data = malloc(starting_capacity * sizeof(T)); \ + T##_darray *d; \ + T *data; \ + d = malloc(sizeof(T##_darray)); \ + data = malloc(starting_capacity * sizeof(T)); \ \ d->len = 0; \ d->capacity = starting_capacity; \ diff --git a/src/std/str.c b/src/std/str.c index 1c687fa..07a8e73 100644 --- a/src/std/str.c +++ b/src/std/str.c @@ -5,6 +5,8 @@ str8 str8_create(u8* buf, size_t len) { return (str8){ .buf = buf, .len = len }; } +str8 str8_cstr_view(char* string) { return str8_create((u8*)string, strlen(string)); } + bool str8_equals(str8 a, str8 b) { if (a.len != b.len) { return false; diff --git a/src/std/str.h b/src/std/str.h index 735b88e..1ebecac 100644 --- a/src/std/str.h +++ b/src/std/str.h @@ -10,6 +10,8 @@ */ #pragma once +#include <ctype.h> + #include "defines.h" #include "mem.h" @@ -36,6 +38,11 @@ char* str8_to_cstr(arena* a, str8 s); #define cstr(a, s) (str8_to_cstr(a, s)) // Shorthand +/** @brief Return a str8 that references a statically allocated string. + `string` therefore must already be null-terminated. + @note The backing `string` cannot be modified. */ +str8 str8_cstr_view(char* string); + // --- Comparisons /** @brief Compare two strings for exact equality */ @@ -70,4 +77,9 @@ str8 str8_concat(arena* a, str8 left, str8 right); static inline bool str8_is_null_term(str8 a) { return a.buf[a.len] == 0; // This doesn't seem safe. YOLO -}
\ No newline at end of file +} + +// TODO: move or delete this and replace with handling using our internal type +static void skip_space(char* p) { + while (isspace((unsigned char)*p)) ++p; +} |