From 15abfcffe8d907412153bd8b00445503309cff9d Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 15 May 2025 10:26:32 -0500 Subject: [PATCH] feat(utf-8 string): implemented remove function --- Include/Fledasty/Strings/UTF8String.h | 1 + Src/Strings/UTF8String.c | 24 ++++++++++++++++++++++++ Tests/Main.c | 2 ++ 3 files changed, 27 insertions(+) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index eea16f1..e887367 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -43,6 +43,7 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *current_string, unsigned char *after_character_string, size_t after_character_string_size, unsigned char *character_string, size_t character_string_size); FledastyError fledasty_utf8_string_pop(FledastyUtf8String *current_string); +FledastyError fledasty_utf8_string_remove(FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_size); FledastyError fledasty_utf8_string_clear(FledastyUtf8String *current_string); FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_string, unsigned char *replace_character_string, size_t replace_character_string_size, unsigned char *character_string, size_t character_string_size); diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index d78c5b1..d6e7bb4 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -198,6 +198,30 @@ FledastyError fledasty_utf8_string_pop(FledastyUtf8String *current_string) { return FLEDASTY_ERROR_NONE; } +FledastyError fledasty_utf8_string_remove(FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_size) { + if (current_string == NULL || character_string == NULL || character_string_size == 0) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + if (!fledasty_utf8_string_validate(character_string, character_string_size)) { + return FLEDASTY_ERROR_INVALID_VALUE; + } + + 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)) { + 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; +} + FledastyError fledasty_utf8_string_clear(FledastyUtf8String *current_string) { if (current_string == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; diff --git a/Tests/Main.c b/Tests/Main.c index 61cc5e5..5c4ecd9 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -241,6 +241,8 @@ int main() { printf("Replace: %s\n", test_utf8_string.character_string); fledasty_utf8_string_pop(&test_utf8_string); 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); if (fledasty_utf8_string_has_string(&test_utf8_string, (unsigned char*)"😀", 4)) { printf("String contains 😀!\n");