feat(string): implemented shrink to fit
This commit is contained in:
parent
267fe05297
commit
a15cd07d38
4 changed files with 32 additions and 3 deletions
|
|
@ -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_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_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; }
|
static inline bool fledasty_string_is_empty(const FledastyString *current_string) { return current_string == NULL || current_string->size == 0; }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -248,3 +248,26 @@ FledastyError fledasty_string_clear(FledastyString *current_string) {
|
||||||
|
|
||||||
return FLEDASTY_ERROR_NONE;
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -266,7 +266,7 @@ FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_st
|
||||||
|
|
||||||
return FLEDASTY_ERROR_NONE;
|
return FLEDASTY_ERROR_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
index += 1;
|
index += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -292,7 +292,7 @@ FledastyError fledasty_utf8_string_shrink_to_fit(FledastyUtf8String *current_str
|
||||||
unsigned char *previous_string = current_string->character_string;
|
unsigned char *previous_string = current_string->character_string;
|
||||||
|
|
||||||
current_string->capacity = current_string->size + 1;
|
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) {
|
if (current_string->character_string == NULL) {
|
||||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
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);
|
hallocy_copy_memory(current_string->character_string, previous_string, current_string->size);
|
||||||
current_string->character_string[current_string->size] = '\0';
|
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;
|
return FLEDASTY_ERROR_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -302,6 +302,8 @@ int main() {
|
||||||
fledasty_string_replace_string(&normal_test_string, "Hello", 5, "Goodbye", 7);
|
fledasty_string_replace_string(&normal_test_string, "Hello", 5, "Goodbye", 7);
|
||||||
printf("Replace: %s\n", normal_test_string.character_string);
|
printf("Replace: %s\n", normal_test_string.character_string);
|
||||||
|
|
||||||
|
fledasty_string_shrink_to_fit(&normal_test_string);
|
||||||
|
|
||||||
fledasty_string_clear(&normal_test_string);
|
fledasty_string_clear(&normal_test_string);
|
||||||
if (fledasty_string_is_empty(&normal_test_string)) {
|
if (fledasty_string_is_empty(&normal_test_string)) {
|
||||||
printf("String is empty!\n");
|
printf("String is empty!\n");
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue