refactor(utf-8 string): changed utf-8 string to work the same as the other datastructures

This commit is contained in:
Mineplay 2025-07-11 04:19:40 -05:00
parent 4bf374e3b1
commit b5fd577504
3 changed files with 13 additions and 40 deletions

View file

@ -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);

View file

@ -30,33 +30,9 @@
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
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);
}

View file

@ -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;
}