From 30a978beca03b02af601ed488c521778979b84da Mon Sep 17 00:00:00 2001 From: thetek Date: Wed, 15 Feb 2023 09:37:20 +0100 Subject: [PATCH] feat: (str) str_find, str_pos --- lib/inc/str.h | 3 +++ lib/str.c | 35 +++++++++++++++++++++++++++++++++++ test/main.c | 5 +++++ 3 files changed, 43 insertions(+) diff --git a/lib/inc/str.h b/lib/inc/str.h index 92d9c69..bfbed58 100644 --- a/lib/inc/str.h +++ b/lib/inc/str.h @@ -3,6 +3,7 @@ #include #include +#include /* represents a string, where length and capacity are stored alongside the string data. */ typedef struct @@ -25,5 +26,7 @@ bool str_starts_with (const str_t *str, const char *find); /* check if a str_t s bool str_starts_with_len (const str_t *str, const char *find, size_t len); /* check if a str_t string starts with a cstring of given length. */ bool str_ends_with (const str_t *str, const char *find); /* check if a str_t string ends with a cstring. */ bool str_ends_with_len (const str_t *str, const char *find, size_t len); /* check if a str_t string ends with a cstring of given length. */ +char *str_find (const str_t *str, const char *find); /* find a substring in a str_t string as a pointer. */ +ssize_t str_pos (const str_t *str, const char *find); /* find the index of a substring in a str_t string. */ #endif // CUTILS_STR_H_ diff --git a/lib/str.c b/lib/str.c index c8ff4ad..7326782 100644 --- a/lib/str.c +++ b/lib/str.c @@ -208,3 +208,38 @@ str_ends_with_len (const str_t *str, const char *find, size_t len) { return strcmp (str->str + str->len - len, find) == 0; } + +/** + * find a substring in a str_t string. a pointer to the beginning of the + * substring is returned. + * + * @param str: the str_t string to search in + * @param find: the cstring to search for + * + * @return beginning of found substring as pointer, or NULL if not found + */ +inline char * +str_find (const str_t *str, const char *find) +{ + return strstr (str->str, find); +} + +/** + * find a substring in a str_t string. the starting index of the substring is + * returned. + * + * @param str: the str_t string to search in + * @param find: the cstring to search for + * + * @return beginning of found substring as string index, or -1 if not found + */ +ssize_t +str_pos (const str_t *str, const char *find) +{ + char *pos; + + pos = str_find (str, find); + if (pos) + return pos - str->str; + return -1; +} diff --git a/test/main.c b/test/main.c index 7e21aec..c1b807f 100644 --- a/test/main.c +++ b/test/main.c @@ -95,6 +95,11 @@ test_str (void) test_add (&group, test_assert (!str_starts_with (&str, "foo")), "str_starts_with false"); test_add (&group, test_assert (str_ends_with (&str, "elit.")), "str_ends_with true"); test_add (&group, test_assert (!str_ends_with (&str, "bar")), "str_ends_with false"); + + test_add (&group, test_assert (str_find (&str, "ipsum") == str.str + 6), "str_find success"); + test_add (&group, test_assert (str_find (&str, "foo") == NULL), "str_find failure"); + test_add (&group, test_assert (str_pos (&str, "ipsum") == 6), "str_pos success"); + test_add (&group, test_assert (str_pos (&str, "foo") == -1), "str_pos failure"); str_free (&str); return test_group_get_results (&group);