diff --git a/src/core/file/file.c b/src/core/file/file.c index 1885211..30550c7 100644 --- a/src/core/file/file.c +++ b/src/core/file/file.c @@ -61,13 +61,13 @@ u64 wapp_file_get_length(File *file) { return output; } -u64 wapp_file_read(u8 *dst_byte_array, File *file, u64 item_count) { - wapp_debug_assert(dst_byte_array != NULL && file != NULL, - "`dst_byte_array` and `file` should not be NULL."); +u64 wapp_file_read(Array *dst_buf, File *file, u64 item_count) { + wapp_debug_assert(dst_buf != NULL && file != NULL, + "`dst_buf` and `file` should not be NULL."); u64 file_length = wapp_file_get_length(file); - u64 item_size = wapp_array_item_size(dst_byte_array); - u64 dst_byte_capacity = wapp_array_capacity(dst_byte_array) * item_size; + u64 item_size = dst_buf->item_size; + u64 dst_byte_capacity = dst_buf->capacity * item_size; u64 req_byte_count = item_count * item_size; u64 copy_byte_count = 0; @@ -77,24 +77,24 @@ u64 wapp_file_read(u8 *dst_byte_array, File *file, u64 item_count) { copy_byte_count = file_length <= dst_byte_capacity ? file_length : dst_byte_capacity; } - u64 count = fread(dst_byte_array, sizeof(u8), copy_byte_count, file); + u64 count = fread(dst_buf, sizeof(u8), copy_byte_count, file); if (ferror(file)) { return 0; } - wapp_array_set_count(dst_byte_array, count / item_size); + dst_buf->count = count / item_size; - return wapp_array_count(dst_byte_array); + return dst_buf->count; } -u64 wapp_file_write(const u8 *src_byte_array, File *file, u64 item_count) { - wapp_debug_assert(src_byte_array != NULL && file != NULL, - "`src_byte_array` and `file` should not be NULL."); +u64 wapp_file_write(const Array *src_buf, File *file, u64 item_count) { + wapp_debug_assert(src_buf != NULL && file != NULL, + "`src_buf` and `file` should not be NULL."); - u64 item_size = wapp_array_item_size(src_byte_array); - u64 src_byte_count = wapp_array_count(src_byte_array) * item_size; + u64 item_size = src_buf->item_size; + u64 src_byte_count = src_buf->count * item_size; u64 req_byte_count = item_count * item_size; u64 to_copy = req_byte_count <= src_byte_count ? req_byte_count : src_byte_count; - return fwrite(src_byte_array, sizeof(u8), to_copy, file); + return fwrite(src_buf, sizeof(u8), to_copy, file); } i32 wapp_file_flush(File *file) { diff --git a/src/core/file/file.h b/src/core/file/file.h index 0a27bd6..868e0f7 100644 --- a/src/core/file/file.h +++ b/src/core/file/file.h @@ -44,8 +44,8 @@ File *wapp_file_open(Str8RO *filename, FileAccessMode mode); u64 wapp_file_get_current_position(File *file); i32 wapp_file_seek(File *file, u64 offset, FileSeekOrigin origin); u64 wapp_file_get_length(File *file); -u64 wapp_file_read(u8 *dst_byte_array, File *file, u64 item_count); -u64 wapp_file_write(const u8 *src_byte_array, File *file, u64 item_count); +u64 wapp_file_read(Array *dst_buf, File *file, u64 item_count); +u64 wapp_file_write(const Array *src_buf, File *file, u64 item_count); i32 wapp_file_flush(File *file); i32 wapp_file_close(File *file); diff --git a/src/primitives/array/array.c b/src/primitives/array/array.c index 86c5cc1..87e0124 100644 --- a/src/primitives/array/array.c +++ b/src/primitives/array/array.c @@ -5,121 +5,71 @@ #include "../mem_allocator/mem_allocator.h" #include "../../common/misc/misc_utils.h" #include "../../common/aliases/aliases.h" -#include "../../common/platform/platform.h" #include -#define _array_header(DATA_PTR) (ArrayHeader *)((u8 *)DATA_PTR - sizeof(ArrayHeader)) +#define _offset_pointer(PTR, OFFSET) ((void *)((uptr)(PTR) + (OFFSET))) -u64 _array_count(u8 *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); +void *_array_get(Array *array, u64 index, u64 item_size) { + wapp_runtime_assert(array != NULL, "`array` should not be NULL"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == array->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == array->item_size, "Invalid item type provided"); + wapp_runtime_assert(index < array->count, "`index` is out of bounds"); - ArrayHeader *arr_header = _array_header(array); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); - - return arr_header->count; + return _offset_pointer(array->items, array->item_size * index); } -u64 _array_capacity(u8 *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - - ArrayHeader *arr_header = _array_header(array); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); - - return arr_header->capacity; +void _array_set(Array *array, u64 index, void *value, u64 item_size) { + void *item = _array_get(array, index, item_size); + memcpy(item, value, array->item_size); } -u64 _array_item_size(u8 *array) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); +void _array_append_capped(Array *array, void *value, u64 item_size) { + wapp_runtime_assert(array != NULL, "`array` should not be NULL"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == array->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == array->item_size, "Invalid item type provided"); - ArrayHeader *arr_header = _array_header(array); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + if (array->count >= array->capacity) { return; } - return arr_header->item_size; -} - -void _array_set_count(u8 *array, u64 count) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - - ArrayHeader *arr_header = _array_header(array); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); - - arr_header->count = count; -} - -u8 *_array_get(u8 *array, u64 index, u64 item_size) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - - ArrayHeader *arr_header = _array_header(array); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); - wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); - wapp_runtime_assert(index < arr_header->count, "`index` is out of bounds"); - - return array + arr_header->item_size * index; -} - -void _array_set(u8 *array, u64 index, u8 *value, u64 item_size) { - u8 *item = _array_get(array, index, item_size); - ArrayHeader *arr_header = _array_header(array); - memcpy((void *)item, (void *)value, arr_header->item_size); -} - -void _array_append_capped(u8 *array, u8 *value, u64 item_size) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); - - ArrayHeader *arr_header = _array_header(array); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); - wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); - - if (arr_header->count >= arr_header->capacity) { return; } - - u64 index = (arr_header->count)++; + u64 index = (array->count)++; _array_set(array, index, value, item_size); } -void _array_extend_capped(u8 *dst_array, const u8 *src_array, u64 item_size) { - wapp_debug_assert(dst_array != NULL && src_array != NULL, "`dst_array` and `src_array` should not be NULL"); +void _array_extend_capped(Array *dst, const Array *src, u64 item_size) { + wapp_runtime_assert(dst != NULL && src != NULL, "`dst` and `src` should not be NULL"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == dst->magic, "`dst` is not a valid wapp array"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == src->magic, "`src` is not a valid wapp array"); + wapp_runtime_assert(item_size == dst->item_size && item_size == src->item_size, "Invalid item type provided"); - ArrayHeader *dst_header = _array_header(dst_array); - ArrayHeader *src_header = _array_header(src_array); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == dst_header->magic, "`dst_array` is not a valid wapp array"); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == src_header->magic, "`src_array` is not a valid wapp array"); - wapp_runtime_assert(item_size == dst_header->item_size && item_size == src_header->item_size, "Invalid item type provided"); + u64 remaining_capacity = dst->capacity - dst->count; - u64 remaining_capacity = dst_header->capacity - dst_header->count; - - u64 copy_count = src_header->count < remaining_capacity ? src_header->count : remaining_capacity; - u8 *dst_ptr = dst_array + dst_header->count * dst_header->item_size; - memcpy((void *)dst_ptr, (const void *)src_array, copy_count * src_header->item_size); - dst_header->count += copy_count; + u64 copy_count = src->count < remaining_capacity ? src->count : remaining_capacity; + void *dst_ptr = _offset_pointer(dst->items, dst->count * dst->item_size); + memcpy(dst_ptr, src->items, copy_count * src->item_size); + dst->count += copy_count; } -void _array_copy_capped(u8 *dst_array, const u8 *src_array, u64 item_size) { - wapp_debug_assert(dst_array != NULL && src_array != NULL, "`dst_array` and `src_array` should not be NULL"); +void _array_copy_capped(Array *dst, const Array *src, u64 item_size) { + wapp_runtime_assert(dst != NULL && src != NULL, "`dst` and `src` should not be NULL"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == dst->magic, "`dst` is not a valid wapp array"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == src->magic, "`src` is not a valid wapp array"); + wapp_runtime_assert(item_size == dst->item_size && item_size == src->item_size, "Invalid item type provided"); - ArrayHeader *dst_header = _array_header(dst_array); - ArrayHeader *src_header = _array_header(src_array); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == dst_header->magic, "`dst_array` is not a valid wapp array"); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == src_header->magic, "`src_array` is not a valid wapp array"); - wapp_runtime_assert(item_size == dst_header->item_size && item_size == src_header->item_size, "Invalid item type provided"); - - _array_clear(dst_array, item_size); - u64 copy_count = src_header->count < dst_header->capacity ? src_header->count : dst_header->capacity; - memcpy((void *)dst_array, (const void *)src_array, copy_count * src_header->item_size); - dst_header->count = copy_count; + _array_clear(dst, item_size); + u64 copy_count = src->count < dst->capacity ? src->count : dst->capacity; + memcpy(dst->items, src->items, copy_count * src->item_size); + dst->count = copy_count; } -u8 *_array_append_alloc(const Allocator *allocator, u8 *array, u8 *value, u64 item_size) { - wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); +Array *_array_append_alloc(const Allocator *allocator, Array *array, void *value, u64 item_size) { + wapp_runtime_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == array->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == array->item_size, "Invalid item type provided"); - ArrayHeader *arr_header = _array_header(array); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); - wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); + Array *output = array; - u8 *output = array; - - if (arr_header->count >= arr_header->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(arr_header->capacity * 2); - output = (u8 *)_array_alloc_capacity(allocator, new_capacity, arr_header->item_size); + if (array->count >= array->capacity) { + u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); + output = (Array *)_array_alloc_capacity(allocator, new_capacity, array->item_size); if (!output) { output = array; goto RETURN_ARRAY_APPEND_ALLOC; @@ -133,101 +83,92 @@ RETURN_ARRAY_APPEND_ALLOC: return output; } -u8 *_array_extend_alloc(const Allocator *allocator, u8 *dst_array, const u8 *src_array, u64 item_size) { - wapp_debug_assert(allocator != NULL && dst_array != NULL && src_array != NULL, "`allocator`, `dst_array` and `src_array` should not be NULL"); +Array *_array_extend_alloc(const Allocator *allocator, Array *dst, const Array *src, u64 item_size) { + wapp_runtime_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == dst->magic, "`dst` is not a valid wapp array"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == src->magic, "`src` is not a valid wapp array"); + wapp_runtime_assert(item_size == dst->item_size && item_size == src->item_size, "Invalid item type provided"); - ArrayHeader *dst_header = _array_header(dst_array); - ArrayHeader *src_header = _array_header(src_array); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == dst_header->magic, "`dst_array` is not a valid wapp array"); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == src_header->magic, "`src_array` is not a valid wapp array"); - wapp_runtime_assert(item_size == dst_header->item_size && item_size == src_header->item_size, "Invalid item type provided"); + Array *output = dst; - u8 *output = dst_array; - - u64 remaining_capacity = dst_header->capacity - dst_header->count; - if (src_header->count >= remaining_capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2); - output = (u8 *)_array_alloc_capacity(allocator, new_capacity, dst_header->item_size); + u64 remaining_capacity = dst->capacity - dst->count; + if (src->count >= remaining_capacity) { + u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); + output = (Array *)_array_alloc_capacity(allocator, new_capacity, dst->item_size); if (!output) { - output = dst_array; + output = dst; goto RETURN_ARRAY_EXTEND_ALLOC; } - _array_copy_capped(output, dst_array, item_size); + _array_copy_capped(output, dst, item_size); } - _array_extend_capped(output, src_array, item_size); + _array_extend_capped(output, src, item_size); RETURN_ARRAY_EXTEND_ALLOC: return output; } -u8 *_array_copy_alloc(const Allocator *allocator, u8 *dst_array, const u8 *src_array, u64 item_size) { - wapp_debug_assert(allocator != NULL && dst_array != NULL && src_array != NULL, "`allocator`, `dst_array` and `src_array` should not be NULL"); +Array *_array_copy_alloc(const Allocator *allocator, Array *dst, const Array *src, u64 item_size) { + wapp_runtime_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == dst->magic, "`dst` is not a valid wapp array"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == src->magic, "`src` is not a valid wapp array"); + wapp_runtime_assert(item_size == dst->item_size && item_size == src->item_size, "Invalid item type provided"); - ArrayHeader *dst_header = _array_header(dst_array); - ArrayHeader *src_header = _array_header(src_array); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == dst_header->magic, "`dst_array` is not a valid wapp array"); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == src_header->magic, "`src_array` is not a valid wapp array"); - wapp_runtime_assert(item_size == dst_header->item_size && item_size == src_header->item_size, "Invalid item type provided"); + Array *output = dst; - u8 *output = dst_array; - - if (src_header->count >= dst_header->capacity) { - u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2); - output = (u8 *)_array_alloc_capacity(allocator, new_capacity, src_header->item_size); + if (src->count >= dst->capacity) { + u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); + output = (Array *)_array_alloc_capacity(allocator, new_capacity, src->item_size); if (!output) { - output = dst_array; + output = dst; goto RETURN_ARRAY_COPY_ALLOC; } } - _array_copy_capped(output, src_array, item_size); + _array_copy_capped(output, src, item_size); RETURN_ARRAY_COPY_ALLOC: return output; } -u8 *_array_pop(u8 *array, u64 item_size) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); +void *_array_pop(Array *array, u64 item_size) { + wapp_runtime_assert(array != NULL, "`array` should not be NULL"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == array->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == array->item_size, "Invalid item type provided"); - ArrayHeader *arr_header = _array_header(array); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); - wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); + if (array->count == 0) { return NULL; } - if (arr_header->count == 0) { return NULL; } - - u64 index = arr_header->count - 1; - u8 *out = _array_get(array, index, item_size); - --(arr_header->count); + u64 index = array->count - 1; + void *out = _array_get(array, index, item_size); + --(array->count); return out; } -void _array_clear(u8 *array, u64 item_size) { - wapp_debug_assert(array != NULL, "`array` should not be NULL"); +void _array_clear(Array *array, u64 item_size) { + wapp_runtime_assert(array != NULL, "`array` should not be NULL"); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == array->magic, "`array` is not a valid wapp array"); + wapp_runtime_assert(item_size == array->item_size, "Invalid item type provided"); - ArrayHeader *arr_header = _array_header(array); - wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); - wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); - - arr_header->count = 0; + array->count = 0; } -u8 *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size) { - wapp_debug_assert(allocator != NULL, "`allocator` should not be NULL"); +Array *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size) { + wapp_runtime_assert(allocator != NULL, "`allocator` should not be NULL"); - u8 *output = NULL; + Array *output = NULL; - u64 allocation_size = sizeof(ArrayHeader) + item_size * capacity; - ArrayHeader *header = wapp_mem_allocator_alloc(allocator, allocation_size); - if (!header) { + u64 allocation_size = sizeof(Array) + item_size * capacity; + + output = wapp_mem_allocator_alloc(allocator, allocation_size); + if (!output) { goto RETURN_ARRAY_ALLOC; } - output = (u8 *)(header + 1); - header->magic = WAPP_ARRAY_MAGIC; - header->count = 0; - header->capacity = capacity; - header->item_size = item_size; + output->magic = WAPP_ARRAY_MAGIC; + output->count = 0; + output->capacity = capacity; + output->item_size = item_size; + output->items = (void *)(output + 1); RETURN_ARRAY_ALLOC: return output; diff --git a/src/primitives/array/array.h b/src/primitives/array/array.h index 4c50ce6..157317e 100644 --- a/src/primitives/array/array.h +++ b/src/primitives/array/array.h @@ -17,7 +17,20 @@ BEGIN_C_LINKAGE #define _calc_array_count(TYPE, ...) wapp_misc_utils_va_args_count(TYPE, __VA_ARGS__) #define _calc_array_capacity(TYPE, ...) wapp_misc_utils_u64_round_up_pow2(_calc_array_count(TYPE, __VA_ARGS__) * 2) -#define wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY) ((TYPE *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(TYPE))) +#define wapp_array_alloc_capacity(ELEM_TYPE, ARRAY_TYPE, ALLOCATOR_PTR, CAPACITY) \ + ((ARRAY_TYPE *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(ELEM_TYPE))) + +#define WAPP_DEF_ARRAY_TYPE(T, NAME) \ + typedef struct { \ + u64 magic; \ + u64 count; \ + u64 capacity; \ + u64 item_size; \ + T *items; \ + } NAME + +#define WAPP_IMPL_ARRAY_TYPE(T, T_STR, NAME) \ + WAPP_DECL_ARRAY_TYPE(T, NAME); #ifdef WAPP_PLATFORM_CPP #define wapp_array(TYPE, ...) ([&]() { \ @@ -28,11 +41,11 @@ BEGIN_C_LINKAGE wapp_persist u8 array[ \ sizeof(ArrayHeader) + _calc_array_capacity(TYPE, __VA_ARGS__) * sizeof(TYPE) \ ] = {0}; \ - ArrayHeader *header = (ArrayHeader *)array; \ - header->magic = WAPP_ARRAY_MAGIC; \ - header->count = _calc_array_count(TYPE, __VA_ARGS__); \ - header->capacity = _calc_array_capacity(TYPE, __VA_ARGS__); \ - header->item_size = sizeof(TYPE); \ + ArrayHeader *header = (ArrayHeader *)array; \ + header->magic = WAPP_ARRAY_MAGIC; \ + header->count = _calc_array_count(TYPE, __VA_ARGS__); \ + header->capacity = _calc_array_capacity(TYPE, __VA_ARGS__); \ + header->item_size = sizeof(TYPE); \ \ u8 *buf = (u8 *)(header + 1); \ memcpy(buf, items, capacity * sizeof(TYPE)); \ @@ -42,11 +55,11 @@ BEGIN_C_LINKAGE wapp_persist u8 array[ \ sizeof(ArrayHeader) + CAPACITY * sizeof(TYPE) \ ] = {0}; \ - ArrayHeader *header = (ArrayHeader *)array; \ - header->magic = WAPP_ARRAY_MAGIC; \ - header->count = 0; \ - header->capacity = CAPACITY; \ - header->item_size = sizeof(TYPE); \ + ArrayHeader *header = (ArrayHeader *)array; \ + header->magic = WAPP_ARRAY_MAGIC; \ + header->count = 0; \ + header->capacity = CAPACITY; \ + header->item_size = sizeof(TYPE); \ \ return (TYPE *)(header + 1); \ }()) @@ -59,88 +72,70 @@ BEGIN_C_LINKAGE return *((TYPE *)_array_pop((u8 *)ARRAY_PTR, sizeof(TYPE))); \ }()) #else -#define _stack_array(TYPE, SIZE) struct { ArrayHeader header; TYPE items[SIZE]; } -#define wapp_array(TYPE, ...) \ - (TYPE *)( \ - (_stack_array(TYPE, _calc_array_capacity(TYPE, __VA_ARGS__))){ \ - .header = { \ - .magic = WAPP_ARRAY_MAGIC, \ - .count = _calc_array_count(TYPE, __VA_ARGS__), \ - .capacity = _calc_array_capacity(TYPE, __VA_ARGS__), \ - .item_size = sizeof(TYPE), \ - }, \ - .items = {__VA_ARGS__}, \ - }.items \ - ) -#define wapp_array_with_capacity(TYPE, CAPACITY) \ - (TYPE *)( \ - (_stack_array(TYPE, CAPACITY)){ \ - .header = { \ - .magic = WAPP_ARRAY_MAGIC, \ - .count = 0, \ - .capacity = CAPACITY, \ - .item_size = sizeof(TYPE), \ - }, \ - .items = {0}, \ - }.items \ - ) -#define wapp_array_pop(TYPE, ARRAY_PTR) \ - (ARRAY_PTR != NULL && _array_count((u8 *)ARRAY_PTR) > 0 ? \ - *((TYPE *)_array_pop((u8 *)ARRAY_PTR, sizeof(TYPE))) : \ - (TYPE){0} \ +#define wapp_array(ELEM_TYPE, ARRAY_TYPE, ...) \ + ((ARRAY_TYPE){ \ + .magic = WAPP_ARRAY_MAGIC, \ + .count = _calc_array_count(ELEM_TYPE, __VA_ARGS__), \ + .capacity = _calc_array_capacity(ELEM_TYPE, __VA_ARGS__), \ + .item_size = sizeof(ELEM_TYPE), \ + .items = (ELEM_TYPE[sizeof(ELEM_TYPE) == sizeof(*((ARRAY_TYPE){0}.items)) ? \ + _calc_array_capacity(ELEM_TYPE, __VA_ARGS__) : \ + -1]){__VA_ARGS__} \ + }) +#define wapp_array_with_capacity(ELEM_TYPE, ARRAY_TYPE, CAPACITY) \ + ((ARRAY_TYPE){ \ + .magic = WAPP_ARRAY_MAGIC, \ + .count = 0, \ + .capacity = CAPACITY, \ + .item_size = sizeof(ELEM_TYPE), \ + .items = (ELEM_TYPE[sizeof(ELEM_TYPE) == sizeof(*((ARRAY_TYPE){0}.items)) ? \ + CAPACITY : -1]){0} \ + }) +#define wapp_array_pop(ELEM_TYPE, ARRAY_PTR) \ + (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *((ELEM_TYPE *)_array_pop((Array *)ARRAY_PTR, sizeof(ELEM_TYPE))) : \ + (ELEM_TYPE){0} \ ) #endif // !WAPP_PLATFORM_CPP -#define wapp_array_count(ARRAY_PTR) \ - _array_count((u8 *)ARRAY_PTR) -#define wapp_array_capacity(ARRAY_PTR) \ - _array_capacity((u8 *)ARRAY_PTR) -#define wapp_array_item_size(ARRAY_PTR) \ - _array_item_size((u8 *)ARRAY_PTR) -#define wapp_array_set_count(ARRAY_PTR, COUNT) \ - _array_set_count((u8 *)ARRAY_PTR, COUNT) -#define wapp_array_get(TYPE, ARRAY_PTR, INDEX) \ - ((TYPE *)_array_get((u8 *)ARRAY_PTR, INDEX, sizeof(TYPE))) -#define wapp_array_set(TYPE, ARRAY_PTR, INDEX, VALUE_PTR) \ - _array_set((u8 *)ARRAY_PTR, INDEX, (u8 *)VALUE_PTR, sizeof(TYPE)) -#define wapp_array_append_capped(TYPE, ARRAY_PTR, VALUE_PTR) \ - _array_append_capped((u8 *)ARRAY_PTR, (u8 *)VALUE_PTR, sizeof(TYPE)) -#define wapp_array_extend_capped(TYPE, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ - _array_extend_capped((u8 *)DST_ARRAY_PTR, (u8 *)SRC_ARRAY_PTR, sizeof(TYPE)) -#define wapp_array_copy_capped(TYPE, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ - _array_copy_capped((u8 *)DST_ARRAY_PTR, (u8 *)SRC_ARRAY_PTR, sizeof(TYPE)) -#define wapp_array_append_alloc(TYPE, ALLOCATOR_PTR, ARRAY_PTR, VALUE_PTR) \ - (TYPE *)_array_append_alloc(ALLOCATOR_PTR, (u8 *)ARRAY_PTR, (u8 *)VALUE_PTR, sizeof(TYPE)) -#define wapp_array_extend_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ - (TYPE *)_array_extend_alloc(ALLOCATOR_PTR, (u8 *)DST_ARRAY_PTR, (u8 *)SRC_ARRAY_PTR, sizeof(TYPE)) -#define wapp_array_copy_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ - (TYPE *)_array_copy_alloc(ALLOCATOR_PTR, (u8 *)DST_ARRAY_PTR, (u8 *)SRC_ARRAY_PTR, sizeof(TYPE)) -#define wapp_array_clear(TYPE, ARRAY_PTR) \ - _array_clear((u8 *)ARRAY_PTR, sizeof(TYPE)) +#define wapp_array_get(ELEM_TYPE, ARRAY_PTR, INDEX) \ + ((ELEM_TYPE *)_array_get((Array *)ARRAY_PTR, INDEX, sizeof(ELEM_TYPE))) +#define wapp_array_set(ELEM_TYPE, ARRAY_PTR, INDEX, VALUE_PTR) \ + _array_set((Array *)ARRAY_PTR, INDEX, (void *)VALUE_PTR, sizeof(ELEM_TYPE)) +#define wapp_array_append_capped(ELEM_TYPE, ARRAY_PTR, VALUE_PTR) \ + _array_append_capped((Array *)ARRAY_PTR, (void *)VALUE_PTR, sizeof(ELEM_TYPE)) +#define wapp_array_extend_capped(ELEM_TYPE, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ + _array_extend_capped((Array *)DST_ARRAY_PTR, (Array *)SRC_ARRAY_PTR, sizeof(ELEM_TYPE)) +#define wapp_array_copy_capped(ELEM_TYPE, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ + _array_copy_capped((Array *)DST_ARRAY_PTR, (Array *)SRC_ARRAY_PTR, sizeof(ELEM_TYPE)) +#define wapp_array_append_alloc(ELEM_TYPE, ARRAY_TYPE, ALLOCATOR_PTR, ARRAY_PTR, VALUE_PTR) \ + (ARRAY_TYPE *)_array_append_alloc(ALLOCATOR_PTR, (Array *)ARRAY_PTR, (void *)VALUE_PTR, sizeof(ELEM_TYPE)) +#define wapp_array_extend_alloc(ELEM_TYPE, ARRAY_TYPE, ALLOCATOR_PTR, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ + (ARRAY_TYPE *)_array_extend_alloc(ALLOCATOR_PTR, (Array *)DST_ARRAY_PTR, (Array *)SRC_ARRAY_PTR, sizeof(ELEM_TYPE)) +#define wapp_array_copy_alloc(ELEM_TYPE, ARRAY_TYPE, ALLOCATOR_PTR, DST_ARRAY_PTR, SRC_ARRAY_PTR) \ + (ARRAY_TYPE *)_array_copy_alloc(ALLOCATOR_PTR, (Array *)DST_ARRAY_PTR, (Array *)SRC_ARRAY_PTR, sizeof(ELEM_TYPE)) +#define wapp_array_clear(ELEM_TYPE, ARRAY_PTR) \ + _array_clear((Array *)ARRAY_PTR, sizeof(ELEM_TYPE)) -typedef struct header ArrayHeader; -struct header { - u64 magic; - u64 count; - u64 capacity; - u64 item_size; -}; +WAPP_DEF_ARRAY_TYPE(void, Array); -u64 _array_count(u8 *array); -u64 _array_capacity(u8 *array); -u64 _array_item_size(u8 *array); -void _array_set_count(u8 *array, u64 count); -u8 *_array_get(u8 *array, u64 index, u64 item_size); -void _array_set(u8 *array, u64 index, u8 *value, u64 item_size); -void _array_append_capped(u8 *array, u8 *value, u64 item_size); -void _array_extend_capped(u8 *dst_array, const u8 *src_array, u64 item_size); -void _array_copy_capped(u8 *dst_array, const u8 *src_array, u64 item_size); -u8 *_array_append_alloc(const Allocator *allocator, u8 *array, u8 *value, u64 item_size); -u8 *_array_extend_alloc(const Allocator *allocator, u8 *dst_array, const u8 *src_array, u64 item_size); -u8 *_array_copy_alloc(const Allocator *allocator, u8 *dst_array, const u8 *src_array, u64 item_size); -u8 *_array_pop(u8 *array, u64 item_size); -void _array_clear(u8 *array, u64 item_size); -u8 *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size); +void *_array_get(Array *array, u64 index, u64 item_size); +void _array_set(Array *array, u64 index, void *value, u64 item_size); +void _array_append_capped(Array *array, void *value, u64 item_size); +void _array_extend_capped(Array *dst, const Array *src, u64 item_size); +void _array_copy_capped(Array *dst, const Array *src, u64 item_size); +Array *_array_append_alloc(const Allocator *allocator, Array *array, void *value, u64 item_size); +Array *_array_extend_alloc(const Allocator *allocator, Array *dst, const Array *src, u64 item_size); +Array *_array_copy_alloc(const Allocator *allocator, Array *dst, const Array *src, u64 item_size); +void *_array_pop(Array *array, u64 item_size); +void _array_clear(Array *array, u64 item_size); +Array *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size); + +typedef struct str8 Str8; + +WAPP_DEF_ARRAY_TYPE(u8, U8Array); +WAPP_DEF_ARRAY_TYPE(i32, I32Array); +WAPP_DEF_ARRAY_TYPE(Str8, Str8Array); #ifdef WAPP_PLATFORM_CPP END_C_LINKAGE diff --git a/src/primitives/strings/str8/str8.c b/src/primitives/strings/str8/str8.c index 6e14884..f041d3c 100644 --- a/src/primitives/strings/str8/str8.c +++ b/src/primitives/strings/str8/str8.c @@ -261,15 +261,15 @@ void wapp_str8_to_upper(Str8 *dst, Str8RO *src) { } } -void wapp_str8_from_bytes(Str8 *dst, const u8 *src_byte_array) { - wapp_debug_assert(src_byte_array != NULL && dst != NULL, "`dst` and `src` should not be NULL"); +void wapp_str8_from_bytes(Str8 *dst, const U8Array *src) { + wapp_debug_assert(src != NULL && dst != NULL, "`dst` and `src` should not be NULL"); - u64 size = wapp_array_count(src_byte_array) * wapp_array_item_size(src_byte_array); + u64 size = src->count * src->item_size; wapp_debug_assert(dst->capacity >= size, "`dst` does not have enough capacity"); dst->size = size; - memcpy(dst->buf, src_byte_array, size); + memcpy(dst->buf, src->items, size); } i64 wapp_str8_find(Str8RO *str, Str8RO substr) { diff --git a/src/primitives/strings/str8/str8.h b/src/primitives/strings/str8/str8.h index fef0288..e13ffc4 100644 --- a/src/primitives/strings/str8/str8.h +++ b/src/primitives/strings/str8/str8.h @@ -99,7 +99,7 @@ void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity); void wapp_str8_format(Str8 *dst, const char *format, ...); void wapp_str8_to_lower(Str8 *dst, Str8RO *src); void wapp_str8_to_upper(Str8 *dst, Str8RO *src); -void wapp_str8_from_bytes(Str8 *dst, const u8 *src_byte_array); +void wapp_str8_from_bytes(Str8 *dst, const U8Array *src); /** * Str8 find functions diff --git a/tests/array/test_i32_array.c b/tests/array/test_i32_array.c index f6a6698..37d1876 100644 --- a/tests/array/test_i32_array.c +++ b/tests/array/test_i32_array.c @@ -4,15 +4,15 @@ TestFuncResult test_i32_array(void) { b8 result; - i32 *array = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7); - result = wapp_array_count(array) == 7 && wapp_array_capacity(array) == 16; + I32Array array = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7); + result = array.count == 7 && array.capacity == 16; i32 *item; - u64 count = wapp_array_count(array); + u64 count = array.count; u64 index = 0; b8 running = true; while (running) { - item = wapp_array_get(i32, array, index); + item = wapp_array_get(i32, &array, index); result = result && item && *item == (i32)(index + 1); ++index; @@ -25,8 +25,8 @@ TestFuncResult test_i32_array(void) { TestFuncResult test_i32_array_with_capacity(void) { b8 result; - i32 *array = wapp_array_with_capacity(i32, 64); - result = wapp_array_count(array) == 0 && wapp_array_capacity(array) == 64; + I32Array array = wapp_array_with_capacity(i32, I32Array, 64); + result = array.count == 0 && array.capacity == 64; return wapp_tester_result(result); } @@ -34,14 +34,14 @@ TestFuncResult test_i32_array_with_capacity(void) { TestFuncResult test_i32_array_get(void) { b8 result = true; - i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); + I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8); i32 *item; - u64 count = wapp_array_count(array); + u64 count = array.count; u64 index = 0; b8 running = true; while (running) { - item = wapp_array_get(i32, array, index); + item = wapp_array_get(i32, &array, index); result = result && item && *item == (i32)index; ++index; @@ -54,16 +54,16 @@ TestFuncResult test_i32_array_get(void) { TestFuncResult test_i32_array_set(void) { b8 result = true; - i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); + I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8); i32 *item; - u64 count = wapp_array_count(array); + u64 count = array.count; u64 index = 0; b8 running = true; while (running) { i32 num = (i32)(index * 2); - wapp_array_set(i32, array, index, &num); - item = wapp_array_get(i32, array, index); + wapp_array_set(i32, &array, index, &num); + item = wapp_array_get(i32, &array, index); result = result && item && *item == (i32)(index * 2); ++index; @@ -76,17 +76,17 @@ TestFuncResult test_i32_array_set(void) { TestFuncResult test_i32_array_append_capped(void) { b8 result; - i32 *array = wapp_array_with_capacity(i32, 64); - wapp_array_append_capped(i32, array, &((i32){10})); + I32Array array = wapp_array_with_capacity(i32, I32Array, 64); + wapp_array_append_capped(i32, &array, &((i32){10})); - result = wapp_array_count(array) == 1; - i32 *item = wapp_array_get(i32, array, 0); + result = array.count == 1; + i32 *item = wapp_array_get(i32, &array, 0); result = result && item && *item == 10; - array = wapp_array(i32, 1); - wapp_array_append_capped(i32, array, &((i32){10})); + array = wapp_array(i32, I32Array, 1); + wapp_array_append_capped(i32, &array, &((i32){10})); - result = result && wapp_array_count(array) == 2; + result = result && array.count == 2; return wapp_tester_result(result); } @@ -94,14 +94,14 @@ TestFuncResult test_i32_array_append_capped(void) { TestFuncResult test_i32_array_extend_capped(void) { b8 result; - i32 *array1 = wapp_array(i32, 1, 2, 3, 4); - i32 *array2 = wapp_array(i32, 10, 20); + I32Array array1 = wapp_array(i32, I32Array, 1, 2, 3, 4); + I32Array array2 = wapp_array(i32, I32Array, 10, 20); - result = wapp_array_count(array1) == 4 && wapp_array_count(array2) == 2; + result = array1.count == 4 && array2.count == 2; - wapp_array_extend_capped(i32, array1, array2); + wapp_array_extend_capped(i32, &array1, &array2); - result = result && wapp_array_count(array1) == 6; + result = result && array1.count == 6; return wapp_tester_result(result); } @@ -109,31 +109,31 @@ TestFuncResult test_i32_array_extend_capped(void) { TestFuncResult test_i32_array_copy_capped(void) { b8 result; - i32 *src = wapp_array(i32, 1, 2, 3, 4, 5); - i32 *dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6); - i32 *dst2 = wapp_array(i32, 1, 2); + I32Array src = wapp_array(i32, I32Array, 1, 2, 3, 4, 5); + I32Array dst1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6); + I32Array dst2 = wapp_array(i32, I32Array, 1, 2); u64 expected_count = 5; - wapp_array_copy_capped(i32, dst1, src); - result = wapp_array_count(dst1) == expected_count; + wapp_array_copy_capped(i32, &dst1, &src); + result = dst1.count == expected_count; u64 index = 0; b8 running = true; while (running) { - result = result && *wapp_array_get(i32, src, index) == *wapp_array_get(i32, dst1, index); + result = result && *wapp_array_get(i32, &src, index) == *wapp_array_get(i32, &dst1, index); ++index; running = index < expected_count; } expected_count = 4; - wapp_array_copy_capped(i32, dst2, src); - result = result && wapp_array_count(dst2) == expected_count; + wapp_array_copy_capped(i32, &dst2, &src); + result = result && dst2.count == expected_count; index = 0; running = true; while (running) { - result = result && *wapp_array_get(i32, src, index) == *wapp_array_get(i32, dst2, index); + result = result && *wapp_array_get(i32, &src, index) == *wapp_array_get(i32, &dst2, index); ++index; running = index < expected_count; @@ -147,9 +147,9 @@ TestFuncResult test_i32_array_alloc_capacity(void) { Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); u64 capacity = 32; - i32 *array = wapp_array_alloc_capacity(i32, &allocator, capacity); + I32Array *array = wapp_array_alloc_capacity(i32, I32Array, &allocator, capacity); - result = array && wapp_array_capacity(array) == capacity; + result = array && array->capacity == capacity; wapp_mem_arena_allocator_destroy(&allocator); @@ -160,23 +160,23 @@ TestFuncResult test_i32_array_append_alloc(void) { b8 result; Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); - i32 *array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8); - i32 *array2 = wapp_array(i32, 1, 2); + I32Array array1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7, 8); + I32Array array2 = wapp_array(i32, I32Array, 1, 2); - i32 *arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &((i32){10})); - result = arr_ptr == array1; + I32Array *arr_ptr = wapp_array_append_alloc(i32, I32Array, &allocator, &array1, &((i32){10})); + result = arr_ptr == &array1; u64 count = 4; u64 index = 0; b8 running = true; while (running) { i32 num = (i32)index; - arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num); + arr_ptr = wapp_array_append_alloc(i32, I32Array, &allocator, &array2, &num); ++index; running = index < count; } - result = result && arr_ptr != array2; + result = result && arr_ptr != &array2; wapp_mem_arena_allocator_destroy(&allocator); @@ -187,15 +187,15 @@ TestFuncResult test_i32_array_extend_alloc(void) { b8 result; Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); - i32 *array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8); - i32 *array2 = wapp_array(i32, 1, 2); - i32 *array3 = wapp_array(i32, 1, 2, 3, 4); + I32Array array1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7, 8); + I32Array array2 = wapp_array(i32, I32Array, 1, 2); + I32Array array3 = wapp_array(i32, I32Array, 1, 2, 3, 4); - i32 *arr_ptr = wapp_array_extend_alloc(i32, &allocator, array1, array3); - result = arr_ptr == array1; + I32Array *arr_ptr = wapp_array_extend_alloc(i32, I32Array, &allocator, &array1, &array3); + result = arr_ptr == &array1; - arr_ptr = wapp_array_extend_alloc(i32, &allocator, array2, array3); - result = result && arr_ptr != array2; + arr_ptr = wapp_array_extend_alloc(i32, I32Array, &allocator, &array2, &array3); + result = result && arr_ptr != &array2; wapp_mem_arena_allocator_destroy(&allocator); @@ -206,32 +206,32 @@ TestFuncResult test_i32_array_copy_alloc(void) { b8 result; Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); - i32 *src = wapp_array(i32, 1, 2, 3, 4, 5); - i32 *dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6); - i32 *dst2 = wapp_array(i32, 1, 2); - i32 *array_ptr = NULL; + I32Array src = wapp_array(i32, I32Array, 1, 2, 3, 4, 5); + I32Array dst1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6); + I32Array dst2 = wapp_array(i32, I32Array, 1, 2); + I32Array *array_ptr = NULL; u64 expected_count = 5; - array_ptr = wapp_array_copy_alloc(i32, &allocator, dst1, src); - result = wapp_array_count(array_ptr) == expected_count && array_ptr == dst1; + array_ptr = wapp_array_copy_alloc(i32, I32Array, &allocator, &dst1, &src); + result = array_ptr->count == expected_count && array_ptr == &dst1; u64 index = 0; b8 running = true; while (running) { - result = result && *wapp_array_get(i32, src, index) == *wapp_array_get(i32, array_ptr, index); + result = result && *wapp_array_get(i32, &src, index) == *wapp_array_get(i32, array_ptr, index); ++index; running = index < expected_count; } expected_count = 5; - array_ptr = wapp_array_copy_alloc(i32, &allocator, dst2, src); - result = result && wapp_array_count(array_ptr) == expected_count && array_ptr != dst2; + array_ptr = wapp_array_copy_alloc(i32, I32Array, &allocator, &dst2, &src); + result = result && array_ptr->count == expected_count && array_ptr != &dst2; index = 0; running = true; while (running) { - result = result && *wapp_array_get(i32, src, index) == *wapp_array_get(i32, array_ptr, index); + result = result && *wapp_array_get(i32, &src, index) == *wapp_array_get(i32, array_ptr, index); ++index; running = index < expected_count; @@ -245,11 +245,11 @@ TestFuncResult test_i32_array_copy_alloc(void) { TestFuncResult test_i32_array_pop(void) { b8 result; - i32 *array1 = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); - i32 *array2 = wapp_array_with_capacity(i32, 32); + I32Array array1 = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8); + I32Array array2 = wapp_array_with_capacity(i32, I32Array, 32); - i32 item1 = wapp_array_pop(i32, array1); - i32 item2 = wapp_array_pop(i32, array2); + i32 item1 = wapp_array_pop(i32, &array1); + i32 item2 = wapp_array_pop(i32, &array2); result = item1 == 8 && item2 == 0; @@ -259,12 +259,12 @@ TestFuncResult test_i32_array_pop(void) { TestFuncResult test_i32_array_clear(void) { b8 result; - i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); - result = wapp_array_count(array) == 9; + I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8); + result = array.count == 9; - wapp_array_clear(i32, array); + wapp_array_clear(i32, &array); - result = result && wapp_array_count(array) == 0; + result = result && array.count == 0; return wapp_tester_result(result); } diff --git a/tests/array/test_str8_array.c b/tests/array/test_str8_array.c index 484691d..b77856b 100644 --- a/tests/array/test_str8_array.c +++ b/tests/array/test_str8_array.c @@ -6,15 +6,15 @@ TestFuncResult test_str8_array(void) { b8 result; Str8 expected[] = {wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")}; - Str8 *array = wapp_array(Str8, wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")); - result = wapp_array_count(array) == 3 && wapp_array_capacity(array) == 8; + Str8Array array = wapp_array(Str8, Str8Array, wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")); + result = array.count == 3 && array.capacity == 8; Str8 *item; - u64 count = wapp_array_count(array); + u64 count = array.count; u64 index = 0; b8 running = true; while (running) { - item = wapp_array_get(Str8, array, index); + item = wapp_array_get(Str8, &array, index); result = result && item && (wapp_str8_equal(item, &expected[index])); ++index; diff --git a/tests/str8/test_str8.c b/tests/str8/test_str8.c index 9694842..f55a94f 100644 --- a/tests/str8/test_str8.c +++ b/tests/str8/test_str8.c @@ -615,11 +615,11 @@ TestFuncResult test_str8_join(void) { TestFuncResult test_str8_from_bytes(void) { b8 result; - Str8 str = wapp_str8_buf(1024); - u8 *bytes = wapp_array(u8, 'W', 'A', 'P', 'P'); - wapp_str8_from_bytes(&str, bytes); + Str8 str = wapp_str8_buf(1024); + U8Array bytes = wapp_array(u8, U8Array, 'W', 'A', 'P', 'P'); + wapp_str8_from_bytes(&str, &bytes); - result = str.size == wapp_array_count(bytes) * wapp_array_item_size(bytes); + result = str.size == bytes.count * bytes.item_size; result = result && wapp_str8_equal(&str, &wapp_str8_lit_ro("WAPP")); return wapp_tester_result(result);