refactor(utf-8 string): improved insert and removed loops and fixed size variable being one character to big

This commit is contained in:
Mineplay 2025-06-06 10:50:56 -05:00
parent c36696255d
commit ebafa37fad
4 changed files with 71 additions and 63 deletions

View file

@ -60,8 +60,7 @@ FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
HallocyError result = hallocy_free(current_string->character_string);
if (result != HALLOCY_ERROR_NONE) {
if (hallocy_free(current_string->character_string) != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
@ -79,11 +78,11 @@ FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, un
}
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;
current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size + 1;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
}
hallocy_copy_memory(current_string->character_string + (current_string->size - 1), character_string, character_string_size);
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';
@ -96,7 +95,7 @@ FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_s
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (index >= current_string->size) {
if (index > current_string->size) {
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
}
@ -113,6 +112,7 @@ FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_s
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size);
current_string->size += character_string_size;
current_string->character_string[current_string->size] = '\0';
return FLEDASTY_ERROR_NONE;
}
@ -127,23 +127,23 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr
size_t index = 0;
while (index < (current_string->size - before_character_string_size) && !hallocy_compare_memory(current_string->character_string + index, before_character_string, before_character_string_size)) {
if (hallocy_compare_memory(current_string->character_string + index, before_character_string, before_character_string_size)) {
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 = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
}
hallocy_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index);
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size);
current_string->size += character_string_size;
return FLEDASTY_ERROR_NONE;
}
index += 1;
}
if (index == current_string->size - before_character_string_size) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
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;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
}
hallocy_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index);
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size);
current_string->size += character_string_size;
return FLEDASTY_ERROR_NONE;
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *current_string, unsigned char *after_character_string, const size_t after_character_string_size, unsigned char *character_string, const size_t character_string_size) {
@ -156,25 +156,25 @@ FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *curre
}
size_t index = 0;
while (index < (current_string->size - after_character_string_size) && !hallocy_compare_memory(current_string->character_string + index, after_character_string, after_character_string_size)) {
while (index < (current_string->size - after_character_string_size)) {
if (hallocy_compare_memory(current_string->character_string + index, after_character_string, after_character_string_size)) {
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 = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
}
index += after_character_string_size;
hallocy_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index + 1);
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size);
current_string->size += character_string_size;
return FLEDASTY_ERROR_NONE;
}
index += 1;
}
if (index == current_string->size - after_character_string_size) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
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;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
}
index += after_character_string_size;
hallocy_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index);
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size);
current_string->size += character_string_size;
return FLEDASTY_ERROR_NONE;
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_utf8_string_pop(FledastyUtf8String *current_string) {
@ -192,7 +192,7 @@ FledastyError fledasty_utf8_string_pop(FledastyUtf8String *current_string) {
current_string->size -= 1;
}
current_string->character_string[current_string->size - 1] = '\0';
current_string->character_string[current_string->size] = '\0';
return FLEDASTY_ERROR_NONE;
}
@ -207,18 +207,19 @@ FledastyError fledasty_utf8_string_remove(FledastyUtf8String *current_string, un
}
size_t index = 0;
while (index < (current_string->size - character_string_size) && !hallocy_compare_memory(current_string->character_string + index, character_string, character_string_size)) {
while (index < (current_string->size - character_string_size)) {
if (hallocy_compare_memory(current_string->character_string + index, character_string, character_string_size)) {
hallocy_move_memory(current_string->character_string + index, current_string->character_string + index + character_string_size, current_string->size - (index + character_string_size));
current_string->size -= character_string_size;
current_string->character_string[current_string->size] = '\0';
return FLEDASTY_ERROR_NONE;
}
index += 1;
}
if (index == current_string->size - character_string_size) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
hallocy_move_memory(current_string->character_string + index, current_string->character_string + index + character_string_size, current_string->size - (index + character_string_size));
current_string->size -= character_string_size;
return FLEDASTY_ERROR_NONE;
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_string, const size_t start_index, const size_t end_index) {
@ -226,13 +227,14 @@ FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_stri
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (start_index > end_index || end_index > current_string->size) {
if (start_index >= end_index || end_index >= current_string->size) {
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
}
hallocy_move_memory(current_string->character_string + start_index, current_string->character_string + end_index, current_string->size - end_index);
current_string->size -= end_index - start_index;
current_string->size -= end_index - start_index;
current_string->character_string[current_string->size] = '\0';
return FLEDASTY_ERROR_NONE;
}
@ -257,19 +259,25 @@ FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_st
}
size_t index = 0;
while (index < current_string->size - replace_character_string_size && !hallocy_compare_memory(current_string->character_string + index, replace_character_string, replace_character_string_size)) {
while (index < current_string->size - replace_character_string_size) {
if (hallocy_compare_memory(current_string->character_string + index, replace_character_string, replace_character_string_size)) {
const size_t new_size = current_string->size + (character_string_size - replace_character_string_size);
if (current_string->capacity <= new_size) {
current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
}
hallocy_move_memory(current_string->character_string + index + character_string_size, current_string->character_string + index + replace_character_string_size, current_string->size - (index + replace_character_string_size));
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size);
current_string->size = new_size;
current_string->character_string[current_string->size] = '\0';
return FLEDASTY_ERROR_NONE;
}
index += 1;
}
if (index == current_string->size - replace_character_string_size) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
hallocy_move_memory(current_string->character_string + index + character_string_size, current_string->character_string + index + replace_character_string_size, current_string->size - (index + replace_character_string_size));
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size);
current_string->size += character_string_size - replace_character_string_size;
return FLEDASTY_ERROR_NONE;
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, const size_t character_string_size) {
@ -345,7 +353,7 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si
}
utf8_string.size = string_index;
if (utf8_string.capacity <= utf8_string.size + 1) {
if (utf8_string.capacity <= utf8_string.size) {
utf8_string.capacity += utf8_string.capacity;
utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
}

View file

@ -237,7 +237,7 @@ int main() {
fledasty_hash_table_destroy(&test_hash_table);
FledastyUtf8String test_utf8_string;
unsigned char *test_string = (unsigned char*)"😀€Testing";
fledasty_utf8_string_initialize(&test_utf8_string, test_string, 15);
fledasty_utf8_string_initialize(&test_utf8_string, test_string, 14);
printf("%s\n", test_string);
printf("%s\n", test_utf8_string.character_string);
@ -247,7 +247,7 @@ int main() {
printf("Insert Before: %s\n", test_utf8_string.character_string);
fledasty_utf8_string_insert_after_string(&test_utf8_string, (unsigned char*)"😀", 4, (unsigned char*)"Bye", 3);
printf("Insert After: %s\n", test_utf8_string.character_string);
fledasty_utf8_string_insert_at_index(&test_utf8_string, test_utf8_string.size - 1, (unsigned char*)"index", 5);
fledasty_utf8_string_insert_at_index(&test_utf8_string, test_utf8_string.size, (unsigned char*)"index", 5);
printf("Insert at Index: %s\n", test_utf8_string.character_string);
fledasty_utf8_string_replace_string(&test_utf8_string, (unsigned char*)"😀", 4, (unsigned char*)"𓃶 ", 5);
printf("Replace: %s\n", test_utf8_string.character_string);
@ -255,7 +255,7 @@ int main() {
printf("Pop: %s\n", test_utf8_string.character_string);
fledasty_utf8_string_remove(&test_utf8_string, (unsigned char*)"𓃶 ", 5);
printf("Remove: %s\n", test_utf8_string.character_string);
fledasty_utf8_string_remove_range(&test_utf8_string, 0, 5);
fledasty_utf8_string_remove_range(&test_utf8_string, 0, 6);
printf("Remove range: %s\n", test_utf8_string.character_string);
if (fledasty_utf8_string_has_string(&test_utf8_string, (unsigned char*)"😀", 4)) {