feat(utf-8): implemented insert before function

This commit is contained in:
Mineplay 2025-05-12 03:48:08 -05:00
parent 243bb533b0
commit bd9fef9f4f
3 changed files with 40 additions and 2 deletions

View file

@ -38,6 +38,7 @@ FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, un
FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string); FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string);
FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_length); FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_length);
FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *current_string, unsigned char *before_character_string, size_t before_character_string_length, unsigned char *character_string, size_t character_string_length);
FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, const size_t size); FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, const size_t size);
uint32_t *fledasty_utf8_string_decode(FledastyUtf8String *current_string, size_t *unicode_string_length); uint32_t *fledasty_utf8_string_decode(FledastyUtf8String *current_string, size_t *unicode_string_length);

View file

@ -78,8 +78,12 @@ FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, un
return FLEDASTY_ERROR_INVALID_POINTER; return FLEDASTY_ERROR_INVALID_POINTER;
} }
if (!fledasty_utf8_string_validate(character_string, character_string_length)) {
return FLEDASTY_ERROR_INVALID_VALUE;
}
if (current_string->capacity <= current_string->size + character_string_length) { if (current_string->capacity <= current_string->size + character_string_length) {
current_string->capacity += current_string->capacity; current_string->capacity += (current_string->capacity > character_string_length) ? current_string->capacity : character_string_length;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char)); current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
} }
@ -91,6 +95,36 @@ FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, un
return FLEDASTY_ERROR_NONE; return FLEDASTY_ERROR_NONE;
} }
FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *current_string, unsigned char *before_character_string, size_t before_character_string_length, unsigned char *character_string, size_t character_string_length) {
if (current_string == NULL || before_character_string == NULL || before_character_string_length == 0 || character_string == NULL || character_string_length == 0) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (!fledasty_utf8_string_validate(before_character_string, before_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 - before_character_string_length) && !hallocy_compare_memory(current_string->character_string + index, before_character_string, before_character_string_length)) {
index += 1;
}
if (index == current_string->size - before_character_string_length) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
if (current_string->capacity <= current_string->size + character_string_length) {
current_string->capacity += (current_string->capacity > character_string_length) ? current_string->capacity : character_string_length;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
}
hallocy_move_memory(current_string->character_string + (index + character_string_length), current_string->character_string + index, current_string->size - index);
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_length);
current_string->size += character_string_length;
return FLEDASTY_ERROR_NONE;
}
FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, const size_t size) { FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, const size_t size) {
FledastyUtf8String utf8_string; FledastyUtf8String utf8_string;
fledasty_utf8_string_initialize(&utf8_string, NULL, 0); fledasty_utf8_string_initialize(&utf8_string, NULL, 0);

View file

@ -19,6 +19,7 @@
* Author: Mineplay * Author: Mineplay
* ----------------------------------------------------------------------------- * -----------------------------------------------------------------------------
*/ */
#include "Fledasty/Utils/Error.h"
#include <stdio.h> #include <stdio.h>
#include <Hallocy/Core/Allocator.h> #include <Hallocy/Core/Allocator.h>
#include <Fledasty/Core/Queue.h> #include <Fledasty/Core/Queue.h>
@ -230,7 +231,9 @@ int main() {
printf("%s\n", test_utf8_string.character_string); printf("%s\n", test_utf8_string.character_string);
fledasty_utf8_string_append(&test_utf8_string, (unsigned char*)"😀", 4); fledasty_utf8_string_append(&test_utf8_string, (unsigned char*)"😀", 4);
printf("%s\n", test_utf8_string.character_string); printf("Append: %s\n", test_utf8_string.character_string);
fledasty_utf8_string_insert_before_string(&test_utf8_string, (unsigned char*)"😀", 4, (unsigned char*)"Hello", 5);
printf("Insert Before: %s\n", test_utf8_string.character_string);
size_t unicode_length = 0; size_t unicode_length = 0;
uint32_t *unicode = fledasty_utf8_string_decode(&test_utf8_string, &unicode_length); uint32_t *unicode = fledasty_utf8_string_decode(&test_utf8_string, &unicode_length);