From 0e67969ff16120be9ded33702d3028b9cb624eaa Mon Sep 17 00:00:00 2001 From: Mineplay Date: Mon, 12 May 2025 14:48:39 -0500 Subject: [PATCH] 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);