[gtksourceview/wip/encodings: 3/3] Add gtk_source_encoding_remove_duplicates()



commit 0b27cb2a8c58cc5c9419f5de6f9cd89db5eebeba
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Aug 1 15:55:39 2014 +0200

    Add gtk_source_encoding_remove_duplicates()
    
    A convenience function.

 docs/reference/gtksourceview-3.0-sections.txt |    1 +
 gtksourceview/gtksourceencoding.c             |   34 +++++++++++++++-
 gtksourceview/gtksourceencoding.h             |    2 +
 tests/Makefile.am                             |    3 +
 tests/test-encoding.c                         |   55 +++++++++++++++++++++++++
 5 files changed, 94 insertions(+), 1 deletions(-)
---
diff --git a/docs/reference/gtksourceview-3.0-sections.txt b/docs/reference/gtksourceview-3.0-sections.txt
index acd010c..16b2d4b 100644
--- a/docs/reference/gtksourceview-3.0-sections.txt
+++ b/docs/reference/gtksourceview-3.0-sections.txt
@@ -223,6 +223,7 @@ gtk_source_encoding_get_name
 gtk_source_encoding_get_charset
 gtk_source_encoding_foreach
 gtk_source_encoding_get_default_candidates
+gtk_source_encoding_remove_duplicates
 gtk_source_encoding_copy
 gtk_source_encoding_free
 <SUBSECTION Standard>
diff --git a/gtksourceview/gtksourceencoding.c b/gtksourceview/gtksourceencoding.c
index c8a2ba9..cb1086b 100644
--- a/gtksourceview/gtksourceencoding.c
+++ b/gtksourceview/gtksourceencoding.c
@@ -564,7 +564,6 @@ gtk_source_encoding_free (GtkSourceEncoding *enc)
        g_return_if_fail (enc != NULL);
 }
 
-/* Will probably be used in the future. */
 static gboolean
 data_exists (GSList         *list,
             const gpointer  data)
@@ -610,6 +609,7 @@ _gtk_source_encoding_strv_to_list (const gchar * const *enc_str)
 }
 
 #if 0
+/* Will probably be used in the future. */
 gchar **
 _gtk_source_encoding_list_to_strv (const GSList *enc_list)
 {
@@ -633,3 +633,35 @@ _gtk_source_encoding_list_to_strv (const GSList *enc_list)
        return (gchar **)g_ptr_array_free (array, FALSE);
 }
 #endif
+
+/**
+ * gtk_source_encoding_remove_duplicates:
+ * @encodings: (element-type GtkSource.Encoding): a list on #GtkSourceEncoding's.
+ *
+ * A convenience function to remove duplicated encodings in a list.
+ * The first occurrence of an encoding is kept.
+ *
+ * Returns: (transfer container) (element-type GtkSource.Encoding): the new
+ * start of the #GSList.
+ */
+GSList *
+gtk_source_encoding_remove_duplicates (GSList *encodings)
+{
+       GSList *new_encodings = NULL;
+       GSList *l;
+
+       for (l = encodings; l != NULL; l = l->next)
+       {
+               gpointer cur_encoding = l->data;
+
+               if (!data_exists (new_encodings, cur_encoding))
+               {
+                       new_encodings = g_slist_prepend (new_encodings, cur_encoding);
+               }
+       }
+
+       new_encodings = g_slist_reverse (new_encodings);
+
+       g_slist_free (encodings);
+       return new_encodings;
+}
diff --git a/gtksourceview/gtksourceencoding.h b/gtksourceview/gtksourceencoding.h
index 2567e3e..14836b5 100644
--- a/gtksourceview/gtksourceencoding.h
+++ b/gtksourceview/gtksourceencoding.h
@@ -76,6 +76,8 @@ G_GNUC_INTERNAL
 gchar                 **_gtk_source_encoding_list_to_strv      (const GSList            *enc);
 #endif
 
+GSList                 *gtk_source_encoding_remove_duplicates  (GSList *encodings);
+
 G_END_DECLS
 
 #endif  /* __GTK_SOURCE_ENCODING_H__ */
diff --git a/tests/Makefile.am b/tests/Makefile.am
index de12842..003d2fc 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -75,6 +75,9 @@ test_completion_model_SOURCES =       test-completion-model.c
 UNIT_TEST_PROGS += test-completion-words
 test_completion_words_SOURCES = test-completion-words.c
 
+UNIT_TEST_PROGS += test-encoding
+test_encoding_SOURCES = test-encoding.c
+
 UNIT_TEST_PROGS += test-file-loader
 test_file_loader_SOURCES = test-file-loader.c
 
diff --git a/tests/test-encoding.c b/tests/test-encoding.c
new file mode 100644
index 0000000..f76ca65
--- /dev/null
+++ b/tests/test-encoding.c
@@ -0,0 +1,55 @@
+/*
+ * test-encoding.c
+ * This file is part of GtkSourceView
+ *
+ * Copyright (C) 2014 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * GtkSourceView is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * GtkSourceView is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include <gtksourceview/gtksource.h>
+
+static void
+test_remove_duplicates (void)
+{
+       GSList *list = NULL;
+       const GtkSourceEncoding *utf8;
+       const GtkSourceEncoding *iso;
+
+       utf8 = gtk_source_encoding_get_utf8 ();
+       iso = gtk_source_encoding_get_from_charset ("ISO-8859-15");
+
+       /* Before: [UTF-8, ISO-8859-15, UTF-8] */
+       list = g_slist_prepend (list, (gpointer) utf8);
+       list = g_slist_prepend (list, (gpointer) iso);
+       list = g_slist_prepend (list, (gpointer) utf8);
+
+       /* After: [UTF-8, ISO-8859-15] */
+       list = gtk_source_encoding_remove_duplicates (list);
+
+       g_assert_cmpint (2, ==, g_slist_length (list));
+       g_assert (list->data == utf8);
+       g_assert (list->next->data == iso);
+}
+
+int
+main (int argc, char **argv)
+{
+       gtk_test_init (&argc, &argv);
+
+       g_test_add_func ("/Encoding/remove_duplicates", test_remove_duplicates);
+
+       return g_test_run ();
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]