feat: (str) str_find, str_pos
This commit is contained in:
parent
05cf50a079
commit
30a978beca
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
/* 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_
|
||||
|
|
35
lib/str.c
35
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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue