feat(utf-8 string): implemented insert at index function
This commit is contained in:
parent
e4a0236328
commit
8e237dae14
3 changed files with 28 additions and 0 deletions
|
|
@ -40,6 +40,7 @@ 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);
|
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);
|
||||||
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);
|
||||||
|
|
||||||
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);
|
||||||
|
|
|
||||||
|
|
@ -156,6 +156,31 @@ FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *curre
|
||||||
return FLEDASTY_ERROR_NONE;
|
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_length) {
|
||||||
|
if (current_string == NULL || character_string == NULL || character_string_length == 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_length)) {
|
||||||
|
return FLEDASTY_ERROR_INVALID_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
|
||||||
|
|
@ -235,6 +235,8 @@ int main() {
|
||||||
printf("Insert Before: %s\n", test_utf8_string.character_string);
|
printf("Insert Before: %s\n", test_utf8_string.character_string);
|
||||||
fledasty_utf8_string_insert_after_string(&test_utf8_string, (unsigned char*)"😀", 4, (unsigned char*)"Bye", 3);
|
fledasty_utf8_string_insert_after_string(&test_utf8_string, (unsigned char*)"😀", 4, (unsigned char*)"Bye", 3);
|
||||||
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);
|
||||||
|
printf("Insert at Index: %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);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue