From 6fb078a86801cb02c360364e35b8b7b33d302fef Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Wed, 16 Apr 2025 00:07:11 +0100 Subject: [PATCH] Remove codegen and implement dbl_list as generic container --- Makefile | 10 +- codegen/__init__.py | 0 codegen/__main__.py | 9 - codegen/constants.py | 5 - codegen/datatypes.py | 319 ------------------- codegen/dbl_list/__init__.py | 0 codegen/dbl_list/gen_dbl_list.py | 206 ------------ codegen/dbl_list/snippets/list_empty | 8 - codegen/dbl_list/snippets/list_get | 13 - codegen/dbl_list/snippets/list_insert | 29 -- codegen/dbl_list/snippets/list_pop_back | 21 -- codegen/dbl_list/snippets/list_pop_front | 21 -- codegen/dbl_list/snippets/list_push_back | 21 -- codegen/dbl_list/snippets/list_push_front | 21 -- codegen/dbl_list/snippets/list_remove | 28 -- codegen/dbl_list/snippets/node_to_list | 15 - codegen/utils.py | 6 - src/containers/dbl_list/dbl_list.c | 209 ++++++++++++ src/containers/dbl_list/dbl_list.h | 72 +++++ src/containers/wapp_containers.c | 6 + src/containers/wapp_containers.h | 7 + src/core/os/cpath/cpath.c | 23 +- src/core/os/cpath/cpath.h | 2 +- src/core/os/shell/commander/commander.c | 2 +- src/core/os/shell/commander/commander.h | 2 +- src/core/strings/str8/str8.c | 58 ++-- src/core/strings/str8/str8.h | 11 +- src/core/strings/str8/str8_list.c | 230 +------------ src/core/strings/str8/str8_list.h | 25 +- src/wapp.c | 1 + src/wapp.h | 1 + tests/cpath/test_cpath.c | 34 +- tests/shell_commander/test_shell_commander.c | 22 +- tests/str8/test_str8.c | 52 +-- tests/str8/test_str8_list.c | 262 +++++++-------- 35 files changed, 552 insertions(+), 1199 deletions(-) delete mode 100644 codegen/__init__.py delete mode 100644 codegen/__main__.py delete mode 100644 codegen/constants.py delete mode 100644 codegen/datatypes.py delete mode 100644 codegen/dbl_list/__init__.py delete mode 100644 codegen/dbl_list/gen_dbl_list.py delete mode 100644 codegen/dbl_list/snippets/list_empty delete mode 100644 codegen/dbl_list/snippets/list_get delete mode 100644 codegen/dbl_list/snippets/list_insert delete mode 100644 codegen/dbl_list/snippets/list_pop_back delete mode 100644 codegen/dbl_list/snippets/list_pop_front delete mode 100644 codegen/dbl_list/snippets/list_push_back delete mode 100644 codegen/dbl_list/snippets/list_push_front delete mode 100644 codegen/dbl_list/snippets/list_remove delete mode 100644 codegen/dbl_list/snippets/node_to_list delete mode 100644 codegen/utils.py create mode 100644 src/containers/dbl_list/dbl_list.c create mode 100644 src/containers/dbl_list/dbl_list.h create mode 100644 src/containers/wapp_containers.c create mode 100644 src/containers/wapp_containers.h diff --git a/Makefile b/Makefile index 93c1ea1..9c4d7cb 100644 --- a/Makefile +++ b/Makefile @@ -24,9 +24,9 @@ ifeq ($(CC),gcc) export ASAN_OPTIONS=verify_asan_link_order=0 endif -.PHONY: all clean builddir codegen build-test run-test build-lib full prng testing uuid core +.PHONY: all clean builddir build-test run-test build-lib full prng testing uuid core containers -all: clean builddir codegen run-test full +all: clean builddir run-test full clean: @rm -rf $(BUILD_DIR) @@ -34,9 +34,6 @@ clean: builddir: @mkdir -p $(BUILD_DIR) -codegen: - python3 -m codegen - build-test: $(CC) $(CFLAGS) $(TEST_INCLUDE) $(TEST_SRC) -o $(TEST_OUT) @@ -61,3 +58,6 @@ uuid: build-lib core: LIB_SRC = src/core/wapp_core.c core: build-lib + +containers: LIB_SRC = src/core/wapp_containers.c +containers: build-lib diff --git a/codegen/__init__.py b/codegen/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/codegen/__main__.py b/codegen/__main__.py deleted file mode 100644 index 9135022..0000000 --- a/codegen/__main__.py +++ /dev/null @@ -1,9 +0,0 @@ -from codegen.dbl_list.gen_dbl_list import gen_dbl_list - - -def main(): - gen_dbl_list() - - -if __name__ == "__main__": - main() diff --git a/codegen/constants.py b/codegen/constants.py deleted file mode 100644 index d092b2c..0000000 --- a/codegen/constants.py +++ /dev/null @@ -1,5 +0,0 @@ -from pathlib import Path - - -PACKAGE_DIR = Path(__file__).parent -WAPP_SRC_ROOT = PACKAGE_DIR.parent / "src" diff --git a/codegen/datatypes.py b/codegen/datatypes.py deleted file mode 100644 index fab1b9d..0000000 --- a/codegen/datatypes.py +++ /dev/null @@ -1,319 +0,0 @@ -from enum import Enum -from pathlib import Path -from typing import Optional, Union -from dataclasses import dataclass, field - - -class CType(Enum): - VOID = "void" - BOOL = "bool" - CHAR = "char" - C8 = "c8" - C16 = "c16" - C32 = "c32" - I8 = "i8" - I16 = "i16" - I32 = "i32" - I64 = "i64" - U8 = "u8" - U16 = "u16" - U32 = "u32" - U64 = "u64" - F32 = "f32" - F64 = "f64" - F128 = "f128" - IPTR = "iptr" - UPTR = "uptr" - - def __str__(self) -> str: - return self.value - - -class CQualifier(Enum): - NONE = "" - CONST = "const " - EXTERNAL = "external " - INTERNAL = "internal " - PERSISTENT = "persistent " - - def __str__(self) -> str: - return self.value - - -class CPointerType(Enum): - NONE = "" - SINGLE = "*" - DOUBLE = "**" - - def __str__(self) -> str: - return self.value - - -@dataclass -class CPointer: - _type: CPointerType = CPointerType.NONE - qualifier: CQualifier = CQualifier.NONE - - def __str__(self) -> str: - return str(self._type) + str(self.qualifier) - - -@dataclass -class CEnumVal: - name: str - value: Optional[int] = None - - def __str__(self) -> str: - return self.name + "" if self.value is None else f" = {self.value}" - - -@dataclass -class CEnum: - name: str - values: list[CEnumVal] - typedef: bool = False - - def __str__(self) -> str: - if self.typedef: - header = "typedef enum {\n" - footer = f"}} {self.name};\n" - else: - header = f"enum {self.name} {{\n" - footer = "};\n" - - values = "" - for value in self.values: - values += f" {str(value)},\n" - - return header + values + footer - - -@dataclass -class CStruct: - name: str - cargs: list["CArg"] - typedef_name: str | None = None - - def __str__(self) -> str: - return self.declare() + self.define() - - def declare(self) -> str: - declaration = f"typedef struct {self.name} {self.typedef_name if self.typedef_name is not None else self.name};\n" - return declaration - - def define(self): - definition = f"struct {self.name} {{\n" - args = "" - for arg in self.cargs: - args += f" {str(arg)};\n" - footer = "};\n" - - return definition + args + footer; - - -CUserType = Union[CStruct, CEnum] -CDataType = Union[CType, CUserType, str] - - -@dataclass -class CArg: - name: str - _type: CDataType - array: bool = False - pointer: CPointer = field(default_factory=CPointer) - qualifier: CQualifier = CQualifier.NONE - - def __str__(self) -> str: - qualifier = str(self.qualifier) - _type = get_datatype_string(self._type) + " " - pointer = str(self.pointer) - array = "[]" if self.array else "" - - return qualifier + _type + pointer + self.name + array - - -@dataclass -class CFunc: - name: str - ret_type: CDataType - args: list[CArg] - body: str - pointer: CPointer = field(default_factory=CPointer) - qualifiers: list[CQualifier] = field(default_factory=list) - - def __str__(self) -> str: - qualifiers = "" - for qualifier in self.qualifiers: - if qualifier == CQualifier.NONE: - continue - if len(qualifiers) > 0: - qualifiers += " " - qualifiers += f"{str(qualifier)}" - - args = "" - for i, arg in enumerate(self.args): - args += f"{str(arg)}" - if i + 1 < len(self.args): - args += ", " - - return qualifiers + get_datatype_string(self.ret_type) + " " + str(self.pointer) + self.name + f"({args})" - - def declare(self) -> str: - return f"{str(self)};\n" - - def define(self) -> str: - return f"{str(self)} {{\n{self.body}\n}}\n\n" - - -@dataclass -class CInclude: - header: Union[str, "CHeader"] - local: bool = False - same_dir: bool = False - - def __str__(self) -> str: - if isinstance(self.header, CHeader): - name = f"{self.header.name}.{self.header.extension}" - else: - name = self.header - - if self.local: - open_symbol = '"' - close_symbol = '"' - - if self.same_dir: - name = f"./{name}" - else: - open_symbol = '<' - close_symbol = '>' - - return f"#include {open_symbol}{name}{close_symbol}\n" - - -@dataclass -class CFile: - name: str - extension: str - decl_types: list[CStruct] = field(default_factory=list) - - def save(self, output_dir: Path): - output_file = output_dir / f"{self.name}.{self.extension}" - with open(output_file, "w+") as outfile: - outfile.write(str(self)) - - def __str__(self) -> str: - return """\ -/** - * THIS FILE IS AUTOMATICALLY GENERATED. ANY MODIFICATIONS TO IT WILL BE OVERWRITTEN - */ - -""" - - -@dataclass -class CHeader(CFile): - extension: str = "h" - includes: list[CInclude] = field(default_factory=list) - types: list[CUserType] = field(default_factory=list) - funcs: list[CFunc] = field(default_factory=list) - - def __str__(self) -> str: - name_upper = self.name.upper() - header_guard_name = f"{name_upper}_H" - header_guard_open = f"#ifndef {header_guard_name}\n#define {header_guard_name}\n\n" - header_guard_close = f"#endif // !{header_guard_name}\n" - - c_linkage_open = "#ifdef __cplusplus\nBEGIN_C_LINKAGE\n#endif // !__cplusplus\n\n" - c_linkage_close = "\n#ifdef __cplusplus\nEND_C_LINKAGE\n#endif // !__cplusplus\n\n" - - includes = _get_includes_string(self.includes) - - forward_declarations = "" - for _type in self.decl_types: - forward_declarations += _type.declare() - if len(forward_declarations) > 0: - forward_declarations += "\n" - - types = "" - for _type in self.types: - types += str(_type) + "\n" - - funcs = "" - for func in self.funcs: - funcs += func.declare() - - return ( - super().__str__() + - header_guard_open + - includes + - c_linkage_open + - forward_declarations + - types + - funcs + - c_linkage_close + - header_guard_close - ) - - -@dataclass -class CSource(CFile): - extension: str = "c" - includes: list[CInclude] = field(default_factory=list) - types: list[CUserType] = field(default_factory=list) - internal_funcs: list[CFunc] = field(default_factory=list) - funcs: list[CFunc] = field(default_factory=list) - - def __str__(self) -> str: - includes = _get_includes_string(self.includes) - - forward_declarations = "" - for _type in self.decl_types: - forward_declarations += _type.declare() - if len(forward_declarations) > 0: - forward_declarations += "\n" - - types = "" - for _type in self.types: - types += str(_type) + "\n" - - internal_funcs_decl = "" - internal_funcs_def = "" - for func in self.internal_funcs: - internal_funcs_decl += func.declare() - internal_funcs_def += func.define() - - if len(internal_funcs_decl) > 0: - internal_funcs_decl += "\n" - - funcs = "" - for func in self.funcs: - funcs += func.define() - - return ( - super().__str__() + - includes + - forward_declarations + - types + - internal_funcs_decl + - funcs + - internal_funcs_def - ) - - -def get_datatype_string(_type: CDataType) -> str: - if isinstance(_type, CType): - return str(_type) - elif isinstance(_type, CStruct) or isinstance(_type, CEnum): - return _type.name - elif isinstance(_type, str): - return _type - - -def _get_includes_string(includes: list[CInclude]) -> str: - output = "" - for include in sorted(includes, key=lambda inc: inc.local, reverse=True): - output += str(include) - if len(output) > 0: - output += "\n" - - return output diff --git a/codegen/dbl_list/__init__.py b/codegen/dbl_list/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/codegen/dbl_list/gen_dbl_list.py b/codegen/dbl_list/gen_dbl_list.py deleted file mode 100644 index be35489..0000000 --- a/codegen/dbl_list/gen_dbl_list.py +++ /dev/null @@ -1,206 +0,0 @@ -from pathlib import Path -from dataclasses import dataclass, field -from codegen.constants import WAPP_SRC_ROOT -from codegen.utils import load_func_body_from_file -from codegen.datatypes import ( - CDataType, - CStruct, - CFunc, - CHeader, - CSource, - CArg, - CType, - CPointer, - CPointerType, - CQualifier, - CInclude, - get_datatype_string, -) - - -@dataclass -class DblListData: - out_dir: Path - common_includes: list[CInclude] = field(default_factory=list) - hdr_includes: list[CInclude] = field(default_factory=list) - src_includes: list[CInclude] = field(default_factory=list) - common_decl_types: list[CStruct] = field(default_factory=list) - hdr_decl_types: list[CStruct] = field(default_factory=list) - src_decl_types: list[CStruct] = field(default_factory=list) - - -def gen_dbl_list(): - def __format_func_body(filename: Path, type_string: str): - return load_func_body_from_file(filename).format( - T=type_string, - Tupper=type_string.upper(), - Tlower=type_string.lower(), - ) - - datatypes: dict[CDataType, DblListData] = { - "Str8": DblListData( - common_includes=[ - CInclude(header="../../../common/aliases/aliases.h", local=True), - ], - src_includes=[ - CInclude(header="./str8.h", local=True), - ], - hdr_decl_types=[ - CStruct(name="str8", cargs=[], typedef_name="Str8"), - ], - out_dir=WAPP_SRC_ROOT / "core/strings/str8/", - ), - } - snippets_dir = Path(__file__).parent / "snippets" - - for _type, dbl_list_data in datatypes.items(): - type_string = get_datatype_string(_type) - - node = CStruct( - name=f"{type_string}Node", - cargs=[ - CArg(name="string", _type=type_string, pointer=CPointer(_type=CPointerType.SINGLE)), - CArg(name="prev", _type=f"{type_string}Node", pointer=CPointer(_type=CPointerType.SINGLE)), - CArg(name="next", _type=f"{type_string}Node", pointer=CPointer(_type=CPointerType.SINGLE)), - ], - ) - - dl_list = CStruct( - name=f"{type_string}List", - cargs=[ - CArg(name="first", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)), - CArg(name="last", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)), - CArg(name="total_size", _type=CType.U64), - CArg(name="node_count", _type=CType.U64), - ], - ) - - get_func = CFunc( - name=f"wapp_{type_string.lower()}_list_get", - ret_type=node, - args=[ - CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE), qualifier=CQualifier.CONST), - CArg(name="index", _type=CType.U64), - ], - body=__format_func_body(snippets_dir / "list_get", type_string), - pointer=CPointer(CPointerType.SINGLE), - ) - - push_front_func = CFunc( - name=f"wapp_{type_string.lower()}_list_push_front", - ret_type=CType.VOID, - args=[ - CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)), - CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)), - ], - body=__format_func_body(snippets_dir / "list_push_front", type_string), - ) - - push_back_func = CFunc( - name=f"wapp_{type_string.lower()}_list_push_back", - ret_type=CType.VOID, - args=[ - CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)), - CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)), - ], - body=__format_func_body(snippets_dir / "list_push_back", type_string), - ) - - insert_func = CFunc( - name=f"wapp_{type_string.lower()}_list_insert", - ret_type=CType.VOID, - args=[ - CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)), - CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)), - CArg(name="index", _type=CType.U64), - ], - body=__format_func_body(snippets_dir / "list_insert", type_string), - ) - - pop_front_func = CFunc( - name=f"wapp_{type_string.lower()}_list_pop_front", - ret_type=node, - args=[ - CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)), - ], - body=__format_func_body(snippets_dir / "list_pop_front", type_string), - pointer=CPointer(CPointerType.SINGLE), - ) - - pop_back_func = CFunc( - name=f"wapp_{type_string.lower()}_list_pop_back", - ret_type=node, - args=[ - CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)), - ], - body=__format_func_body(snippets_dir / "list_pop_back", type_string), - pointer=CPointer(CPointerType.SINGLE), - ) - - remove_func = CFunc( - name=f"wapp_{type_string.lower()}_list_remove", - ret_type=node, - args=[ - CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)), - CArg(name="index", _type=CType.U64), - ], - body=__format_func_body(snippets_dir / "list_remove", type_string), - pointer=CPointer(CPointerType.SINGLE), - ) - - empty_func = CFunc( - name=f"wapp_{type_string.lower()}_list_empty", - ret_type=CType.VOID, - args=[ - CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)), - ], - body=__format_func_body(snippets_dir / "list_empty", type_string), - ) - - node_to_list_func = CFunc( - name=f"{type_string.lower()}_node_to_list", - ret_type=dl_list, - args=[ - CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)), - ], - body=__format_func_body(snippets_dir / "node_to_list", type_string), - qualifiers=[CQualifier.INTERNAL], - ) - - header = CHeader( - name=f"{type_string.lower()}_list", - decl_types=dbl_list_data.common_decl_types + dbl_list_data.hdr_decl_types, - includes=[], - types=[node, dl_list], - funcs=[ - get_func, - push_front_func, - push_back_func, - insert_func, - pop_front_func, - pop_back_func, - remove_func, - empty_func, - ] - ) - - source = CSource( - name=header.name, - decl_types=dbl_list_data.common_decl_types + dbl_list_data.src_decl_types, - includes=[CInclude(header, local=True, same_dir=True), CInclude(header="stddef.h")], - internal_funcs=[node_to_list_func], - funcs=header.funcs - ) - - if len(dbl_list_data.common_includes) > 0: - header.includes.extend(dbl_list_data.common_includes) - source.includes.extend(dbl_list_data.common_includes) - - if len(dbl_list_data.hdr_includes) > 0: - header.includes.extend(dbl_list_data.hdr_includes) - - if len(dbl_list_data.src_includes) > 0: - source.includes.extend(dbl_list_data.src_includes) - - header.save(dbl_list_data.out_dir) - source.save(dbl_list_data.out_dir) diff --git a/codegen/dbl_list/snippets/list_empty b/codegen/dbl_list/snippets/list_empty deleted file mode 100644 index c14804f..0000000 --- a/codegen/dbl_list/snippets/list_empty +++ /dev/null @@ -1,8 +0,0 @@ - if (!list) {{ - return; - }} - - u64 count = list->node_count; - for (u64 i = 0; i < count; ++i) {{ - wapp_str8_list_pop_back(list); - }} diff --git a/codegen/dbl_list/snippets/list_get b/codegen/dbl_list/snippets/list_get deleted file mode 100644 index c65985e..0000000 --- a/codegen/dbl_list/snippets/list_get +++ /dev/null @@ -1,13 +0,0 @@ - if (index >= list->node_count) {{ - return NULL; - }} - - {T}Node *output = NULL; - {T}Node *current = list->first; - for (u64 i = 1; i <= index; ++i) {{ - current = current->next; - }} - - output = current; - - return output; diff --git a/codegen/dbl_list/snippets/list_insert b/codegen/dbl_list/snippets/list_insert deleted file mode 100644 index 0b6aa52..0000000 --- a/codegen/dbl_list/snippets/list_insert +++ /dev/null @@ -1,29 +0,0 @@ - if (!list || !node || !(node->string)) {{ - return; - }} - - if (index == 0) {{ - wapp_str8_list_push_front(list, node); - return; - }} else if (index == list->node_count) {{ - wapp_str8_list_push_back(list, node); - return; - }} - - {T}Node *dst_node = wapp_str8_list_get(list, index); - if (!dst_node) {{ - return; - }} - - {T}List node_list = {Tlower}_node_to_list(node); - - list->total_size += node_list.total_size; - list->node_count += node_list.node_count; - - {T}Node *prev = dst_node->prev; - - dst_node->prev = node_list.last; - prev->next = node_list.first; - - node_list.first->prev = prev; - node_list.last->next = dst_node; diff --git a/codegen/dbl_list/snippets/list_pop_back b/codegen/dbl_list/snippets/list_pop_back deleted file mode 100644 index 3ee8a15..0000000 --- a/codegen/dbl_list/snippets/list_pop_back +++ /dev/null @@ -1,21 +0,0 @@ - {T}Node *output = NULL; - - if (!list || list->node_count == 0) {{ - goto RETURN_{Tupper}_LIST_POP_BACK; - }} - - output = list->last; - - if (list->node_count == 1) {{ - *list = ({T}List){{0}}; - goto RETURN_{Tupper}_LIST_POP_BACK; - }} - - --(list->node_count); - list->total_size -= output->string->size; - list->last = output->prev; - - output->prev = output->next = NULL; - -RETURN_{Tupper}_LIST_POP_BACK: - return output; diff --git a/codegen/dbl_list/snippets/list_pop_front b/codegen/dbl_list/snippets/list_pop_front deleted file mode 100644 index caba8c9..0000000 --- a/codegen/dbl_list/snippets/list_pop_front +++ /dev/null @@ -1,21 +0,0 @@ - {T}Node *output = NULL; - - if (!list || list->node_count == 0) {{ - goto RETURN_{Tupper}_LIST_POP_FRONT; - }} - - output = list->first; - - if (list->node_count == 1) {{ - *list = ({T}List){{0}}; - goto RETURN_{Tupper}_LIST_POP_FRONT; - }} - - --(list->node_count); - list->total_size -= output->string->size; - list->first = output->next; - - output->prev = output->next = NULL; - -RETURN_{Tupper}_LIST_POP_FRONT: - return output; diff --git a/codegen/dbl_list/snippets/list_push_back b/codegen/dbl_list/snippets/list_push_back deleted file mode 100644 index cc51dac..0000000 --- a/codegen/dbl_list/snippets/list_push_back +++ /dev/null @@ -1,21 +0,0 @@ - if (!list || !node || !(node->string)) {{ - return; - }} - - {T}List node_list = {Tlower}_node_to_list(node); - - if (list->node_count == 0) {{ - *list = node_list; - return; - }} - - list->total_size += node_list.total_size; - list->node_count += node_list.node_count; - - {T}Node *last = list->last; - if (last) {{ - last->next = node_list.first; - }} - - list->last = node_list.last; - node_list.first->prev = last; diff --git a/codegen/dbl_list/snippets/list_push_front b/codegen/dbl_list/snippets/list_push_front deleted file mode 100644 index 8989beb..0000000 --- a/codegen/dbl_list/snippets/list_push_front +++ /dev/null @@ -1,21 +0,0 @@ - if (!list || !node || !(node->string)) {{ - return; - }} - - {T}List node_list = {Tlower}_node_to_list(node); - - if (list->node_count == 0) {{ - *list = node_list; - return; - }} - - list->total_size += node_list.total_size; - list->node_count += node_list.node_count; - - {T}Node *first = list->first; - if (first) {{ - first->prev = node_list.last; - }} - - list->first = node_list.first; - node_list.last->next = first; diff --git a/codegen/dbl_list/snippets/list_remove b/codegen/dbl_list/snippets/list_remove deleted file mode 100644 index cc213c4..0000000 --- a/codegen/dbl_list/snippets/list_remove +++ /dev/null @@ -1,28 +0,0 @@ - {T}Node *output = NULL; - if (!list) {{ - goto RETURN_{Tupper}_LIST_REMOVE; - }} - - if (index == 0) {{ - output = wapp_str8_list_pop_front(list); - goto RETURN_{Tupper}_LIST_REMOVE; - }} else if (index == list->node_count) {{ - output = wapp_str8_list_pop_back(list); - goto RETURN_{Tupper}_LIST_REMOVE; - }} - - output = wapp_str8_list_get(list, index); - if (!output) {{ - goto RETURN_{Tupper}_LIST_REMOVE; - }} - - output->prev->next = output->next; - output->next->prev = output->prev; - - --(list->node_count); - list->total_size -= output->string->size; - - output->prev = output->next = NULL; - -RETURN_{Tupper}_LIST_REMOVE: - return output; diff --git a/codegen/dbl_list/snippets/node_to_list b/codegen/dbl_list/snippets/node_to_list deleted file mode 100644 index 2c52609..0000000 --- a/codegen/dbl_list/snippets/node_to_list +++ /dev/null @@ -1,15 +0,0 @@ - {T}List output = {{.first = node, .last = node, .total_size = node->string->size, .node_count = 1}}; - - while (output.first->prev != NULL) {{ - output.total_size += output.first->prev->string->size; - output.first = output.first->prev; - ++(output.node_count); - }} - - while (output.last->next != NULL) {{ - output.total_size += output.last->next->string->size; - output.last = output.last->next; - ++(output.node_count); - }} - - return output; diff --git a/codegen/utils.py b/codegen/utils.py deleted file mode 100644 index 5247512..0000000 --- a/codegen/utils.py +++ /dev/null @@ -1,6 +0,0 @@ -from pathlib import Path - - -def load_func_body_from_file(filename: Path) -> str: - with open(filename, "r") as infile: - return infile.read() diff --git a/src/containers/dbl_list/dbl_list.c b/src/containers/dbl_list/dbl_list.c new file mode 100644 index 0000000..eede8a7 --- /dev/null +++ b/src/containers/dbl_list/dbl_list.c @@ -0,0 +1,209 @@ +#include "./dbl_list.h" +#include "../../common/aliases/aliases.h" +#include + +internal DBL_LIST(void) node_to_list(DBL_NODE(void) *node); + +DBL_NODE(void) *_dbl_list_get(const DBL_LIST(void) *list, u64 index) { + if (index >= list->node_count) { + return NULL; + } + + DBL_NODE(void) *output = NULL; + DBL_NODE(void) *current = list->first; + for (u64 i = 1; i <= index; ++i) { + current = current->next; + } + + output = current; + + return output; + +} + +void _dbl_list_push_front(DBL_LIST(void) *list, DBL_NODE(void) *node) { + if (!list || !node || !(node->item)) { + return; + } + + DBL_LIST(void) node_list = node_to_list(node); + + if (list->node_count == 0) { + *list = node_list; + return; + } + + list->node_count += node_list.node_count; + + DBL_NODE(void) *first = list->first; + if (first) { + first->prev = node_list.last; + } + + list->first = node_list.first; + node_list.last->next = first; + +} + +void _dbl_list_push_back(DBL_LIST(void) *list, DBL_NODE(void) *node) { + if (!list || !node || !(node->item)) { + return; + } + + DBL_LIST(void) node_list = node_to_list(node); + + if (list->node_count == 0) { + *list = node_list; + return; + } + + list->node_count += node_list.node_count; + + DBL_NODE(void) *last = list->last; + if (last) { + last->next = node_list.first; + } + + list->last = node_list.last; + node_list.first->prev = last; + +} + +void _dbl_list_insert(DBL_LIST(void) *list, DBL_NODE(void) *node, u64 index) { + if (!list || !node || !(node->item)) { + return; + } + + if (index == 0) { + _dbl_list_push_front(list, node); + return; + } else if (index == list->node_count) { + _dbl_list_push_back(list, node); + return; + } + + DBL_NODE(void) *dst_node = _dbl_list_get(list, index); + if (!dst_node) { + return; + } + + DBL_LIST(void) node_list = node_to_list(node); + + list->node_count += node_list.node_count; + + DBL_NODE(void) *prev = dst_node->prev; + + dst_node->prev = node_list.last; + prev->next = node_list.first; + + node_list.first->prev = prev; + node_list.last->next = dst_node; + +} + +DBL_NODE(void) *_dbl_list_pop_front(DBL_LIST(void) *list) { + DBL_NODE(void) *output = NULL; + + if (!list || list->node_count == 0) { + goto RETURN_STR8_LIST_POP_FRONT; + } + + output = list->first; + + if (list->node_count == 1) { + *list = (DBL_LIST(void)){0}; + goto RETURN_STR8_LIST_POP_FRONT; + } + + --(list->node_count); + list->first = output->next; + + output->prev = output->next = NULL; + +RETURN_STR8_LIST_POP_FRONT: + return output; + +} + +DBL_NODE(void) *_dbl_list_pop_back(DBL_LIST(void) *list) { + DBL_NODE(void) *output = NULL; + + if (!list || list->node_count == 0) { + goto RETURN_STR8_LIST_POP_BACK; + } + + output = list->last; + + if (list->node_count == 1) { + *list = (DBL_LIST(void)){0}; + goto RETURN_STR8_LIST_POP_BACK; + } + + --(list->node_count); + list->last = output->prev; + + output->prev = output->next = NULL; + +RETURN_STR8_LIST_POP_BACK: + return output; + +} + +DBL_NODE(void) *_dbl_list_remove(DBL_LIST(void) *list, u64 index) { + DBL_NODE(void) *output = NULL; + if (!list) { + goto RETURN_STR8_LIST_REMOVE; + } + + if (index == 0) { + output = _dbl_list_pop_front(list); + goto RETURN_STR8_LIST_REMOVE; + } else if (index == list->node_count) { + output = _dbl_list_pop_back(list); + goto RETURN_STR8_LIST_REMOVE; + } + + output = _dbl_list_get(list, index); + if (!output) { + goto RETURN_STR8_LIST_REMOVE; + } + + output->prev->next = output->next; + output->next->prev = output->prev; + + --(list->node_count); + + output->prev = output->next = NULL; + +RETURN_STR8_LIST_REMOVE: + return output; + +} + +void _dbl_list_empty(DBL_LIST(void) *list) { + if (!list) { + return; + } + + u64 count = list->node_count; + for (u64 i = 0; i < count; ++i) { + _dbl_list_pop_back(list); + } + +} + +internal DBL_LIST(void) node_to_list(DBL_NODE(void) *node) { + DBL_LIST(void) output = {.first = node, .last = node, .node_count = 1}; + + while (output.first->prev != NULL) { + output.first = output.first->prev; + ++(output.node_count); + } + + while (output.last->next != NULL) { + output.last = output.last->next; + ++(output.node_count); + } + + return output; +} diff --git a/src/containers/dbl_list/dbl_list.h b/src/containers/dbl_list/dbl_list.h new file mode 100644 index 0000000..87b2620 --- /dev/null +++ b/src/containers/dbl_list/dbl_list.h @@ -0,0 +1,72 @@ +#ifndef DBL_LIST_H +#define DBL_LIST_H + +#include "../../common/aliases/aliases.h" + +#ifdef __cplusplus +BEGIN_C_LINKAGE +#endif // !__cplusplus + +#define DBL_NODE(T) T##Node +#define DBL_LIST(T) T##List + +#define CAST_NODE(NODE) ((DBL_NODE(void))(NODE)) +#define CAST_LIST(LIST) ((DBL_LIST(void))(LIST)) + +#define CAST_NODE_PTR(NODE_PTR) ((DBL_NODE(void)*)(NODE_PTR)) +#define CAST_LIST_PTR(LIST_PTR) ((DBL_LIST(void)*)(LIST_PTR)) + +#define DBL_LIST_DECL(T) typedef struct DBL_NODE(T) DBL_NODE(T); \ + struct DBL_NODE(T) { \ + T *item; \ + DBL_NODE(T) *prev; \ + DBL_NODE(T) *next; \ + }; \ + \ + typedef struct DBL_LIST(T) DBL_LIST(T); \ + struct DBL_LIST(T) { \ + DBL_NODE(T) *first; \ + DBL_NODE(T) *last; \ + u64 node_count; \ + } + +DBL_LIST_DECL(void); + +#define wapp_dbl_list_get(T, LIST_PTR, INDEX) \ + (DBL_NODE(T)*)_dbl_list_get(CAST_LIST_PTR(LIST_PTR), INDEX) + +#define wapp_dbl_list_push_front(T, LIST_PTR, NODE_PTR) \ + _dbl_list_push_front(CAST_LIST_PTR(LIST_PTR), CAST_NODE_PTR(NODE_PTR)) + +#define wapp_dbl_list_push_back(T, LIST_PTR, NODE_PTR) \ + _dbl_list_push_back(CAST_LIST_PTR(LIST_PTR), CAST_NODE_PTR(NODE_PTR)) + +#define wapp_dbl_list_insert(T, LIST_PTR, NODE_PTR, INDEX) \ + _dbl_list_insert(CAST_LIST_PTR(LIST_PTR), CAST_NODE_PTR(NODE_PTR), INDEX) + +#define wapp_dbl_list_pop_front(T, LIST_PTR) \ + (DBL_NODE(T)*)_dbl_list_pop_front(CAST_LIST_PTR(LIST_PTR)) + +#define wapp_dbl_list_pop_back(T, LIST_PTR) \ + (DBL_NODE(T)*)_dbl_list_pop_back(CAST_LIST_PTR(LIST_PTR)) + +#define wapp_dbl_list_remove(T, LIST_PTR, INDEX) \ + (DBL_NODE(T)*)_dbl_list_remove(CAST_LIST_PTR(LIST_PTR), INDEX) + +#define wapp_dbl_list_empty(T, LIST_PTR) \ + _dbl_list_empty(CAST_LIST_PTR(LIST_PTR)) + +DBL_NODE(void) *_dbl_list_get(const DBL_LIST(void) *list, u64 index); +void _dbl_list_push_front(DBL_LIST(void) *list, DBL_NODE(void) *node); +void _dbl_list_push_back(DBL_LIST(void) *list, DBL_NODE(void) *node); +void _dbl_list_insert(DBL_LIST(void) *list, DBL_NODE(void) *node, u64 index); +DBL_NODE(void) *_dbl_list_pop_front(DBL_LIST(void) *list); +DBL_NODE(void) *_dbl_list_pop_back(DBL_LIST(void) *list); +DBL_NODE(void) *_dbl_list_remove(DBL_LIST(void) *list, u64 index); +void _dbl_list_empty(DBL_LIST(void) *list); + +#ifdef __cplusplus +END_C_LINKAGE +#endif // !__cplusplus + +#endif // !DBL_LIST_H diff --git a/src/containers/wapp_containers.c b/src/containers/wapp_containers.c new file mode 100644 index 0000000..6fc30b2 --- /dev/null +++ b/src/containers/wapp_containers.c @@ -0,0 +1,6 @@ +#ifndef WAPP_CONTAINERS_C +#define WAPP_CONTAINERS_C + +#include "dbl_list/dbl_list.c" + +#endif // !WAPP_CONTAINERS_C diff --git a/src/containers/wapp_containers.h b/src/containers/wapp_containers.h new file mode 100644 index 0000000..0c48a9f --- /dev/null +++ b/src/containers/wapp_containers.h @@ -0,0 +1,7 @@ +#ifndef WAPP_CONTAINERS_H +#define WAPP_CONTAINERS_H + +#include "dbl_list/dbl_list.h" +#include "../common/wapp_common.h" + +#endif // !WAPP_CONTAINERS_H diff --git a/src/core/os/cpath/cpath.c b/src/core/os/cpath/cpath.c index e234448..8175871 100644 --- a/src/core/os/cpath/cpath.c +++ b/src/core/os/cpath/cpath.c @@ -4,12 +4,13 @@ #include "../../mem/allocator/mem_allocator.h" #include "../../mem/arena/mem_arena_allocator.h" #include "../../strings/str8/str8.h" +#include "../../strings/str8/str8_list.h" #include #include #include #include -u32 wapp_cpath_join_path(Str8 *dst, const Str8List *parts) { +u32 wapp_cpath_join_path(Str8 *dst, const DBL_LIST(Str8) *parts) { if (!dst || !parts) { return CPATH_JOIN_INVALID_ARGS; } @@ -21,29 +22,29 @@ u32 wapp_cpath_join_path(Str8 *dst, const Str8List *parts) { Str8 separator = wapp_str8_buf(4); wapp_str8_push_back(&separator, PATH_SEP); - u64 required_capacity = parts->node_count * separator.size + parts->total_size; + u64 required_capacity = parts->node_count * separator.size + wapp_str8_list_total_size(parts); if (dst->capacity < required_capacity) { return CPATH_JOIN_INSUFFICIENT_DST_CAPACITY; } // Handle first node - const Str8Node *first_node = wapp_str8_list_get(parts, 0); - wapp_str8_copy_str8_capped(dst, first_node->string); + const DBL_NODE(Str8) *first_node = wapp_dbl_list_get(Str8, parts, 0); + wapp_str8_copy_str8_capped(dst, first_node->item); // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // MSVC Spectre mitigation warnings - const Str8Node *node = first_node; + const DBL_NODE(Str8) *node = first_node; u64 node_index = 1; bool running = true; while (running && node->next) { node = node->next; - if (node->string->size == 0) { + if (node->item->size == 0) { continue; } if (dst->size > 0) { char dst_last = wapp_str8_get(dst, dst->size - 1); - char node_start = wapp_str8_get(node->string, 0); + char node_start = wapp_str8_get(node->item, 0); bool add_path_sep = dst_last != PATH_SEP && node_start != PATH_SEP; if (add_path_sep) { @@ -51,7 +52,7 @@ u32 wapp_cpath_join_path(Str8 *dst, const Str8List *parts) { } } - wapp_str8_concat_capped(dst, node->string); + wapp_str8_concat_capped(dst, node->item); ++node_index; running = node_index < parts->node_count; @@ -90,7 +91,7 @@ Str8 *dirup(const Allocator *allocator, Str8RO *path, u64 levels) { goto RETURN_DIRUP; } - Str8List *parts = wapp_str8_split(&tmp_arena, path, &separator); + DBL_LIST(Str8) *parts = wapp_str8_split(&tmp_arena, path, &separator); if (!parts) { goto RETURN_DIRUP; } @@ -104,11 +105,11 @@ Str8 *dirup(const Allocator *allocator, Str8RO *path, u64 levels) { wapp_str8_push_back(output, absolute ? PATH_SEP : '.'); } else { for (u64 i = 0; i < levels; ++i) { - wapp_str8_list_pop_back(parts); + wapp_dbl_list_pop_back(Str8, parts); } u64 alignment = sizeof(void *) * 2; - u64 alloc_size = parts->total_size + parts->node_count * separator.size; + u64 alloc_size = wapp_str8_list_total_size(parts) + parts->node_count * separator.size; u64 modulo = alloc_size & (alignment - 1); alloc_size += alignment - modulo; diff --git a/src/core/os/cpath/cpath.h b/src/core/os/cpath/cpath.h index 61808df..5b21e5a 100644 --- a/src/core/os/cpath/cpath.h +++ b/src/core/os/cpath/cpath.h @@ -28,7 +28,7 @@ enum { CPATH_JOIN_INSUFFICIENT_DST_CAPACITY, }; -u32 wapp_cpath_join_path(Str8 *dst, const Str8List *parts); +u32 wapp_cpath_join_path(Str8 *dst, const DBL_LIST(Str8) *parts); Str8 *dirup(const Allocator *allocator, Str8RO *path, u64 levels); #ifdef __cplusplus diff --git a/src/core/os/shell/commander/commander.c b/src/core/os/shell/commander/commander.c index 551287e..a030f3e 100644 --- a/src/core/os/shell/commander/commander.c +++ b/src/core/os/shell/commander/commander.c @@ -18,7 +18,7 @@ internal inline CMDResult execute_command(Str8RO *cmd, CMDOutHandling out_handling, Str8 *out_buf); internal inline CMDError get_command_output(FILE *fp, CMDOutHandling out_handling, Str8 *out_buf); -CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, Str8 *out_buf, const Str8List *cmd) { +CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, Str8 *out_buf, const DBL_LIST(Str8) *cmd) { if (!cmd) { return CMD_NO_EXIT(SHELL_ERR_INVALID_ARGS); } diff --git a/src/core/os/shell/commander/commander.h b/src/core/os/shell/commander/commander.h index c82269c..b219c29 100644 --- a/src/core/os/shell/commander/commander.h +++ b/src/core/os/shell/commander/commander.h @@ -14,7 +14,7 @@ BEGIN_C_LINKAGE #define CMD_NO_EXIT(ERR) ((CMDResult){.exited = false, .exit_code = EXIT_FAILURE, .error = ERR}) -CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, Str8 *out_buf, const Str8List *cmd); +CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, Str8 *out_buf, const DBL_LIST(Str8) *cmd); external CMDError get_output_status(FILE *fp, i32 *status_out); diff --git a/src/core/strings/str8/str8.c b/src/core/strings/str8/str8.c index 59d82e3..2a04b55 100644 --- a/src/core/strings/str8/str8.c +++ b/src/core/strings/str8/str8.c @@ -1,5 +1,7 @@ #include "str8.h" +#include "str8_list.h" #include "../../../common/aliases/aliases.h" +#include "../../../containers/dbl_list/dbl_list.h" #include "../../mem/allocator/mem_allocator.h" #include #include @@ -300,19 +302,19 @@ i64 wapp_str8_rfind(Str8RO *str, Str8RO substr) { return -1; } -Str8List *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits) { +DBL_LIST(Str8) *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits) { if (!allocator || !str || !delimiter) { return NULL; } - Str8List *output = wapp_mem_allocator_alloc(allocator, sizeof(Str8List)); + DBL_LIST(Str8) *output = wapp_mem_allocator_alloc(allocator, sizeof(DBL_LIST(Str8))); if (delimiter->size > str->size) { Str8 *full = wapp_str8_alloc_str8(allocator, str); - Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node)); + DBL_NODE(Str8) *node = wapp_mem_allocator_alloc(allocator, sizeof(DBL_NODE(Str8))); if (node) { - node->string = full; - wapp_str8_list_push_back(output, node); + node->item = full; + wapp_dbl_list_push_back(Str8, output, node); } goto RETURN_STR8_SPLIT; @@ -330,10 +332,10 @@ Str8List *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8 } before_str = wapp_str8_alloc_substr(allocator, str, start, start + end); - Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node)); + DBL_NODE(Str8) *node = wapp_mem_allocator_alloc(allocator, sizeof(DBL_NODE(Str8))); if (node) { - node->string = before_str; - wapp_str8_list_push_back(output, node); + node->item = before_str; + wapp_dbl_list_push_back(Str8, output, node); } wapp_mem_allocator_free(allocator, (void **)&rest, sizeof(Str8)); @@ -344,30 +346,30 @@ Str8List *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8 } // Ensure the last part of the string after the delimiter is added to the list - rest = wapp_str8_alloc_substr(allocator, str, start, str->size); - Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node)); + rest = wapp_str8_alloc_substr(allocator, str, start, str->size); + DBL_NODE(Str8) *node = wapp_mem_allocator_alloc(allocator, sizeof(DBL_NODE(Str8))); if (node) { - node->string = rest; - wapp_str8_list_push_back(output, node); + node->item = rest; + wapp_dbl_list_push_back(Str8, output, node); } RETURN_STR8_SPLIT: return output; } -Str8List *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits) { +DBL_LIST(Str8) *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits) { if (!allocator || !str || !delimiter) { return NULL; } - Str8List *output = wapp_mem_allocator_alloc(allocator, sizeof(Str8List)); + DBL_LIST(Str8) *output = wapp_mem_allocator_alloc(allocator, sizeof(DBL_LIST(Str8))); if (delimiter->size > str->size) { Str8 *full = wapp_str8_alloc_str8(allocator, str); - Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node)); + DBL_NODE(Str8) *node = wapp_mem_allocator_alloc(allocator, sizeof(DBL_NODE(Str8))); if (node) { - node->string = full; - wapp_str8_list_push_back(output, node); + node->item = full; + wapp_dbl_list_push_back(Str8, output, node); } goto RETURN_STR8_SPLIT; @@ -384,10 +386,10 @@ Str8List *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str } after_str = wapp_str8_alloc_substr(allocator, rest, end + delimiter->size, str->size); - Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node)); + DBL_NODE(Str8) *node = wapp_mem_allocator_alloc(allocator, sizeof(DBL_NODE(Str8))); if (node) { - node->string = after_str; - wapp_str8_list_push_front(output, node); + node->item = after_str; + wapp_dbl_list_push_front(Str8, output, node); } wapp_mem_allocator_free(allocator, (void **)&rest, sizeof(Str8)); @@ -397,32 +399,32 @@ Str8List *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str } rest = wapp_str8_alloc_substr(allocator, str, 0, rest->size); - Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node)); + DBL_NODE(Str8) *node = wapp_mem_allocator_alloc(allocator, sizeof(DBL_NODE(Str8))); if (node) { - node->string = rest; - wapp_str8_list_push_front(output, node); + node->item = rest; + wapp_dbl_list_push_front(Str8, output, node); } RETURN_STR8_SPLIT: return output; } -Str8 *wapp_str8_join(const Allocator *allocator, const Str8List *list, Str8RO *delimiter) { +Str8 *wapp_str8_join(const Allocator *allocator, const DBL_LIST(Str8) *list, Str8RO *delimiter) { if (!allocator || !list || !delimiter) { return NULL; } - u64 capacity = list->total_size + (delimiter->size * (list->node_count - 1)); + u64 capacity = wapp_str8_list_total_size(list) + (delimiter->size * (list->node_count - 1)); Str8 *output = wapp_str8_alloc_buf(allocator, capacity * 2); // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // MSVC Spectre mitigation warnings - Str8Node *node; + DBL_NODE(Str8) *node; u64 node_index = 0; bool running = true; while (running) { - node = wapp_str8_list_get(list, node_index); - wapp_str8_concat_capped(output, node->string); + node = wapp_dbl_list_get(Str8, list, node_index); + wapp_str8_concat_capped(output, node->item); if (node_index + 1 < list->node_count) { wapp_str8_concat_capped(output, delimiter); } diff --git a/src/core/strings/str8/str8.h b/src/core/strings/str8/str8.h index 894e4c2..7b89132 100644 --- a/src/core/strings/str8/str8.h +++ b/src/core/strings/str8/str8.h @@ -3,6 +3,7 @@ #include "./str8_list.h" #include "../../../common/aliases/aliases.h" +#include "../../../containers/dbl_list/dbl_list.h" #include "../../mem/allocator/mem_allocator.h" #include #include @@ -84,15 +85,15 @@ i64 wapp_str8_rfind(Str8RO *str, Str8RO substr); */ #define wapp_str8_split(ALLOCATOR, STR, DELIMITER) wapp_str8_split_with_max(ALLOCATOR, STR, DELIMITER, -1) #define wapp_str8_rsplit(ALLOCATOR, STR, DELIMITER) wapp_str8_rsplit_with_max(ALLOCATOR, STR, DELIMITER, -1) -Str8List *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits); -Str8List *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits); -Str8 *wapp_str8_join(const Allocator *allocator, const Str8List *list, Str8RO *delimiter); +DBL_LIST(Str8) *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits); +DBL_LIST(Str8) *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits); +Str8 *wapp_str8_join(const Allocator *allocator, const DBL_LIST(Str8) *list, Str8RO *delimiter); /** * Str8 list utilities */ -#define wapp_str8_node_from_cstr(STRING) ((Str8Node){.string = &wapp_str8_lit(STRING)}) -#define wapp_str8_node_from_str8(STRING) ((Str8Node){.string = &(STRING)}) +#define wapp_str8_node_from_cstr(STRING) ((DBL_NODE(Str8)){.item = &wapp_str8_lit(STRING)}) +#define wapp_str8_node_from_str8(STRING) ((DBL_NODE(Str8)){.item = &(STRING)}) #ifdef __cplusplus END_C_LINKAGE diff --git a/src/core/strings/str8/str8_list.c b/src/core/strings/str8/str8_list.c index a3cebbe..ea4418d 100644 --- a/src/core/strings/str8/str8_list.c +++ b/src/core/strings/str8/str8_list.c @@ -1,224 +1,18 @@ -/** - * THIS FILE IS AUTOMATICALLY GENERATED. ANY MODIFICATIONS TO IT WILL BE OVERWRITTEN - */ - #include "./str8_list.h" -#include "../../../common/aliases/aliases.h" #include "./str8.h" -#include +#include "../../../common/aliases/aliases.h" +#include "../../../containers/dbl_list/dbl_list.h" -internal Str8List str8_node_to_list(Str8Node *node); +u64 wapp_str8_list_total_size(const DBL_LIST(Str8) *list) { + if (!list) { + return 0; + } -Str8Node *wapp_str8_list_get(const Str8List *list, u64 index) { - if (index >= list->node_count) { - return NULL; - } - - Str8Node *output = NULL; - Str8Node *current = list->first; - for (u64 i = 1; i <= index; ++i) { - current = current->next; - } - - output = current; - - return output; + u64 output = 0; + for (u64 i = 0; i < list->node_count; ++i) { + DBL_NODE(Str8) *node = wapp_dbl_list_get(Str8, list, i); + output += node->item->size; + } + return output; } - -void wapp_str8_list_push_front(Str8List *list, Str8Node *node) { - if (!list || !node || !(node->string)) { - return; - } - - Str8List node_list = str8_node_to_list(node); - - if (list->node_count == 0) { - *list = node_list; - return; - } - - list->total_size += node_list.total_size; - list->node_count += node_list.node_count; - - Str8Node *first = list->first; - if (first) { - first->prev = node_list.last; - } - - list->first = node_list.first; - node_list.last->next = first; - -} - -void wapp_str8_list_push_back(Str8List *list, Str8Node *node) { - if (!list || !node || !(node->string)) { - return; - } - - Str8List node_list = str8_node_to_list(node); - - if (list->node_count == 0) { - *list = node_list; - return; - } - - list->total_size += node_list.total_size; - list->node_count += node_list.node_count; - - Str8Node *last = list->last; - if (last) { - last->next = node_list.first; - } - - list->last = node_list.last; - node_list.first->prev = last; - -} - -void wapp_str8_list_insert(Str8List *list, Str8Node *node, u64 index) { - if (!list || !node || !(node->string)) { - return; - } - - if (index == 0) { - wapp_str8_list_push_front(list, node); - return; - } else if (index == list->node_count) { - wapp_str8_list_push_back(list, node); - return; - } - - Str8Node *dst_node = wapp_str8_list_get(list, index); - if (!dst_node) { - return; - } - - Str8List node_list = str8_node_to_list(node); - - list->total_size += node_list.total_size; - list->node_count += node_list.node_count; - - Str8Node *prev = dst_node->prev; - - dst_node->prev = node_list.last; - prev->next = node_list.first; - - node_list.first->prev = prev; - node_list.last->next = dst_node; - -} - -Str8Node *wapp_str8_list_pop_front(Str8List *list) { - Str8Node *output = NULL; - - if (!list || list->node_count == 0) { - goto RETURN_STR8_LIST_POP_FRONT; - } - - output = list->first; - - if (list->node_count == 1) { - *list = (Str8List){0}; - goto RETURN_STR8_LIST_POP_FRONT; - } - - --(list->node_count); - list->total_size -= output->string->size; - list->first = output->next; - - output->prev = output->next = NULL; - -RETURN_STR8_LIST_POP_FRONT: - return output; - -} - -Str8Node *wapp_str8_list_pop_back(Str8List *list) { - Str8Node *output = NULL; - - if (!list || list->node_count == 0) { - goto RETURN_STR8_LIST_POP_BACK; - } - - output = list->last; - - if (list->node_count == 1) { - *list = (Str8List){0}; - goto RETURN_STR8_LIST_POP_BACK; - } - - --(list->node_count); - list->total_size -= output->string->size; - list->last = output->prev; - - output->prev = output->next = NULL; - -RETURN_STR8_LIST_POP_BACK: - return output; - -} - -Str8Node *wapp_str8_list_remove(Str8List *list, u64 index) { - Str8Node *output = NULL; - if (!list) { - goto RETURN_STR8_LIST_REMOVE; - } - - if (index == 0) { - output = wapp_str8_list_pop_front(list); - goto RETURN_STR8_LIST_REMOVE; - } else if (index == list->node_count) { - output = wapp_str8_list_pop_back(list); - goto RETURN_STR8_LIST_REMOVE; - } - - output = wapp_str8_list_get(list, index); - if (!output) { - goto RETURN_STR8_LIST_REMOVE; - } - - output->prev->next = output->next; - output->next->prev = output->prev; - - --(list->node_count); - list->total_size -= output->string->size; - - output->prev = output->next = NULL; - -RETURN_STR8_LIST_REMOVE: - return output; - -} - -void wapp_str8_list_empty(Str8List *list) { - if (!list) { - return; - } - - u64 count = list->node_count; - for (u64 i = 0; i < count; ++i) { - wapp_str8_list_pop_back(list); - } - -} - -internal Str8List str8_node_to_list(Str8Node *node) { - Str8List output = {.first = node, .last = node, .total_size = node->string->size, .node_count = 1}; - - while (output.first->prev != NULL) { - output.total_size += output.first->prev->string->size; - output.first = output.first->prev; - ++(output.node_count); - } - - while (output.last->next != NULL) { - output.total_size += output.last->next->string->size; - output.last = output.last->next; - ++(output.node_count); - } - - return output; - -} - diff --git a/src/core/strings/str8/str8_list.h b/src/core/strings/str8/str8_list.h index 2476871..8a2e62f 100644 --- a/src/core/strings/str8/str8_list.h +++ b/src/core/strings/str8/str8_list.h @@ -6,6 +6,7 @@ #define STR8_LIST_H #include "../../../common/aliases/aliases.h" +#include "../../../containers/dbl_list/dbl_list.h" #ifdef __cplusplus BEGIN_C_LINKAGE @@ -13,29 +14,9 @@ BEGIN_C_LINKAGE typedef struct str8 Str8; -typedef struct Str8Node Str8Node; -struct Str8Node { - Str8 *string; - Str8Node *prev; - Str8Node *next; -}; +DBL_LIST_DECL(Str8); -typedef struct Str8List Str8List; -struct Str8List { - Str8Node *first; - Str8Node *last; - u64 total_size; - u64 node_count; -}; - -Str8Node *wapp_str8_list_get(const Str8List *list, u64 index); -void wapp_str8_list_push_front(Str8List *list, Str8Node *node); -void wapp_str8_list_push_back(Str8List *list, Str8Node *node); -void wapp_str8_list_insert(Str8List *list, Str8Node *node, u64 index); -Str8Node *wapp_str8_list_pop_front(Str8List *list); -Str8Node *wapp_str8_list_pop_back(Str8List *list); -Str8Node *wapp_str8_list_remove(Str8List *list, u64 index); -void wapp_str8_list_empty(Str8List *list); +u64 wapp_str8_list_total_size(const DBL_LIST(Str8) *list); #ifdef __cplusplus END_C_LINKAGE diff --git a/src/wapp.c b/src/wapp.c index 1459e5d..202c27f 100644 --- a/src/wapp.c +++ b/src/wapp.c @@ -2,6 +2,7 @@ #define WAPP_C #include "wapp.h" +#include "containers/wapp_containers.c" #include "core/wapp_core.c" #include "prng/wapp_prng.c" #include "uuid/uuid.c" diff --git a/src/wapp.h b/src/wapp.h index 68094dc..c051e1e 100644 --- a/src/wapp.h +++ b/src/wapp.h @@ -2,6 +2,7 @@ #define WAPP_H #include "common/wapp_common.h" +#include "containers/wapp_containers.h" #include "core/wapp_core.h" #include "prng/wapp_prng.h" #include "uuid/wapp_uuid.h" diff --git a/tests/cpath/test_cpath.c b/tests/cpath/test_cpath.c index 003d740..065067f 100644 --- a/tests/cpath/test_cpath.c +++ b/tests/cpath/test_cpath.c @@ -17,16 +17,16 @@ TestFuncResult test_cpath_join_path(void) { wapp_str8_format(&expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); wapp_str8_format(&tmp, "%c", PATH_SEP); - Str8List parts = {0}; - wapp_str8_list_push_back(&parts, &wapp_str8_node_from_str8(tmp)); - wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr("home")); - wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr("abdelrahman")); - wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr("Documents")); + DBL_LIST(Str8) parts = {0}; + wapp_dbl_list_push_back(Str8, &parts, &wapp_str8_node_from_str8(tmp)); + wapp_dbl_list_push_back(Str8, &parts, &wapp_str8_node_from_cstr("home")); + wapp_dbl_list_push_back(Str8, &parts, &wapp_str8_node_from_cstr("abdelrahman")); + wapp_dbl_list_push_back(Str8, &parts, &wapp_str8_node_from_cstr("Documents")); wapp_cpath_join_path(&out, &parts); result = wapp_str8_equal(&out, &expected); - wapp_str8_list_pop_front(&parts); + wapp_dbl_list_pop_front(Str8, &parts); wapp_str8_format(&expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); @@ -34,8 +34,8 @@ TestFuncResult test_cpath_join_path(void) { result = result && wapp_str8_equal(&out, &expected); wapp_str8_concat_capped(&tmp, &wapp_str8_lit_ro("home")); - wapp_str8_list_pop_front(&parts); - wapp_str8_list_push_front(&parts, &wapp_str8_node_from_str8(tmp)); + wapp_dbl_list_pop_front(Str8, &parts); + wapp_dbl_list_push_front(Str8, &parts, &wapp_str8_node_from_str8(tmp)); wapp_str8_format(&expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); @@ -43,35 +43,35 @@ TestFuncResult test_cpath_join_path(void) { result = result && wapp_str8_equal(&out, &expected); wapp_str8_format(&tmp, "home%c", PATH_SEP); - wapp_str8_list_pop_front(&parts); - wapp_str8_list_push_front(&parts, &wapp_str8_node_from_cstr("home")); + wapp_dbl_list_pop_front(Str8, &parts); + wapp_dbl_list_push_front(Str8, &parts, &wapp_str8_node_from_cstr("home")); wapp_str8_format(&expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); wapp_cpath_join_path(&out, &parts); result = result && wapp_str8_equal(&out, &expected); - wapp_str8_list_empty(&parts); + wapp_dbl_list_empty(Str8, &parts); wapp_str8_format(&tmp, "%chome", PATH_SEP); - wapp_str8_list_push_back(&parts, &wapp_str8_node_from_str8(tmp)); - wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr("")); + wapp_dbl_list_push_back(Str8, &parts, &wapp_str8_node_from_str8(tmp)); + wapp_dbl_list_push_back(Str8, &parts, &wapp_str8_node_from_cstr("")); wapp_str8_format(&expected, "%chome", PATH_SEP); wapp_cpath_join_path(&out, &parts); result = result && wapp_str8_equal(&out, &expected); - wapp_str8_list_pop_front(&parts); - wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr("")); + wapp_dbl_list_pop_front(Str8, &parts); + wapp_dbl_list_push_back(Str8, &parts, &wapp_str8_node_from_cstr("")); wapp_str8_format(&expected, "%s", ""); wapp_cpath_join_path(&out, &parts); result = result && wapp_str8_equal(&out, &expected); - wapp_str8_list_pop_back(&parts); - wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr("home")); + wapp_dbl_list_pop_back(Str8, &parts); + wapp_dbl_list_push_back(Str8, &parts, &wapp_str8_node_from_cstr("home")); wapp_str8_copy_cstr_capped(&expected, "home"); diff --git a/tests/shell_commander/test_shell_commander.c b/tests/shell_commander/test_shell_commander.c index 3682320..8f5d083 100644 --- a/tests/shell_commander/test_shell_commander.c +++ b/tests/shell_commander/test_shell_commander.c @@ -6,9 +6,9 @@ #include TestFuncResult test_commander_cmd_success(void) { - Str8List cmd = {0}; - wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr("echo")); - wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr("hello world")); + DBL_LIST(Str8) cmd = {0}; + wapp_dbl_list_push_back(Str8, &cmd, &wapp_str8_node_from_cstr("echo")); + wapp_dbl_list_push_back(Str8, &cmd, &wapp_str8_node_from_cstr("hello world")); CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, NULL, &cmd); bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS && @@ -18,8 +18,8 @@ TestFuncResult test_commander_cmd_success(void) { } TestFuncResult test_commander_cmd_failure(void) { - Str8List cmd = {0}; - wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr("grep")); + DBL_LIST(Str8) cmd = {0}; + wapp_dbl_list_push_back(Str8, &cmd, &wapp_str8_node_from_cstr("grep")); CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, NULL, &cmd); bool failed = result.exited && result.exit_code != EXIT_SUCCESS && @@ -34,9 +34,9 @@ TestFuncResult test_commander_cmd_out_buf_success(void) { char msg[] = "hello world"; wapp_str8_copy_cstr_capped(&expected, msg); - Str8List cmd = {0}; - wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr("echo")); - wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr(msg)); + DBL_LIST(Str8) cmd = {0}; + wapp_dbl_list_push_back(Str8, &cmd, &wapp_str8_node_from_cstr("echo")); + wapp_dbl_list_push_back(Str8, &cmd, &wapp_str8_node_from_cstr(msg)); CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, &buf, &cmd); bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS && @@ -51,9 +51,9 @@ TestFuncResult test_commander_cmd_out_buf_failure(void) { char msg[] = "hello world"; wapp_str8_copy_cstr_capped(&expected, msg); - Str8List cmd = {0}; - wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr("echo")); - wapp_str8_list_push_back(&cmd, &wapp_str8_node_from_cstr(msg)); + DBL_LIST(Str8) cmd = {0}; + wapp_dbl_list_push_back(Str8, &cmd, &wapp_str8_node_from_cstr("echo")); + wapp_dbl_list_push_back(Str8, &cmd, &wapp_str8_node_from_cstr(msg)); CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, &buf, &cmd); bool failed = !result.exited && result.exit_code != EXIT_SUCCESS && diff --git a/tests/str8/test_str8.c b/tests/str8/test_str8.c index adf41af..f0289a2 100644 --- a/tests/str8/test_str8.c +++ b/tests/str8/test_str8.c @@ -416,8 +416,8 @@ TestFuncResult test_str8_split(void) { Str8 str = wapp_str8_lit("hello world from me"); Str8 delim1 = wapp_str8_lit(" "); Str8 delim2 = wapp_str8_lit("from"); - Str8List *list1 = wapp_str8_split(&arena, &str, &delim1); - Str8List *list2 = wapp_str8_split(&arena, &str, &delim2); + DBL_LIST(Str8) *list1 = wapp_str8_split(&arena, &str, &delim1); + DBL_LIST(Str8) *list2 = wapp_str8_split(&arena, &str, &delim2); Str8RO splits1[] = { wapp_str8_slice(&str, 0, 5), @@ -438,14 +438,14 @@ TestFuncResult test_str8_split(void) { u64 count2 = ARRLEN(splits2); bool running2 = true; - result = list1->node_count == count1 && list1->total_size == str.size - 3; - result = result && list2->node_count == count2 && list2->total_size == str.size - 4; + result = list1->node_count == count1 && wapp_str8_list_total_size(list1) == str.size - 3; + result = result && list2->node_count == count2 && wapp_str8_list_total_size(list2) == str.size - 4; // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // MSVC Spectre mitigation warnings while (running1) { - Str8Node *node = wapp_str8_list_get(list1, index1); - result = result && wapp_str8_equal(node->string, &(splits1[index1])); + DBL_NODE(Str8) *node = wapp_dbl_list_get(Str8, list1, index1); + result = result && wapp_str8_equal(node->item, &(splits1[index1])); ++index1; running1 = index1 < count1; @@ -454,8 +454,8 @@ TestFuncResult test_str8_split(void) { // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // MSVC Spectre mitigation warnings while (running2) { - Str8Node *node = wapp_str8_list_get(list2, index2); - result = result && wapp_str8_equal(node->string, &(splits2[index2])); + DBL_NODE(Str8) *node = wapp_dbl_list_get(Str8, list2, index2); + result = result && wapp_str8_equal(node->item, &(splits2[index2])); ++index2; running2 = index2 < count2; @@ -472,7 +472,7 @@ TestFuncResult test_str8_split_with_max(void) { Str8 str = wapp_str8_lit("hello world from me"); Str8 delim = wapp_str8_lit(" "); - Str8List *list = wapp_str8_split_with_max(&arena, &str, &delim, 2); + DBL_LIST(Str8) *list = wapp_str8_split_with_max(&arena, &str, &delim, 2); Str8RO splits[] = { wapp_str8_slice(&str, 0, 5), @@ -484,13 +484,13 @@ TestFuncResult test_str8_split_with_max(void) { u64 count = ARRLEN(splits); bool running = true; - result = list->node_count == count && list->total_size == str.size - 2; + result = list->node_count == count && wapp_str8_list_total_size(list) == str.size - 2; // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // MSVC Spectre mitigation warnings while (running) { - Str8Node *node = wapp_str8_list_get(list, index); - result = result && wapp_str8_equal(node->string, &(splits[index])); + DBL_NODE(Str8) *node = wapp_dbl_list_get(Str8, list, index); + result = result && wapp_str8_equal(node->item, &(splits[index])); ++index; running = index < count; @@ -508,8 +508,8 @@ TestFuncResult test_str8_rsplit(void) { Str8 str = wapp_str8_lit("hello world from me"); Str8 delim1 = wapp_str8_lit(" "); Str8 delim2 = wapp_str8_lit("from"); - Str8List *list1 = wapp_str8_rsplit(&arena, &str, &delim1); - Str8List *list2 = wapp_str8_rsplit(&arena, &str, &delim2); + DBL_LIST(Str8) *list1 = wapp_str8_rsplit(&arena, &str, &delim1); + DBL_LIST(Str8) *list2 = wapp_str8_rsplit(&arena, &str, &delim2); Str8RO splits1[] = { wapp_str8_slice(&str, 0, 5), @@ -530,14 +530,14 @@ TestFuncResult test_str8_rsplit(void) { u64 count2 = ARRLEN(splits2); bool running2 = true; - result = list1->node_count == count1 && list1->total_size == str.size - 3; - result = result && list2->node_count == count2 && list2->total_size == str.size - 4; + result = list1->node_count == count1 && wapp_str8_list_total_size(list1) == str.size - 3; + result = result && list2->node_count == count2 && wapp_str8_list_total_size(list2) == str.size - 4; // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // MSVC Spectre mitigation warnings while (running1) { - Str8Node *node = wapp_str8_list_get(list1, index1); - result = result && wapp_str8_equal(node->string, &(splits1[index1])); + DBL_NODE(Str8) *node = wapp_dbl_list_get(Str8, list1, index1); + result = result && wapp_str8_equal(node->item, &(splits1[index1])); ++index1; running1 = index1 < count1; @@ -546,8 +546,8 @@ TestFuncResult test_str8_rsplit(void) { // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // MSVC Spectre mitigation warnings while (running2) { - Str8Node *node = wapp_str8_list_get(list2, index2); - result = result && wapp_str8_equal(node->string, &(splits2[index2])); + DBL_NODE(Str8) *node = wapp_dbl_list_get(Str8, list2, index2); + result = result && wapp_str8_equal(node->item, &(splits2[index2])); ++index2; running2 = index2 < count2; @@ -564,7 +564,7 @@ TestFuncResult test_str8_rsplit_with_max(void) { Str8 str = wapp_str8_lit("hello world from me"); Str8 delim = wapp_str8_lit(" "); - Str8List *list = wapp_str8_rsplit_with_max(&arena, &str, &delim, 2); + DBL_LIST(Str8) *list = wapp_str8_rsplit_with_max(&arena, &str, &delim, 2); Str8RO splits[] = { wapp_str8_slice(&str, 0, 11), @@ -576,13 +576,13 @@ TestFuncResult test_str8_rsplit_with_max(void) { u64 count = ARRLEN(splits); bool running = true; - result = list->node_count == count && list->total_size == str.size - 2; + result = list->node_count == count && wapp_str8_list_total_size(list) == str.size - 2; // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // MSVC Spectre mitigation warnings while (running) { - Str8Node *node = wapp_str8_list_get(list, index); - result = result && wapp_str8_equal(node->string, &(splits[index])); + DBL_NODE(Str8) *node = wapp_dbl_list_get(Str8, list, index); + result = result && wapp_str8_equal(node->item, &(splits[index])); ++index; running = index < count; @@ -600,8 +600,8 @@ TestFuncResult test_str8_join(void) { Str8 str = wapp_str8_lit("hello world from me"); Str8 delim1 = wapp_str8_lit(" "); Str8 delim2 = wapp_str8_lit("from"); - Str8List *list1 = wapp_str8_rsplit(&arena, &str, &delim1); - Str8List *list2 = wapp_str8_rsplit(&arena, &str, &delim2); + DBL_LIST(Str8) *list1 = wapp_str8_rsplit(&arena, &str, &delim1); + DBL_LIST(Str8) *list2 = wapp_str8_rsplit(&arena, &str, &delim2); Str8 *join1 = wapp_str8_join(&arena, list1, &delim1); Str8 *join2 = wapp_str8_join(&arena, list2, &delim2); diff --git a/tests/str8/test_str8_list.c b/tests/str8/test_str8_list.c index b11ded9..7d5d2fa 100644 --- a/tests/str8/test_str8_list.c +++ b/tests/str8/test_str8_list.c @@ -10,33 +10,33 @@ TestFuncResult test_str8_list_get(void) { Str8 s4 = wapp_str8_lit("4"); Str8 s5 = wapp_str8_lit("5"); - Str8List list = {0}; - Str8Node n1 = { .string = &s1 }; - Str8Node n2 = { .string = &s2 }; - Str8Node n3 = { .string = &s3 }; - Str8Node n4 = { .string = &s4 }; - Str8Node n5 = { .string = &s5 }; + DBL_LIST(Str8) list = {0}; + DBL_NODE(Str8) n1 = { .item = &s1 }; + DBL_NODE(Str8) n2 = { .item = &s2 }; + DBL_NODE(Str8) n3 = { .item = &s3 }; + DBL_NODE(Str8) n4 = { .item = &s4 }; + DBL_NODE(Str8) n5 = { .item = &s5 }; - wapp_str8_list_push_back(&list, &n1); - wapp_str8_list_push_back(&list, &n2); - wapp_str8_list_push_back(&list, &n3); - wapp_str8_list_push_back(&list, &n4); - wapp_str8_list_push_back(&list, &n5); + wapp_dbl_list_push_back(Str8, &list, &n1); + wapp_dbl_list_push_back(Str8, &list, &n2); + wapp_dbl_list_push_back(Str8, &list, &n3); + wapp_dbl_list_push_back(Str8, &list, &n4); + wapp_dbl_list_push_back(Str8, &list, &n5); - Str8Node *node = wapp_str8_list_get(&list, 0); - result = node->string == &s1 && wapp_str8_equal(node->string, &s1); + DBL_NODE(Str8) *node = wapp_dbl_list_get(Str8, &list, 0); + result = node->item == &s1 && wapp_str8_equal(node->item, &s1); - node = wapp_str8_list_get(&list, 1); - result = result && node->string == &s2 && wapp_str8_equal(node->string, &s2); + node = wapp_dbl_list_get(Str8, &list, 1); + result = result && node->item == &s2 && wapp_str8_equal(node->item, &s2); - node = wapp_str8_list_get(&list, 2); - result = result && node->string == &s3 && wapp_str8_equal(node->string, &s3); + node = wapp_dbl_list_get(Str8, &list, 2); + result = result && node->item == &s3 && wapp_str8_equal(node->item, &s3); - node = wapp_str8_list_get(&list, 3); - result = result && node->string == &s4 && wapp_str8_equal(node->string, &s4); + node = wapp_dbl_list_get(Str8, &list, 3); + result = result && node->item == &s4 && wapp_str8_equal(node->item, &s4); - node = wapp_str8_list_get(&list, 4); - result = result && node->string == &s5 && wapp_str8_equal(node->string, &s5); + node = wapp_dbl_list_get(Str8, &list, 4); + result = result && node->item == &s5 && wapp_str8_equal(node->item, &s5); return wapp_tester_result(result); } @@ -48,19 +48,19 @@ TestFuncResult test_str8_list_push_front(void) { Str8 s2 = wapp_str8_lit("2"); Str8 s3 = wapp_str8_lit("3"); - Str8List list = {0}; - Str8Node n1 = { .string = &s1 }; - Str8Node n2 = { .string = &s2 }; - Str8Node n3 = { .string = &s3 }; + DBL_LIST(Str8) list = {0}; + DBL_NODE(Str8) n1 = { .item = &s1 }; + DBL_NODE(Str8) n2 = { .item = &s2 }; + DBL_NODE(Str8) n3 = { .item = &s3 }; - wapp_str8_list_push_front(&list, &n1); - result = list.first == list.last && list.first == &n1 && list.first->string == &s1 && list.total_size == 1 && list.node_count == 1; + wapp_dbl_list_push_front(Str8, &list, &n1); + result = list.first == list.last && list.first == &n1 && list.first->item == &s1 && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1; - wapp_str8_list_push_front(&list, &n2); - result = result && list.first == &n2 && list.first->string == &s2 && list.total_size == 2 && list.node_count == 2; + wapp_dbl_list_push_front(Str8, &list, &n2); + result = result && list.first == &n2 && list.first->item == &s2 && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2; - wapp_str8_list_push_front(&list, &n3); - result = result && list.first == &n3 && list.first->string == &s3 && list.total_size == 3 && list.node_count == 3; + wapp_dbl_list_push_front(Str8, &list, &n3); + result = result && list.first == &n3 && list.first->item == &s3 && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3; return wapp_tester_result(result); } @@ -72,19 +72,19 @@ TestFuncResult test_str8_list_push_back(void) { Str8 s2 = wapp_str8_lit("2"); Str8 s3 = wapp_str8_lit("3"); - Str8List list = {0}; - Str8Node n1 = { .string = &s1 }; - Str8Node n2 = { .string = &s2 }; - Str8Node n3 = { .string = &s3 }; + DBL_LIST(Str8) list = {0}; + DBL_NODE(Str8) n1 = { .item = &s1 }; + DBL_NODE(Str8) n2 = { .item = &s2 }; + DBL_NODE(Str8) n3 = { .item = &s3 }; - wapp_str8_list_push_back(&list, &n1); - result = list.first == list.last && list.last == &n1 && list.last->string == &s1 && list.total_size == 1 && list.node_count == 1; + wapp_dbl_list_push_back(Str8, &list, &n1); + result = list.first == list.last && list.last == &n1 && list.last->item == &s1 && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1; - wapp_str8_list_push_back(&list, &n2); - result = result && list.last == &n2 && list.last->string == &s2 && list.total_size == 2 && list.node_count == 2; + wapp_dbl_list_push_back(Str8, &list, &n2); + result = result && list.last == &n2 && list.last->item == &s2 && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2; - wapp_str8_list_push_back(&list, &n3); - result = result && list.last == &n3 && list.last->string == &s3 && list.total_size == 3 && list.node_count == 3; + wapp_dbl_list_push_back(Str8, &list, &n3); + result = result && list.last == &n3 && list.last->item == &s3 && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3; return wapp_tester_result(result); } @@ -100,28 +100,28 @@ TestFuncResult test_str8_list_insert(void) { Str8 s6 = wapp_str8_lit("6"); Str8 s7 = wapp_str8_lit("7"); - Str8List list = {0}; - Str8Node n1 = { .string = &s1 }; - Str8Node n2 = { .string = &s2 }; - Str8Node n3 = { .string = &s3 }; - Str8Node n4 = { .string = &s4 }; - Str8Node n5 = { .string = &s5 }; - Str8Node n6 = { .string = &s6 }; - Str8Node n7 = { .string = &s7 }; + DBL_LIST(Str8) list = {0}; + DBL_NODE(Str8) n1 = { .item = &s1 }; + DBL_NODE(Str8) n2 = { .item = &s2 }; + DBL_NODE(Str8) n3 = { .item = &s3 }; + DBL_NODE(Str8) n4 = { .item = &s4 }; + DBL_NODE(Str8) n5 = { .item = &s5 }; + DBL_NODE(Str8) n6 = { .item = &s6 }; + DBL_NODE(Str8) n7 = { .item = &s7 }; - wapp_str8_list_push_back(&list, &n1); - wapp_str8_list_push_back(&list, &n2); - wapp_str8_list_push_back(&list, &n3); - wapp_str8_list_push_back(&list, &n4); - wapp_str8_list_push_back(&list, &n5); + wapp_dbl_list_push_back(Str8, &list, &n1); + wapp_dbl_list_push_back(Str8, &list, &n2); + wapp_dbl_list_push_back(Str8, &list, &n3); + wapp_dbl_list_push_back(Str8, &list, &n4); + wapp_dbl_list_push_back(Str8, &list, &n5); - Str8Node *node; - wapp_str8_list_insert(&list, &n6, 2); - node = wapp_str8_list_get(&list, 2); - result = node != NULL && node->string == &s6 && list.total_size == 6 && list.node_count == 6; - wapp_str8_list_insert(&list, &n7, 5); - node = wapp_str8_list_get(&list, 5); - result = result && node != NULL && node->string == &s7 && list.total_size == 7 && list.node_count == 7; + DBL_NODE(Str8) *node; + wapp_dbl_list_insert(Str8, &list, &n6, 2); + node = wapp_dbl_list_get(Str8, &list, 2); + result = node != NULL && node->item == &s6 && wapp_str8_list_total_size(&list) == 6 && list.node_count == 6; + wapp_dbl_list_insert(Str8, &list, &n7, 5); + node = wapp_dbl_list_get(Str8, &list, 5); + result = result && node != NULL && node->item == &s7 && wapp_str8_list_total_size(&list) == 7 && list.node_count == 7; return wapp_tester_result(result); } @@ -135,33 +135,33 @@ TestFuncResult test_str8_list_pop_front(void) { Str8 s4 = wapp_str8_lit("4"); Str8 s5 = wapp_str8_lit("5"); - Str8List list = {0}; - Str8Node n1 = { .string = &s1 }; - Str8Node n2 = { .string = &s2 }; - Str8Node n3 = { .string = &s3 }; - Str8Node n4 = { .string = &s4 }; - Str8Node n5 = { .string = &s5 }; + DBL_LIST(Str8) list = {0}; + DBL_NODE(Str8) n1 = { .item = &s1 }; + DBL_NODE(Str8) n2 = { .item = &s2 }; + DBL_NODE(Str8) n3 = { .item = &s3 }; + DBL_NODE(Str8) n4 = { .item = &s4 }; + DBL_NODE(Str8) n5 = { .item = &s5 }; - wapp_str8_list_push_back(&list, &n1); - wapp_str8_list_push_back(&list, &n2); - wapp_str8_list_push_back(&list, &n3); - wapp_str8_list_push_back(&list, &n4); - wapp_str8_list_push_back(&list, &n5); + wapp_dbl_list_push_back(Str8, &list, &n1); + wapp_dbl_list_push_back(Str8, &list, &n2); + wapp_dbl_list_push_back(Str8, &list, &n3); + wapp_dbl_list_push_back(Str8, &list, &n4); + wapp_dbl_list_push_back(Str8, &list, &n5); - Str8Node *node = wapp_str8_list_pop_front(&list); - result = node == &n1 && node->string == &s1 && wapp_str8_equal(node->string, &s1) && list.total_size == 4 && list.node_count == 4; + DBL_NODE(Str8) *node = wapp_dbl_list_pop_front(Str8, &list); + result = node == &n1 && node->item == &s1 && wapp_str8_equal(node->item, &s1) && wapp_str8_list_total_size(&list) == 4 && list.node_count == 4; - node = wapp_str8_list_pop_front(&list); - result = result && node == &n2 && node->string == &s2 && wapp_str8_equal(node->string, &s2) && list.total_size == 3 && list.node_count == 3; + node = wapp_dbl_list_pop_front(Str8, &list); + result = result && node == &n2 && node->item == &s2 && wapp_str8_equal(node->item, &s2) && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3; - node = wapp_str8_list_pop_front(&list); - result = result && node == &n3 && node->string == &s3 && wapp_str8_equal(node->string, &s3) && list.total_size == 2 && list.node_count == 2; + node = wapp_dbl_list_pop_front(Str8, &list); + result = result && node == &n3 && node->item == &s3 && wapp_str8_equal(node->item, &s3) && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2; - node = wapp_str8_list_pop_front(&list); - result = result && node == &n4 && node->string == &s4 && wapp_str8_equal(node->string, &s4) && list.total_size == 1 && list.node_count == 1; + node = wapp_dbl_list_pop_front(Str8, &list); + result = result && node == &n4 && node->item == &s4 && wapp_str8_equal(node->item, &s4) && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1; - node = wapp_str8_list_pop_front(&list); - result = result && node == &n5 && node->string == &s5 && wapp_str8_equal(node->string, &s5) && list.total_size == 0 && list.node_count == 0; + node = wapp_dbl_list_pop_front(Str8, &list); + result = result && node == &n5 && node->item == &s5 && wapp_str8_equal(node->item, &s5) && wapp_str8_list_total_size(&list) == 0 && list.node_count == 0; return wapp_tester_result(result); } @@ -175,33 +175,33 @@ TestFuncResult test_str8_list_pop_back(void) { Str8 s4 = wapp_str8_lit("4"); Str8 s5 = wapp_str8_lit("5"); - Str8List list = {0}; - Str8Node n1 = { .string = &s1 }; - Str8Node n2 = { .string = &s2 }; - Str8Node n3 = { .string = &s3 }; - Str8Node n4 = { .string = &s4 }; - Str8Node n5 = { .string = &s5 }; + DBL_LIST(Str8) list = {0}; + DBL_NODE(Str8) n1 = { .item = &s1 }; + DBL_NODE(Str8) n2 = { .item = &s2 }; + DBL_NODE(Str8) n3 = { .item = &s3 }; + DBL_NODE(Str8) n4 = { .item = &s4 }; + DBL_NODE(Str8) n5 = { .item = &s5 }; - wapp_str8_list_push_front(&list, &n1); - wapp_str8_list_push_front(&list, &n2); - wapp_str8_list_push_front(&list, &n3); - wapp_str8_list_push_front(&list, &n4); - wapp_str8_list_push_front(&list, &n5); + wapp_dbl_list_push_front(Str8, &list, &n1); + wapp_dbl_list_push_front(Str8, &list, &n2); + wapp_dbl_list_push_front(Str8, &list, &n3); + wapp_dbl_list_push_front(Str8, &list, &n4); + wapp_dbl_list_push_front(Str8, &list, &n5); - Str8Node *node = wapp_str8_list_pop_back(&list); - result = node == &n1 && node->string == &s1 && wapp_str8_equal(node->string, &s1) && list.total_size == 4 && list.node_count == 4; + DBL_NODE(Str8) *node = wapp_dbl_list_pop_back(Str8, &list); + result = node == &n1 && node->item == &s1 && wapp_str8_equal(node->item, &s1) && wapp_str8_list_total_size(&list) == 4 && list.node_count == 4; - node = wapp_str8_list_pop_back(&list); - result = result && node == &n2 && node->string == &s2 && wapp_str8_equal(node->string, &s2) && list.total_size == 3 && list.node_count == 3; + node = wapp_dbl_list_pop_back(Str8, &list); + result = result && node == &n2 && node->item == &s2 && wapp_str8_equal(node->item, &s2) && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3; - node = wapp_str8_list_pop_back(&list); - result = result && node == &n3 && node->string == &s3 && wapp_str8_equal(node->string, &s3) && list.total_size == 2 && list.node_count == 2; + node = wapp_dbl_list_pop_back(Str8, &list); + result = result && node == &n3 && node->item == &s3 && wapp_str8_equal(node->item, &s3) && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2; - node = wapp_str8_list_pop_back(&list); - result = result && node == &n4 && node->string == &s4 && wapp_str8_equal(node->string, &s4) && list.total_size == 1 && list.node_count == 1; + node = wapp_dbl_list_pop_back(Str8, &list); + result = result && node == &n4 && node->item == &s4 && wapp_str8_equal(node->item, &s4) && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1; - node = wapp_str8_list_pop_back(&list); - result = result && node == &n5 && node->string == &s5 && wapp_str8_equal(node->string, &s5) && list.total_size == 0 && list.node_count == 0; + node = wapp_dbl_list_pop_back(Str8, &list); + result = result && node == &n5 && node->item == &s5 && wapp_str8_equal(node->item, &s5) && wapp_str8_list_total_size(&list) == 0 && list.node_count == 0; return wapp_tester_result(result); } @@ -215,33 +215,33 @@ TestFuncResult test_str8_list_remove(void) { Str8 s4 = wapp_str8_lit("4"); Str8 s5 = wapp_str8_lit("5"); - Str8List list = {0}; - Str8Node n1 = { .string = &s1 }; - Str8Node n2 = { .string = &s2 }; - Str8Node n3 = { .string = &s3 }; - Str8Node n4 = { .string = &s4 }; - Str8Node n5 = { .string = &s5 }; + DBL_LIST(Str8) list = {0}; + DBL_NODE(Str8) n1 = { .item = &s1 }; + DBL_NODE(Str8) n2 = { .item = &s2 }; + DBL_NODE(Str8) n3 = { .item = &s3 }; + DBL_NODE(Str8) n4 = { .item = &s4 }; + DBL_NODE(Str8) n5 = { .item = &s5 }; - wapp_str8_list_push_back(&list, &n1); - wapp_str8_list_push_back(&list, &n2); - wapp_str8_list_push_back(&list, &n3); - wapp_str8_list_push_back(&list, &n4); - wapp_str8_list_push_back(&list, &n5); + wapp_dbl_list_push_back(Str8, &list, &n1); + wapp_dbl_list_push_back(Str8, &list, &n2); + wapp_dbl_list_push_back(Str8, &list, &n3); + wapp_dbl_list_push_back(Str8, &list, &n4); + wapp_dbl_list_push_back(Str8, &list, &n5); - Str8Node *node = wapp_str8_list_remove(&list, 0); - result = node == &n1 && node->string == &s1 && wapp_str8_equal(node->string, &s1) && list.total_size == 4 && list.node_count == 4; + DBL_NODE(Str8) *node = wapp_dbl_list_remove(Str8, &list, 0); + result = node == &n1 && node->item == &s1 && wapp_str8_equal(node->item, &s1) && wapp_str8_list_total_size(&list) == 4 && list.node_count == 4; - node = wapp_str8_list_remove(&list, 0); - result = result && node == &n2 && node->string == &s2 && wapp_str8_equal(node->string, &s2) && list.total_size == 3 && list.node_count == 3; + node = wapp_dbl_list_remove(Str8, &list, 0); + result = result && node == &n2 && node->item == &s2 && wapp_str8_equal(node->item, &s2) && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3; - node = wapp_str8_list_remove(&list, 0); - result = result && node == &n3 && node->string == &s3 && wapp_str8_equal(node->string, &s3) && list.total_size == 2 && list.node_count == 2; + node = wapp_dbl_list_remove(Str8, &list, 0); + result = result && node == &n3 && node->item == &s3 && wapp_str8_equal(node->item, &s3) && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2; - node = wapp_str8_list_remove(&list, 0); - result = result && node == &n4 && node->string == &s4 && wapp_str8_equal(node->string, &s4) && list.total_size == 1 && list.node_count == 1; + node = wapp_dbl_list_remove(Str8, &list, 0); + result = result && node == &n4 && node->item == &s4 && wapp_str8_equal(node->item, &s4) && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1; - node = wapp_str8_list_remove(&list, 0); - result = result && node == &n5 && node->string == &s5 && wapp_str8_equal(node->string, &s5) && list.total_size == 0 && list.node_count == 0; + node = wapp_dbl_list_remove(Str8, &list, 0); + result = result && node == &n5 && node->item == &s5 && wapp_str8_equal(node->item, &s5) && wapp_str8_list_total_size(&list) == 0 && list.node_count == 0; return wapp_tester_result(result); } @@ -249,15 +249,15 @@ TestFuncResult test_str8_list_remove(void) { TestFuncResult test_str8_list_empty(void) { bool result; - Str8List list = {0}; - wapp_str8_list_push_back(&list, &wapp_str8_node_from_cstr("Hello")); - wapp_str8_list_push_back(&list, &wapp_str8_node_from_cstr("from")); - wapp_str8_list_push_back(&list, &wapp_str8_node_from_cstr("wizapp")); - wapp_str8_list_push_back(&list, &wapp_str8_node_from_cstr("stdlib")); + DBL_LIST(Str8) list = {0}; + wapp_dbl_list_push_back(Str8, &list, &wapp_str8_node_from_cstr("Hello")); + wapp_dbl_list_push_back(Str8, &list, &wapp_str8_node_from_cstr("from")); + wapp_dbl_list_push_back(Str8, &list, &wapp_str8_node_from_cstr("wizapp")); + wapp_dbl_list_push_back(Str8, &list, &wapp_str8_node_from_cstr("stdlib")); - wapp_str8_list_empty(&list); + wapp_dbl_list_empty(Str8, &list); - result = list.first == NULL && list.last == NULL && list.node_count == 0 && list.total_size == 0; + result = list.first == NULL && list.last == NULL && list.node_count == 0 && wapp_str8_list_total_size(&list) == 0; return wapp_tester_result(result); }