feat(utf-8 string): implemented replace string function

This commit is contained in:
Mineplay 2025-05-13 04:04:31 -05:00
parent 0e67969ff1
commit fbdebde492
3 changed files with 29 additions and 1 deletions

View file

@ -42,6 +42,8 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr
FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *current_string, unsigned char *after_character_string, size_t after_character_string_length, unsigned char *character_string, size_t character_string_length); FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *current_string, unsigned char *after_character_string, size_t after_character_string_length, unsigned char *character_string, size_t character_string_length);
FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_string, size_t index, unsigned char *character_string, size_t character_string_length); FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_string, size_t index, unsigned char *character_string, size_t character_string_length);
FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_string, unsigned char *replace_character_string, size_t replace_character_string_length, unsigned char *character_string, size_t character_string_length);
bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_length); bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_length);
static inline bool fledasty_utf8_string_is_empty(const FledastyUtf8String *current_string) { return current_string == NULL || current_string->size == 0; } static inline bool fledasty_utf8_string_is_empty(const FledastyUtf8String *current_string) { return current_string == NULL || current_string->size == 0; }

View file

@ -29,7 +29,6 @@
#include <Hallocy/Core/Allocator.h> #include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h> #include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h> #include <Hallocy/Utils/Error.h>
#include <stdint.h>
FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, size_t character_string_length) { FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, size_t character_string_length) {
if (new_string == NULL) { if (new_string == NULL) {
@ -181,6 +180,31 @@ FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_s
return FLEDASTY_ERROR_NONE; return FLEDASTY_ERROR_NONE;
} }
FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_string, unsigned char *replace_character_string, size_t replace_character_string_length, unsigned char *character_string, size_t character_string_length) {
if (current_string == NULL || replace_character_string == NULL || replace_character_string_length == 0 || character_string == NULL || character_string_length == 0) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (!fledasty_utf8_string_validate(replace_character_string, replace_character_string_length) || !fledasty_utf8_string_validate(character_string, character_string_length)) {
return FLEDASTY_ERROR_INVALID_VALUE;
}
size_t index = 0;
while (index < current_string->size - replace_character_string_length && !hallocy_compare_memory(current_string->character_string + index, replace_character_string, replace_character_string_length)) {
index += 1;
}
if (index == current_string->size - replace_character_string_length) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
hallocy_move_memory(current_string->character_string + index + character_string_length, current_string->character_string + index + replace_character_string_length, current_string->size - (index + replace_character_string_length));
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_length);
current_string->size += character_string_length - replace_character_string_length;
return FLEDASTY_ERROR_NONE;
}
bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_length) { bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_length) {
if (current_string == NULL || character_string == NULL || character_string_length == 0) { if (current_string == NULL || character_string == NULL || character_string_length == 0) {
return false; return false;

View file

@ -237,6 +237,8 @@ int main() {
printf("Insert After: %s\n", test_utf8_string.character_string); printf("Insert After: %s\n", test_utf8_string.character_string);
fledasty_utf8_string_insert_at_index(&test_utf8_string, test_utf8_string.size - 1, (unsigned char*)"index", 5); fledasty_utf8_string_insert_at_index(&test_utf8_string, test_utf8_string.size - 1, (unsigned char*)"index", 5);
printf("Insert at Index: %s\n", test_utf8_string.character_string); printf("Insert at Index: %s\n", test_utf8_string.character_string);
fledasty_utf8_string_replace_string(&test_utf8_string, (unsigned char*)"😀", 4, (unsigned char*)"𓃶 ", 5);
printf("Replace: %s\n", test_utf8_string.character_string);
if (fledasty_utf8_string_has_string(&test_utf8_string, (unsigned char*)"😀", 4)) { if (fledasty_utf8_string_has_string(&test_utf8_string, (unsigned char*)"😀", 4)) {
printf("String contains 😀!\n"); printf("String contains 😀!\n");