feat(string): implemented insert before string function

This commit is contained in:
Mineplay 2025-07-11 06:24:39 -05:00
parent 4f9a0ef189
commit 01d01b16fa
4 changed files with 37 additions and 4 deletions

View file

@ -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

View file

@ -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;
}

View file

@ -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;

View file

@ -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);