refactor(utf-8 string): changed parameters name to use size instead of length to be more consistent
This commit is contained in:
parent
fbdebde492
commit
1ab803f0f2
2 changed files with 74 additions and 74 deletions
|
|
@ -34,20 +34,20 @@ typedef struct {
|
|||
unsigned char *character_string;
|
||||
} FledastyUtf8String;
|
||||
|
||||
FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, size_t character_string_length);
|
||||
FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, 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, 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_at_index(FledastyUtf8String *current_string, size_t index, 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_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_replace_string(FledastyUtf8String *current_string, unsigned char *replace_character_string, size_t replace_character_string_length, unsigned char *character_string, size_t character_string_length);
|
||||
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);
|
||||
|
||||
bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_length);
|
||||
bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, 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_length);
|
||||
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_length);
|
||||
bool fledasty_utf8_string_validate(unsigned char *character_string, const size_t character_string_size);
|
||||
|
|
@ -30,22 +30,22 @@
|
|||
#include <Hallocy/Core/Memory.h>
|
||||
#include <Hallocy/Utils/Error.h>
|
||||
|
||||
FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, size_t character_string_length) {
|
||||
FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, size_t character_string_size) {
|
||||
if (new_string == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
if (character_string == NULL || character_string_length == 0) {
|
||||
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_length)) {
|
||||
if (!fledasty_utf8_string_validate(character_string, character_string_size)) {
|
||||
return FLEDASTY_ERROR_INVALID_VALUE;
|
||||
}
|
||||
|
||||
new_string->size = character_string_length;
|
||||
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);
|
||||
|
|
@ -72,91 +72,91 @@ 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) {
|
||||
FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, 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 (!fledasty_utf8_string_validate(character_string, character_string_length)) {
|
||||
if (!fledasty_utf8_string_validate(character_string, character_string_size)) {
|
||||
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;
|
||||
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_length);
|
||||
hallocy_copy_memory(current_string->character_string + (current_string->size - 1), character_string, character_string_size);
|
||||
|
||||
current_string->size += character_string_length;
|
||||
current_string->size += character_string_size;
|
||||
current_string->character_string[current_string->size] = '\0';
|
||||
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
if (!fledasty_utf8_string_validate(before_character_string, before_character_string_length) || !fledasty_utf8_string_validate(character_string, character_string_length)) {
|
||||
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_length) && !hallocy_compare_memory(current_string->character_string + index, before_character_string, before_character_string_length)) {
|
||||
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_length) {
|
||||
if (index == current_string->size - before_character_string_size) {
|
||||
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;
|
||||
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_length), current_string->character_string + index, current_string->size - index);
|
||||
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_length);
|
||||
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_length;
|
||||
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, size_t after_character_string_length, unsigned char *character_string, size_t character_string_length) {
|
||||
if (current_string == NULL || after_character_string == NULL || after_character_string_length == 0 || character_string == NULL || character_string_length == 0) {
|
||||
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) {
|
||||
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_length) || !fledasty_utf8_string_validate(character_string, character_string_length)) {
|
||||
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_length) && !hallocy_compare_memory(current_string->character_string + index, after_character_string, after_character_string_length)) {
|
||||
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_length) {
|
||||
if (index == current_string->size - after_character_string_size) {
|
||||
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;
|
||||
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_length;
|
||||
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);
|
||||
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_length;
|
||||
current_string->size += character_string_size;
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
@ -164,55 +164,55 @@ FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_s
|
|||
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
|
||||
}
|
||||
|
||||
if (!fledasty_utf8_string_validate(character_string, character_string_length)) {
|
||||
if (!fledasty_utf8_string_validate(character_string, character_string_size)) {
|
||||
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;
|
||||
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_length), current_string->character_string + index, current_string->size - index);
|
||||
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_length);
|
||||
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_length;
|
||||
current_string->size += character_string_size;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_string, unsigned char *replace_character_string, size_t replace_character_string_length, unsigned char *character_string, size_t character_string_length) {
|
||||
if (current_string == NULL || replace_character_string == NULL || replace_character_string_length == 0 || character_string == NULL || character_string_length == 0) {
|
||||
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) {
|
||||
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_length) || !fledasty_utf8_string_validate(character_string, character_string_length)) {
|
||||
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_length && !hallocy_compare_memory(current_string->character_string + index, replace_character_string, replace_character_string_length)) {
|
||||
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_length) {
|
||||
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_length, current_string->character_string + index + replace_character_string_length, current_string->size - (index + replace_character_string_length));
|
||||
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_length);
|
||||
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_length - replace_character_string_length;
|
||||
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, size_t character_string_length) {
|
||||
if (current_string == NULL || character_string == NULL || character_string_length == 0) {
|
||||
bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_size) {
|
||||
if (current_string == NULL || character_string == NULL || character_string_size == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
while (index < current_string->size - character_string_length) {
|
||||
if (hallocy_compare_memory(current_string->character_string + index, character_string, character_string_length)) {
|
||||
while (index < current_string->size - character_string_size) {
|
||||
if (hallocy_compare_memory(current_string->character_string + index, character_string, character_string_size)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -283,44 +283,44 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si
|
|||
return utf8_string;
|
||||
}
|
||||
|
||||
uint32_t *fledasty_utf8_string_decode(const FledastyUtf8String *current_string, size_t *unicode_string_length) {
|
||||
if (current_string == NULL || unicode_string_length == NULL) {
|
||||
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_length) = 0;
|
||||
(*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_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);
|
||||
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_length] = ((current_string->character_string[index] & 0x0F) << 12) | ((current_string->character_string[index + 1] & 0x3F) << 6) | (current_string->character_string[index + 2] & 0x3F);
|
||||
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_length] = ((current_string->character_string[index] & 0x1F) << 6) | (current_string->character_string[index + 1] & 0x3F);
|
||||
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_length] = current_string->character_string[index];
|
||||
unicode_string[*unicode_string_size] = current_string->character_string[index];
|
||||
index += 1;
|
||||
}
|
||||
|
||||
(*unicode_string_length) += 1;
|
||||
(*unicode_string_size) += 1;
|
||||
}
|
||||
|
||||
return unicode_string;
|
||||
}
|
||||
|
||||
bool fledasty_utf8_string_validate(unsigned char *character_string, const size_t character_string_length) {
|
||||
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_length) {
|
||||
while (index < character_string_size) {
|
||||
if ((character_string[index] & 0xF0) == 0xF0) {
|
||||
if (index + 3 >= character_string_length) {
|
||||
if (index + 3 >= character_string_size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -334,7 +334,7 @@ bool fledasty_utf8_string_validate(unsigned char *character_string, const size_t
|
|||
|
||||
index += 4;
|
||||
} else if ((character_string[index] & 0xE0) == 0xC0) {
|
||||
if (index + 2 >= character_string_length) {
|
||||
if (index + 2 >= character_string_size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -346,7 +346,7 @@ bool fledasty_utf8_string_validate(unsigned char *character_string, const size_t
|
|||
|
||||
index += 3;
|
||||
} else if ((character_string[index] & 0xC0) == 0xC0) {
|
||||
if (index + 1 >= character_string_length) {
|
||||
if (index + 1 >= character_string_size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue