From f70920015c5882213e7de269c4d4ef5d45f5e7e7 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 19 May 2016 12:02:56 -0400 Subject: [PATCH] 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 --- core/string.c | 12 ++++++++++++ include/core/string.h | 3 +++ tests/core/string.c | 10 ++++++++++ 3 files changed, 25 insertions(+) diff --git a/core/string.c b/core/string.c index 0bef7953..512a1202 100644 --- a/core/string.c +++ b/core/string.c @@ -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; +} diff --git a/include/core/string.h b/include/core/string.h index 4f154448..1fab6ef7 100644 --- a/include/core/string.h +++ b/include/core/string.h @@ -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) { diff --git a/tests/core/string.c b/tests/core/string.c index 727f8ed6..9ed35550 100644 --- a/tests/core/string.c +++ b/tests/core/string.c @@ -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), );