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



commit f68b8d6c55c13c6304810eff54870fd1f514130c
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 |    2 +
 gtksourceview/gtksourceencoding.c             |   76 ++++++++++++++++++++++++-
 gtksourceview/gtksourceencoding.h             |   19 ++++++
 tests/Makefile.am                             |    3 +
 tests/test-encoding.c                         |   67 ++++++++++++++++++++++
 5 files changed, 166 insertions(+), 1 deletions(-)
---
diff --git a/docs/reference/gtksourceview-3.0-sections.txt b/docs/reference/gtksourceview-3.0-sections.txt
index acd010c..231a7fd 100644
--- a/docs/reference/gtksourceview-3.0-sections.txt
+++ b/docs/reference/gtksourceview-3.0-sections.txt
@@ -214,6 +214,7 @@ gtk_source_completion_words_get_type
 <TITLE>GtkSourceEncoding</TITLE>
 GtkSourceEncoding
 GtkSourceEncodingForeachFunc
+GtkSourceEncodingDuplicates
 <SUBSECTION>
 gtk_source_encoding_get_utf8
 gtk_source_encoding_get_current
@@ -223,6 +224,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 d6b76e8..6e65132 100644
--- a/gtksourceview/gtksourceencoding.c
+++ b/gtksourceview/gtksourceencoding.c
@@ -565,7 +565,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)
@@ -611,6 +610,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)
 {
@@ -634,3 +634,77 @@ _gtk_source_encoding_list_to_strv (const GSList *enc_list)
        return (gchar **)g_ptr_array_free (array, FALSE);
 }
 #endif
+
+static GSList *
+remove_duplicates_keep_first (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;
+}
+
+static GSList *
+remove_duplicates_keep_last (GSList *encodings)
+{
+       GSList *new_encodings = NULL;
+       GSList *l;
+
+       encodings = g_slist_reverse (encodings);
+
+       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);
+               }
+       }
+
+       g_slist_free (encodings);
+       return new_encodings;
+}
+
+/**
+ * gtk_source_encoding_remove_duplicates:
+ * @encodings: (element-type GtkSource.Encoding): a list of #GtkSourceEncoding's.
+ * @removal_type: the #GtkSourceEncodingDuplicates.
+ *
+ * A convenience function to remove duplicated encodings in a list.
+ *
+ * Returns: (transfer container) (element-type GtkSource.Encoding): the new
+ * start of the #GSList.
+ * Since: 3.14
+ */
+GSList *
+gtk_source_encoding_remove_duplicates (GSList                      *encodings,
+                                      GtkSourceEncodingDuplicates  removal_type)
+{
+       switch (removal_type)
+       {
+               case GTK_SOURCE_ENCODING_DUPLICATES_KEEP_FIRST:
+                       return remove_duplicates_keep_first (encodings);
+
+               case GTK_SOURCE_ENCODING_DUPLICATES_KEEP_LAST:
+                       return remove_duplicates_keep_last (encodings);
+
+               default:
+                       break;
+       }
+
+       g_return_val_if_reached (encodings);
+}
diff --git a/gtksourceview/gtksourceencoding.h b/gtksourceview/gtksourceencoding.h
index 2567e3e..4ed38e1 100644
--- a/gtksourceview/gtksourceencoding.h
+++ b/gtksourceview/gtksourceencoding.h
@@ -45,6 +45,22 @@ G_BEGIN_DECLS
 typedef void (*GtkSourceEncodingForeachFunc) (const GtkSourceEncoding *encoding,
                                              gpointer                 userdata);
 
+/**
+ * GtkSourceEncodingDuplicates:
+ * @GTK_SOURCE_ENCODING_DUPLICATES_KEEP_FIRST: Keep the first occurrence.
+ * @GTK_SOURCE_ENCODING_DUPLICATES_KEEP_LAST: Keep the last occurrence.
+ *
+ * Specifies which encoding occurrence to keep when removing duplicated
+ * encodings in a list with gtk_source_encoding_remove_duplicates().
+ *
+ * Since: 3.14
+ */
+typedef enum _GtkSourceEncodingDuplicates
+{
+       GTK_SOURCE_ENCODING_DUPLICATES_KEEP_FIRST,
+       GTK_SOURCE_ENCODING_DUPLICATES_KEEP_LAST
+} GtkSourceEncodingDuplicates;
+
 GType                   gtk_source_encoding_get_type           (void) G_GNUC_CONST;
 
 const GtkSourceEncoding        *gtk_source_encoding_get_from_charset   (const gchar             *charset);
@@ -63,6 +79,9 @@ void                   gtk_source_encoding_foreach            (GtkSourceEncodingForeachFunc 
func,
 GSList                 *gtk_source_encoding_get_default_candidates
                                                                (void);
 
+GSList                 *gtk_source_encoding_remove_duplicates  (GSList                      *encodings,
+                                                                GtkSourceEncodingDuplicates  removal_type);
+
 /* These should not be used, they are just to make python bindings happy */
 GtkSourceEncoding      *gtk_source_encoding_copy               (const GtkSourceEncoding *enc);
 void                    gtk_source_encoding_free               (GtkSourceEncoding       *enc);
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..1e52cff
--- /dev/null
+++ b/tests/test-encoding.c
@@ -0,0 +1,67 @@
+/*
+ * 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, GTK_SOURCE_ENCODING_DUPLICATES_KEEP_FIRST);
+
+       g_assert_cmpint (2, ==, g_slist_length (list));
+       g_assert (list->data == utf8);
+       g_assert (list->next->data == iso);
+
+       /* Before: [UTF-8, ISO-8859-15, UTF-8] */
+       list = g_slist_append (list, (gpointer) utf8);
+
+       /* After: [ISO-8859-15, UTF-8] */
+       list = gtk_source_encoding_remove_duplicates (list, GTK_SOURCE_ENCODING_DUPLICATES_KEEP_LAST);
+
+       g_assert_cmpint (2, ==, g_slist_length (list));
+       g_assert (list->data == iso);
+       g_assert (list->next->data == utf8);
+
+       g_slist_free (list);
+}
+
+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]