feat(string): implemented insert at index function
This commit is contained in:
parent
c64a4a2011
commit
4f9a0ef189
3 changed files with 30 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue