diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index 291953f..1d07d55 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -45,10 +45,12 @@ FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *curre FledastyError fledasty_utf8_string_pop(FledastyUtf8String *current_string); FledastyError fledasty_utf8_string_remove(FledastyUtf8String *current_string, unsigned char *character_string, const size_t character_string_size); FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_string, const size_t start_index, const size_t end_index); -FledastyError fledasty_utf8_string_clear(FledastyUtf8String *current_string); FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_string, unsigned char *replace_character_string, const size_t replace_character_string_size, unsigned char *character_string, const size_t character_string_size); +FledastyError fledasty_utf8_string_clear(FledastyUtf8String *current_string); +FledastyError fledasty_utf8_string_shrink_to_fit(FledastyUtf8String *current_string); + bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, const size_t character_string_size); static inline bool fledasty_utf8_string_is_empty(const FledastyUtf8String *current_string) { return current_string == NULL || current_string->size == 0; } diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index c4a86d2..5baa196 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -24,6 +24,7 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Fledasty/Strings/UTF8String.h" +#include "Fledasty/Utils/Error.h" #include #include @@ -238,17 +239,6 @@ FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_stri return FLEDASTY_ERROR_NONE; } -FledastyError fledasty_utf8_string_clear(FledastyUtf8String *current_string) { - if (current_string == NULL) { - return FLEDASTY_ERROR_INVALID_POINTER; - } - - current_string->size = 0; - current_string->character_string[0] = '\0'; - - return FLEDASTY_ERROR_NONE; -} - FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_string, unsigned char *replace_character_string, const size_t replace_character_string_size, unsigned char *character_string, const size_t character_string_size) { if (current_string == NULL || replace_character_string == NULL || replace_character_string_size == 0 || character_string == NULL || character_string_size == 0) { return FLEDASTY_ERROR_INVALID_POINTER; @@ -280,6 +270,33 @@ FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_st return FLEDASTY_ERROR_VALUE_NOT_FOUND; } +FledastyError fledasty_utf8_string_clear(FledastyUtf8String *current_string) { + if (current_string == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + current_string->size = 0; + current_string->character_string[0] = '\0'; + + return FLEDASTY_ERROR_NONE; +} + +FledastyError fledasty_utf8_string_shrink_to_fit(FledastyUtf8String *current_string) { + if (current_string == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + 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); + hallocy_copy_memory(current_string->character_string, previous_string, current_string->size); + current_string->character_string[current_string->size] = '\0'; + + hallocy_free(previous_string); + return FLEDASTY_ERROR_NONE; +} + bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, const size_t character_string_size) { if (current_string == NULL || character_string == NULL || character_string_size == 0) { return false; diff --git a/Tests/Main.c b/Tests/Main.c index 9ea11ef..b928c5a 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -258,6 +258,7 @@ int main() { fledasty_utf8_string_remove_range(&test_utf8_string, 0, 6); printf("Remove range: %s\n", test_utf8_string.character_string); + fledasty_utf8_string_shrink_to_fit(&test_utf8_string); if (fledasty_utf8_string_has_string(&test_utf8_string, (unsigned char*)"😀", 4)) { printf("String contains 😀!\n"); }