From 685682e1c800347692b08e2ec53bfbd3904e3432 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Mon, 7 Oct 2024 22:35:56 +0000 Subject: [PATCH] revamp-strings (#1) Co-authored-by: Abdelrahman Said Reviewed-on: https://git.thewizardapprentice.com/abdelrahman/wizapp-stdlib/pulls/1 Co-authored-by: Abdelrahman Co-committed-by: Abdelrahman --- .gitignore | 1 + compile | 6 +- .../strings/basic_strings/basic_strings.h | 33 --- src/core/strings/dstr/dstr.c | 191 ------------------ src/core/strings/dstr/dstr.h | 41 ---- src/core/strings/str8/str8.c | 27 +++ src/core/strings/str8/str8.h | 25 +++ src/os/cpath/cpath.c | 2 +- tests/cpath/test_cpath.c | 69 ++++--- tests/shell_commander/test_shell_commander.c | 2 +- 10 files changed, 94 insertions(+), 303 deletions(-) delete mode 100644 src/core/strings/basic_strings/basic_strings.h delete mode 100644 src/core/strings/dstr/dstr.c delete mode 100644 src/core/strings/dstr/dstr.h create mode 100644 src/core/strings/str8/str8.c create mode 100644 src/core/strings/str8/str8.h diff --git a/.gitignore b/.gitignore index 351e8c1..2853634 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ test test.* *.dSYM +.DS_Store *.pdb *.obj compile_commands.json diff --git a/compile b/compile index 0b32abf..75c8ad2 100755 --- a/compile +++ b/compile @@ -16,7 +16,7 @@ while [[ $# > 0 ]];do done CC=clang -CFLAGS="-Wall -Werror -pedantic " +CFLAGS="-Wall -Werror -pedantic" LIBFLAGS="-fPIC -shared" INCLUDE="$(find src -type d | xargs -I{} echo -n "-I{} ")" @@ -32,9 +32,9 @@ fi mkdir -p $BUILD_DIR if [[ $BUILD_TYPE == "release" ]]; then - CFLAGS+="-O3" + CFLAGS+=" -O3" else - CFLAGS+="-g -fsanitize=address -fsanitize=undefined" + CFLAGS+=" -g -fsanitize=address -fsanitize=undefined" fi OUT="$BUILD_DIR/libwapp.so" diff --git a/src/core/strings/basic_strings/basic_strings.h b/src/core/strings/basic_strings/basic_strings.h deleted file mode 100644 index 1c93c00..0000000 --- a/src/core/strings/basic_strings/basic_strings.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef BASIC_STRING_H -#define BASIC_STRING_H - -#include "aliases.h" -#include -#include - -#ifdef __cplusplus -BEGIN_C_LINKAGE -#endif // __cplusplus - -typedef struct bstr BasicString; -struct bstr { - u64 size; - const char *buf; -}; - -typedef struct strvw StringView; -struct strvw { - const u64 size; - const char *buf; -}; - -#define new_string(STR) { .size = strlen(STR), .buf = STR } -#define wapp_bstr_new(STR) ((BasicString)new_string(STR)) -#define wapp_strvw_new(STR) ((StringView)new_string(STR)) -#define wapp_string_print(STR) (printf("%.*s\n", (i32)STR.size, STR.buf)) - -#ifdef __cplusplus -END_C_LINKAGE -#endif // __cplusplus - -#endif // !BASIC_STRING_H diff --git a/src/core/strings/dstr/dstr.c b/src/core/strings/dstr/dstr.c deleted file mode 100644 index a583db9..0000000 --- a/src/core/strings/dstr/dstr.c +++ /dev/null @@ -1,191 +0,0 @@ -#include "dstr.h" -#include "aliases.h" -#include "mem_arena.h" -#include "platform.h" -#include -#include -#include - -// Use this scalar to allocate extra memory in order to avoid having to -// constantly reallocate -#define CAPACITY_SCALAR 8 -#define MINIMUM_DSTR_CAPACITY 1024 - -struct dstr { - u64 capacity; - u64 size; - -#ifdef WAPP_PLATFORM_WINDOWS - char *buf; -#else - char buf[]; -#endif // WAPP_PLATFORM_WINDOWS -}; - -String *wapp_dstr_with_capacity(u64 capacity, Arena *arena) { - if (!arena) { - return NULL; - } - - String *out = - (String *)wapp_mem_arena_alloc(arena, sizeof(String) + capacity + 1); - if (!out) { - return NULL; - } - - out->capacity = capacity; - out->size = 0; - - return out; -} - -String *wapp_dstr_from_string(const char *str, Arena *arena) { - if (!str || !arena) { - return NULL; - } - - u64 length = strlen(str); - u64 capacity = length * CAPACITY_SCALAR; - capacity = - capacity >= MINIMUM_DSTR_CAPACITY ? capacity : MINIMUM_DSTR_CAPACITY; - - String *out = wapp_dstr_with_capacity(capacity, arena); - if (!out) { - return NULL; - } - - out->size = length; - strncpy(out->buf, str, length + 1); - - return out; -} - -StringUpdate wapp_dstr_update(String **dst, const char *src, Arena *arena) { - if (!dst || !(*dst)) { - return (StringUpdate){.updated = false, .str = *dst}; - } - - u64 length = strlen(src); - - String *str = *dst; - - if (length >= str->capacity) { - if (!arena) { - return (StringUpdate){.updated = false, .str = *dst}; - } - - String *new_str = wapp_dstr_from_string(src, arena); - if (!new_str) { - return (StringUpdate){.updated = false, .str = *dst}; - } - - return (StringUpdate){.updated = true, .str = new_str}; - } - - memset(str->buf, 0, str->capacity); - - str->size = length; - - strncpy(str->buf, src, length + 1); - - return (StringUpdate){.updated = true, .str = *dst}; -} - -StringUpdate wapp_dstr_concat(String **dst, const char *src, Arena *arena) { - if (!dst || !(*dst)) { - return (StringUpdate){.updated = false, .str = *dst}; - } - - u64 src_length = strlen(src); - if (src_length == 0) { - return (StringUpdate){.updated = false, .str = *dst}; - } - - u64 new_length = (*dst)->size + src_length; - -#ifdef WAPP_PLATFORM_WINDOWS - char *str = - wapp_mem_util_alloc(NULL, new_length + 1, WAPP_MEM_ACCESS_READ_WRITE, - WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, - WAPP_MEM_INIT_INITIALISED); - - if (!str) { - return (StringUpdate){.updated = false, .str = *dst}; - } -#else - char str[new_length + 1]; - memset(str, 0, new_length + 1); -#endif /* ifdef WAPP_PLATFORM_WINDOWS */ - - strncpy(str, (*dst)->buf, (*dst)->size); - strncat(str, src, new_length + 1 - (*dst)->size); - - return wapp_dstr_update(dst, str, arena); -} - -void wapp_dstr_clear(String *str) { - if (!str || str->size == 0) { - return; - } - - memset(str->buf, 0, str->capacity); - str->size = 0; -} - -void wapp_dstr_print(const String *str) { - if (!str) { - return; - } - - printf("%.*s\n", (i32)str->size, str->buf); -} - -i64 wapp_dstr_find(const String *str, const char *substr) { - if (!str || !substr) { - return -1; - } - - u64 substr_length = strlen(substr); - if (substr_length == 0 || substr_length > str->size) { - return -1; - } - - const char *s1; - for (u64 i = 0; i < str->size; ++i) { - if (i + substr_length > str->size) { - break; - } - - s1 = &(str->buf[i]); - - if (strncmp(s1, substr, substr_length) == 0) { - return i; - } - } - - return -1; -} - -u64 wapp_dstr_length(const String *str) { - if (!str) { - return 0; - } - - return str->size; -} - -u64 wapp_dstr_capacity(const String *str) { - if (!str) { - return 0; - } - - return str->capacity; -} - -const char *wapp_dstr_to_cstr(const String *str) { - if (!str) { - return ""; - } - - return str->buf; -} diff --git a/src/core/strings/dstr/dstr.h b/src/core/strings/dstr/dstr.h deleted file mode 100644 index a6fd83e..0000000 --- a/src/core/strings/dstr/dstr.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef DSTR_H -#define DSTR_H - -#include "aliases.h" -#include "mem_arena.h" -#include "misc_utils.h" -#include "platform.h" -#include - -#ifdef __cplusplus -BEGIN_C_LINKAGE -#endif // __cplusplus - -typedef struct dstr String; - -typedef struct string_update StringUpdate; -struct string_update { - String *str; - bool updated; - -#ifdef WAPP_PLATFORM_WINDOWS - wapp_misc_utils_padding_size(sizeof(bool) + sizeof(String *)); -#endif // WAPP_PLATFORM_WINDOWS -}; - -String *wapp_dstr_with_capacity(u64 capacity, Arena *arena); -String *wapp_dstr_from_string(const char *str, Arena *arena); -StringUpdate wapp_dstr_update(String **dst, const char *src, Arena *arena); -StringUpdate wapp_dstr_concat(String **dst, const char *src, Arena *arena); -void wapp_dstr_clear(String *str); -void wapp_dstr_print(const String *str); -i64 wapp_dstr_find(const String *str, const char *substr); -u64 wapp_dstr_length(const String *str); -u64 wapp_dstr_capacity(const String *str); -const char *wapp_dstr_to_cstr(const String *str); - -#ifdef __cplusplus -END_C_LINKAGE -#endif // __cplusplus - -#endif // !DSTR_H diff --git a/src/core/strings/str8/str8.c b/src/core/strings/str8/str8.c new file mode 100644 index 0000000..faa4355 --- /dev/null +++ b/src/core/strings/str8/str8.c @@ -0,0 +1,27 @@ +#include "str8.h" +#include + +Str8 wapp_str8_lit(char *str) { + if (!str) { + return (Str8){.capacity = 0, .size = 0, .buf = (u8 *)""}; + } + + u64 size = strlen(str); + return (Str8){.capacity = size, .size = size, .buf = (u8 *)str}; +} + +char wapp_str8_get(const Str8 *str, u64 index) { + if (index >= str->size) { + return '\0'; + } + + return (char)(str->buf[index]); +} + +void wapp_str8_set(Str8 *str, u64 index, char c) { + if (index >= str->size) { + return; + } + + str->buf[index] = (u8)c; +} diff --git a/src/core/strings/str8/str8.h b/src/core/strings/str8/str8.h new file mode 100644 index 0000000..3f26b2d --- /dev/null +++ b/src/core/strings/str8/str8.h @@ -0,0 +1,25 @@ +#ifndef STR8_H +#define STR8_H + +#include "aliases.h" + +#ifdef __cplusplus +BEGIN_C_LINKAGE +#endif // !__cplusplus + +typedef struct str8 Str8; +struct str8 { + u64 capacity; + u64 size; + u8 *buf; +}; + +Str8 wapp_str8_lit(char *str); +char wapp_str8_get(const Str8 *str, u64 index); +void wapp_str8_set(Str8 *str, u64 index, char c); + +#ifdef __cplusplus +END_C_LINKAGE +#endif // !__cplusplus + +#endif // !STR8_H diff --git a/src/os/cpath/cpath.c b/src/os/cpath/cpath.c index 6629d1c..02057c4 100644 --- a/src/os/cpath/cpath.c +++ b/src/os/cpath/cpath.c @@ -47,7 +47,7 @@ void dirup(char *dst, u64 levels, const char *path) { } char tmp[256]; - sprintf(tmp, "%c", PATH_SEP); + snprintf(tmp, 2, "%c", PATH_SEP); // NOTE (Abdelrahman): Conditions stored in variables to silence MSVC warning C5045 bool insufficient_levels = sep_count < levels; bool path_to_copy_is_separator = strncmp(path, tmp, copy_count) != 0; diff --git a/tests/cpath/test_cpath.c b/tests/cpath/test_cpath.c index 91f6970..4fd2928 100644 --- a/tests/cpath/test_cpath.c +++ b/tests/cpath/test_cpath.c @@ -5,13 +5,16 @@ #include #include -TestFuncResult test_cpath_join_path(void) { - char expected[4096] = {0}; - char out[4096] = {0}; - char tmp[1024] = {0}; +#define MAIN_BUF_SIZE 4096 +#define TMP_BUF_SIZE 1024 - sprintf(expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); - sprintf(tmp, "%c", PATH_SEP); +TestFuncResult test_cpath_join_path(void) { + char expected[MAIN_BUF_SIZE] = {0}; + char out[MAIN_BUF_SIZE] = {0}; + char tmp[TMP_BUF_SIZE] = {0}; + + snprintf(expected, MAIN_BUF_SIZE, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); + snprintf(tmp, 2, "%c", PATH_SEP); wapp_cpath_join_path(out, tmp, "home", "abdelrahman", "Documents"); bool result = strcmp(out, expected) == 0; @@ -20,7 +23,7 @@ TestFuncResult test_cpath_join_path(void) { } memset(out, 0, strlen(out)); - sprintf(expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); + snprintf(expected, MAIN_BUF_SIZE, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); wapp_cpath_join_path(out, "home", "abdelrahman", "Documents"); result = result && strcmp(out, expected) == 0; @@ -30,8 +33,8 @@ TestFuncResult test_cpath_join_path(void) { memset(out, 0, strlen(out)); memset(tmp, 0, strlen(tmp)); - sprintf(expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); - sprintf(tmp, "%chome", PATH_SEP); + snprintf(expected, MAIN_BUF_SIZE, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "%chome", PATH_SEP); wapp_cpath_join_path(out, tmp, "abdelrahman", "Documents"); result = result && strcmp(out, expected) == 0; @@ -41,8 +44,8 @@ TestFuncResult test_cpath_join_path(void) { memset(out, 0, strlen(out)); memset(tmp, 0, strlen(tmp)); - sprintf(expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); - sprintf(tmp, "home%c", PATH_SEP); + snprintf(expected, MAIN_BUF_SIZE, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "home%c", PATH_SEP); wapp_cpath_join_path(out, tmp, "abdelrahman", "Documents"); result = result && strcmp(out, expected) == 0; @@ -52,8 +55,8 @@ TestFuncResult test_cpath_join_path(void) { memset(out, 0, strlen(out)); memset(tmp, 0, strlen(tmp)); - sprintf(expected, "%chome", PATH_SEP); - sprintf(tmp, "%chome", PATH_SEP); + snprintf(expected, MAIN_BUF_SIZE, "%chome", PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "%chome", PATH_SEP); wapp_cpath_join_path(out, tmp, ""); result = result && strcmp(out, expected) == 0; @@ -62,7 +65,7 @@ TestFuncResult test_cpath_join_path(void) { } memset(out, 0, strlen(out)); - sprintf(expected, ""); + snprintf(expected, 1, ""); wapp_cpath_join_path(out, "", ""); result = result && strcmp(out, expected) == 0; @@ -71,7 +74,7 @@ TestFuncResult test_cpath_join_path(void) { } memset(out, 0, strlen(out)); - sprintf(expected, "home"); + snprintf(expected, MAIN_BUF_SIZE, "home"); wapp_cpath_join_path(out, "", "home"); result = result && strcmp(out, expected) == 0; @@ -81,11 +84,11 @@ TEST_JOIN_PATH_EXIT: } TestFuncResult test_cpath_dirname(void) { - char dst[4096] = {0}; - char expected[4096] = {0}; - char tmp[1024] = {0}; + char dst[MAIN_BUF_SIZE] = {0}; + char expected[MAIN_BUF_SIZE] = {0}; + char tmp[TMP_BUF_SIZE] = {0}; - sprintf(tmp, "%c", PATH_SEP); + snprintf(tmp, 2, "%c", PATH_SEP); wapp_cpath_dirname(dst, tmp); bool result = strcmp(dst, tmp) == 0; if (!result) { @@ -107,8 +110,8 @@ TestFuncResult test_cpath_dirname(void) { } memset(dst, 0, strlen(dst)); - sprintf(tmp, "%chome%ctest", PATH_SEP, PATH_SEP); - sprintf(expected, "%chome", PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "%chome%ctest", PATH_SEP, PATH_SEP); + snprintf(expected, MAIN_BUF_SIZE, "%chome", PATH_SEP); wapp_cpath_dirname(dst, tmp); result = strcmp(dst, expected) == 0; if (!result) { @@ -118,8 +121,8 @@ TestFuncResult test_cpath_dirname(void) { memset(dst, 0, strlen(dst)); memset(tmp, 0, strlen(tmp)); memset(expected, 0, strlen(expected)); - sprintf(tmp, "%chome%ctest%c", PATH_SEP, PATH_SEP, PATH_SEP); - sprintf(expected, "%chome", PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "%chome%ctest%c", PATH_SEP, PATH_SEP, PATH_SEP); + snprintf(expected, MAIN_BUF_SIZE, "%chome", PATH_SEP); wapp_cpath_dirname(dst, tmp); result = strcmp(dst, expected) == 0; @@ -128,11 +131,11 @@ TEST_DIRNAME_EXIT: } TestFuncResult test_cpath_dirup(void) { - char dst[4096] = {0}; - char expected[4096] = {0}; - char tmp[1024] = {0}; + char dst[MAIN_BUF_SIZE] = {0}; + char expected[MAIN_BUF_SIZE] = {0}; + char tmp[TMP_BUF_SIZE] = {0}; - sprintf(tmp, "%c", PATH_SEP); + snprintf(tmp, 2, "%c", PATH_SEP); wapp_cpath_dirup(dst, 3, tmp); bool result = strcmp(dst, tmp) == 0; if (!result) { @@ -141,8 +144,8 @@ TestFuncResult test_cpath_dirup(void) { memset(dst, 0, strlen(dst)); memset(tmp, 0, strlen(tmp)); - sprintf(tmp, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); - sprintf(expected, "%c", PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); + snprintf(expected, MAIN_BUF_SIZE, "%c", PATH_SEP); wapp_cpath_dirup(dst, 3, tmp); result = strcmp(dst, expected) == 0; if (!result) { @@ -151,7 +154,7 @@ TestFuncResult test_cpath_dirup(void) { memset(dst, 0, strlen(dst)); memset(tmp, 0, strlen(tmp)); - sprintf(tmp, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); wapp_cpath_dirup(dst, 3, tmp); result = strcmp(dst, ".") == 0; if (!result) { @@ -161,8 +164,8 @@ TestFuncResult test_cpath_dirup(void) { memset(dst, 0, strlen(dst)); memset(tmp, 0, strlen(tmp)); memset(expected, 0, strlen(expected)); - sprintf(tmp, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); - sprintf(expected, "%chome", PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); + snprintf(expected, MAIN_BUF_SIZE, "%chome", PATH_SEP); wapp_cpath_dirup(dst, 2, tmp); result = strcmp(dst, expected) == 0; if (!result) { @@ -171,7 +174,7 @@ TestFuncResult test_cpath_dirup(void) { memset(dst, 0, strlen(dst)); memset(tmp, 0, strlen(tmp)); - sprintf(tmp, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); wapp_cpath_dirup(dst, 2, tmp); result = strcmp(dst, "home") == 0; diff --git a/tests/shell_commander/test_shell_commander.c b/tests/shell_commander/test_shell_commander.c index 0df9655..84e4060 100644 --- a/tests/shell_commander/test_shell_commander.c +++ b/tests/shell_commander/test_shell_commander.c @@ -27,7 +27,7 @@ TestFuncResult test_commander_cmd_out_buf_success(void) { char expected_output[64] = {0}; const char *msg = "hello world"; u64 length = strlen(msg); - sprintf(expected_output, "%s\n", msg); + snprintf(expected_output, length + 2, "%s\n", msg); CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, buf, 64, "echo", msg); bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&