From b5fd577504671030c1278934a9a7db6c6d4af9d8 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 04:19:40 -0500 Subject: [PATCH] refactor(utf-8 string): changed utf-8 string to work the same as the other datastructures --- Include/Fledasty/Strings/UTF8String.h | 3 +- Src/Strings/UTF8String.c | 42 +++++---------------------- Tests/Main.c | 8 ++--- 3 files changed, 13 insertions(+), 40 deletions(-) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index cf7915c..7b52c30 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -37,8 +37,7 @@ typedef struct { unsigned char *character_string; } FledastyUtf8String; -FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, const size_t character_string_size); -FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string); +FledastyError fledasty_utf8_string_free(FledastyUtf8String *current_string); FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, unsigned char *character_string, const size_t character_string_size); FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_string, size_t index, unsigned char *character_string, const size_t character_string_size); diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index 5baa196..be36e34 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -30,33 +30,9 @@ #include #include -FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, const size_t character_string_size) { - if (new_string == NULL) { - return FLEDASTY_ERROR_INVALID_POINTER; - } +static const size_t FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE = 16; - if (character_string == NULL || character_string_size == 0) { - new_string->size = 0; - new_string->capacity = 10; - - new_string->character_string = (unsigned char*)hallocy_malloc(new_string->capacity); - } else { - if (!fledasty_utf8_string_validate(character_string, character_string_size)) { - return FLEDASTY_ERROR_INVALID_VALUE; - } - - new_string->size = character_string_size; - new_string->capacity = new_string->size + new_string->size; - - new_string->character_string = (unsigned char*)hallocy_malloc(new_string->capacity); - hallocy_copy_memory(new_string->character_string, character_string, character_string_size); - } - - new_string->character_string[new_string->size] = '\0'; - return FLEDASTY_ERROR_NONE; -} - -FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string) { +FledastyError fledasty_utf8_string_free(FledastyUtf8String *current_string) { if (current_string == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } @@ -319,9 +295,7 @@ bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, u } FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const size_t size) { - FledastyUtf8String utf8_string; - fledasty_utf8_string_initialize(&utf8_string, NULL, 0); - + FledastyUtf8String utf8_string = { 0, 0, NULL }; if (unicode == NULL) { return utf8_string; } @@ -330,7 +304,7 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si for (size_t index = 0; index < size; index += 1) { if (unicode[index] <= 0x00007F) { if (utf8_string.capacity <= string_index) { - utf8_string.capacity += utf8_string.capacity; + utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE; utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity); } @@ -338,7 +312,7 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si string_index += 1; } else if (unicode[index] <= 0x0007FF) { if (utf8_string.capacity <= string_index + 2) { - utf8_string.capacity += utf8_string.capacity; + utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE; utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity); } @@ -347,7 +321,7 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si string_index += 2; } else if (unicode[index] <= 0x00FFFF) { if (utf8_string.capacity <= string_index + 3) { - utf8_string.capacity += utf8_string.capacity; + utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE; utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity); } @@ -357,7 +331,7 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si string_index += 3; } else if (unicode[index] <= 0x10FFFF) { if (utf8_string.capacity <= string_index + 4) { - utf8_string.capacity += utf8_string.capacity; + utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE; utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity); } @@ -371,7 +345,7 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si utf8_string.size = string_index; if (utf8_string.capacity <= utf8_string.size) { - utf8_string.capacity += utf8_string.capacity; + utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE; utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity); } diff --git a/Tests/Main.c b/Tests/Main.c index 8b66987..264e5d2 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -216,9 +216,9 @@ int main() { fledasty_hash_table_int_int_free(&test_hash_table); - FledastyUtf8String test_utf8_string; + FledastyUtf8String test_utf8_string = { 0, 0, NULL }; unsigned char *test_string = (unsigned char*)"đŸ˜€â‚¬Testing"; - fledasty_utf8_string_initialize(&test_utf8_string, test_string, 14); + fledasty_utf8_string_append(&test_utf8_string, test_string, 14); printf("%s\n", test_string); printf("%s\n", test_utf8_string.character_string); @@ -278,9 +278,9 @@ int main() { hallocy_free(invalid_utf8); hallocy_free(unicode); - fledasty_utf8_string_destroy(&encoded_string); + fledasty_utf8_string_free(&encoded_string); - fledasty_utf8_string_destroy(&test_utf8_string); + fledasty_utf8_string_free(&test_utf8_string); printf("Done\n"); return 0; } \ No newline at end of file