From bbfca9674bf9a2fdc9233778c5225cc45ef822c5 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 06:37:26 -0500 Subject: [PATCH] feat(string): implemented remove range --- Include/Fledasty/Strings/String.h | 1 + Src/Strings/String.c | 19 ++++++++++++++++++- Src/Strings/UTF8String.c | 3 ++- Tests/Main.c | 14 ++++++++------ 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Include/Fledasty/Strings/String.h b/Include/Fledasty/Strings/String.h index 2620f76..e8d08b1 100644 --- a/Include/Fledasty/Strings/String.h +++ b/Include/Fledasty/Strings/String.h @@ -45,5 +45,6 @@ FledastyError fledasty_string_insert_after_string(FledastyString *current_string FledastyError fledasty_string_pop(FledastyString *current_string); FledastyError fledasty_string_remove(FledastyString *current_string, char *character_string, const size_t character_string_size); +FledastyError fledasty_string_remove_range(FledastyString *current_string, const size_t start_index, const size_t end_index); #endif diff --git a/Src/Strings/String.c b/Src/Strings/String.c index 9b3dbdf..a6e345c 100644 --- a/Src/Strings/String.c +++ b/Src/Strings/String.c @@ -178,7 +178,7 @@ FledastyError fledasty_string_remove(FledastyString *current_string, char *chara current_string->size -= character_string_size; current_string->character_string[current_string->size] = '\0'; - + return FLEDASTY_ERROR_NONE; } @@ -187,3 +187,20 @@ FledastyError fledasty_string_remove(FledastyString *current_string, char *chara return FLEDASTY_ERROR_VALUE_NOT_FOUND; } + +FledastyError fledasty_string_remove_range(FledastyString *current_string, const size_t start_index, const size_t end_index) { + if (current_string == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + 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->character_string[current_string->size] = '\0'; + + return FLEDASTY_ERROR_NONE; +} diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index ff17acb..5f67b3e 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -209,7 +209,7 @@ FledastyError fledasty_utf8_string_remove(FledastyUtf8String *current_string, un current_string->size -= character_string_size; current_string->character_string[current_string->size] = '\0'; - + return FLEDASTY_ERROR_NONE; } @@ -232,6 +232,7 @@ FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_stri current_string->size -= end_index - start_index; current_string->character_string[current_string->size] = '\0'; + return FLEDASTY_ERROR_NONE; } diff --git a/Tests/Main.c b/Tests/Main.c index 3a36cdf..6bbe7a9 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -286,17 +286,19 @@ int main() { FledastyString normal_test_string = { 0, 0, NULL }; fledasty_string_append(&normal_test_string, "Testing", 7); - printf("%s\n", normal_test_string.character_string); + printf("Append: %s\n", normal_test_string.character_string); fledasty_string_insert_at_index(&normal_test_string, normal_test_string.size, "index", 5); - printf("%s\n", normal_test_string.character_string); + printf("Insert at index: %s\n", normal_test_string.character_string); fledasty_string_insert_before_string(&normal_test_string, "nd", 2, "Hello", 5); - printf("%s\n", normal_test_string.character_string); + printf("Insert before: %s\n", normal_test_string.character_string); fledasty_string_insert_after_string(&normal_test_string, "nd", 2, "Bye", 3); - printf("%s\n", normal_test_string.character_string); + printf("Insert after: %s\n", normal_test_string.character_string); fledasty_string_pop(&normal_test_string); - printf("%s\n", normal_test_string.character_string); + printf("Pop: %s\n", normal_test_string.character_string); fledasty_string_remove(&normal_test_string, "Bye", 3); - printf("%s\n", normal_test_string.character_string); + printf("Removed string: %s\n", normal_test_string.character_string); + fledasty_string_remove_range(&normal_test_string, 0, 6); + printf("Remove range: %s\n", normal_test_string.character_string); fledasty_string_free(&normal_test_string); printf("Done\n");