From a15cd07d38295effb1c6ebe9c4cd9fee0c9fd326 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 06:44:59 -0500 Subject: [PATCH] feat(string): implemented shrink to fit --- Include/Fledasty/Strings/String.h | 1 + Src/Strings/String.c | 23 +++++++++++++++++++++++ Src/Strings/UTF8String.c | 9 ++++++--- Tests/Main.c | 2 ++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Include/Fledasty/Strings/String.h b/Include/Fledasty/Strings/String.h index 16ac40e..1d07577 100644 --- a/Include/Fledasty/Strings/String.h +++ b/Include/Fledasty/Strings/String.h @@ -50,6 +50,7 @@ FledastyError fledasty_string_remove_range(FledastyString *current_string, const FledastyError fledasty_string_replace_string(FledastyString *current_string, char *replace_character_string, const size_t replace_character_string_size, char *character_string, const size_t character_string_size); FledastyError fledasty_string_clear(FledastyString *current_string); +FledastyError fledasty_string_shrink_to_fit(FledastyString *current_string); static inline bool fledasty_string_is_empty(const FledastyString *current_string) { return current_string == NULL || current_string->size == 0; } diff --git a/Src/Strings/String.c b/Src/Strings/String.c index 475de31..5b2bde3 100644 --- a/Src/Strings/String.c +++ b/Src/Strings/String.c @@ -248,3 +248,26 @@ FledastyError fledasty_string_clear(FledastyString *current_string) { return FLEDASTY_ERROR_NONE; } + +FledastyError fledasty_string_shrink_to_fit(FledastyString *current_string) { + if (current_string == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + char *previous_string = current_string->character_string; + + current_string->capacity = current_string->size + 1; + current_string->character_string = (char*)hallocy_malloc(current_string->capacity * sizeof(char*)); + if (current_string->character_string == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + hallocy_copy_memory(current_string->character_string, previous_string, current_string->size); + current_string->character_string[current_string->size] = '\0'; + + if (hallocy_free(previous_string) != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + return FLEDASTY_ERROR_NONE; +} diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index ef7e4e2..62bc3af 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -266,7 +266,7 @@ FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_st return FLEDASTY_ERROR_NONE; } - + index += 1; } @@ -292,7 +292,7 @@ FledastyError fledasty_utf8_string_shrink_to_fit(FledastyUtf8String *current_str unsigned char *previous_string = current_string->character_string; current_string->capacity = current_string->size + 1; - current_string->character_string = (unsigned char*)hallocy_malloc(current_string->capacity); + current_string->character_string = (unsigned char*)hallocy_malloc(current_string->capacity * sizeof(unsigned char*)); if (current_string->character_string == NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } @@ -300,7 +300,10 @@ FledastyError fledasty_utf8_string_shrink_to_fit(FledastyUtf8String *current_str hallocy_copy_memory(current_string->character_string, previous_string, current_string->size); current_string->character_string[current_string->size] = '\0'; - hallocy_free(previous_string); + if (hallocy_free(previous_string) != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + return FLEDASTY_ERROR_NONE; } diff --git a/Tests/Main.c b/Tests/Main.c index f144e43..3609abf 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -302,6 +302,8 @@ int main() { fledasty_string_replace_string(&normal_test_string, "Hello", 5, "Goodbye", 7); printf("Replace: %s\n", normal_test_string.character_string); + fledasty_string_shrink_to_fit(&normal_test_string); + fledasty_string_clear(&normal_test_string); if (fledasty_string_is_empty(&normal_test_string)) { printf("String is empty!\n");