feat(utf-8 string): implemented shrink to fit for utf-8 string

This commit is contained in:
Mineplay 2025-06-07 06:58:41 -05:00
parent ebafa37fad
commit e1d5b72764
3 changed files with 32 additions and 12 deletions

View file

@ -45,10 +45,12 @@ FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *curre
FledastyError fledasty_utf8_string_pop(FledastyUtf8String *current_string);
FledastyError fledasty_utf8_string_remove(FledastyUtf8String *current_string, unsigned char *character_string, const size_t character_string_size);
FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_string, const size_t start_index, const size_t end_index);
FledastyError fledasty_utf8_string_clear(FledastyUtf8String *current_string);
FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_string, unsigned char *replace_character_string, const size_t replace_character_string_size, unsigned char *character_string, const size_t character_string_size);
FledastyError fledasty_utf8_string_clear(FledastyUtf8String *current_string);
FledastyError fledasty_utf8_string_shrink_to_fit(FledastyUtf8String *current_string);
bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, const size_t character_string_size);
static inline bool fledasty_utf8_string_is_empty(const FledastyUtf8String *current_string) { return current_string == NULL || current_string->size == 0; }

View file

@ -24,6 +24,7 @@
* -----------------------------------------------------------------------------
*/
#include "../../Include/Fledasty/Strings/UTF8String.h"
#include "Fledasty/Utils/Error.h"
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
@ -238,17 +239,6 @@ FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_stri
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_utf8_string_clear(FledastyUtf8String *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;
}
FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_string, unsigned char *replace_character_string, const size_t replace_character_string_size, unsigned 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;
@ -280,6 +270,33 @@ FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_st
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_utf8_string_clear(FledastyUtf8String *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;
}
FledastyError fledasty_utf8_string_shrink_to_fit(FledastyUtf8String *current_string) {
if (current_string == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
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);
hallocy_copy_memory(current_string->character_string, previous_string, current_string->size);
current_string->character_string[current_string->size] = '\0';
hallocy_free(previous_string);
return FLEDASTY_ERROR_NONE;
}
bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, const size_t character_string_size) {
if (current_string == NULL || character_string == NULL || character_string_size == 0) {
return false;

View file

@ -258,6 +258,7 @@ int main() {
fledasty_utf8_string_remove_range(&test_utf8_string, 0, 6);
printf("Remove range: %s\n", test_utf8_string.character_string);
fledasty_utf8_string_shrink_to_fit(&test_utf8_string);
if (fledasty_utf8_string_has_string(&test_utf8_string, (unsigned char*)"😀", 4)) {
printf("String contains 😀!\n");
}