feat: (str) str_starts_with_*, str_ends_with_*
This commit is contained in:
parent
197f826067
commit
05cf50a079
|
@ -21,5 +21,9 @@ void str_append (str_t *str, const char *src); /* append a string to the end of
|
|||
void str_append_len (str_t *str, const char *src, size_t len); /* append a string to the end of a str_t string with a given length. */
|
||||
int str_cmp (const str_t *str, const char *s2); /* compare a str_t string with a cstring. */
|
||||
bool str_eq (const str_t *str, const char *s2); /* check if a str_t string equals a cstring. */
|
||||
bool str_starts_with (const str_t *str, const char *find); /* check if a str_t string starts with a cstring. */
|
||||
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. */
|
||||
|
||||
#endif // CUTILS_STR_H_
|
||||
|
|
60
lib/str.c
60
lib/str.c
|
@ -131,7 +131,7 @@ str_append_len (str_t *str, const char *src, size_t len)
|
|||
* @return integer less than, equal to, or greater than zero if str is found
|
||||
* to be less than, to match, or to be greater than s2
|
||||
*/
|
||||
int
|
||||
inline int
|
||||
str_cmp (const str_t *str, const char *s2)
|
||||
{
|
||||
return strcmp (str->str, s2);
|
||||
|
@ -150,3 +150,61 @@ str_eq (const str_t *str, const char *s2)
|
|||
{
|
||||
return str_cmp (str, s2) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if a str_t string starts with a cstring.
|
||||
*
|
||||
* @param str: the str_t string to search in
|
||||
* @param find: the cstring to search for
|
||||
*
|
||||
* @return true if `str` starts with `find`, else false
|
||||
*/
|
||||
inline bool
|
||||
str_starts_with (const str_t *str, const char *find)
|
||||
{
|
||||
return str_starts_with_len (str, find, strlen (find));
|
||||
}
|
||||
|
||||
/**
|
||||
* check if a str_t string starts with a cstring of given length.
|
||||
*
|
||||
* @param str: the str_t string to search in
|
||||
* @param find: the cstring to search for
|
||||
* @param len: the length of `find`
|
||||
*
|
||||
* @return true if `str` starts with `find`, else false
|
||||
*/
|
||||
inline bool
|
||||
str_starts_with_len (const str_t *str, const char *find, size_t len)
|
||||
{
|
||||
return strncmp (str->str, find, len) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if a str_t string ends with a cstring.
|
||||
*
|
||||
* @param str: the str_t string to search in
|
||||
* @param find: the cstring to search for
|
||||
*
|
||||
* @return true if `str` ends with `find`, else false
|
||||
*/
|
||||
inline bool
|
||||
str_ends_with (const str_t *str, const char *find)
|
||||
{
|
||||
return str_ends_with_len (str, find, strlen (find));
|
||||
}
|
||||
|
||||
/**
|
||||
* check if a str_t string ends with a cstring of given length.
|
||||
*
|
||||
* @param str: the str_t string to search in
|
||||
* @param find: the cstring to search for
|
||||
* @param len: the length of `find`
|
||||
*
|
||||
* @return true if `str` ends with `find`, else false
|
||||
*/
|
||||
inline bool
|
||||
str_ends_with_len (const str_t *str, const char *find, size_t len)
|
||||
{
|
||||
return strcmp (str->str + str->len - len, find) == 0;
|
||||
}
|
||||
|
|
|
@ -90,6 +90,11 @@ test_str (void)
|
|||
test_add (&group, test_assert (str_cmp (&str, "A") > 0), "str_eq greater");
|
||||
test_add (&group, test_assert (str_cmp (&str, s) == 0), "str_eq equal");
|
||||
test_add (&group, test_assert (str_cmp (&str, "Z") < 0), "str_eq less");
|
||||
|
||||
test_add (&group, test_assert (str_starts_with (&str, "Lorem")), "str_starts_with true");
|
||||
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");
|
||||
str_free (&str);
|
||||
|
||||
return test_group_get_results (&group);
|
||||
|
|
Loading…
Reference in New Issue