mirror of
https://github.com/Tilo-K/tstd.git
synced 2026-01-09 14:31:01 +00:00
feat: add contains functions
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#ifndef TSTD_LIST_H
|
#ifndef TSTD_LIST_H
|
||||||
#define TSTD_LIST_H
|
#define TSTD_LIST_H
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
@@ -133,4 +134,19 @@ void list_free_elements(list* list);
|
|||||||
*/
|
*/
|
||||||
void* list_delete_element(list* list, int index);
|
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
|
#endif //TSTD_LIST_H
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#ifndef TSTD_STRING_H
|
#ifndef TSTD_STRING_H
|
||||||
#define TSTD_STRING_H
|
#define TSTD_STRING_H
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -80,4 +81,11 @@ char* str_concat(const char* str1, const char* str2);
|
|||||||
*/
|
*/
|
||||||
char* str_trim(const char* str);
|
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
|
#endif //TSTD_STRING_H
|
||||||
|
|||||||
12
list.c
12
list.c
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include "tstd/list.h"
|
#include "tstd/list.h"
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
@@ -64,4 +65,13 @@ void* list_delete_element(list* list, int index) {
|
|||||||
list->data[i] = list->data[i + 1];
|
list->data[i] = list->data[i + 1];
|
||||||
}
|
}
|
||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|||||||
6
string.c
6
string.c
@@ -139,4 +139,8 @@ char* str_trim(const char* str) {
|
|||||||
new_str[end-start+1] = '\0';
|
new_str[end-start+1] = '\0';
|
||||||
|
|
||||||
return new_str;
|
return new_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t str_contains(const char* str, const char* substr) {
|
||||||
|
return strstr(str, substr) != NULL;
|
||||||
|
}
|
||||||
|
|||||||
@@ -51,6 +51,20 @@ int test_delete_element() {
|
|||||||
return 0;
|
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 main() {
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
|
||||||
@@ -58,6 +72,7 @@ int main() {
|
|||||||
result += test_list_add_element();
|
result += test_list_add_element();
|
||||||
result += test_add_multiple_elements();
|
result += test_add_multiple_elements();
|
||||||
result += test_delete_element();
|
result += test_delete_element();
|
||||||
|
result += test_list_contains();
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ int test_split_function_by_substring() {
|
|||||||
const size_t size = str_split_by_substring(test_str, "split", &parts);
|
const size_t size = str_split_by_substring(test_str, "split", &parts);
|
||||||
assert(parts != NULL);
|
assert(parts != NULL);
|
||||||
|
|
||||||
printf("size: %llu\n", size);
|
printf("size: %lu\n", size);
|
||||||
|
|
||||||
assert(size == 4);
|
assert(size == 4);
|
||||||
assert(strcmp(parts[0], "this") == 0);
|
assert(strcmp(parts[0], "this") == 0);
|
||||||
@@ -112,6 +112,15 @@ int test_trim() {
|
|||||||
return 0;
|
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 test_string_functions() {
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
|
||||||
@@ -123,10 +132,12 @@ int test_string_functions() {
|
|||||||
result += test_string_to_lower();
|
result += test_string_to_lower();
|
||||||
result += test_string_concat();
|
result += test_string_concat();
|
||||||
result += test_trim();
|
result += test_trim();
|
||||||
|
result += test_string_contains();
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
return test_string_functions();
|
return test_string_functions();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user