From c4db620fb42704bfeaf8865397fbc3a5e4104cd4 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 2 May 2025 08:09:46 -0500 Subject: [PATCH 01/18] feat(utf-8 string): added utf-8 string structure and implemented initialize and destroy --- Include/Fledasty/Strings/UTF8String.h | 38 ++++++++++++++++ Src/Strings/UTF8String.c | 65 +++++++++++++++++++++++++++ Tests/Main.c | 5 +++ 3 files changed, 108 insertions(+) create mode 100644 Include/Fledasty/Strings/UTF8String.h create mode 100644 Src/Strings/UTF8String.c diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h new file mode 100644 index 0000000..c111dac --- /dev/null +++ b/Include/Fledasty/Strings/UTF8String.h @@ -0,0 +1,38 @@ +/* + * 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 +#include + +#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, size_t character_string_length); +FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string); \ No newline at end of file diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c new file mode 100644 index 0000000..f77840d --- /dev/null +++ b/Src/Strings/UTF8String.c @@ -0,0 +1,65 @@ +/* + * 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 +#include +#include + +FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, size_t character_string_length) { + if (new_string == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + if (character_string == NULL || character_string_length == 0) { + new_string->size = 0; + new_string->capacity = 10; + + new_string->character_string = hallocy_malloc(new_string->capacity); + } else { + 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[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; +} diff --git a/Tests/Main.c b/Tests/Main.c index f0cd693..003cf44 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -26,6 +26,7 @@ #include #include #include +#include static inline size_t integer_hash_function(void *key) { return *(size_t*)key; } @@ -221,6 +222,10 @@ int main() { } fledasty_hash_table_destroy(&test_hash_table); + FledastyUtf8String test_utf8_string; + fledasty_utf8_string_initialize(&test_utf8_string, "€Testing", 11); + + fledasty_utf8_string_destroy(&test_utf8_string); printf("Done\n"); return 0; } \ No newline at end of file -- 2.39.5 From 23533581995f0f707cb49233c0bf7986345ea668 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Sun, 4 May 2025 06:54:17 -0500 Subject: [PATCH 02/18] feat(utf-8 string): added setting value of utf-8 string through unsigned char* --- Include/Fledasty/Strings/UTF8String.h | 4 +++- Src/Strings/UTF8String.c | 3 +++ Tests/Main.c | 5 ++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index c111dac..1c7f962 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -35,4 +35,6 @@ typedef struct { } FledastyUtf8String; 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); \ No newline at end of file +FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string); + +FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, unsigned char *character_string); \ No newline at end of file diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index f77840d..ecdb15e 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -44,6 +44,9 @@ FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, un new_string->capacity = new_string->size + new_string->size; new_string->character_string = hallocy_malloc(new_string->capacity); + for (size_t index = 0; index < new_string->size; index += 1) { + new_string->character_string[index] = character_string[index]; + } } new_string->character_string[new_string->size] = '\0'; diff --git a/Tests/Main.c b/Tests/Main.c index 003cf44..0c34757 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -223,7 +223,10 @@ int main() { fledasty_hash_table_destroy(&test_hash_table); FledastyUtf8String test_utf8_string; - fledasty_utf8_string_initialize(&test_utf8_string, "€Testing", 11); + unsigned char *test_string = (unsigned char*)"πŸ˜€β‚¬Testing"; + fledasty_utf8_string_initialize(&test_utf8_string, test_string, 11); + printf("%s\n", test_string); + printf("%s\n", test_utf8_string.character_string); fledasty_utf8_string_destroy(&test_utf8_string); printf("Done\n"); -- 2.39.5 From e9d8cdd8a333b36df3d8bf5412fd51d73e3cf220 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Wed, 7 May 2025 04:33:25 -0500 Subject: [PATCH 03/18] feat(utf-8 string): implemented encoding and decoding of utf-8 string with unicode --- Include/Fledasty/Strings/UTF8String.h | 4 ++ Src/Strings/UTF8String.c | 93 ++++++++++++++++++++++++++- Tests/Main.c | 12 +++- 3 files changed, 107 insertions(+), 2 deletions(-) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index 1c7f962..d46ee75 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -25,6 +25,7 @@ */ #include +#include #include #include "../Utils/Error.h" @@ -37,4 +38,7 @@ 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); +FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, size_t size); +uint32_t *fledasty_utf8_string_decode(FledastyUtf8String *current_string, size_t *unicode_string_length); + FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, unsigned char *character_string); \ No newline at end of file diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index ecdb15e..147c26c 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -28,13 +28,14 @@ #include #include #include +#include FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, size_t character_string_length) { if (new_string == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } - if (character_string == NULL || character_string_length == 0) { + if (character_string == NULL) { new_string->size = 0; new_string->capacity = 10; @@ -66,3 +67,93 @@ FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string) { current_string->character_string = NULL; return FLEDASTY_ERROR_NONE; } + +FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, 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 = 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 = 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 = 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 = 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 = 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(FledastyUtf8String *current_string, size_t *unicode_string_length) { + if (current_string == NULL) { + return NULL; + } + + (*unicode_string_length) = 0; + size_t index = 0; + uint32_t *unicode_string = 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); + 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); + 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); + index += 2; + } else { + unicode_string[*unicode_string_length] = current_string->character_string[index]; + index += 1; + } + + (*unicode_string_length) += 1; + } + + return unicode_string; +} diff --git a/Tests/Main.c b/Tests/Main.c index 0c34757..01895d8 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -20,6 +20,7 @@ * ----------------------------------------------------------------------------- */ #include +#include #include #include #include @@ -224,10 +225,19 @@ 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, 11); + fledasty_utf8_string_initialize(&test_utf8_string, test_string, 15); printf("%s\n", test_string); 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); + + printf("%s\n", encoded_string.character_string); + + hallocy_free(unicode); + fledasty_utf8_string_destroy(&encoded_string); + fledasty_utf8_string_destroy(&test_utf8_string); printf("Done\n"); return 0; -- 2.39.5 From 13a95d9027bc40bd8b52b5b40719638ab92fdb6d Mon Sep 17 00:00:00 2001 From: Mineplay Date: Sun, 11 May 2025 15:24:45 -0500 Subject: [PATCH 04/18] feat(utf-8 string): implemented validation of utf-8 string --- Include/Fledasty/Strings/UTF8String.h | 5 +-- Include/Fledasty/Utils/Error.h | 1 + Src/Strings/UTF8String.c | 61 +++++++++++++++++++++++++-- Tests/Main.c | 16 +++++++ 4 files changed, 77 insertions(+), 6 deletions(-) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index d46ee75..797385e 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -23,7 +23,6 @@ * Author: Mineplay * ----------------------------------------------------------------------------- */ - #include #include #include @@ -38,7 +37,7 @@ 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); -FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, 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); -FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, unsigned char *character_string); \ No newline at end of file +bool fledasty_utf8_string_validate(unsigned char *character_string, const size_t character_string_length); diff --git a/Include/Fledasty/Utils/Error.h b/Include/Fledasty/Utils/Error.h index e52af8f..e9841fd 100644 --- a/Include/Fledasty/Utils/Error.h +++ b/Include/Fledasty/Utils/Error.h @@ -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 \ No newline at end of file diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index 147c26c..2a7bec6 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -35,12 +35,16 @@ FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, un return FLEDASTY_ERROR_INVALID_POINTER; } - if (character_string == NULL) { + if (character_string == NULL || character_string_length == 0) { new_string->size = 0; new_string->capacity = 10; new_string->character_string = hallocy_malloc(new_string->capacity); } else { + if (!fledasty_utf8_string_validate(character_string, character_string_length)) { + return FLEDASTY_ERROR_INVALID_VALUE; + } + new_string->size = character_string_length; new_string->capacity = new_string->size + new_string->size; @@ -68,7 +72,7 @@ FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string) { return FLEDASTY_ERROR_NONE; } -FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, size_t size) { +FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, const size_t size) { FledastyUtf8String utf8_string; fledasty_utf8_string_initialize(&utf8_string, NULL, 0); @@ -130,7 +134,7 @@ FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, size_t size) { } uint32_t *fledasty_utf8_string_decode(FledastyUtf8String *current_string, size_t *unicode_string_length) { - if (current_string == NULL) { + if (current_string == NULL || unicode_string_length == NULL) { return NULL; } @@ -157,3 +161,54 @@ uint32_t *fledasty_utf8_string_decode(FledastyUtf8String *current_string, size_t return unicode_string; } + +bool fledasty_utf8_string_validate(unsigned char *character_string, const size_t character_string_length) { + if (character_string == NULL) { + return false; + } + + size_t index = 0; + while (index < character_string_length) { + if ((character_string[index] & 0xF0) == 0xF0) { + if (index + 3 >= character_string_length) { + 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_length) { + 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_length) { + return false; + } + + if ((character_string[index + 1] & 0xC0) != 0x80) { + return false; + } + + index += 2; + } else { + index += 1; + } + } + + return true; +} diff --git a/Tests/Main.c b/Tests/Main.c index 01895d8..0c7a3e6 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -235,6 +235,22 @@ int main() { 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"); + } + + hallocy_free(invalid_utf8); hallocy_free(unicode); fledasty_utf8_string_destroy(&encoded_string); -- 2.39.5 From 243bb533b02dcb17b9f6bf46f75b912f873d9ffb Mon Sep 17 00:00:00 2001 From: Mineplay Date: Sun, 11 May 2025 16:39:01 -0500 Subject: [PATCH 05/18] feat(utf-8 string): implemented append function --- Include/Fledasty/Strings/UTF8String.h | 2 ++ Src/Strings/UTF8String.c | 35 +++++++++++++++++++++------ Tests/Main.c | 3 +++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index 797385e..7d3d077 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -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); diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index 2a7bec6..f2aa758 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -24,6 +24,7 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Fledasty/Strings/UTF8String.h" +#include "Fledasty/Utils/Error.h" #include #include @@ -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); diff --git a/Tests/Main.c b/Tests/Main.c index 0c7a3e6..35a54a9 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -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); -- 2.39.5 From bd9fef9f4f1c7c300d3c0992e868e73931848eb6 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Mon, 12 May 2025 03:48:08 -0500 Subject: [PATCH 06/18] feat(utf-8): implemented insert before function --- Include/Fledasty/Strings/UTF8String.h | 1 + Src/Strings/UTF8String.c | 36 ++++++++++++++++++++++++++- Tests/Main.c | 5 +++- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index 7d3d077..310781c 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -38,6 +38,7 @@ 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_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); 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); diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index f2aa758..4d838ec 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -78,8 +78,12 @@ FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, un return FLEDASTY_ERROR_INVALID_POINTER; } + 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; + 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)); } @@ -91,6 +95,36 @@ FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, un 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) { + 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)) { + 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)) { + index += 1; + } + + if (index == current_string->size - before_character_string_length) { + 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; + 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 utf8_string; fledasty_utf8_string_initialize(&utf8_string, NULL, 0); diff --git a/Tests/Main.c b/Tests/Main.c index 35a54a9..479057f 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -19,6 +19,7 @@ * Author: Mineplay * ----------------------------------------------------------------------------- */ +#include "Fledasty/Utils/Error.h" #include #include #include @@ -230,7 +231,9 @@ int main() { 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); + 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); size_t unicode_length = 0; uint32_t *unicode = fledasty_utf8_string_decode(&test_utf8_string, &unicode_length); -- 2.39.5 From e4a023632825df42bef465b370c78bb4fbe0a405 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Mon, 12 May 2025 04:09:25 -0500 Subject: [PATCH 07/18] feat(utf-8 string): implemented insert after function --- Include/Fledasty/Strings/UTF8String.h | 1 + Src/Strings/UTF8String.c | 31 +++++++++++++++++++++++++++ Tests/Main.c | 3 ++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index 310781c..6a35530 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -39,6 +39,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_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); 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); diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index 4d838ec..88107c3 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -125,6 +125,37 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr 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) { + 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)) { + 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)) { + index += 1; + } + + if (index == current_string->size - after_character_string_length) { + 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; + 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); + + current_string->size += character_string_length; + 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); diff --git a/Tests/Main.c b/Tests/Main.c index 479057f..29bdc2d 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -19,7 +19,6 @@ * Author: Mineplay * ----------------------------------------------------------------------------- */ -#include "Fledasty/Utils/Error.h" #include #include #include @@ -234,6 +233,8 @@ int main() { 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); size_t unicode_length = 0; uint32_t *unicode = fledasty_utf8_string_decode(&test_utf8_string, &unicode_length); -- 2.39.5 From 8e237dae14ab237622bc20bdc295142e6380ee55 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Mon, 12 May 2025 04:23:23 -0500 Subject: [PATCH 08/18] feat(utf-8 string): implemented insert at index function --- Include/Fledasty/Strings/UTF8String.h | 1 + Src/Strings/UTF8String.c | 25 +++++++++++++++++++++++++ Tests/Main.c | 2 ++ 3 files changed, 28 insertions(+) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index 6a35530..ecd8391 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -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_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); 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); diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index 88107c3..401cb7a 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -156,6 +156,31 @@ 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_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 utf8_string; fledasty_utf8_string_initialize(&utf8_string, NULL, 0); diff --git a/Tests/Main.c b/Tests/Main.c index 29bdc2d..89091ab 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -235,6 +235,8 @@ int main() { 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); size_t unicode_length = 0; uint32_t *unicode = fledasty_utf8_string_decode(&test_utf8_string, &unicode_length); -- 2.39.5 From 062d10805f7d1b64bb29c506cc0a3b3ca864170c Mon Sep 17 00:00:00 2001 From: Mineplay Date: Mon, 12 May 2025 05:56:18 -0500 Subject: [PATCH 09/18] feat(utf-8 string): implemented is empty function --- Include/Fledasty/Core/DoublyLinkedList.h | 2 +- Include/Fledasty/Core/DynamicArray.h | 2 +- Include/Fledasty/Core/HashTable.h | 2 +- Include/Fledasty/Core/LinkedList.h | 2 +- Include/Fledasty/Core/Queue.h | 2 +- Include/Fledasty/Core/Stack.h | 2 +- Include/Fledasty/Strings/UTF8String.h | 8 +++++--- Src/Strings/UTF8String.c | 4 ++-- Tests/Main.c | 4 ++++ 9 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Include/Fledasty/Core/DoublyLinkedList.h b/Include/Fledasty/Core/DoublyLinkedList.h index c588131..f5ac708 100644 --- a/Include/Fledasty/Core/DoublyLinkedList.h +++ b/Include/Fledasty/Core/DoublyLinkedList.h @@ -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 \ No newline at end of file diff --git a/Include/Fledasty/Core/DynamicArray.h b/Include/Fledasty/Core/DynamicArray.h index 1f64b18..a58098a 100644 --- a/Include/Fledasty/Core/DynamicArray.h +++ b/Include/Fledasty/Core/DynamicArray.h @@ -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 diff --git a/Include/Fledasty/Core/HashTable.h b/Include/Fledasty/Core/HashTable.h index 1ccd35b..e1d0b30 100644 --- a/Include/Fledasty/Core/HashTable.h +++ b/Include/Fledasty/Core/HashTable.h @@ -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 \ No newline at end of file diff --git a/Include/Fledasty/Core/LinkedList.h b/Include/Fledasty/Core/LinkedList.h index 7505a73..49f5bc7 100644 --- a/Include/Fledasty/Core/LinkedList.h +++ b/Include/Fledasty/Core/LinkedList.h @@ -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 diff --git a/Include/Fledasty/Core/Queue.h b/Include/Fledasty/Core/Queue.h index 9a4b16a..ab9f472 100644 --- a/Include/Fledasty/Core/Queue.h +++ b/Include/Fledasty/Core/Queue.h @@ -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 diff --git a/Include/Fledasty/Core/Stack.h b/Include/Fledasty/Core/Stack.h index bd17de3..cbee423 100644 --- a/Include/Fledasty/Core/Stack.h +++ b/Include/Fledasty/Core/Stack.h @@ -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 diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index ecd8391..43aef64 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -42,7 +42,9 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr 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); -uint32_t *fledasty_utf8_string_decode(FledastyUtf8String *current_string, size_t *unicode_string_length); +static inline bool fledasty_utf8_string_is_empty(const FledastyUtf8String *current_string) { return current_string == NULL || current_string->size == 0; } -bool fledasty_utf8_string_validate(unsigned char *character_string, const size_t character_string_length); +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); + +bool fledasty_utf8_string_validate(unsigned char *character_string, const size_t character_string_length); \ No newline at end of file diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index 401cb7a..ea0bf71 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -181,7 +181,7 @@ FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_s return FLEDASTY_ERROR_NONE; } -FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, const size_t size) { +FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const size_t size) { FledastyUtf8String utf8_string; fledasty_utf8_string_initialize(&utf8_string, NULL, 0); @@ -242,7 +242,7 @@ FledastyUtf8String fledasty_utf8_string_encode(uint32_t *unicode, const size_t s return utf8_string; } -uint32_t *fledasty_utf8_string_decode(FledastyUtf8String *current_string, size_t *unicode_string_length) { +uint32_t *fledasty_utf8_string_decode(const FledastyUtf8String *current_string, size_t *unicode_string_length) { if (current_string == NULL || unicode_string_length == NULL) { return NULL; } diff --git a/Tests/Main.c b/Tests/Main.c index 89091ab..96a0f55 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -259,6 +259,10 @@ int main() { printf("UTF-8 invalid string is invalid!\n"); } + 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); -- 2.39.5 From 0e67969ff16120be9ded33702d3028b9cb624eaa Mon Sep 17 00:00:00 2001 From: Mineplay Date: Mon, 12 May 2025 14:48:39 -0500 Subject: [PATCH 10/18] feat(utf-8): implemented has string function --- Include/Fledasty/Strings/UTF8String.h | 1 + Src/Strings/UTF8String.c | 17 +++++++++++++++++ Tests/Main.c | 4 ++++ 3 files changed, 22 insertions(+) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index 43aef64..09cb06f 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -42,6 +42,7 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr 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); +bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_length); 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); diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index ea0bf71..28a5be6 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -181,6 +181,23 @@ FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_s 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) { + 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)) { + 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); diff --git a/Tests/Main.c b/Tests/Main.c index 96a0f55..598fe88 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -238,6 +238,10 @@ int main() { 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); + 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); -- 2.39.5 From fbdebde492421d81da03e7817850ec90963e3ac1 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Tue, 13 May 2025 04:04:31 -0500 Subject: [PATCH 11/18] feat(utf-8 string): implemented replace string function --- Include/Fledasty/Strings/UTF8String.h | 2 ++ Src/Strings/UTF8String.c | 26 +++++++++++++++++++++++++- Tests/Main.c | 2 ++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index 09cb06f..31104ff 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -42,6 +42,8 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr 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_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); + bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_length); static inline bool fledasty_utf8_string_is_empty(const FledastyUtf8String *current_string) { return current_string == NULL || current_string->size == 0; } diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index 28a5be6..1ee07f5 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -29,7 +29,6 @@ #include #include #include -#include FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, size_t character_string_length) { if (new_string == NULL) { @@ -181,6 +180,31 @@ FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_s 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) { + 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)) { + 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)) { + index += 1; + } + + if (index == current_string->size - replace_character_string_length) { + 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); + + current_string->size += character_string_length - replace_character_string_length; + 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) { return false; diff --git a/Tests/Main.c b/Tests/Main.c index 598fe88..bd7cc1e 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -237,6 +237,8 @@ int main() { 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); if (fledasty_utf8_string_has_string(&test_utf8_string, (unsigned char*)"πŸ˜€", 4)) { printf("String contains πŸ˜€!\n"); -- 2.39.5 From 1ab803f0f2b74f4d7952b8633b3b1e564ccc6cb6 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Tue, 13 May 2025 07:47:05 -0500 Subject: [PATCH 12/18] refactor(utf-8 string): changed parameters name to use size instead of length to be more consistent --- Include/Fledasty/Strings/UTF8String.h | 18 ++-- Src/Strings/UTF8String.c | 130 +++++++++++++------------- 2 files changed, 74 insertions(+), 74 deletions(-) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index 31104ff..a7fb399 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -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); \ No newline at end of file +bool fledasty_utf8_string_validate(unsigned char *character_string, const size_t character_string_size); \ No newline at end of file diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index 1ee07f5..72c30fe 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -30,22 +30,22 @@ #include #include -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; } -- 2.39.5 From 6ccf2e70bf6b0b425a6db944439778c1d875a9cd Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 15 May 2025 10:17:00 -0500 Subject: [PATCH 13/18] feat(utf-8 string): implemented pop function --- Include/Fledasty/Strings/UTF8String.h | 6 ++- Src/Strings/UTF8String.c | 60 ++++++++++++++++++--------- Tests/Main.c | 2 + 3 files changed, 47 insertions(+), 21 deletions(-) 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"); -- 2.39.5 From 681ca8f4896ef41cf47bf7fe82860015196c72bc Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 15 May 2025 10:20:54 -0500 Subject: [PATCH 14/18] feat(utf-8 string): implemented clear function --- Include/Fledasty/Strings/UTF8String.h | 1 + Src/Strings/UTF8String.c | 11 +++++++++++ Tests/Main.c | 3 ++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index b25f8dd..eea16f1 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -43,6 +43,7 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr 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_pop(FledastyUtf8String *current_string); +FledastyError fledasty_utf8_string_clear(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); diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index e50786f..d78c5b1 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -198,6 +198,17 @@ FledastyError fledasty_utf8_string_pop(FledastyUtf8String *current_string) { 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, 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; diff --git a/Tests/Main.c b/Tests/Main.c index 0906c20..61cc5e5 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -266,7 +266,8 @@ int main() { 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"); } -- 2.39.5 From 15abfcffe8d907412153bd8b00445503309cff9d Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 15 May 2025 10:26:32 -0500 Subject: [PATCH 15/18] feat(utf-8 string): implemented remove function --- Include/Fledasty/Strings/UTF8String.h | 1 + Src/Strings/UTF8String.c | 24 ++++++++++++++++++++++++ Tests/Main.c | 2 ++ 3 files changed, 27 insertions(+) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index eea16f1..e887367 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -43,6 +43,7 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr 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_pop(FledastyUtf8String *current_string); +FledastyError fledasty_utf8_string_remove(FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_size); FledastyError fledasty_utf8_string_clear(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); diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index d78c5b1..d6e7bb4 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -198,6 +198,30 @@ FledastyError fledasty_utf8_string_pop(FledastyUtf8String *current_string) { return FLEDASTY_ERROR_NONE; } +FledastyError fledasty_utf8_string_remove(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_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_clear(FledastyUtf8String *current_string) { if (current_string == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; diff --git a/Tests/Main.c b/Tests/Main.c index 61cc5e5..5c4ecd9 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -241,6 +241,8 @@ int main() { 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); if (fledasty_utf8_string_has_string(&test_utf8_string, (unsigned char*)"πŸ˜€", 4)) { printf("String contains πŸ˜€!\n"); -- 2.39.5 From 8b3be3d1cb902db69873a162baa0e25fbe2eb654 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 15 May 2025 10:31:39 -0500 Subject: [PATCH 16/18] feat(utf-8 string): implemented remove range function --- Include/Fledasty/Strings/UTF8String.h | 1 + Src/Strings/UTF8String.c | 15 +++++++++++++++ Tests/Main.c | 2 ++ 3 files changed, 18 insertions(+) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index e887367..10526a5 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -44,6 +44,7 @@ FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *curre FledastyError fledasty_utf8_string_pop(FledastyUtf8String *current_string); FledastyError fledasty_utf8_string_remove(FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_size); +FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_string, size_t start_index, 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, size_t replace_character_string_size, unsigned char *character_string, size_t character_string_size); diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index d6e7bb4..eda8264 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -222,6 +222,21 @@ FledastyError fledasty_utf8_string_remove(FledastyUtf8String *current_string, un return FLEDASTY_ERROR_NONE; } +FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_string, size_t start_index, 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; diff --git a/Tests/Main.c b/Tests/Main.c index 5c4ecd9..963c553 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -243,6 +243,8 @@ int main() { 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"); -- 2.39.5 From 016bc759f376026e7f36efb37b761adcc83382b2 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 15 May 2025 10:33:24 -0500 Subject: [PATCH 17/18] feat(utf-8 string): implemented get size function --- Include/Fledasty/Strings/UTF8String.h | 1 + Src/Strings/UTF8String.c | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index 10526a5..e878d41 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -56,3 +56,4 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si 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(unsigned char *character_string); \ No newline at end of file diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index eda8264..2d8962f 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -434,3 +434,12 @@ bool fledasty_utf8_string_validate(unsigned char *character_string, const size_t return true; } + +size_t fledasty_utf8_string_get_size(unsigned char *character_string) { + size_t size = 0; + while (character_string[size] != '\0') { + size += 1; + } + + return size; +} -- 2.39.5 From 200e3e0ccf163903d98492b592907e8638a0b858 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 15 May 2025 10:36:51 -0500 Subject: [PATCH 18/18] refactor(utf-8 string): added const to size and index parameters in functions --- Include/Fledasty/Strings/UTF8String.h | 20 ++++++++++---------- Src/Strings/UTF8String.c | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Include/Fledasty/Strings/UTF8String.h b/Include/Fledasty/Strings/UTF8String.h index e878d41..a75699d 100644 --- a/Include/Fledasty/Strings/UTF8String.h +++ b/Include/Fledasty/Strings/UTF8String.h @@ -34,26 +34,26 @@ typedef struct { unsigned char *character_string; } FledastyUtf8String; -FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, size_t character_string_size); +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, 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_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, size_t character_string_size); -FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_string, size_t start_index, size_t end_index); +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, size_t replace_character_string_size, unsigned char *character_string, size_t character_string_size); +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, 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(unsigned char *character_string); \ No newline at end of file +size_t fledasty_utf8_string_get_size(const unsigned char *character_string); \ No newline at end of file diff --git a/Src/Strings/UTF8String.c b/Src/Strings/UTF8String.c index 2d8962f..31697ce 100644 --- a/Src/Strings/UTF8String.c +++ b/Src/Strings/UTF8String.c @@ -30,7 +30,7 @@ #include #include -FledastyError fledasty_utf8_string_initialize(FledastyUtf8String *new_string, unsigned char *character_string, size_t character_string_size) { +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; } @@ -70,7 +70,7 @@ 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_size) { +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; } @@ -92,7 +92,7 @@ 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) { +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; } @@ -117,7 +117,7 @@ FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_s 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) { +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; } @@ -147,7 +147,7 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr return FLEDASTY_ERROR_NONE; } -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_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; } @@ -198,7 +198,7 @@ FledastyError fledasty_utf8_string_pop(FledastyUtf8String *current_string) { return FLEDASTY_ERROR_NONE; } -FledastyError fledasty_utf8_string_remove(FledastyUtf8String *current_string, unsigned char *character_string, size_t character_string_size) { +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; } @@ -222,7 +222,7 @@ FledastyError fledasty_utf8_string_remove(FledastyUtf8String *current_string, un return FLEDASTY_ERROR_NONE; } -FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_string, size_t start_index, size_t end_index) { +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; } @@ -248,7 +248,7 @@ FledastyError fledasty_utf8_string_clear(FledastyUtf8String *current_string) { return FLEDASTY_ERROR_NONE; } -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) { +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; } @@ -273,7 +273,7 @@ FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_st return FLEDASTY_ERROR_NONE; } -bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, 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) { if (current_string == NULL || character_string == NULL || character_string_size == 0) { return false; } @@ -435,7 +435,7 @@ bool fledasty_utf8_string_validate(unsigned char *character_string, const size_t return true; } -size_t fledasty_utf8_string_get_size(unsigned char *character_string) { +size_t fledasty_utf8_string_get_size(const unsigned char *character_string) { size_t size = 0; while (character_string[size] != '\0') { size += 1; -- 2.39.5