[gtksourceview/wip/encodings: 5/6] encoding: add private function remove_duplicates()



commit 830d9dc362fb0aa733c49b7445c0d95d0e479a8d
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Tue Aug 12 15:59:05 2014 +0200

    encoding: add private function remove_duplicates()
    
    It'll be used later.

 docs/reference/Makefile.am                |    1 +
 gtksourceview/Makefile.am                 |    1 +
 gtksourceview/gtksourceencoding-private.h |   51 +++++++++++++++++++
 gtksourceview/gtksourceencoding.c         |   75 +++++++++++++++++++++++++++++
 tests/Makefile.am                         |    3 +
 tests/test-encoding.c                     |   68 ++++++++++++++++++++++++++
 6 files changed, 199 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/Makefile.am b/docs/reference/Makefile.am
index 998a0d3..01b90f1 100644
--- a/docs/reference/Makefile.am
+++ b/docs/reference/Makefile.am
@@ -32,6 +32,7 @@ IGNORE_HFILES =                                       \
        gtksourcecompletionwordsproposal.h      \
        gtksourcecompletionwordsutils.h         \
        gtksourcecontextengine.h                \
+       gtksourceencoding-private.h             \
        gtksourceengine.h                       \
        gtksourcegutter-private.h               \
        gtksourcegutterrendererlines.h          \
diff --git a/gtksourceview/Makefile.am b/gtksourceview/Makefile.am
index 3b90e96..ea9b9b5 100644
--- a/gtksourceview/Makefile.am
+++ b/gtksourceview/Makefile.am
@@ -59,6 +59,7 @@ libgtksourceview_private_headers =            \
        gtksourcecompletionmodel.h              \
        gtksourcecompletion-private.h           \
        gtksourcecontextengine.h                \
+       gtksourceencoding-private.h             \
        gtksourceengine.h                       \
        gtksourcegutter-private.h               \
        gtksourcegutterrendererlines.h          \
diff --git a/gtksourceview/gtksourceencoding-private.h b/gtksourceview/gtksourceencoding-private.h
new file mode 100644
index 0000000..133042b
--- /dev/null
+++ b/gtksourceview/gtksourceencoding-private.h
@@ -0,0 +1,51 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
+/* gtksourceencoding-private.h
+ * 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
+ */
+
+#ifndef __GTK_SOURCE_ENCODING_PRIVATE_H__
+#define __GTK_SOURCE_ENCODING_PRIVATE_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+/*
+ * 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;
+
+G_GNUC_INTERNAL
+GSList *               _gtk_source_encoding_remove_duplicates  (GSList                      *encodings,
+                                                                GtkSourceEncodingDuplicates  removal_type);
+
+G_END_DECLS
+
+#endif  /* __GTK_SOURCE_ENCODING_PRIVATE_H__ */
diff --git a/gtksourceview/gtksourceencoding.c b/gtksourceview/gtksourceencoding.c
index b0fc819..40421e4 100644
--- a/gtksourceview/gtksourceencoding.c
+++ b/gtksourceview/gtksourceencoding.c
@@ -21,6 +21,7 @@
  */
 
 #include "gtksourceencoding.h"
+#include "gtksourceencoding-private.h"
 #include "gtksourceview-i18n.h"
 
 /**
@@ -531,6 +532,80 @@ strv_to_list (const gchar * const *enc_str)
        return g_slist_reverse (res);
 }
 
+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);
+}
+
 /**
  * gtk_source_encoding_get_default_candidates:
  *
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..7ab5da6
--- /dev/null
+++ b/tests/test-encoding.c
@@ -0,0 +1,68 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
+/* 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>
+#include "gtksourceview/gtksourceencoding-private.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]