gstrfuncs: Add g_strv_contains()

Includes unit tests.

https://bugzilla.gnome.org/show_bug.cgi?id=685880
This commit is contained in:
Xavier Claessens 2013-04-15 14:54:31 +02:00 committed by Philip Withnall
parent 3f5a78a248
commit 71944b1bfd
4 changed files with 51 additions and 0 deletions

View File

@ -1380,6 +1380,7 @@ g_strconcat
g_strjoin
g_strjoinv
g_strv_length
g_strv_contains
<SUBSECTION>
g_strerror

View File

@ -3077,3 +3077,30 @@ one_matched:
return matched;
}
/**
* g_strv_contains:
* @strv: a %NULL-terminated array of strings
* @str: a string
*
* Checks if @strv contains @str. @strv must not be %NULL.
*
* Returns: %TRUE if @str is an element of @strv, according to g_str_equal().
*
* Since: 2.44
*/
gboolean
g_strv_contains (const gchar * const *strv,
const gchar *str)
{
g_return_val_if_fail (strv != NULL, FALSE);
g_return_val_if_fail (str != NULL, FALSE);
for (; *strv != NULL; strv++)
{
if (g_str_equal (str, *strv))
return TRUE;
}
return FALSE;
}

View File

@ -301,6 +301,10 @@ gboolean g_str_match_string (const g
const gchar *potential_hit,
gboolean accept_alternates);
GLIB_AVAILABLE_IN_2_44
gboolean g_strv_contains (const gchar * const *strv,
const gchar *str);
G_END_DECLS
#endif /* __G_STRFUNCS_H__ */

View File

@ -1461,6 +1461,24 @@ test_transliteration (void)
g_free (out);
}
static void
test_strv_contains (void)
{
static const gchar *strv_simple[] = { "hello", "there", NULL };
static const gchar *strv_dupe[] = { "dupe", "dupe", NULL };
static const gchar *strv_empty[] = { NULL };
g_assert_true (g_strv_contains (strv_simple, "hello"));
g_assert_true (g_strv_contains (strv_simple, "there"));
g_assert_false (g_strv_contains (strv_simple, "non-existent"));
g_assert_false (g_strv_contains (strv_simple, ""));
g_assert_true (g_strv_contains (strv_dupe, "dupe"));
g_assert_false (g_strv_contains (strv_empty, "empty!"));
g_assert_false (g_strv_contains (strv_empty, ""));
}
int
main (int argc,
char *argv[])
@ -1496,6 +1514,7 @@ main (int argc,
g_test_add_func ("/strfuncs/strsignal", test_strsignal);
g_test_add_func ("/strfuncs/strup", test_strup);
g_test_add_func ("/strfuncs/transliteration", test_transliteration);
g_test_add_func ("/strfuncs/strv-contains", test_strv_contains);
return g_test_run();
}