diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index a7fb399..b25f8dd 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -38,9 +38,11 @@ FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, un FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string); FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_size); +FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_string, size_t index, unsigned char *character_string, size_t character_string_size); FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *current_string, unsigned char *before_character_string, size_t before_character_string_size, unsigned char *character_string, size_t character_string_size); FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *current_string, unsigned char *after_character_string, size_t after_character_string_size, unsigned char *character_string, size_t character_string_size); -FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_string, size_t index, unsigned char *character_string, size_t character_string_size); + +FledastyError fledasty_utf8_string_pop(FledastyUtf8String *current_string); FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_string, unsigned char *replace_character_string, size_t replace_character_string_size, unsigned char *character_string, size_t character_string_size); @@ -50,4 +52,4 @@ static inline bool fledasty_utf8_string_is_empty(const FledastyUtf8String *curre FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const size_t size); uint32_t *fledasty_utf8_string_decode(const FledastyUtf8String *current_string, size_t *unicode_string_size); -bool fledasty_utf8_string_validate(unsigned char *character_string, const size_t character_string_size); \ No newline at end of file +bool fledasty_utf8_string_validate(unsigned char *character_string, const size_t character_string_size); diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index 72c30fe..e50786f 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -49,9 +49,7 @@ FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, un new_string->capacity = new_string->size + new_string->size; new_string->character_string = (unsigned char*)hallocy_malloc(new_string->capacity); - for (size_t index = 0; index < new_string->size; index += 1) { - new_string->character_string[index] = character_string[index]; - } + hallocy_copy_memory(new_string->character_string, character_string, character_string_size); } new_string->character_string[new_string->size] = '\0'; @@ -94,6 +92,31 @@ FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, un return FLEDASTY_ERROR_NONE; } +FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_string, size_t index, unsigned char *character_string, 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 (!fledasty_utf8_string_validate(character_string, character_string_size)) { + return FLEDASTY_ERROR_INVALID_VALUE; + } + + 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 = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char)); + } + + 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; + return FLEDASTY_ERROR_NONE; +} + FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *current_string, unsigned char *before_character_string, size_t before_character_string_size, unsigned char *character_string, size_t character_string_size) { if (current_string == NULL || before_character_string == NULL || before_character_string_size == 0 || character_string == NULL || character_string_size == 0) { return FLEDASTY_ERROR_INVALID_POINTER; @@ -155,28 +178,23 @@ FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *curre return FLEDASTY_ERROR_NONE; } -FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_string, size_t index, unsigned char *character_string, size_t character_string_size) { - if (current_string == NULL || character_string == NULL || character_string_size == 0) { +FledastyError fledasty_utf8_string_pop(FledastyUtf8String *current_string) { + if (current_string == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } - if (index >= current_string->size) { - return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; + if ((current_string->character_string[current_string->size - 5] & 0xF0) == 0xF0) { + current_string->size -= 4; + } else if ((current_string->character_string[current_string->size - 4] & 0xE0) == 0xC0) { + current_string->size -= 3; + } else if ((current_string->character_string[current_string->size - 3] & 0xC0) == 0xC0) { + current_string->size -= 2; + } else { + current_string->size -= 1; } - if (!fledasty_utf8_string_validate(character_string, character_string_size)) { - return FLEDASTY_ERROR_INVALID_VALUE; - } + current_string->character_string[current_string->size - 1] = '\0'; - 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 = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char)); - } - - 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; return FLEDASTY_ERROR_NONE; } @@ -210,6 +228,10 @@ bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, u return false; } + if (!fledasty_utf8_string_validate(character_string, character_string_size)) { + return false; + } + size_t index = 0; while (index < current_string->size - character_string_size) { if (hallocy_compare_memory(current_string->character_string + index, character_string, character_string_size)) { diff --git a/Tests/Main.c b/Tests/Main.c index bd7cc1e..0906c20 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -239,6 +239,8 @@ int main() { 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); + fledasty_utf8_string_pop(&test_utf8_string); + printf("Pop: %s\n", test_utf8_string.character_string); if (fledasty_utf8_string_has_string(&test_utf8_string, (unsigned char*)"😀", 4)) { printf("String contains 😀!\n");