From 5a30a63904a8c91b71937d72ac91f30a774d4a9c Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 26 Apr 2016 07:37:58 -0400 Subject: [PATCH] core/string: Add string_match() function This function is used to determine if both strings exist and have the same contents. Signed-off-by: Anna Schumaker --- include/core/string.h | 7 +++++++ tests/core/string.c | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/core/string.h b/include/core/string.h index 0ae8daec..8f2e7814 100644 --- a/include/core/string.h +++ b/include/core/string.h @@ -9,6 +9,7 @@ #include #include +#include /* Convert number of seconds into a string with format mm:ss. */ @@ -32,6 +33,12 @@ gchar *string_lowercase(const gchar *); */ int string_compare(const gchar *, const gchar *); +/* Returns True if the two strings match. */ +static inline bool string_match(const gchar *a, const gchar *b) +{ + return (a && b) ? string_compare(a, b) == 0 : false; +} + /* Return the length of the string, with NULL checks */ static inline int string_length(const gchar *str) { diff --git a/tests/core/string.c b/tests/core/string.c index edf62b67..f378b1dd 100644 --- a/tests/core/string.c +++ b/tests/core/string.c @@ -101,6 +101,15 @@ void test_compare() test_equal(string_compare("A", ""), -1); } +void test_match() +{ + test_equal(string_match(NULL, NULL), (bool)false); + test_equal(string_match(NULL, "A"), (bool)false); + test_equal(string_match("A", NULL), (bool)false); + test_equal(string_match("A", "A"), (bool)true); + test_equal(string_match("A", "B"), (bool)false); +} + void test_length() { test_equal(string_length(NULL), 0); @@ -115,5 +124,6 @@ DECLARE_UNIT_TESTS( UNIT_TEST("Date to String", test_date), UNIT_TEST("String Lowercase", test_lowercase), UNIT_TEST("String Comparison", test_compare), + UNIT_TEST("String Match", test_match), UNIT_TEST("String Length", test_length), );