From f27562c23edba9f1fe0027f2a85eb5f38b315e37 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 05:46:42 -0500 Subject: [PATCH 01/14] feat(string): added simple string struct and free function --- Include/Fledasty/Strings/String.h | 41 +++++++++++++++++++++++++++++ Src/Strings/String.c | 43 +++++++++++++++++++++++++++++++ Tests/Main.c | 5 ++++ 3 files changed, 89 insertions(+) create mode 100644 Include/Fledasty/Strings/String.h create mode 100644 Src/Strings/String.c diff --git a/Include/Fledasty/Strings/String.h b/Include/Fledasty/Strings/String.h new file mode 100644 index 0000000..b4ae353 --- /dev/null +++ b/Include/Fledasty/Strings/String.h @@ -0,0 +1,41 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: String.h + * Description: + * This file contains the String structure and the functions for modifying it. + * It includes functions to append, Insert at index, insert before character, + * insert before string, insert after character, insert after string, replace, + * copy, pop, remove, remove range, clear, check if contains string, check if + * empty. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#ifndef FLEDASTY_STRING +#define FLEDASTY_STRING + +#include +#include + +#include "../Utils/Error.h" + +typedef struct { + size_t size, capacity; + char *character_string; +} FledastyString; + +FledastyError fledasty_string_free(FledastyString *current_string); + +#endif diff --git a/Src/Strings/String.c b/Src/Strings/String.c new file mode 100644 index 0000000..6caa733 --- /dev/null +++ b/Src/Strings/String.c @@ -0,0 +1,43 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: String.c + * Description: + * This file contains the functions for modifying the String. It includes + * functions to append, Insert at index, insert before character, + * insert before string, insert after character, insert after string, replace, + * copy, pop, remove, remove range, clear, check if contains string, check if + * empty. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#include "../../Include/Fledasty/Strings/String.h" + +#include +#include +#include + +FledastyError fledasty_string_free(FledastyString *current_string) { + if (current_string == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + if (hallocy_free(current_string->character_string) != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + current_string->character_string = NULL; + return FLEDASTY_ERROR_NONE; +} diff --git a/Tests/Main.c b/Tests/Main.c index 264e5d2..0ad08ae 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -28,6 +28,7 @@ #include #include #include +#include #include static inline bool compare_integers(const int first_value, const int second_value) { return first_value == second_value; } @@ -281,6 +282,10 @@ int main() { fledasty_utf8_string_free(&encoded_string); fledasty_utf8_string_free(&test_utf8_string); + + FledastyString normal_test_string = { 0, 0, NULL }; + + fledasty_string_free(&normal_test_string); printf("Done\n"); return 0; } \ No newline at end of file From 8714361e85d05d3d5d796255c5744607ff7d5753 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 05:49:12 -0500 Subject: [PATCH 02/14] fix(utf-8 string): added missing error checks for memory allocation --- Src/Strings/UTF8String.c | 45 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index be36e34..4c3fc86 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -24,7 +24,6 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Fledasty/Strings/UTF8String.h" -#include "Fledasty/Utils/Error.h" #include #include @@ -57,6 +56,10 @@ 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 + 1; current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char)); + + if (current_string->character_string == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } } hallocy_copy_memory(current_string->character_string + current_string->size, character_string, character_string_size); @@ -83,6 +86,10 @@ FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_s 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)); + + if (current_string->character_string == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } } hallocy_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index); @@ -108,6 +115,10 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr 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)); + + if (current_string->character_string == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } } hallocy_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index); @@ -138,6 +149,10 @@ FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *curre 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)); + + if (current_string->character_string == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } } index += after_character_string_size; @@ -231,6 +246,10 @@ FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_st 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)); + + if (current_string->character_string == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } } 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)); @@ -266,6 +285,10 @@ FledastyError fledasty_utf8_string_shrink_to_fit(FledastyUtf8String *current_str current_string->capacity = current_string->size + 1; current_string->character_string = (unsigned char*)hallocy_malloc(current_string->capacity); + if (current_string->character_string == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + hallocy_copy_memory(current_string->character_string, previous_string, current_string->size); current_string->character_string[current_string->size] = '\0'; @@ -306,6 +329,10 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si if (utf8_string.capacity <= string_index) { utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE; utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity); + + if (utf8_string.character_string == NULL) { + return utf8_string; + } } utf8_string.character_string[string_index] = unicode[index]; @@ -314,6 +341,10 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si if (utf8_string.capacity <= string_index + 2) { utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE; utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity); + + if (utf8_string.character_string == NULL) { + return utf8_string; + } } utf8_string.character_string[string_index] = 0xC0 | ((unicode[index] >> 6) & 0x07); @@ -323,6 +354,10 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si if (utf8_string.capacity <= string_index + 3) { utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE; utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity); + + if (utf8_string.character_string == NULL) { + return utf8_string; + } } utf8_string.character_string[string_index] = 0xE0 | ((unicode[index] >> 12) & 0x07); @@ -333,6 +368,10 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si if (utf8_string.capacity <= string_index + 4) { utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE; utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity); + + if (utf8_string.character_string == NULL) { + return utf8_string; + } } utf8_string.character_string[string_index] = 0xF0 | ((unicode[index] >> 18) & 0x07); @@ -361,6 +400,10 @@ uint32_t *fledasty_utf8_string_decode(const FledastyUtf8String *current_string, (*unicode_string_size) = 0; size_t index = 0; uint32_t *unicode_string = (uint32_t*)hallocy_malloc(current_string->size * sizeof(uint32_t)); + if (unicode_string == NULL) { + return NULL; + } + while (index < current_string->size) { if ((current_string->character_string[index] & 0xF0) == 0xF0) { unicode_string[*unicode_string_size] = ((current_string->character_string[index] & 0x07) << 18) | ((current_string->character_string[index + 1] & 0x3F) << 12) | ((current_string->character_string[index + 2] & 0x3F) << 6) | (current_string->character_string[index + 3] & 0x3F); From c64a4a201161e622ae1d549e78d5a4b6d7328ce6 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 06:14:40 -0500 Subject: [PATCH 03/14] feat(string): implemented append function --- Include/Fledasty/Strings/String.h | 2 ++ Src/Strings/String.c | 22 ++++++++++++++++++++++ Tests/Main.c | 2 ++ 3 files changed, 26 insertions(+) diff --git a/Include/Fledasty/Strings/String.h b/Include/Fledasty/Strings/String.h index b4ae353..d27e3f9 100644 --- a/Include/Fledasty/Strings/String.h +++ b/Include/Fledasty/Strings/String.h @@ -38,4 +38,6 @@ typedef struct { FledastyError fledasty_string_free(FledastyString *current_string); +FledastyError fledasty_string_append(FledastyString *current_string, char *character_string, const size_t character_string_size); + #endif diff --git a/Src/Strings/String.c b/Src/Strings/String.c index 6caa733..891fe71 100644 --- a/Src/Strings/String.c +++ b/Src/Strings/String.c @@ -41,3 +41,25 @@ FledastyError fledasty_string_free(FledastyString *current_string) { current_string->character_string = NULL; return FLEDASTY_ERROR_NONE; } + +FledastyError fledasty_string_append(FledastyString *current_string, char *character_string, const size_t character_string_size) { + if (current_string == NULL || character_string == NULL || character_string_size == 0) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + 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 = (char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char)); + + if (current_string->character_string == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + } + + 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'; + + return FLEDASTY_ERROR_NONE; +} diff --git a/Tests/Main.c b/Tests/Main.c index 0ad08ae..13e1e6b 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -284,6 +284,8 @@ int main() { fledasty_utf8_string_free(&test_utf8_string); FledastyString normal_test_string = { 0, 0, NULL }; + fledasty_string_append(&normal_test_string, "Testing", 7); + printf("%s\n", normal_test_string.character_string); fledasty_string_free(&normal_test_string); printf("Done\n"); From 4f9a0ef1894c98ebe44742fcb32e353e165a900e Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 06:17:15 -0500 Subject: [PATCH 04/14] feat(string): implemented insert at index function --- Include/Fledasty/Strings/String.h | 1 + Src/Strings/String.c | 26 ++++++++++++++++++++++++++ Tests/Main.c | 3 +++ 3 files changed, 30 insertions(+) diff --git a/Include/Fledasty/Strings/String.h b/Include/Fledasty/Strings/String.h index d27e3f9..1cd4416 100644 --- a/Include/Fledasty/Strings/String.h +++ b/Include/Fledasty/Strings/String.h @@ -39,5 +39,6 @@ typedef struct { FledastyError fledasty_string_free(FledastyString *current_string); FledastyError fledasty_string_append(FledastyString *current_string, char *character_string, const size_t character_string_size); +FledastyError fledasty_string_insert_at_index(FledastyString *current_string, const size_t index, char *character_string, const size_t character_string_size); #endif diff --git a/Src/Strings/String.c b/Src/Strings/String.c index 891fe71..2df815f 100644 --- a/Src/Strings/String.c +++ b/Src/Strings/String.c @@ -63,3 +63,29 @@ FledastyError fledasty_string_append(FledastyString *current_string, char *chara return FLEDASTY_ERROR_NONE; } + +FledastyError fledasty_string_insert_at_index(FledastyString *current_string, const size_t index, char *character_string, const size_t character_string_size) { + if (current_string == NULL || character_string == NULL || character_string_size == 0) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + if (index > current_string->size) { + return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; + } + + 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 = (char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char)); + + if (current_string->character_string == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + } + + 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; + current_string->character_string[current_string->size] = '\0'; + return FLEDASTY_ERROR_NONE; +} diff --git a/Tests/Main.c b/Tests/Main.c index 13e1e6b..a4a3495 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -284,8 +284,11 @@ int main() { fledasty_utf8_string_free(&test_utf8_string); FledastyString normal_test_string = { 0, 0, NULL }; + fledasty_string_append(&normal_test_string, "Testing", 7); printf("%s\n", normal_test_string.character_string); + fledasty_string_insert_at_index(&normal_test_string, normal_test_string.size, (char*)"index", 5); + printf("%s\n", normal_test_string.character_string); fledasty_string_free(&normal_test_string); printf("Done\n"); From 01d01b16faf7d53d3d36c2e6fd74833d3437d226 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 06:24:39 -0500 Subject: [PATCH 05/14] feat(string): implemented insert before string function --- Include/Fledasty/Strings/String.h | 1 + Src/Strings/String.c | 34 +++++++++++++++++++++++++++++-- Src/Strings/UTF8String.c | 2 +- Tests/Main.c | 4 +++- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Include/Fledasty/Strings/String.h b/Include/Fledasty/Strings/String.h index 1cd4416..89f54a3 100644 --- a/Include/Fledasty/Strings/String.h +++ b/Include/Fledasty/Strings/String.h @@ -40,5 +40,6 @@ FledastyError fledasty_string_free(FledastyString *current_string); FledastyError fledasty_string_append(FledastyString *current_string, char *character_string, const size_t character_string_size); FledastyError fledasty_string_insert_at_index(FledastyString *current_string, const size_t index, char *character_string, const size_t character_string_size); +FledastyError fledasty_string_insert_before_string(FledastyString *current_string, char *before_character_string, const size_t before_character_string_size, char *character_string, const size_t character_string_size); #endif diff --git a/Src/Strings/String.c b/Src/Strings/String.c index 2df815f..a8608ab 100644 --- a/Src/Strings/String.c +++ b/Src/Strings/String.c @@ -49,7 +49,7 @@ FledastyError fledasty_string_append(FledastyString *current_string, char *chara 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 = (char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char)); + current_string->character_string = (char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(char)); if (current_string->character_string == NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION; @@ -75,7 +75,7 @@ FledastyError fledasty_string_insert_at_index(FledastyString *current_string, co 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 = (char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char)); + current_string->character_string = (char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(char)); if (current_string->character_string == NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION; @@ -89,3 +89,33 @@ FledastyError fledasty_string_insert_at_index(FledastyString *current_string, co current_string->character_string[current_string->size] = '\0'; return FLEDASTY_ERROR_NONE; } + +FledastyError fledasty_string_insert_before_string(FledastyString *current_string, char *before_character_string, const size_t before_character_string_size, char *character_string, const size_t character_string_size) { + if (current_string == NULL || before_character_string == NULL || before_character_string_size == 0 || character_string == NULL || character_string_size == 0) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + size_t index = 0; + while (index < (current_string->size - 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 = (char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(char)); + + if (current_string->character_string == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + } + + 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; + } + + return FLEDASTY_ERROR_VALUE_NOT_FOUND; +} diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index 4c3fc86..0418c34 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -110,7 +110,7 @@ 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)) { + while (index < (current_string->size - 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; diff --git a/Tests/Main.c b/Tests/Main.c index a4a3495..44a994d 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -287,7 +287,9 @@ int main() { fledasty_string_append(&normal_test_string, "Testing", 7); printf("%s\n", normal_test_string.character_string); - fledasty_string_insert_at_index(&normal_test_string, normal_test_string.size, (char*)"index", 5); + fledasty_string_insert_at_index(&normal_test_string, normal_test_string.size, "index", 5); + printf("%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); fledasty_string_free(&normal_test_string); From 6641181e0a078caf3e05ee4080fb807f45b9198b Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 06:29:03 -0500 Subject: [PATCH 06/14] feat(string): implemented insert after string function --- Include/Fledasty/Strings/String.h | 1 + Src/Strings/String.c | 35 +++++++++++++++++++++++++++++++ Src/Strings/UTF8String.c | 4 ++++ Tests/Main.c | 2 ++ 4 files changed, 42 insertions(+) diff --git a/Include/Fledasty/Strings/String.h b/Include/Fledasty/Strings/String.h index 89f54a3..83c22e3 100644 --- a/Include/Fledasty/Strings/String.h +++ b/Include/Fledasty/Strings/String.h @@ -41,5 +41,6 @@ FledastyError fledasty_string_free(FledastyString *current_string); FledastyError fledasty_string_append(FledastyString *current_string, char *character_string, const size_t character_string_size); FledastyError fledasty_string_insert_at_index(FledastyString *current_string, const size_t index, char *character_string, const size_t character_string_size); FledastyError fledasty_string_insert_before_string(FledastyString *current_string, char *before_character_string, const size_t before_character_string_size, char *character_string, const size_t character_string_size); +FledastyError fledasty_string_insert_after_string(FledastyString *current_string, char *after_character_string, const size_t after_character_string_size, char *character_string, const size_t character_string_size); #endif diff --git a/Src/Strings/String.c b/Src/Strings/String.c index a8608ab..80d1f95 100644 --- a/Src/Strings/String.c +++ b/Src/Strings/String.c @@ -111,6 +111,8 @@ FledastyError fledasty_string_insert_before_string(FledastyString *current_strin 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; } @@ -119,3 +121,36 @@ FledastyError fledasty_string_insert_before_string(FledastyString *current_strin return FLEDASTY_ERROR_VALUE_NOT_FOUND; } + +FledastyError fledasty_string_insert_after_string(FledastyString *current_string, char *after_character_string, const size_t after_character_string_size, char *character_string, const size_t character_string_size) { + if (current_string == NULL || after_character_string == NULL || after_character_string_size == 0 || character_string == NULL || character_string_size == 0) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + size_t index = 0; + 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 = (char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(char)); + + if (current_string->character_string == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + } + + 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; + current_string->character_string[current_string->size] = '\0'; + + return FLEDASTY_ERROR_NONE; + } + + index += 1; + } + + return FLEDASTY_ERROR_VALUE_NOT_FOUND; +} diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index 0418c34..3b1c281 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -125,6 +125,8 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr 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; } @@ -160,6 +162,8 @@ FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *curre 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; } diff --git a/Tests/Main.c b/Tests/Main.c index 44a994d..ed49f2e 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -291,6 +291,8 @@ int main() { printf("%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); + fledasty_string_insert_after_string(&normal_test_string, "nd", 2, "Bye", 3); + printf("%s\n", normal_test_string.character_string); fledasty_string_free(&normal_test_string); printf("Done\n"); From 107f8cc4162667569c51168602d3c09ec074d616 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 06:30:46 -0500 Subject: [PATCH 07/14] feat(string): implemented pop function --- Include/Fledasty/Strings/String.h | 2 ++ Src/Strings/String.c | 13 ++++++++++++- Tests/Main.c | 2 ++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Include/Fledasty/Strings/String.h b/Include/Fledasty/Strings/String.h index 83c22e3..53fb789 100644 --- a/Include/Fledasty/Strings/String.h +++ b/Include/Fledasty/Strings/String.h @@ -43,4 +43,6 @@ FledastyError fledasty_string_insert_at_index(FledastyString *current_string, co FledastyError fledasty_string_insert_before_string(FledastyString *current_string, char *before_character_string, const size_t before_character_string_size, char *character_string, const size_t character_string_size); FledastyError fledasty_string_insert_after_string(FledastyString *current_string, char *after_character_string, const size_t after_character_string_size, char *character_string, const size_t character_string_size); +FledastyError fledasty_string_pop(FledastyString *current_string); + #endif diff --git a/Src/Strings/String.c b/Src/Strings/String.c index 80d1f95..a5f2d41 100644 --- a/Src/Strings/String.c +++ b/Src/Strings/String.c @@ -145,7 +145,7 @@ FledastyError fledasty_string_insert_after_string(FledastyString *current_string current_string->size += character_string_size; current_string->character_string[current_string->size] = '\0'; - + return FLEDASTY_ERROR_NONE; } @@ -154,3 +154,14 @@ FledastyError fledasty_string_insert_after_string(FledastyString *current_string return FLEDASTY_ERROR_VALUE_NOT_FOUND; } + +FledastyError fledasty_string_pop(FledastyString *current_string) { + if (current_string == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + current_string->size -= 1; + current_string->character_string[current_string->size] = '\0'; + + return FLEDASTY_ERROR_NONE; +} diff --git a/Tests/Main.c b/Tests/Main.c index ed49f2e..a54e812 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -293,6 +293,8 @@ int main() { printf("%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); + fledasty_string_pop(&normal_test_string); + printf("%s\n", normal_test_string.character_string); fledasty_string_free(&normal_test_string); printf("Done\n"); From 719d491216c5a33323c970d6c06f4a71b9b49e1e Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 06:32:48 -0500 Subject: [PATCH 08/14] feat(string): implemented remove function --- Include/Fledasty/Strings/String.h | 1 + Src/Strings/String.c | 22 ++++++++++++++++++++++ Src/Strings/UTF8String.c | 3 ++- Tests/Main.c | 2 ++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Include/Fledasty/Strings/String.h b/Include/Fledasty/Strings/String.h index 53fb789..2620f76 100644 --- a/Include/Fledasty/Strings/String.h +++ b/Include/Fledasty/Strings/String.h @@ -44,5 +44,6 @@ FledastyError fledasty_string_insert_before_string(FledastyString *current_strin FledastyError fledasty_string_insert_after_string(FledastyString *current_string, char *after_character_string, const size_t after_character_string_size, char *character_string, const size_t character_string_size); FledastyError fledasty_string_pop(FledastyString *current_string); +FledastyError fledasty_string_remove(FledastyString *current_string, char *character_string, const size_t character_string_size); #endif diff --git a/Src/Strings/String.c b/Src/Strings/String.c index a5f2d41..9b3dbdf 100644 --- a/Src/Strings/String.c +++ b/Src/Strings/String.c @@ -165,3 +165,25 @@ FledastyError fledasty_string_pop(FledastyString *current_string) { return FLEDASTY_ERROR_NONE; } + +FledastyError fledasty_string_remove(FledastyString *current_string, char *character_string, const size_t character_string_size) { + if (current_string == NULL || character_string == NULL || character_string_size == 0) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + size_t index = 0; + 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; + } + + return FLEDASTY_ERROR_VALUE_NOT_FOUND; +} diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index 3b1c281..ff17acb 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -126,7 +126,7 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr current_string->size += character_string_size; current_string->character_string[current_string->size] = '\0'; - + return FLEDASTY_ERROR_NONE; } @@ -209,6 +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; } diff --git a/Tests/Main.c b/Tests/Main.c index a54e812..3a36cdf 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -295,6 +295,8 @@ int main() { printf("%s\n", normal_test_string.character_string); fledasty_string_pop(&normal_test_string); printf("%s\n", normal_test_string.character_string); + fledasty_string_remove(&normal_test_string, "Bye", 3); + printf("%s\n", normal_test_string.character_string); fledasty_string_free(&normal_test_string); printf("Done\n"); From bbfca9674bf9a2fdc9233778c5225cc45ef822c5 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 06:37:26 -0500 Subject: [PATCH 09/14] 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"); From c9c8e8a95f7273e95c17c745566f40c6640fd96e Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 06:40:29 -0500 Subject: [PATCH 10/14] feat(string): implemented replace string function --- Include/Fledasty/Strings/String.h | 2 ++ Src/Strings/String.c | 35 ++++++++++++++++++++++++++++++- Src/Strings/UTF8String.c | 4 +++- Tests/Main.c | 2 ++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/Include/Fledasty/Strings/String.h b/Include/Fledasty/Strings/String.h index e8d08b1..4995882 100644 --- a/Include/Fledasty/Strings/String.h +++ b/Include/Fledasty/Strings/String.h @@ -47,4 +47,6 @@ 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); +FledastyError fledasty_string_replace_string(FledastyString *current_string, char *replace_character_string, const size_t replace_character_string_size, char *character_string, const size_t character_string_size); + #endif diff --git a/Src/Strings/String.c b/Src/Strings/String.c index a6e345c..8735788 100644 --- a/Src/Strings/String.c +++ b/Src/Strings/String.c @@ -201,6 +201,39 @@ FledastyError fledasty_string_remove_range(FledastyString *current_string, const current_string->size -= end_index - start_index; current_string->character_string[current_string->size] = '\0'; - + return FLEDASTY_ERROR_NONE; } + +FledastyError fledasty_string_replace_string(FledastyString *current_string, char *replace_character_string, const size_t replace_character_string_size, char *character_string, const size_t character_string_size) { + if (current_string == NULL || replace_character_string == NULL || replace_character_string_size == 0 || character_string == NULL || character_string_size == 0) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + size_t index = 0; + 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 = (char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(char)); + + if (current_string->character_string == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + } + + 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; + } + + return FLEDASTY_ERROR_VALUE_NOT_FOUND; +} diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index 5f67b3e..ef7e4e2 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -232,7 +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; } @@ -263,8 +263,10 @@ FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_st current_string->size = new_size; current_string->character_string[current_string->size] = '\0'; + return FLEDASTY_ERROR_NONE; } + index += 1; } diff --git a/Tests/Main.c b/Tests/Main.c index 6bbe7a9..78ac370 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -299,6 +299,8 @@ int main() { 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_replace_string(&normal_test_string, "Hello", 5, "Goodbye", 7); + printf("Replace: %s\n", normal_test_string.character_string); fledasty_string_free(&normal_test_string); printf("Done\n"); From 267fe0529761067e5543c15cf91d40a73b562062 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 06:42:16 -0500 Subject: [PATCH 11/14] feat(string): implemented clear and is empty function --- Include/Fledasty/Strings/String.h | 4 ++++ Src/Strings/String.c | 13 ++++++++++++- Tests/Main.c | 5 +++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Include/Fledasty/Strings/String.h b/Include/Fledasty/Strings/String.h index 4995882..16ac40e 100644 --- a/Include/Fledasty/Strings/String.h +++ b/Include/Fledasty/Strings/String.h @@ -49,4 +49,8 @@ FledastyError fledasty_string_remove_range(FledastyString *current_string, const FledastyError fledasty_string_replace_string(FledastyString *current_string, char *replace_character_string, const size_t replace_character_string_size, char *character_string, const size_t character_string_size); +FledastyError fledasty_string_clear(FledastyString *current_string); + +static inline bool fledasty_string_is_empty(const FledastyString *current_string) { return current_string == NULL || current_string->size == 0; } + #endif diff --git a/Src/Strings/String.c b/Src/Strings/String.c index 8735788..475de31 100644 --- a/Src/Strings/String.c +++ b/Src/Strings/String.c @@ -231,9 +231,20 @@ FledastyError fledasty_string_replace_string(FledastyString *current_string, cha return FLEDASTY_ERROR_NONE; } - + index += 1; } return FLEDASTY_ERROR_VALUE_NOT_FOUND; } + +FledastyError fledasty_string_clear(FledastyString *current_string) { + if (current_string == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + current_string->size = 0; + current_string->character_string[0] = '\0'; + + return FLEDASTY_ERROR_NONE; +} diff --git a/Tests/Main.c b/Tests/Main.c index 78ac370..f144e43 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -302,6 +302,11 @@ int main() { fledasty_string_replace_string(&normal_test_string, "Hello", 5, "Goodbye", 7); printf("Replace: %s\n", normal_test_string.character_string); + fledasty_string_clear(&normal_test_string); + if (fledasty_string_is_empty(&normal_test_string)) { + printf("String is empty!\n"); + } + fledasty_string_free(&normal_test_string); printf("Done\n"); return 0; From a15cd07d38295effb1c6ebe9c4cd9fee0c9fd326 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 06:44:59 -0500 Subject: [PATCH 12/14] feat(string): implemented shrink to fit --- Include/Fledasty/Strings/String.h | 1 + Src/Strings/String.c | 23 +++++++++++++++++++++++ Src/Strings/UTF8String.c | 9 ++++++--- Tests/Main.c | 2 ++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Include/Fledasty/Strings/String.h b/Include/Fledasty/Strings/String.h index 16ac40e..1d07577 100644 --- a/Include/Fledasty/Strings/String.h +++ b/Include/Fledasty/Strings/String.h @@ -50,6 +50,7 @@ FledastyError fledasty_string_remove_range(FledastyString *current_string, const FledastyError fledasty_string_replace_string(FledastyString *current_string, char *replace_character_string, const size_t replace_character_string_size, char *character_string, const size_t character_string_size); FledastyError fledasty_string_clear(FledastyString *current_string); +FledastyError fledasty_string_shrink_to_fit(FledastyString *current_string); static inline bool fledasty_string_is_empty(const FledastyString *current_string) { return current_string == NULL || current_string->size == 0; } diff --git a/Src/Strings/String.c b/Src/Strings/String.c index 475de31..5b2bde3 100644 --- a/Src/Strings/String.c +++ b/Src/Strings/String.c @@ -248,3 +248,26 @@ FledastyError fledasty_string_clear(FledastyString *current_string) { return FLEDASTY_ERROR_NONE; } + +FledastyError fledasty_string_shrink_to_fit(FledastyString *current_string) { + if (current_string == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + char *previous_string = current_string->character_string; + + current_string->capacity = current_string->size + 1; + current_string->character_string = (char*)hallocy_malloc(current_string->capacity * sizeof(char*)); + if (current_string->character_string == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + hallocy_copy_memory(current_string->character_string, previous_string, current_string->size); + current_string->character_string[current_string->size] = '\0'; + + if (hallocy_free(previous_string) != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + return FLEDASTY_ERROR_NONE; +} diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index ef7e4e2..62bc3af 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -266,7 +266,7 @@ FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_st return FLEDASTY_ERROR_NONE; } - + index += 1; } @@ -292,7 +292,7 @@ FledastyError fledasty_utf8_string_shrink_to_fit(FledastyUtf8String *current_str unsigned char *previous_string = current_string->character_string; current_string->capacity = current_string->size + 1; - current_string->character_string = (unsigned char*)hallocy_malloc(current_string->capacity); + current_string->character_string = (unsigned char*)hallocy_malloc(current_string->capacity * sizeof(unsigned char*)); if (current_string->character_string == NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } @@ -300,7 +300,10 @@ FledastyError fledasty_utf8_string_shrink_to_fit(FledastyUtf8String *current_str hallocy_copy_memory(current_string->character_string, previous_string, current_string->size); current_string->character_string[current_string->size] = '\0'; - hallocy_free(previous_string); + if (hallocy_free(previous_string) != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + return FLEDASTY_ERROR_NONE; } diff --git a/Tests/Main.c b/Tests/Main.c index f144e43..3609abf 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -302,6 +302,8 @@ int main() { fledasty_string_replace_string(&normal_test_string, "Hello", 5, "Goodbye", 7); printf("Replace: %s\n", normal_test_string.character_string); + fledasty_string_shrink_to_fit(&normal_test_string); + fledasty_string_clear(&normal_test_string); if (fledasty_string_is_empty(&normal_test_string)) { printf("String is empty!\n"); From 53f07630907ebb5f3b705a2fa1fd3ba461db9b8d Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 06:46:39 -0500 Subject: [PATCH 13/14] feat(string): implemented has string function --- Include/Fledasty/Strings/String.h | 1 + Src/Strings/String.c | 19 ++++++++++++++++++- Tests/Main.c | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Include/Fledasty/Strings/String.h b/Include/Fledasty/Strings/String.h index 1d07577..c0c7fe4 100644 --- a/Include/Fledasty/Strings/String.h +++ b/Include/Fledasty/Strings/String.h @@ -52,6 +52,7 @@ FledastyError fledasty_string_replace_string(FledastyString *current_string, cha FledastyError fledasty_string_clear(FledastyString *current_string); FledastyError fledasty_string_shrink_to_fit(FledastyString *current_string); +bool fledasty_string_has_string(const FledastyString *current_string, char *character_string, const size_t character_string_size); static inline bool fledasty_string_is_empty(const FledastyString *current_string) { return current_string == NULL || current_string->size == 0; } #endif diff --git a/Src/Strings/String.c b/Src/Strings/String.c index 5b2bde3..3664c3b 100644 --- a/Src/Strings/String.c +++ b/Src/Strings/String.c @@ -268,6 +268,23 @@ FledastyError fledasty_string_shrink_to_fit(FledastyString *current_string) { if (hallocy_free(previous_string) != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } - + return FLEDASTY_ERROR_NONE; } + +bool fledasty_string_has_string(const FledastyString *current_string, char *character_string, const size_t character_string_size) { + if (current_string == NULL || character_string == NULL || character_string_size == 0) { + return false; + } + + size_t index = 0; + while (index < current_string->size - character_string_size) { + if (hallocy_compare_memory(current_string->character_string + index, character_string, character_string_size)) { + return true; + } + + index += 1; + } + + return false; +} diff --git a/Tests/Main.c b/Tests/Main.c index 3609abf..b449818 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -303,6 +303,9 @@ int main() { printf("Replace: %s\n", normal_test_string.character_string); fledasty_string_shrink_to_fit(&normal_test_string); + if (fledasty_string_has_string(&normal_test_string, "bye", 3)) { + printf("String contains bye!\n"); + } fledasty_string_clear(&normal_test_string); if (fledasty_string_is_empty(&normal_test_string)) { From 54a1c83d71d06b153c27867ddadef145c9041e66 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Jul 2025 06:48:05 -0500 Subject: [PATCH 14/14] feat(string): implemented get size function --- Include/Fledasty/Strings/String.h | 2 ++ Src/Strings/String.c | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/Include/Fledasty/Strings/String.h b/Include/Fledasty/Strings/String.h index c0c7fe4..cdd6e79 100644 --- a/Include/Fledasty/Strings/String.h +++ b/Include/Fledasty/Strings/String.h @@ -55,4 +55,6 @@ FledastyError fledasty_string_shrink_to_fit(FledastyString *current_string); bool fledasty_string_has_string(const FledastyString *current_string, char *character_string, const size_t character_string_size); static inline bool fledasty_string_is_empty(const FledastyString *current_string) { return current_string == NULL || current_string->size == 0; } +size_t fledasty_string_get_size(const char *character_string); + #endif diff --git a/Src/Strings/String.c b/Src/Strings/String.c index 3664c3b..d7b6e08 100644 --- a/Src/Strings/String.c +++ b/Src/Strings/String.c @@ -288,3 +288,12 @@ bool fledasty_string_has_string(const FledastyString *current_string, char *char return false; } + +size_t fledasty_string_get_size(const char *character_string) { + size_t size = 0; + while (character_string[size] != '\0') { + size += 1; + } + + return size; +}