diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index 7d3d077..310781c 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -38,6 +38,7 @@ FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, un FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string); FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_length); +FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *current_string, unsigned char *before_character_string, size_t before_character_string_length, unsigned char *character_string, size_t character_string_length); FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, const size_t size); uint32_t *fledasty_utf8_string_decode(FledastyUtf8String *current_string, size_t *unicode_string_length); diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index f2aa758..4d838ec 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -78,8 +78,12 @@ FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, un return FLEDASTY_ERROR_INVALID_POINTER; } + if (!fledasty_utf8_string_validate(character_string, character_string_length)) { + return FLEDASTY_ERROR_INVALID_VALUE; + } + if (current_string->capacity <= current_string->size + character_string_length) { - current_string->capacity += current_string->capacity; + current_string->capacity += (current_string->capacity > character_string_length) ? current_string->capacity : character_string_length; current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char)); } @@ -91,6 +95,36 @@ FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, un return FLEDASTY_ERROR_NONE; } +FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *current_string, unsigned char *before_character_string, size_t before_character_string_length, unsigned char *character_string, size_t character_string_length) { + if (current_string == NULL || before_character_string == NULL || before_character_string_length == 0 || character_string == NULL || character_string_length == 0) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + if (!fledasty_utf8_string_validate(before_character_string, before_character_string_length) || !fledasty_utf8_string_validate(character_string, character_string_length)) { + return FLEDASTY_ERROR_INVALID_VALUE; + } + + size_t index = 0; + while (index < (current_string->size - before_character_string_length) && !hallocy_compare_memory(current_string->character_string + index, before_character_string, before_character_string_length)) { + index += 1; + } + + if (index == current_string->size - before_character_string_length) { + return FLEDASTY_ERROR_VALUE_NOT_FOUND; + } + + if (current_string->capacity <= current_string->size + character_string_length) { + current_string->capacity += (current_string->capacity > character_string_length) ? current_string->capacity : character_string_length; + 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_length), current_string->character_string + index, current_string->size - index); + hallocy_copy_memory(current_string->character_string + index, character_string, character_string_length); + + current_string->size += character_string_length; + return FLEDASTY_ERROR_NONE; +} + FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, const size_t size) { FledastyUtf8String utf8_string; fledasty_utf8_string_initialize(&utf8_string, NULL, 0); diff --git a/Tests/Main.c b/Tests/Main.c index 35a54a9..479057f 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -19,6 +19,7 @@ * Author: Mineplay * ----------------------------------------------------------------------------- */ +#include "Fledasty/Utils/Error.h" #include #include #include @@ -230,7 +231,9 @@ int main() { printf("%s\n", test_utf8_string.character_string); fledasty_utf8_string_append(&test_utf8_string, (unsigned char*)"😀", 4); - printf("%s\n", test_utf8_string.character_string); + printf("Append: %s\n", test_utf8_string.character_string); + fledasty_utf8_string_insert_before_string(&test_utf8_string, (unsigned char*)"😀", 4, (unsigned char*)"Hello", 5); + printf("Insert Before: %s\n", test_utf8_string.character_string); size_t unicode_length = 0; uint32_t *unicode = fledasty_utf8_string_decode(&test_utf8_string, &unicode_length);