From c64a4a201161e622ae1d549e78d5a4b6d7328ce6 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 06:14:40 -0500 Subject: [PATCH] feat(string): implemented append function --- Include/Fledasty/Strings/String.h | 2 ++ Src/Strings/String.c | 22 ++++++++++++++++++++++ Tests/Main.c | 2 ++ 3 files changed, 26 insertions(+) diff --git a/Include/Fledasty/Strings/String.h b/Include/Fledasty/Strings/String.h index b4ae353..d27e3f9 100644 --- a/Include/Fledasty/Strings/String.h +++ b/Include/Fledasty/Strings/String.h @@ -38,4 +38,6 @@ typedef struct { FledastyError fledasty_string_free(FledastyString *current_string); +FledastyError fledasty_string_append(FledastyString *current_string, char *character_string, const size_t character_string_size); + #endif diff --git a/Src/Strings/String.c b/Src/Strings/String.c index 6caa733..891fe71 100644 --- a/Src/Strings/String.c +++ b/Src/Strings/String.c @@ -41,3 +41,25 @@ FledastyError fledasty_string_free(FledastyString *current_string) { current_string->character_string = NULL; return FLEDASTY_ERROR_NONE; } + +FledastyError fledasty_string_append(FledastyString *current_string, char *character_string, const size_t character_string_size) { + if (current_string == NULL || character_string == NULL || character_string_size == 0) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + if (current_string->capacity <= current_string->size + character_string_size) { + current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size + 1; + current_string->character_string = (char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char)); + + if (current_string->character_string == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + } + + hallocy_copy_memory(current_string->character_string + current_string->size, character_string, character_string_size); + + current_string->size += character_string_size; + current_string->character_string[current_string->size] = '\0'; + + return FLEDASTY_ERROR_NONE; +} diff --git a/Tests/Main.c b/Tests/Main.c index 0ad08ae..13e1e6b 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -284,6 +284,8 @@ int main() { fledasty_utf8_string_free(&test_utf8_string); FledastyString normal_test_string = { 0, 0, NULL }; + fledasty_string_append(&normal_test_string, "Testing", 7); + printf("%s\n", normal_test_string.character_string); fledasty_string_free(&normal_test_string); printf("Done\n");