diff --git a/src/common.c b/src/common.c index 832e85dd..bd8b1742 100644 --- a/src/common.c +++ b/src/common.c @@ -192,6 +192,12 @@ str_replace(const char *string, const char *substr, return newstr; } +gboolean +str_contains_str(char *searchstr, char *substr) +{ + return g_strrstr(searchstr, substr) != NULL; +} + int str_contains(const char str[], int size, char ch) { diff --git a/src/common.h b/src/common.h index 9da0c974..1e50a87a 100644 --- a/src/common.h +++ b/src/common.h @@ -104,6 +104,7 @@ gboolean create_dir(char *name); gboolean mkdir_recursive(const char *dir); char * str_replace(const char *string, const char *substr, const char *replacement); +gboolean str_contains_str(char *searchstr, char *substr); int str_contains(const char str[], int size, char ch); gboolean strtoi_range(char *str, int *saveptr, int min, int max, char **err_msg); int utf8_display_len(const char * const str); diff --git a/tests/unittests/test_common.c b/tests/unittests/test_common.c index 980f2198..ac1e562a 100644 --- a/tests/unittests/test_common.c +++ b/tests/unittests/test_common.c @@ -631,3 +631,58 @@ void strip_quotes_strips_both(void **state) free(result); } +void str_not_contains_str(void **state) +{ + char *main = "somestring"; + char *occur = "not"; + + assert_false(str_contains_str(main, occur)); +} + +void str_contains_str_at_start(void **state) +{ + char *main = "somestring"; + char *occur = "some"; + + assert_true(str_contains_str(main, occur)); +} + +void str_contains_str_at_end(void **state) +{ + char *main = "somestring"; + char *occur = "string"; + + assert_true(str_contains_str(main, occur)); +} + +void str_contains_str_in_middle(void **state) +{ + char *main = "somestring"; + char *occur = "str"; + + assert_true(str_contains_str(main, occur)); +} + +void str_empty_not_contains_str(void **state) +{ + char *main = NULL; + char *occur = "str"; + + assert_false(str_contains_str(main, occur)); +} + +void str_not_contains_str_empty(void **state) +{ + char *main = "somestring"; + char *occur = NULL; + + assert_false(str_contains_str(main, occur)); +} + +void str_empty_not_contains_str_empty(void **state) +{ + char *main = NULL; + char *occur = NULL; + + assert_false(str_contains_str(main, occur)); +} diff --git a/tests/unittests/test_common.h b/tests/unittests/test_common.h index b4b98e5a..1f24e6b1 100644 --- a/tests/unittests/test_common.h +++ b/tests/unittests/test_common.h @@ -56,3 +56,10 @@ void strip_quotes_does_nothing_when_no_quoted(void **state); void strip_quotes_strips_first(void **state); void strip_quotes_strips_last(void **state); void strip_quotes_strips_both(void **state); +void str_not_contains_str(void **state); +void str_contains_str_at_start(void **state); +void str_contains_str_at_end(void **state); +void str_contains_str_in_middle(void **state); +void str_empty_not_contains_str(void **state); +void str_not_contains_str_empty(void **state); +void str_empty_not_contains_str_empty(void **state); diff --git a/tests/unittests/unittests.c b/tests/unittests/unittests.c index 0545d6a4..6e88debb 100644 --- a/tests/unittests/unittests.c +++ b/tests/unittests/unittests.c @@ -95,6 +95,13 @@ int main(int argc, char* argv[]) { unit_test(strip_quotes_strips_first), unit_test(strip_quotes_strips_last), unit_test(strip_quotes_strips_both), + unit_test(str_not_contains_str), + unit_test(str_contains_str_at_start), + unit_test(str_contains_str_at_end), + unit_test(str_contains_str_in_middle), + unit_test(str_empty_not_contains_str), + unit_test(str_not_contains_str_empty), + unit_test(str_empty_not_contains_str_empty), unit_test(clear_empty), unit_test(reset_after_create),