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 <Anna@OcarinaProject.net>
This commit is contained in:
parent
b7cf2509f7
commit
5a30a63904
|
@ -9,6 +9,7 @@
|
|||
|
||||
#include <glib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
/* 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)
|
||||
{
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue