[tepl] Utils: improve get_file_extension() and get_file_shortname()



commit e7c1e480695e2429858381bd41cb60d30f262dd5
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Mon Nov 18 13:26:55 2019 +0100

    Utils: improve get_file_extension() and get_file_shortname()
    
    - Support MS Windows.
    - Support UTF-8 non-ASCII characters in the file extension (why not).

 tepl/tepl-utils.c      |  4 ++--
 testsuite/test-utils.c | 16 +++++++++++++---
 2 files changed, 15 insertions(+), 5 deletions(-)
---
diff --git a/tepl/tepl-utils.c b/tepl/tepl-utils.c
index 45a609b..cd94f1b 100644
--- a/tepl/tepl-utils.c
+++ b/tepl/tepl-utils.c
@@ -150,7 +150,7 @@ get_extension_position (const gchar *filename)
        {
                pos = g_utf8_find_prev_char (filename, pos);
 
-               if (pos == NULL || pos[0] == '/')
+               if (pos == NULL || pos[0] == G_DIR_SEPARATOR)
                {
                        break;
                }
@@ -184,7 +184,7 @@ tepl_utils_get_file_extension (const gchar *filename)
 {
        gint pos = get_extension_position (filename);
 
-       return g_ascii_strdown (filename + pos, -1);
+       return g_utf8_strdown (filename + pos, -1);
 }
 
 /**
diff --git a/testsuite/test-utils.c b/testsuite/test-utils.c
index 85db02d..9dd17cd 100644
--- a/testsuite/test-utils.c
+++ b/testsuite/test-utils.c
@@ -56,13 +56,18 @@ test_get_file_extension (void)
        g_assert_cmpstr (extension, ==, ".gz");
        g_free (extension);
 
-       extension = tepl_utils_get_file_extension ("path/to/file.pdf");
+       extension = tepl_utils_get_file_extension ("path" G_DIR_SEPARATOR_S "file.pdf");
        g_assert_cmpstr (extension, ==, ".pdf");
        g_free (extension);
 
        extension = tepl_utils_get_file_extension ("file");
        g_assert_cmpstr (extension, ==, "");
        g_free (extension);
+
+       /* UTF-8 */
+       extension = tepl_utils_get_file_extension ("filé.éÉÈè");
+       g_assert_cmpstr (extension, ==, ".ééèè");
+       g_free (extension);
 }
 
 static void
@@ -82,8 +87,13 @@ test_get_file_shortname (void)
        g_assert_cmpstr (shortname, ==, "file");
        g_free (shortname);
 
-       shortname = tepl_utils_get_file_shortname ("dir.ext/blah");
-       g_assert_cmpstr (shortname, ==, "dir.ext/blah");
+       shortname = tepl_utils_get_file_shortname ("dir.ext" G_DIR_SEPARATOR_S "blah");
+       g_assert_cmpstr (shortname, ==, "dir.ext" G_DIR_SEPARATOR_S "blah");
+       g_free (shortname);
+
+       /* UTF-8 */
+       shortname = tepl_utils_get_file_shortname ("filé.éÉÈè");
+       g_assert_cmpstr (shortname, ==, "filé");
        g_free (shortname);
 }
 


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