diff options
Diffstat (limited to 'src/std')
-rw-r--r-- | src/std/containers/darray.h | 46 | ||||
-rw-r--r-- | src/std/str.c | 10 | ||||
-rw-r--r-- | src/std/str.h | 2 |
3 files changed, 35 insertions, 23 deletions
diff --git a/src/std/containers/darray.h b/src/std/containers/darray.h index d8c1e6d..080afb4 100644 --- a/src/std/containers/darray.h +++ b/src/std/containers/darray.h @@ -27,12 +27,12 @@ /* @brief current number of items in the array */ \ size_t len; \ size_t capacity; \ - T *data; \ + T* data; \ } #define typed_array_iterator(T) \ struct { \ - T##_darray *array; \ + T##_darray* array; \ size_t current_idx; \ } @@ -45,9 +45,9 @@ typedef typed_array_iterator(Type) Type##_darray_iter; \ \ /* Create a new one growable array */ \ - PREFIX Type##_darray *Type##_darray_new(size_t starting_capacity) { \ - Type##_darray *d; \ - T *data; \ + PREFIX Type##_darray* Type##_darray_new(size_t starting_capacity) { \ + Type##_darray* d; \ + T* data; \ d = malloc(sizeof(Type##_darray)); \ data = malloc(starting_capacity * sizeof(T)); \ \ @@ -58,16 +58,16 @@ return d; \ } \ \ - PREFIX void Type##_darray_free(Type##_darray *d) { \ + PREFIX void Type##_darray_free(Type##_darray* d) { \ if (d != NULL) { \ free(d->data); \ free(d); \ } \ } \ \ - PREFIX T *Type##_darray_resize(Type##_darray *d, size_t capacity) { \ + PREFIX T* Type##_darray_resize(Type##_darray* d, size_t capacity) { \ /* resize the internal data block */ \ - T *new_data = realloc(d->data, sizeof(T) * capacity); \ + T* new_data = realloc(d->data, sizeof(T) * capacity); \ /* TODO: handle OOM error */ \ \ d->capacity = capacity; \ @@ -75,11 +75,11 @@ return new_data; \ } \ \ - PREFIX void Type##_darray_push(Type##_darray *d, T value) { \ + PREFIX void Type##_darray_push(Type##_darray* d, T value) { \ if (d->len >= d->capacity) { \ size_t new_capacity = \ d->capacity > 0 ? d->capacity * DARRAY_RESIZE_FACTOR : DARRAY_DEFAULT_CAPACITY; \ - T *resized = Type##_darray_resize(d, new_capacity); \ + T* resized = Type##_darray_resize(d, new_capacity); \ (void)resized; \ } \ \ @@ -87,37 +87,37 @@ d->len += 1; \ } \ \ - PREFIX void Type##_darray_push_copy(Type##_darray *d, const T *value) { \ + PREFIX void Type##_darray_push_copy(Type##_darray* d, const T* value) { \ if (d->len >= d->capacity) { \ size_t new_capacity = \ d->capacity > 0 ? d->capacity * DARRAY_RESIZE_FACTOR : DARRAY_DEFAULT_CAPACITY; \ - T *resized = Type##_darray_resize(d, new_capacity); \ + T* resized = Type##_darray_resize(d, new_capacity); \ (void)resized; \ } \ \ - T *place = d->data + d->len; \ + T* place = d->data + d->len; \ d->len += 1; \ memcpy(place, value, sizeof(T)); \ } \ \ - PREFIX void Type##_darray_pop(Type##_darray *d, T *dest) { \ - T *item = d->data + (d->len - 1); \ + PREFIX void Type##_darray_pop(Type##_darray* d, T* dest) { \ + T* item = d->data + (d->len - 1); \ d->len -= 1; \ memcpy(dest, item, sizeof(T)); \ } \ \ - PREFIX void Type##_darray_ins(Type##_darray *d, const T *value, size_t index) { \ + PREFIX void Type##_darray_ins(Type##_darray* d, const T* value, size_t index) { \ /* check if requires resize */ \ if (d->len + 1 > d->capacity) { \ size_t new_capacity = \ d->capacity > 0 ? d->capacity * DARRAY_RESIZE_FACTOR : DARRAY_DEFAULT_CAPACITY; \ - T *resized = Type##_darray_resize(d, new_capacity); \ + T* resized = Type##_darray_resize(d, new_capacity); \ (void)resized; \ } \ \ /* shift existing data after index */ \ - T *insert_dest = d->data + index; \ - T *shift_dest = insert_dest + 1; \ + T* insert_dest = d->data + index; \ + T* shift_dest = insert_dest + 1; \ \ int num_items = d->len - index; \ \ @@ -126,21 +126,21 @@ memcpy(insert_dest, value, sizeof(T)); \ } \ \ - PREFIX void Type##_darray_clear(Type##_darray *d) { \ + PREFIX void Type##_darray_clear(Type##_darray* d) { \ d->len = 0; \ memset(d->data, 0, d->capacity * sizeof(T)); \ } \ \ - PREFIX size_t Type##_darray_len(Type##_darray *d) { return d->len; } \ + PREFIX size_t Type##_darray_len(Type##_darray* d) { return d->len; } \ \ - PREFIX Type##_darray_iter Type##_darray_iter_new(Type##_darray *d) { \ + PREFIX Type##_darray_iter Type##_darray_iter_new(Type##_darray* d) { \ Type##_darray_iter iterator; \ iterator.array = d; \ iterator.current_idx = 0; \ return iterator; \ } \ \ - PREFIX void *Type##_darray_iter_next(Type##_darray_iter *iterator) { \ + PREFIX void* Type##_darray_iter_next(Type##_darray_iter* iterator) { \ if (iterator->current_idx < iterator->array->len) { \ return &iterator->array->data[iterator->current_idx++]; \ } else { \ diff --git a/src/std/str.c b/src/std/str.c index 2aac15f..89c76a0 100644 --- a/src/std/str.c +++ b/src/std/str.c @@ -1,6 +1,7 @@ #include "str.h" #include <assert.h> #include <string.h> +#include "log.h" #include "mem.h" Str8 Str8_create(u8* buf, size_t len) { return (Str8){ .buf = buf, .len = len }; } @@ -33,6 +34,15 @@ char* Str8_to_cstr(arena* a, Str8 s) { return (char*)dest; } +char* Clone_cstr(arena* a, const char* s) { + if (s == NULL) { + WARN("Tried to clone a NULL char*"); + return NULL; + } + Str8 st = Str8_cstr_view(s); + return Str8_to_cstr(a, st); +} + Str8 Str8_concat(arena* a, Str8 left, Str8 right) { size_t n_bytes = left.len + right.len + 1; diff --git a/src/std/str.h b/src/std/str.h index c25c615..a29bf9a 100644 --- a/src/std/str.h +++ b/src/std/str.h @@ -45,6 +45,8 @@ char* Str8_to_cstr(arena* a, Str8 s); @note The backing `string` cannot be modified. */ Str8 Str8_cstr_view(char* string); +char* Clone_cstr(arena* a, const char* s); + // --- Comparisons /** @brief Compare two strings for exact equality */ |