From 12e8515b27e02197ad33d724e54279be0db64803 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 7 Sep 2025 04:37:31 +0100 Subject: [PATCH] Update primitives --- codegen/array/make_array.py | 6 +--- codegen/dbl_list/make_dbl_list.py | 12 ------- src/primitives/array/array.c | 1 - src/primitives/array/array.h | 1 - src/primitives/dbl_list/dbl_list.c | 2 -- src/primitives/dbl_list/dbl_list.h | 1 - src/primitives/strings/str8/str8.c | 51 ++++++++++++++++++++++++++++-- src/primitives/strings/str8/str8.h | 2 ++ 8 files changed, 52 insertions(+), 24 deletions(-) diff --git a/codegen/array/make_array.py b/codegen/array/make_array.py index eb0a66b..92b8977 100644 --- a/codegen/array/make_array.py +++ b/codegen/array/make_array.py @@ -54,10 +54,6 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}): out_dir.mkdir(parents=True, exist_ok=True) common_includes: List[CInclude] = [ - CInclude( - header=str(convert_to_relative(WAPP_SRC_ROOT / "common" / "aliases" / "aliases.h", out_dir)).replace("\\", "/"), - local=True, - ), CInclude( header=str(convert_to_relative(WAPP_SRC_ROOT / "primitives" / "mem_allocator" / "mem_allocator.h", out_dir)).replace("\\", "/"), local=True, @@ -108,11 +104,11 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}): decl_types=[*common_decl_types], includes=[ CInclude(header, local=True, same_dir=True), - CInclude(header="stddef.h"), CInclude( header=str(convert_to_relative(WAPP_SRC_ROOT / "common" / "assert" / "assert.h", out_dir)).replace("\\", "/"), local=True ), + CInclude(header="stddef.h"), ], internal_funcs=[], funcs=header.funcs diff --git a/codegen/dbl_list/make_dbl_list.py b/codegen/dbl_list/make_dbl_list.py index 4bcc167..bf387bb 100644 --- a/codegen/dbl_list/make_dbl_list.py +++ b/codegen/dbl_list/make_dbl_list.py @@ -56,13 +56,6 @@ def make_dbl_list(user_datatypes: Dict[CDataType, DblListData] = {}): out_dir = WAPP_SRC_ROOT / "primitives" / "dbl_list" out_dir.mkdir(parents=True, exist_ok=True) - common_includes: List[CInclude] = [ - CInclude( - header=str(convert_to_relative(WAPP_SRC_ROOT / "common" / "aliases" / "aliases.h", out_dir)).replace("\\", "/"), - local=True, - ) - ] - common_decl_types: List[CStruct] = [] datatypes: dict[CDataType, DblListData] = { @@ -111,16 +104,11 @@ def make_dbl_list(user_datatypes: Dict[CDataType, DblListData] = {}): local=True ), CInclude(header="stddef.h"), - CInclude(header="assert.h"), ], internal_funcs=[], funcs=header.funcs ) - if len(common_includes) > 0: - header.includes.extend(common_includes) - source.includes.extend(common_includes) - for _type, dbl_list_data in datatypes.items(): type_string = get_datatype_string(_type) clean_type_string = type_string.replace(" ", "").replace("*", "_ptr") diff --git a/src/primitives/array/array.c b/src/primitives/array/array.c index 207e8f1..77e4626 100644 --- a/src/primitives/array/array.c +++ b/src/primitives/array/array.c @@ -4,7 +4,6 @@ #include "./array.h" #include "../../common/assert/assert.h" -#include "../../common/aliases/aliases.h" #include "../mem_allocator/mem_allocator.h" #include "../../common/misc/misc_utils.h" #include "../../common/aliases/aliases.h" diff --git a/src/primitives/array/array.h b/src/primitives/array/array.h index a786faa..4a30fd6 100644 --- a/src/primitives/array/array.h +++ b/src/primitives/array/array.h @@ -5,7 +5,6 @@ #ifndef ARRAY_H #define ARRAY_H -#include "../../common/aliases/aliases.h" #include "../mem_allocator/mem_allocator.h" #include "../../common/misc/misc_utils.h" #include "../../common/aliases/aliases.h" diff --git a/src/primitives/dbl_list/dbl_list.c b/src/primitives/dbl_list/dbl_list.c index 9578b7f..b0fab6f 100644 --- a/src/primitives/dbl_list/dbl_list.c +++ b/src/primitives/dbl_list/dbl_list.c @@ -5,10 +5,8 @@ #include "./dbl_list.h" #include "../../common/assert/assert.h" #include "../../common/aliases/aliases.h" -#include "../../common/aliases/aliases.h" #include "../../common/platform/platform.h" #include -#include internal Str8List str8_node_to_list(Str8Node *node); internal VoidPList void_ptr_node_to_list(VoidPNode *node); diff --git a/src/primitives/dbl_list/dbl_list.h b/src/primitives/dbl_list/dbl_list.h index 80f3eae..e6a7c98 100644 --- a/src/primitives/dbl_list/dbl_list.h +++ b/src/primitives/dbl_list/dbl_list.h @@ -5,7 +5,6 @@ #ifndef DBL_LIST_H #define DBL_LIST_H -#include "../../common/aliases/aliases.h" #include "../../common/aliases/aliases.h" #include "../../common/platform/platform.h" diff --git a/src/primitives/strings/str8/str8.c b/src/primitives/strings/str8/str8.c index 7a507db..11c4f3d 100644 --- a/src/primitives/strings/str8/str8.c +++ b/src/primitives/strings/str8/str8.c @@ -163,7 +163,7 @@ Str8 *wapp_str8_alloc_concat(const Allocator *allocator, Str8 *dst, Str8RO *src) u64 remaining = dst->capacity - dst->size; if (src->size <= remaining) { output = dst; - goto COPY_STRING_STR8_CONCAT; + goto SOURCE_STRING_STR8_CONCAT; } u64 capacity = dst->capacity + src->size; @@ -175,13 +175,60 @@ Str8 *wapp_str8_alloc_concat(const Allocator *allocator, Str8 *dst, Str8RO *src) wapp_str8_concat_capped(output, dst); -COPY_STRING_STR8_CONCAT: +SOURCE_STRING_STR8_CONCAT: wapp_str8_concat_capped(output, src); RETURN_STR8_CONCAT: return output; } +Str8 *wapp_str8_alloc_copy_cstr(const Allocator *allocator, Str8 *dst, const char *src) { + wapp_debug_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL"); + + Str8 *output = NULL; + u64 size = strlen(src); + if (size <= dst->capacity) { + output = dst; + goto SOURCE_CSTRING_STR8_COPY; + } + + u64 capacity = dst->capacity + size; + + output = wapp_str8_alloc_buf(allocator, capacity); + if (!output) { + goto RETURN_CSTRING_STR8_COPY; + } + +SOURCE_CSTRING_STR8_COPY: + wapp_str8_copy_cstr_capped(output, src); + +RETURN_CSTRING_STR8_COPY: + return output; +} + +Str8 *wapp_str8_alloc_copy_str8(const Allocator *allocator, Str8 *dst, Str8RO *src) { + wapp_debug_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL"); + + Str8 *output = NULL; + if (src->size <= dst->capacity) { + output = dst; + goto SOURCE_STRING_STR8_COPY; + } + + u64 capacity = dst->capacity + src->size; + + output = wapp_str8_alloc_buf(allocator, capacity); + if (!output) { + goto RETURN_STRING_STR8_COPY; + } + +SOURCE_STRING_STR8_COPY: + wapp_str8_copy_str8_capped(output, src); + +RETURN_STRING_STR8_COPY: + return output; +} + void wapp_str8_concat_capped(Str8 *dst, Str8RO *src) { wapp_debug_assert(dst != NULL && src != NULL, "`dst` and `src` should not be NULL"); diff --git a/src/primitives/strings/str8/str8.h b/src/primitives/strings/str8/str8.h index b74ffc8..a134f6a 100644 --- a/src/primitives/strings/str8/str8.h +++ b/src/primitives/strings/str8/str8.h @@ -79,6 +79,8 @@ Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str); Str8 *wapp_str8_alloc_str8(const Allocator *allocator, Str8RO *str); Str8 *wapp_str8_alloc_substr(const Allocator *allocator, Str8RO *str, u64 start, u64 end); Str8 *wapp_str8_alloc_concat(const Allocator *allocator, Str8 *dst, Str8RO *src); +Str8 *wapp_str8_alloc_copy_cstr(const Allocator *allocator, Str8 *dst, const char *src); +Str8 *wapp_str8_alloc_copy_str8(const Allocator *allocator, Str8 *dst, Str8RO *src); // Only needed for allocators like malloc where each allocation has to be freed on its own. // No need to use it for allocators like Arena. void wapp_str8_dealloc_buf(const Allocator *allocator, Str8 **str);