From 8e237dae14ab237622bc20bdc295142e6380ee55 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Mon, 12 May 2025 04:23:23 -0500 Subject: [PATCH] feat(utf-8 string): implemented insert at index function --- Include/Fledasty/Strings/UTF8String.h | 1 + Src/Strings/UTF8String.c | 25 +++++++++++++++++++++++++ Tests/Main.c | 2 ++ 3 files changed, 28 insertions(+) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index 6a35530..ecd8391 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -40,6 +40,7 @@ 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); FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *current_string, unsigned char *after_character_string, size_t after_character_string_length, unsigned char *character_string, size_t character_string_length); +FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_string, size_t index, 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 88107c3..401cb7a 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -156,6 +156,31 @@ FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *curre return FLEDASTY_ERROR_NONE; } +FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_string, size_t index, unsigned char *character_string, size_t character_string_length) { + if (current_string == NULL || character_string == NULL || character_string_length == 0) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + if (index >= current_string->size) { + return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; + } + + 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 > 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 29bdc2d..89091ab 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -235,6 +235,8 @@ 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); + printf("Insert at Index: %s\n", test_utf8_string.character_string); size_t unicode_length = 0; uint32_t *unicode = fledasty_utf8_string_decode(&test_utf8_string, &unicode_length);