gstrfuncs: Add g_strv_contains()
authorXavier Claessens <xavier.claessens@collabora.co.uk>
Mon, 15 Apr 2013 12:54:31 +0000 (14:54 +0200)
committerMaciej Wereski <m.wereski@partner.samsung.com>
Fri, 10 Jul 2015 09:47:42 +0000 (11:47 +0200)
Includes unit tests.

https://bugzilla.gnome.org/show_bug.cgi?id=685880

docs/reference/glib/glib-sections.txt
glib/gstrfuncs.c
glib/gstrfuncs.h
glib/tests/strfuncs.c

index 19bae6f..20b7101 100644 (file)
@@ -1380,6 +1380,7 @@ g_strconcat
 g_strjoin
 g_strjoinv
 g_strv_length
+g_strv_contains
 
 <SUBSECTION>
 g_strerror
index 0a9b109..e2b1fb3 100644 (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;
+}
index 41fc839..76004aa 100644 (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__ */
index 17f6f73..27dfb45 100644 (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();
 }