diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index a75699d..291953f 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -56,4 +56,4 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si uint32_t *fledasty_utf8_string_decode(const FledastyUtf8String *current_string, size_t *unicode_string_size); bool fledasty_utf8_string_validate(unsigned char *character_string, const size_t character_string_size); -size_t fledasty_utf8_string_get_size(const unsigned char *character_string); \ No newline at end of file +size_t fledasty_utf8_string_get_size(const unsigned char *character_string); diff --git a/Include/Fledasty/Utils/Error.h b/Include/Fledasty/Utils/Error.h index e9841fd..ce48894 100644 --- a/Include/Fledasty/Utils/Error.h +++ b/Include/Fledasty/Utils/Error.h @@ -33,4 +33,4 @@ typedef enum { FLEDASTY_ERROR_INVALID_VALUE = 6, } FledastyError; -#endif \ No newline at end of file +#endif diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index 3d18f49..c4a86d2 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -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); } diff --git a/Tests/Main.c b/Tests/Main.c index fab8be1..9ea11ef 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -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)) {