mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-25 06:56:14 +01:00
gstrfuncs: Add g_strv_contains()
Includes unit tests. https://bugzilla.gnome.org/show_bug.cgi?id=685880
This commit is contained in:
parent
3f5a78a248
commit
71944b1bfd
@ -1380,6 +1380,7 @@ g_strconcat
|
||||
g_strjoin
|
||||
g_strjoinv
|
||||
g_strv_length
|
||||
g_strv_contains
|
||||
|
||||
<SUBSECTION>
|
||||
g_strerror
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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__ */
|
||||
|
@ -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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user