[gthumb] utils: added some string utility functions



commit d15b097c6914116ffa77fff6c79bef9147424fdf
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Mon May 17 13:26:27 2021 +0200

    utils: added some string utility functions

 gthumb/str-utils.c       | 31 +++++++++++++++++++++++++++++++
 gthumb/str-utils.h       |  3 +++
 gthumb/test-glib-utils.c | 20 ++++++++++++++++++++
 3 files changed, 54 insertions(+)
---
diff --git a/gthumb/str-utils.c b/gthumb/str-utils.c
index 9b00107f..8a054958 100644
--- a/gthumb/str-utils.c
+++ b/gthumb/str-utils.c
@@ -22,6 +22,7 @@
 #include <config.h>
 #include <glib.h>
 #include <glib/gi18n.h>
+#include "glib-utils.h"
 #include "str-utils.h"
 
 
@@ -46,6 +47,13 @@ _g_str_n_equal (const char *str1,
 }
 
 
+gboolean
+_g_str_empty (const char *str)
+{
+       return (str == NULL) || (str[0] == 0);
+}
+
+
 void
 _g_str_set (char       **str,
            const char  *value)
@@ -329,6 +337,29 @@ _g_utf8_find_str (const char *haystack,
 }
 
 
+char *
+_g_utf8_find_expr (const char *str,
+                  const char *pattern)
+{
+       GRegex     *regex;
+       GMatchInfo *match_info;
+       char       *value = NULL;
+
+       if ((str == NULL) || _g_str_empty (pattern))
+               return NULL;
+
+       regex = g_regex_new (pattern, 0, 0, NULL);
+       g_regex_match (regex, str, 0, &match_info);
+       if (g_match_info_matches (match_info))
+               value = g_match_info_fetch (match_info, 0);
+
+       g_match_info_free (match_info);
+       g_regex_unref (regex);
+
+       return value;
+}
+
+
 /* -- _g_utf8_split -- */
 
 
diff --git a/gthumb/str-utils.h b/gthumb/str-utils.h
index 0235d942..acc2ef9a 100644
--- a/gthumb/str-utils.h
+++ b/gthumb/str-utils.h
@@ -33,6 +33,7 @@ gboolean      _g_str_equal            (const char      *str1,
 gboolean       _g_str_n_equal          (const char      *str1,
                                         const char      *str2,
                                         gsize            size);
+gboolean       _g_str_empty            (const char      *str);
 void           _g_str_set              (char           **str,
                                         const char      *value);
 char **        _g_strv_take_from_str_list
@@ -64,6 +65,8 @@ char *                _g_utf8_strndup (const char      *str,
                                         gssize   size);
 const char *   _g_utf8_find_str        (const char      *haystack,
                                         const char      *needle);
+char *         _g_utf8_find_expr       (const char      *str,
+                                        const char      *pattern);
 char **        _g_utf8_split           (const char      *str,
                                         const char      *separator,
                                         int              max_tokens);
diff --git a/gthumb/test-glib-utils.c b/gthumb/test-glib-utils.c
index f300ed04..8cfbdfd2 100644
--- a/gthumb/test-glib-utils.c
+++ b/gthumb/test-glib-utils.c
@@ -176,6 +176,25 @@ test_g_utf8_remove_string_properties (void)
 }
 
 
+static void
+test_g_utf8_find_expr (void)
+{
+       g_assert_cmpstr (_g_utf8_find_expr (NULL, NULL), ==, NULL);
+       g_assert_cmpstr (_g_utf8_find_expr ("", NULL), ==, NULL);
+       g_assert_cmpstr (_g_utf8_find_expr (NULL, ""), ==, NULL);
+       g_assert_cmpstr (_g_utf8_find_expr ("", ""), ==, NULL);
+       g_assert_cmpstr (_g_utf8_find_expr ("日", ""), ==, NULL);
+       g_assert_cmpstr (_g_utf8_find_expr ("日", "日"), ==, "日");
+       g_assert_cmpstr (_g_utf8_find_expr ("日本語", "日"), ==, "日");
+       g_assert_cmpstr (_g_utf8_find_expr ("日本語", "語"), ==, "語");
+       g_assert_cmpstr (_g_utf8_find_expr ("日本語", "本本"), ==, NULL);
+       g_assert_cmpstr (_g_utf8_find_expr ("日本語", "[0-9]+"), ==, NULL);
+       g_assert_cmpstr (_g_utf8_find_expr ("日本1234語", "[0-9]+"), ==, "1234");
+       g_assert_cmpstr (_g_utf8_find_expr ("日1本9876語", "[0-9]+"), ==, "1");
+       g_assert_cmpstr (_g_utf8_find_expr ("日1234本9876語", "[0-9]+"), ==, "1234");
+}
+
+
 static void
 test_g_path_get_parent_all (void)
 {
@@ -1210,6 +1229,7 @@ main (int   argc,
        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_utf8_find_expr", test_g_utf8_find_expr);
 
        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);


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