Merge pull request 'f7-utf-8-string' (#23) from f7-utf-8-string into main
Reviewed-on: #23
This commit is contained in:
commit
b8baffdb39
10 changed files with 571 additions and 6 deletions
|
|
@ -55,6 +55,6 @@ FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList
|
|||
FledastyError fledasty_doubly_linked_list_clear(FledastyDoublyLinkedList *current_doubly_linked_list);
|
||||
|
||||
bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value);
|
||||
static inline bool fledasty_doubly_linked_list_is_empty(const FledastyDoublyLinkedList *current_doubly_linked_list) { return current_doubly_linked_list->size == 0; }
|
||||
static inline bool fledasty_doubly_linked_list_is_empty(const FledastyDoublyLinkedList *current_doubly_linked_list) { return current_doubly_linked_list == NULL || current_doubly_linked_list->size == 0; }
|
||||
|
||||
#endif
|
||||
|
|
@ -53,6 +53,6 @@ FledastyError fledasty_dynamic_array_remove_value(FledastyDynamicArray *current_
|
|||
FledastyError fledasty_dynamic_array_clear(FledastyDynamicArray *current_dynamic_array);
|
||||
|
||||
bool fledasty_dynamic_array_has_value(const FledastyDynamicArray *current_dynamic_array, void *value);
|
||||
inline static bool fledasty_dynamic_array_is_empty(const FledastyDynamicArray *current_dynamic_array) { return current_dynamic_array->size == 0; }
|
||||
inline static bool fledasty_dynamic_array_is_empty(const FledastyDynamicArray *current_dynamic_array) { return current_dynamic_array == NULL || current_dynamic_array->size == 0; }
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -53,6 +53,6 @@ FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table,
|
|||
FledastyError fledasty_hash_table_clear(FledastyHashTable *current_hash_table);
|
||||
|
||||
bool fledasty_hash_table_has_key(const FledastyHashTable *current_hash_table, void *key);
|
||||
static inline bool fledasty_hash_table_is_empty(const FledastyHashTable *current_hash_table) { return current_hash_table->size == 0; }
|
||||
static inline bool fledasty_hash_table_is_empty(const FledastyHashTable *current_hash_table) { return current_hash_table == NULL || current_hash_table->size == 0; }
|
||||
|
||||
#endif
|
||||
|
|
@ -54,6 +54,6 @@ FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_link
|
|||
FledastyError fledasty_linked_list_clear(FledastyLinkedList *current_linked_list);
|
||||
|
||||
bool fledasty_linked_list_has_value(const FledastyLinkedList *current_linked_list, void *value);
|
||||
static inline bool fledasty_linked_list_is_empty(const FledastyLinkedList *current_linked_list) { return current_linked_list->size == 0; }
|
||||
static inline bool fledasty_linked_list_is_empty(const FledastyLinkedList *current_linked_list) { return current_linked_list == NULL || current_linked_list->size == 0; }
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -45,6 +45,6 @@ void *fledasty_queue_pop(FledastyQueue *current_queue);
|
|||
|
||||
FledastyError fledasty_queue_clear(FledastyQueue *current_queue);
|
||||
|
||||
static inline bool fledasty_queue_is_empty(const FledastyQueue *current_queue) { return current_queue->size == 0; }
|
||||
static inline bool fledasty_queue_is_empty(const FledastyQueue *current_queue) { return current_queue == NULL || current_queue->size == 0; }
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -44,6 +44,6 @@ void *fledasty_stack_pop(FledastyStack *current_stack);
|
|||
|
||||
FledastyError fledasty_stack_clear(FledastyStack *current_stack);
|
||||
|
||||
static inline bool fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack->size == 0; }
|
||||
static inline bool fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack == NULL || current_stack->size == 0; }
|
||||
|
||||
#endif
|
||||
|
|
|
|||
59
Include/Fledasty/Strings/UTF8String.h
Normal file
59
Include/Fledasty/Strings/UTF8String.h
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
* File: UTF8String.h
|
||||
* Description:
|
||||
* This file contains the UTF8String structure and the functions for modifying it.
|
||||
* It includes functions to append, Insert at index, insert before character,
|
||||
* insert before string, insert after character, insert after string, replace,
|
||||
* copy, pop, remove, remove range, clear, check if contains string, check if
|
||||
* empty.
|
||||
*
|
||||
* Author: Mineplay
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "../Utils/Error.h"
|
||||
|
||||
typedef struct {
|
||||
size_t size, capacity;
|
||||
unsigned char *character_string;
|
||||
} FledastyUtf8String;
|
||||
|
||||
FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, const size_t character_string_size);
|
||||
FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string);
|
||||
|
||||
FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, unsigned char *character_string, const size_t character_string_size);
|
||||
FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_string, size_t index, unsigned char *character_string, const size_t character_string_size);
|
||||
FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *current_string, unsigned char *before_character_string, const size_t before_character_string_size, unsigned char *character_string, const size_t character_string_size);
|
||||
FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *current_string, unsigned char *after_character_string, const size_t after_character_string_size, unsigned char *character_string, const size_t character_string_size);
|
||||
|
||||
FledastyError fledasty_utf8_string_pop(FledastyUtf8String *current_string);
|
||||
FledastyError fledasty_utf8_string_remove(FledastyUtf8String *current_string, unsigned char *character_string, const size_t character_string_size);
|
||||
FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_string, const size_t start_index, const size_t end_index);
|
||||
FledastyError fledasty_utf8_string_clear(FledastyUtf8String *current_string);
|
||||
|
||||
FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_string, unsigned char *replace_character_string, const size_t replace_character_string_size, unsigned char *character_string, const size_t character_string_size);
|
||||
|
||||
bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, const size_t character_string_size);
|
||||
static inline bool fledasty_utf8_string_is_empty(const FledastyUtf8String *current_string) { return current_string == NULL || current_string->size == 0; }
|
||||
|
||||
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);
|
||||
size_t fledasty_utf8_string_get_size(const unsigned char *character_string);
|
||||
|
|
@ -30,6 +30,7 @@ typedef enum {
|
|||
FLEDASTY_ERROR_INDEX_OUT_OF_RANGE = 3,
|
||||
FLEDASTY_ERROR_VALUE_NOT_FOUND = 4,
|
||||
FLEDASTY_ERROR_KEY_NOT_FOUND = 5,
|
||||
FLEDASTY_ERROR_INVALID_VALUE = 6,
|
||||
} FledastyError;
|
||||
|
||||
#endif
|
||||
445
Src/Strings/UTF8String.c
Normal file
445
Src/Strings/UTF8String.c
Normal file
|
|
@ -0,0 +1,445 @@
|
|||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
* File: UTF8String.c
|
||||
* Description:
|
||||
* This file contains the functions for modifying the UTF-8 String. It includes
|
||||
* functions to append, Insert at index, insert before character,
|
||||
* insert before string, insert after character, insert after string, replace,
|
||||
* copy, pop, remove, remove range, clear, check if contains string, check if
|
||||
* empty.
|
||||
*
|
||||
* Author: Mineplay
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
#include "../../Include/Fledasty/Strings/UTF8String.h"
|
||||
#include "Fledasty/Utils/Error.h"
|
||||
|
||||
#include <Hallocy/Core/Allocator.h>
|
||||
#include <Hallocy/Core/Memory.h>
|
||||
#include <Hallocy/Utils/Error.h>
|
||||
|
||||
FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, const size_t character_string_size) {
|
||||
if (new_string == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
if (character_string == NULL || character_string_size == 0) {
|
||||
new_string->size = 0;
|
||||
new_string->capacity = 10;
|
||||
|
||||
new_string->character_string = (unsigned char*)hallocy_malloc(new_string->capacity);
|
||||
} else {
|
||||
if (!fledasty_utf8_string_validate(character_string, character_string_size)) {
|
||||
return FLEDASTY_ERROR_INVALID_VALUE;
|
||||
}
|
||||
|
||||
new_string->size = character_string_size;
|
||||
new_string->capacity = new_string->size + new_string->size;
|
||||
|
||||
new_string->character_string = (unsigned char*)hallocy_malloc(new_string->capacity);
|
||||
hallocy_copy_memory(new_string->character_string, character_string, character_string_size);
|
||||
}
|
||||
|
||||
new_string->character_string[new_string->size] = '\0';
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string) {
|
||||
if (current_string == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
HallocyError result = hallocy_free(current_string->character_string);
|
||||
if (result != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
current_string->character_string = NULL;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, unsigned char *character_string, const size_t character_string_size) {
|
||||
if (current_string == NULL || character_string == NULL || character_string_size == 0) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
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_copy_memory(current_string->character_string + (current_string->size - 1), character_string, character_string_size);
|
||||
|
||||
current_string->size += character_string_size;
|
||||
current_string->character_string[current_string->size] = '\0';
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_string, const size_t index, unsigned char *character_string, const 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, const size_t before_character_string_size, unsigned char *character_string, const 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;
|
||||
}
|
||||
|
||||
if (!fledasty_utf8_string_validate(before_character_string, before_character_string_size) || !fledasty_utf8_string_validate(character_string, character_string_size)) {
|
||||
return FLEDASTY_ERROR_INVALID_VALUE;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
while (index < (current_string->size - before_character_string_size) && !hallocy_compare_memory(current_string->character_string + index, before_character_string, before_character_string_size)) {
|
||||
index += 1;
|
||||
}
|
||||
|
||||
if (index == current_string->size - before_character_string_size) {
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
|
||||
}
|
||||
|
||||
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_after_string(FledastyUtf8String *current_string, unsigned char *after_character_string, const size_t after_character_string_size, unsigned char *character_string, const size_t character_string_size) {
|
||||
if (current_string == NULL || after_character_string == NULL || after_character_string_size == 0 || character_string == NULL || character_string_size == 0) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
if (!fledasty_utf8_string_validate(after_character_string, after_character_string_size) || !fledasty_utf8_string_validate(character_string, character_string_size)) {
|
||||
return FLEDASTY_ERROR_INVALID_VALUE;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
while (index < (current_string->size - after_character_string_size) && !hallocy_compare_memory(current_string->character_string + index, after_character_string, after_character_string_size)) {
|
||||
index += 1;
|
||||
}
|
||||
|
||||
if (index == current_string->size - after_character_string_size) {
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
index += after_character_string_size;
|
||||
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_pop(FledastyUtf8String *current_string) {
|
||||
if (current_string == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
current_string->character_string[current_string->size - 1] = '\0';
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_utf8_string_remove(FledastyUtf8String *current_string, unsigned char *character_string, const size_t character_string_size) {
|
||||
if (current_string == NULL || character_string == NULL || character_string_size == 0) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
if (!fledasty_utf8_string_validate(character_string, character_string_size)) {
|
||||
return FLEDASTY_ERROR_INVALID_VALUE;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
while (index < (current_string->size - character_string_size) && !hallocy_compare_memory(current_string->character_string + index, character_string, character_string_size)) {
|
||||
index += 1;
|
||||
}
|
||||
|
||||
if (index == current_string->size - character_string_size) {
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
|
||||
}
|
||||
|
||||
hallocy_move_memory(current_string->character_string + index, current_string->character_string + index + character_string_size, current_string->size - (index + character_string_size));
|
||||
current_string->size -= character_string_size;
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_string, const size_t start_index, const size_t end_index) {
|
||||
if (current_string == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
if (start_index > end_index || end_index > current_string->size) {
|
||||
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
|
||||
}
|
||||
|
||||
hallocy_move_memory(current_string->character_string + start_index, current_string->character_string + end_index, current_string->size - end_index);
|
||||
current_string->size -= end_index - start_index;
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_utf8_string_clear(FledastyUtf8String *current_string) {
|
||||
if (current_string == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
current_string->size = 0;
|
||||
current_string->character_string[0] = '\0';
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_string, unsigned char *replace_character_string, const size_t replace_character_string_size, unsigned char *character_string, const size_t character_string_size) {
|
||||
if (current_string == NULL || replace_character_string == NULL || replace_character_string_size == 0 || character_string == NULL || character_string_size == 0) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
if (!fledasty_utf8_string_validate(replace_character_string, replace_character_string_size) || !fledasty_utf8_string_validate(character_string, character_string_size)) {
|
||||
return FLEDASTY_ERROR_INVALID_VALUE;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
while (index < current_string->size - replace_character_string_size && !hallocy_compare_memory(current_string->character_string + index, replace_character_string, replace_character_string_size)) {
|
||||
index += 1;
|
||||
}
|
||||
|
||||
if (index == current_string->size - replace_character_string_size) {
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
|
||||
}
|
||||
|
||||
hallocy_move_memory(current_string->character_string + index + character_string_size, current_string->character_string + index + replace_character_string_size, current_string->size - (index + replace_character_string_size));
|
||||
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size);
|
||||
|
||||
current_string->size += character_string_size - replace_character_string_size;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, const size_t character_string_size) {
|
||||
if (current_string == NULL || character_string == NULL || character_string_size == 0) {
|
||||
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)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
index += 1;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const size_t size) {
|
||||
FledastyUtf8String utf8_string;
|
||||
fledasty_utf8_string_initialize(&utf8_string, NULL, 0);
|
||||
|
||||
if (unicode == NULL) {
|
||||
return utf8_string;
|
||||
}
|
||||
|
||||
size_t string_index = 0;
|
||||
for (size_t index = 0; index < size; index += 1) {
|
||||
if (unicode[index] <= 0x00007F) {
|
||||
if (utf8_string.capacity <= string_index) {
|
||||
utf8_string.capacity += 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];
|
||||
string_index += 1;
|
||||
} else if (unicode[index] <= 0x0007FF) {
|
||||
if (utf8_string.capacity <= string_index + 2) {
|
||||
utf8_string.capacity += 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);
|
||||
utf8_string.character_string[string_index + 1] = 0x80 | (unicode[index] & 0x3F);
|
||||
string_index += 2;
|
||||
} else if (unicode[index] <= 0x00FFFF) {
|
||||
if (utf8_string.capacity <= string_index + 3) {
|
||||
utf8_string.capacity += 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);
|
||||
utf8_string.character_string[string_index + 1] = 0x80 | ((unicode[index] >> 6) & 0x3F);
|
||||
utf8_string.character_string[string_index + 2] = 0x80 | (unicode[index] & 0x3F);
|
||||
string_index += 3;
|
||||
} else if (unicode[index] <= 0x10FFFF) {
|
||||
if (utf8_string.capacity <= string_index + 4) {
|
||||
utf8_string.capacity += 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);
|
||||
utf8_string.character_string[string_index + 1] = 0x80 | ((unicode[index] >> 12) & 0x3F);
|
||||
utf8_string.character_string[string_index + 2] = 0x80 | ((unicode[index] >> 6) & 0x3F);
|
||||
utf8_string.character_string[string_index + 3] = 0x80 | (unicode[index] & 0x3F);
|
||||
string_index += 4;
|
||||
}
|
||||
}
|
||||
|
||||
utf8_string.size = string_index;
|
||||
if (utf8_string.capacity <= utf8_string.size + 1) {
|
||||
utf8_string.capacity += 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';
|
||||
return utf8_string;
|
||||
}
|
||||
|
||||
uint32_t *fledasty_utf8_string_decode(const FledastyUtf8String *current_string, size_t *unicode_string_size) {
|
||||
if (current_string == NULL || unicode_string_size == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
(*unicode_string_size) = 0;
|
||||
size_t index = 0;
|
||||
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_size] = ((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);
|
||||
index += 4;
|
||||
} else if ((current_string->character_string[index] & 0xE0) == 0xE0) {
|
||||
unicode_string[*unicode_string_size] = ((current_string->character_string[index] & 0x0F) << 12) | ((current_string->character_string[index + 1] & 0x3F) << 6) | (current_string->character_string[index + 2] & 0x3F);
|
||||
index += 3;
|
||||
} else if ((current_string->character_string[index] & 0xC0) == 0xC0) {
|
||||
unicode_string[*unicode_string_size] = ((current_string->character_string[index] & 0x1F) << 6) | (current_string->character_string[index + 1] & 0x3F);
|
||||
index += 2;
|
||||
} else {
|
||||
unicode_string[*unicode_string_size] = current_string->character_string[index];
|
||||
index += 1;
|
||||
}
|
||||
|
||||
(*unicode_string_size) += 1;
|
||||
}
|
||||
|
||||
return unicode_string;
|
||||
}
|
||||
|
||||
bool fledasty_utf8_string_validate(unsigned char *character_string, const size_t character_string_size) {
|
||||
if (character_string == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
while (index < character_string_size) {
|
||||
if ((character_string[index] & 0xF0) == 0xF0) {
|
||||
if (index + 3 >= character_string_size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((character_string[index + 1] & 0xC0) != 0x80) {
|
||||
return false;
|
||||
} else if ((character_string[index + 2] & 0xC0) != 0x80) {
|
||||
return false;
|
||||
} else if ((character_string[index + 3] & 0xC0) != 0x80) {
|
||||
return false;
|
||||
}
|
||||
|
||||
index += 4;
|
||||
} else if ((character_string[index] & 0xE0) == 0xC0) {
|
||||
if (index + 2 >= character_string_size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((character_string[index + 1] & 0xC0) != 0x80) {
|
||||
return false;
|
||||
} else if ((character_string[index + 2] & 0xC0) != 0x80) {
|
||||
return false;
|
||||
}
|
||||
|
||||
index += 3;
|
||||
} else if ((character_string[index] & 0xC0) == 0xC0) {
|
||||
if (index + 1 >= character_string_size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((character_string[index + 1] & 0xC0) != 0x80) {
|
||||
return false;
|
||||
}
|
||||
|
||||
index += 2;
|
||||
} else {
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t fledasty_utf8_string_get_size(const unsigned char *character_string) {
|
||||
size_t size = 0;
|
||||
while (character_string[size] != '\0') {
|
||||
size += 1;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
60
Tests/Main.c
60
Tests/Main.c
|
|
@ -20,12 +20,14 @@
|
|||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <Hallocy/Core/Allocator.h>
|
||||
#include <Fledasty/Core/Queue.h>
|
||||
#include <Fledasty/Core/Stack.h>
|
||||
#include <Fledasty/Core/DynamicArray.h>
|
||||
#include <Fledasty/Core/LinkedList.h>
|
||||
#include <Fledasty/Core/DoublyLinkedList.h>
|
||||
#include <Fledasty/Core/HashTable.h>
|
||||
#include <Fledasty/Strings/UTF8String.h>
|
||||
|
||||
static inline size_t integer_hash_function(void *key) { return *(size_t*)key; }
|
||||
|
||||
|
|
@ -221,6 +223,64 @@ int main() {
|
|||
}
|
||||
|
||||
fledasty_hash_table_destroy(&test_hash_table);
|
||||
FledastyUtf8String test_utf8_string;
|
||||
unsigned char *test_string = (unsigned char*)"😀€Testing";
|
||||
fledasty_utf8_string_initialize(&test_utf8_string, test_string, 15);
|
||||
printf("%s\n", test_string);
|
||||
printf("%s\n", test_utf8_string.character_string);
|
||||
|
||||
fledasty_utf8_string_append(&test_utf8_string, (unsigned char*)"😀", 4);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
fledasty_utf8_string_remove(&test_utf8_string, (unsigned char*)"𓃶 ", 5);
|
||||
printf("Remove: %s\n", test_utf8_string.character_string);
|
||||
fledasty_utf8_string_remove_range(&test_utf8_string, 0, 5);
|
||||
printf("Remove range: %s\n", test_utf8_string.character_string);
|
||||
|
||||
if (fledasty_utf8_string_has_string(&test_utf8_string, (unsigned char*)"😀", 4)) {
|
||||
printf("String contains 😀!\n");
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
printf("%s\n", encoded_string.character_string);
|
||||
|
||||
if (fledasty_utf8_string_validate(test_utf8_string.character_string, encoded_string.size)) {
|
||||
printf("UTF-8 test string is valid!\n");
|
||||
}
|
||||
|
||||
if (fledasty_utf8_string_validate(encoded_string.character_string, encoded_string.size)) {
|
||||
printf("UTF-8 encoded string is valid!\n");
|
||||
}
|
||||
|
||||
unsigned char *invalid_utf8 = (unsigned char*)hallocy_malloc(2 * sizeof(unsigned char));
|
||||
invalid_utf8[0] = 0xDF;
|
||||
invalid_utf8[1] = 0xFF;
|
||||
if (!fledasty_utf8_string_validate(invalid_utf8, 2)) {
|
||||
printf("UTF-8 invalid string is invalid!\n");
|
||||
}
|
||||
|
||||
fledasty_utf8_string_clear(&test_utf8_string);
|
||||
if (fledasty_utf8_string_is_empty(&test_utf8_string)) {
|
||||
printf("UTF-8 string is empty!\n");
|
||||
}
|
||||
|
||||
hallocy_free(invalid_utf8);
|
||||
hallocy_free(unicode);
|
||||
fledasty_utf8_string_destroy(&encoded_string);
|
||||
|
||||
fledasty_utf8_string_destroy(&test_utf8_string);
|
||||
printf("Done\n");
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue