[amtk] utils: implement remove_mnemonic()



commit 8f700c4c461b3f49f56d56dd38baa19201bc980b
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Apr 14 11:45:26 2018 +0200

    utils: implement remove_mnemonic()
    
    It seems that there is no public function doing that in GLib or GTK+, so
    I've implemented my own.

 amtk/amtk-utils.c                    |   43 ++++++++++++++++++++++++++++++++++
 amtk/amtk-utils.h                    |    2 +
 docs/reference/amtk-5.0-sections.txt |    1 +
 testsuite/test-utils.c               |   29 +++++++++++++++++++++++
 4 files changed, 75 insertions(+), 0 deletions(-)
---
diff --git a/amtk/amtk-utils.c b/amtk/amtk-utils.c
index 0e97091..fb6cb47 100644
--- a/amtk/amtk-utils.c
+++ b/amtk/amtk-utils.c
@@ -110,6 +110,49 @@ _amtk_utils_strv_copy (const gchar * const *strv)
        return new_strv;
 }
 
+/**
+ * amtk_utils_remove_mnemonic:
+ * @str: a string.
+ *
+ * Removes the mnemonics from @str. Single underscores are removed, and two
+ * consecutive underscores are replaced by one underscore (see the documentation
+ * of gtk_label_new_with_mnemonic()).
+ *
+ * Returns: (transfer full): the new string with the mnemonics removed. Free
+ * with g_free() when no longer needed.
+ * Since: 5.0
+ */
+gchar *
+amtk_utils_remove_mnemonic (const gchar *str)
+{
+       gchar *new_str;
+       gint str_pos;
+       gint new_str_pos = 0;
+       gboolean prev_char_is_underscore_skipped = FALSE;
+
+       g_return_val_if_fail (str != NULL, NULL);
+
+       new_str = g_malloc (strlen (str) + 1);
+
+       for (str_pos = 0; str[str_pos] != '\0'; str_pos++)
+       {
+               gchar cur_char = str[str_pos];
+
+               if (cur_char == '_' && !prev_char_is_underscore_skipped)
+               {
+                       prev_char_is_underscore_skipped = TRUE;
+               }
+               else
+               {
+                       new_str[new_str_pos++] = cur_char;
+                       prev_char_is_underscore_skipped = FALSE;
+               }
+       }
+
+       new_str[new_str_pos] = '\0';
+       return new_str;
+}
+
 static gint
 get_menu_item_position (GtkMenuShell *menu_shell,
                        GtkMenuItem  *item)
diff --git a/amtk/amtk-utils.h b/amtk/amtk-utils.h
index ebb22fc..3ce6b72 100644
--- a/amtk/amtk-utils.h
+++ b/amtk/amtk-utils.h
@@ -38,6 +38,8 @@ gchar *               _amtk_utils_replace_home_dir_with_tilde         (const gchar 
*filename);
 G_GNUC_INTERNAL
 gchar **       _amtk_utils_strv_copy                           (const gchar * const *strv);
 
+gchar *                amtk_utils_remove_mnemonic                      (const gchar *str);
+
 /* GTK+ utilities */
 
 gchar *                amtk_utils_recent_chooser_menu_get_item_uri     (GtkRecentChooserMenu *menu,
diff --git a/docs/reference/amtk-5.0-sections.txt b/docs/reference/amtk-5.0-sections.txt
index fcf435e..df1ae10 100644
--- a/docs/reference/amtk-5.0-sections.txt
+++ b/docs/reference/amtk-5.0-sections.txt
@@ -168,6 +168,7 @@ amtk_menu_shell_get_type
 
 <SECTION>
 <FILE>amtk-utils</FILE>
+amtk_utils_remove_mnemonic
 amtk_utils_recent_chooser_menu_get_item_uri
 amtk_utils_bind_g_action_to_gtk_action
 amtk_utils_create_gtk_action
diff --git a/testsuite/test-utils.c b/testsuite/test-utils.c
index b74f395..51d26a1 100644
--- a/testsuite/test-utils.c
+++ b/testsuite/test-utils.c
@@ -80,6 +80,34 @@ test_strv_copy (void)
        g_strfreev (heap_strv);
 }
 
+static void
+check_remove_mnemonic (const gchar *str_with_mnemonic,
+                      const gchar *expected_str_without_mnemonic)
+{
+       gchar *str_without_mnemonic;
+
+       str_without_mnemonic = amtk_utils_remove_mnemonic (str_with_mnemonic);
+       g_assert_cmpstr (str_without_mnemonic, ==, expected_str_without_mnemonic);
+       g_free (str_without_mnemonic);
+}
+
+static void
+test_remove_mnemonic (void)
+{
+       check_remove_mnemonic ("", "");
+       check_remove_mnemonic ("a", "a");
+       check_remove_mnemonic ("_a", "a");
+       check_remove_mnemonic ("__a", "_a");
+       check_remove_mnemonic ("___a", "_a");
+       check_remove_mnemonic ("S_maller Text", "Smaller Text");
+
+       /* With multi-byte UTF-8 chars. */
+       check_remove_mnemonic ("___ß_é__c____d", "_ßé_c__d");
+
+       /* Not valid mnemonic, but the function must not crash. */
+       check_remove_mnemonic ("a_", "a");
+}
+
 int
 main (int    argc,
       char **argv)
@@ -87,6 +115,7 @@ main (int    argc,
        g_test_init (&argc, &argv, NULL);
 
        g_test_add_func ("/utils/strv-copy", test_strv_copy);
+       g_test_add_func ("/utils/remove-mnemonic", test_remove_mnemonic);
 
        return g_test_run ();
 }


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