[gthumb/gthumb-3-10] exiv2: remove the charset= prefix from strings



commit 1160f9fdfd268bceba5d6dacac161ac31d7bdac9
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Thu Dec 31 11:49:45 2020 +0100

    exiv2: remove the charset= prefix from strings
    
    Fixes https://gitlab.gnome.org/GNOME/gthumb/-/issues/137

 extensions/exiv2_tools/exiv2-utils.cpp | 10 +++-------
 gthumb/str-utils.c                     | 20 ++++++++++++++++++++
 gthumb/str-utils.h                     |  2 ++
 gthumb/test-glib-utils.c               | 26 +++++++++++++++++---------
 4 files changed, 42 insertions(+), 16 deletions(-)
---
diff --git a/extensions/exiv2_tools/exiv2-utils.cpp b/extensions/exiv2_tools/exiv2-utils.cpp
index 39344d3e..e20ba1e4 100644
--- a/extensions/exiv2_tools/exiv2-utils.cpp
+++ b/extensions/exiv2_tools/exiv2-utils.cpp
@@ -273,14 +273,10 @@ create_metadata (const char *key,
                else
                        formatted_value_utf8 = g_locale_to_utf8 (formatted_value, -1, NULL, NULL, NULL);
        }
-       else if (_g_utf8_has_prefix (formatted_value_utf8, "lang=")) {
-               const char *after_space;
-               char       *formatted_clean;
-
-               after_space = _g_utf8_after_ascii_space (formatted_value_utf8);
-               formatted_clean = g_strdup (after_space);
+       else {
+               char *tmp = _g_utf8_remove_string_properties (formatted_value_utf8);
                g_free (formatted_value_utf8);
-               formatted_value_utf8 = formatted_clean;
+               formatted_value_utf8 = tmp;
        }
 
        if (formatted_value_utf8 == NULL)
diff --git a/gthumb/str-utils.c b/gthumb/str-utils.c
index a431ac73..828f6202 100644
--- a/gthumb/str-utils.c
+++ b/gthumb/str-utils.c
@@ -995,3 +995,23 @@ _g_utf8_text_escape_xml    (const char *str)
 {
        return _g_utf8_escape_xml_flags (str, XML_ESCAPE_TEXT);
 }
+
+
+char *
+_g_utf8_remove_string_properties (const char *str)
+{
+       const char *known_properties[] = { "lang=", "charset=" };
+       const char *after_properties = str;
+       int         i;
+
+       for (i = 0; i < G_N_ELEMENTS (known_properties); /* void */) {
+               if (_g_utf8_has_prefix (after_properties, known_properties[i])) {
+                       after_properties = _g_utf8_after_ascii_space (after_properties);
+                       i = 0;
+               }
+               else
+                       i++;
+       }
+
+       return (after_properties != NULL) ? g_strdup (after_properties) : NULL;
+}
diff --git a/gthumb/str-utils.h b/gthumb/str-utils.h
index 0f91d7c1..cbd32656 100644
--- a/gthumb/str-utils.h
+++ b/gthumb/str-utils.h
@@ -96,6 +96,8 @@ char *                _g_utf8_translate       (const char      *str,
                                         ...) G_GNUC_NULL_TERMINATED;
 char *         _g_utf8_escape_xml      (const char      *str);
 char *         _g_utf8_text_escape_xml (const char      *str);
+char *         _g_utf8_remove_string_properties
+                                       (const char      *str);
 
 G_END_DECLS
 
diff --git a/gthumb/test-glib-utils.c b/gthumb/test-glib-utils.c
index 4e368176..cf92f40c 100644
--- a/gthumb/test-glib-utils.c
+++ b/gthumb/test-glib-utils.c
@@ -146,13 +146,12 @@ test_g_utf8_find_str (void)
 
 
 static void
-test_remove_lang_from_utf8_string (const char *value,
-                                  const char *expected)
+test_remove_properties_from_utf8_string (const char *value,
+                                        const char *expected)
 {
        char *result = NULL;
 
-       if (_g_utf8_has_prefix (value, "lang="))
-               result = g_strdup (_g_utf8_after_ascii_space (value));
+       result = _g_utf8_remove_string_properties (value);
        g_assert_cmpstr (result, ==, expected);
 
        g_free (result);
@@ -160,11 +159,20 @@ test_remove_lang_from_utf8_string (const char *value,
 
 
 static void
-test_remove_lang_from_utf8_string_all (void)
+test_g_utf8_remove_string_properties (void)
 {
-       test_remove_lang_from_utf8_string ("lang=EN hello", "hello");
-       test_remove_lang_from_utf8_string ("lang=FR langue d’oïl", "langue d’oïl");
-       test_remove_lang_from_utf8_string ("lang=正體字/繁體字 中华人民共和国", "中华人民共和国");
+       test_remove_properties_from_utf8_string (NULL, NULL);
+       test_remove_properties_from_utf8_string ("", "");
+       test_remove_properties_from_utf8_string ("正體字", "正體字");
+       test_remove_properties_from_utf8_string ("langue d’oïl", "langue d’oïl");
+       test_remove_properties_from_utf8_string ("lang=FR langue d’oïl", "langue d’oïl");
+       test_remove_properties_from_utf8_string ("lang=正體字/繁體字 中华人民共和国", "中华人民共和国");
+       test_remove_properties_from_utf8_string ("charset=UTF-8 langue d’oïl", "langue d’oïl");
+       test_remove_properties_from_utf8_string ("lang=FR charset=UTF-8 langue d’oïl", "langue d’oïl");
+       test_remove_properties_from_utf8_string ("charset=UTF-8 lang=FR langue d’oïl", "langue d’oïl");
+       test_remove_properties_from_utf8_string ("lang=FR charset=UTF-8 field=value langue d’oïl", 
"field=value langue d’oïl");
+       test_remove_properties_from_utf8_string ("lang=FR field=value charset=UTF-8 langue d’oïl", 
"field=value charset=UTF-8 langue d’oïl");
+       test_remove_properties_from_utf8_string ("field=value lang=FR charset=UTF-8 langue d’oïl", 
"field=value lang=FR charset=UTF-8 langue d’oïl");
 }
 
 
@@ -926,6 +934,7 @@ main (int   argc,
        g_test_add_func ("/glib-utils/_g_utf8_split_template", test_g_utf8_split_template_all);
        g_test_add_func ("/glib-utils/_g_utf8_strip", test_g_utf8_strip_all);
        g_test_add_func ("/glib-utils/_g_utf8_translate", test_g_utf8_translate_all);
+       g_test_add_func ("/glib-utils/_g_utf8_remove_string_properties", 
test_g_utf8_remove_string_properties);
 
        g_test_add_func ("/glib-utils/_g_path_get_basename", test_g_path_get_basename_all);
        g_test_add_func ("/glib-utils/_g_path_get_extension", test_g_path_get_extension_all);
@@ -949,7 +958,6 @@ main (int   argc,
        g_test_add_func ("/glib-utils/_g_file_get_display_name", test_g_file_get_display_name_all);
        g_test_add_func ("/glib-utils/_g_rand_string", test_g_rand_string);
        g_test_add_func ("/glib-utils/regex", test_regexp);
-       g_test_add_func ("/glib-utils/remove_lang_from_utf8_string", test_remove_lang_from_utf8_string_all);
 
        return g_test_run ();
 }


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