From 4245d152fdaed2d930ba09dbd9d6c5c976ad66e3 Mon Sep 17 00:00:00 2001 From: Tilo-K Date: Fri, 2 Jan 2026 19:13:56 +0100 Subject: [PATCH] feat: add contains functions --- include/tstd/list.h | 16 ++++++++++++++++ include/tstd/string.h | 8 ++++++++ list.c | 12 +++++++++++- string.c | 6 +++++- test/test_list.c | 17 ++++++++++++++++- test/test_string.c | 15 +++++++++++++-- 6 files changed, 69 insertions(+), 5 deletions(-) diff --git a/include/tstd/list.h b/include/tstd/list.h index 7f33a60..6a22393 100644 --- a/include/tstd/list.h +++ b/include/tstd/list.h @@ -7,6 +7,7 @@ #ifndef TSTD_LIST_H #define TSTD_LIST_H +#include #include #include @@ -133,4 +134,19 @@ void list_free_elements(list* list); */ void* list_delete_element(list* list, int index); + +/** +* @brief Checks if the list contains a specific element. +* +* This function checks if the list contains the specified element. It iterates +* through the list's elements and compares each one with the given element +* using the `==` operator. If a match is found, the function returns 1, otherwise +* it returns 0. +* +* @param list A pointer to the list to search. +* @param element A pointer to the element to search for. +* @return 1 if the list contains the element, 0 otherwise. +*/ +uint8_t list_contains(list* list, void* element); + #endif //TSTD_LIST_H diff --git a/include/tstd/string.h b/include/tstd/string.h index 6db16ed..ba730ae 100644 --- a/include/tstd/string.h +++ b/include/tstd/string.h @@ -7,6 +7,7 @@ #ifndef TSTD_STRING_H #define TSTD_STRING_H +#include #include #include @@ -80,4 +81,11 @@ char* str_concat(const char* str1, const char* str2); */ char* str_trim(const char* str); +/** + * @brief Check if a character is a whitespace character. + * @param substr Null-terminated input string (must not be NULL). + * @return 1 if the character is a whitespace character, 0 otherwise. + */ +uint8_t str_contains(const char* str, const char* substr); + #endif //TSTD_STRING_H diff --git a/list.c b/list.c index 83ba916..83ecba8 100644 --- a/list.c +++ b/list.c @@ -4,6 +4,7 @@ #include "tstd/list.h" +#include #include #include "helper.h" @@ -64,4 +65,13 @@ void* list_delete_element(list* list, int index) { list->data[i] = list->data[i + 1]; } return element; -} \ No newline at end of file +} + +uint8_t list_contains(list* list, void* element) { + for (int i = 0; i < list->length; i++) { + if (list->data[i] == element) { + return 1; + } + } + return 0; +} diff --git a/string.c b/string.c index e830f8d..e128eed 100644 --- a/string.c +++ b/string.c @@ -139,4 +139,8 @@ char* str_trim(const char* str) { new_str[end-start+1] = '\0'; return new_str; -} \ No newline at end of file +} + +uint8_t str_contains(const char* str, const char* substr) { + return strstr(str, substr) != NULL; +} diff --git a/test/test_list.c b/test/test_list.c index 43c3ba7..8443ad1 100644 --- a/test/test_list.c +++ b/test/test_list.c @@ -51,6 +51,20 @@ int test_delete_element() { return 0; } +int test_list_contains() { + list* list = list_create(); + list_add_element(list, "Hello"); + list_add_element(list, "World"); + list_add_element(list, "Test"); + + assert(list_contains(list, "Hello") == 1); + assert(list_contains(list, "World") == 1); + assert(list_contains(list, "Test") == 1); + assert(list_contains(list, "Other") == 0); + + return 0; +} + int main() { int result = 0; @@ -58,6 +72,7 @@ int main() { result += test_list_add_element(); result += test_add_multiple_elements(); result += test_delete_element(); + result += test_list_contains(); return result; -} \ No newline at end of file +} diff --git a/test/test_string.c b/test/test_string.c index 9f048c9..b37f811 100644 --- a/test/test_string.c +++ b/test/test_string.c @@ -62,7 +62,7 @@ int test_split_function_by_substring() { const size_t size = str_split_by_substring(test_str, "split", &parts); assert(parts != NULL); - printf("size: %llu\n", size); + printf("size: %lu\n", size); assert(size == 4); assert(strcmp(parts[0], "this") == 0); @@ -112,6 +112,15 @@ int test_trim() { return 0; } +int test_string_contains() { + char* test_str = "Hello, test. test"; + assert(str_contains(test_str, "Hello") == 1); + assert(str_contains(test_str, "test") == 1); + assert(str_contains(test_str, "World") == 0); + + return 0; +} + int test_string_functions() { int result = 0; @@ -123,10 +132,12 @@ int test_string_functions() { result += test_string_to_lower(); result += test_string_concat(); result += test_trim(); + result += test_string_contains(); return result; } + int main() { return test_string_functions(); -} \ No newline at end of file +}