core/string: Add a function for matching tokens

This will be used to replace the current filtering code with a token
comparison for each track.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-05-19 12:02:56 -04:00 committed by Anna Schumaker
parent de0446120e
commit f70920015c
3 changed files with 25 additions and 0 deletions

View File

@ -118,3 +118,15 @@ int string_compare_tokens(gchar **lhs, gchar **rhs)
return -1;
return 0;
}
bool string_match_token(const gchar *prefix, gchar **tokens)
{
unsigned int i;
for (i = 0; tokens[i]; i++) {
if (g_str_has_prefix(tokens[i], prefix))
return true;
}
return false;
}

View File

@ -40,6 +40,9 @@ static inline bool string_match(const gchar *a, const gchar *b)
return (a && b) ? string_compare(a, b) == 0 : false;
}
/* Returns True if one of the tokens begins with the specified prefix. */
bool string_match_token(const gchar *, gchar **);
/* Return the length of the string, with NULL checks */
static inline int string_length(const gchar *str)
{

View File

@ -136,6 +136,15 @@ void test_match()
test_equal(string_match("A", "B"), (bool)false);
}
void test_match_tokens()
{
gchar *tokens[3] = { "hyrule", "symphony", NULL };
test_equal(string_match_token("hyr", tokens), (bool)true);
test_equal(string_match_token("sym", tokens), (bool)true);
test_equal(string_match_token("rule", tokens), (bool)false);
}
void test_length()
{
test_equal(string_length(NULL), 0);
@ -152,5 +161,6 @@ DECLARE_UNIT_TESTS(
UNIT_TEST("String Comparison", test_compare),
UNIT_TEST("String Comparison (Tokens)", test_compare_tokens),
UNIT_TEST("String Match", test_match),
UNIT_TEST("String Match (Tokens)", test_match_tokens),
UNIT_TEST("String Length", test_length),
);