feat(string): implemented replace string function
This commit is contained in:
parent
bbfca9674b
commit
c9c8e8a95f
4 changed files with 41 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue