feat(utf-8 string): implemented append function
This commit is contained in:
parent
13a95d9027
commit
243bb533b0
3 changed files with 32 additions and 8 deletions
|
|
@ -37,6 +37,8 @@ typedef struct {
|
|||
FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, size_t character_string_length);
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
@ -39,7 +40,7 @@ FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, un
|
|||
new_string->size = 0;
|
||||
new_string->capacity = 10;
|
||||
|
||||
new_string->character_string = hallocy_malloc(new_string->capacity);
|
||||
new_string->character_string = (unsigned char*)hallocy_malloc(new_string->capacity);
|
||||
} else {
|
||||
if (!fledasty_utf8_string_validate(character_string, character_string_length)) {
|
||||
return FLEDASTY_ERROR_INVALID_VALUE;
|
||||
|
|
@ -48,7 +49,7 @@ FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, un
|
|||
new_string->size = character_string_length;
|
||||
new_string->capacity = new_string->size + new_string->size;
|
||||
|
||||
new_string->character_string = hallocy_malloc(new_string->capacity);
|
||||
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];
|
||||
}
|
||||
|
|
@ -72,6 +73,24 @@ FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string) {
|
|||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_length) {
|
||||
if (current_string == NULL || character_string == NULL || character_string_length == 0) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
if (current_string->capacity <= current_string->size + character_string_length) {
|
||||
current_string->capacity += current_string->capacity;
|
||||
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
|
||||
}
|
||||
|
||||
hallocy_copy_memory(current_string->character_string + (current_string->size - 1), character_string, character_string_length);
|
||||
|
||||
current_string->size += character_string_length;
|
||||
current_string->character_string[current_string->size] = '\0';
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, const size_t size) {
|
||||
FledastyUtf8String utf8_string;
|
||||
fledasty_utf8_string_initialize(&utf8_string, NULL, 0);
|
||||
|
|
@ -85,7 +104,7 @@ FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, const size_t s
|
|||
if (unicode[index] <= 0x00007F) {
|
||||
if (utf8_string.capacity <= string_index) {
|
||||
utf8_string.capacity += utf8_string.capacity;
|
||||
utf8_string.character_string = hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
|
||||
utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
|
||||
}
|
||||
|
||||
utf8_string.character_string[string_index] = unicode[index];
|
||||
|
|
@ -93,7 +112,7 @@ FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, const size_t s
|
|||
} else if (unicode[index] <= 0x0007FF) {
|
||||
if (utf8_string.capacity <= string_index + 2) {
|
||||
utf8_string.capacity += utf8_string.capacity;
|
||||
utf8_string.character_string = hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
|
||||
utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
|
||||
}
|
||||
|
||||
utf8_string.character_string[string_index] = 0xC0 | ((unicode[index] >> 6) & 0x07);
|
||||
|
|
@ -102,7 +121,7 @@ FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, const size_t s
|
|||
} else if (unicode[index] <= 0x00FFFF) {
|
||||
if (utf8_string.capacity <= string_index + 3) {
|
||||
utf8_string.capacity += utf8_string.capacity;
|
||||
utf8_string.character_string = hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
|
||||
utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
|
||||
}
|
||||
|
||||
utf8_string.character_string[string_index] = 0xE0 | ((unicode[index] >> 12) & 0x07);
|
||||
|
|
@ -112,7 +131,7 @@ FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, const size_t s
|
|||
} else if (unicode[index] <= 0x10FFFF) {
|
||||
if (utf8_string.capacity <= string_index + 4) {
|
||||
utf8_string.capacity += utf8_string.capacity;
|
||||
utf8_string.character_string = hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
|
||||
utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
|
||||
}
|
||||
|
||||
utf8_string.character_string[string_index] = 0xF0 | ((unicode[index] >> 18) & 0x07);
|
||||
|
|
@ -126,7 +145,7 @@ FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, const size_t s
|
|||
utf8_string.size = string_index;
|
||||
if (utf8_string.capacity <= utf8_string.size + 1) {
|
||||
utf8_string.capacity += utf8_string.capacity;
|
||||
utf8_string.character_string = hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
|
||||
utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
|
||||
}
|
||||
|
||||
utf8_string.character_string[utf8_string.size] = '\0';
|
||||
|
|
@ -140,7 +159,7 @@ uint32_t *fledasty_utf8_string_decode(FledastyUtf8String *current_string, size_t
|
|||
|
||||
(*unicode_string_length) = 0;
|
||||
size_t index = 0;
|
||||
uint32_t *unicode_string = hallocy_malloc(current_string->size * sizeof(uint32_t));
|
||||
uint32_t *unicode_string = (uint32_t*)hallocy_malloc(current_string->size * sizeof(uint32_t));
|
||||
while (index < current_string->size) {
|
||||
if ((current_string->character_string[index] & 0xF0) == 0xF0) {
|
||||
unicode_string[*unicode_string_length] = ((current_string->character_string[index] & 0x07) << 18) | ((current_string->character_string[index + 1] & 0x3F) << 12) | ((current_string->character_string[index + 2] & 0x3F) << 6) | (current_string->character_string[index + 3] & 0x3F);
|
||||
|
|
|
|||
|
|
@ -229,6 +229,9 @@ int main() {
|
|||
printf("%s\n", test_string);
|
||||
printf("%s\n", test_utf8_string.character_string);
|
||||
|
||||
fledasty_utf8_string_append(&test_utf8_string, (unsigned char*)"😀", 4);
|
||||
printf("%s\n", test_utf8_string.character_string);
|
||||
|
||||
size_t unicode_length = 0;
|
||||
uint32_t *unicode = fledasty_utf8_string_decode(&test_utf8_string, &unicode_length);
|
||||
FledastyUtf8String encoded_string = fledasty_utf8_string_encode(unicode, unicode_length);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue