[gtk/ci-file-filters: 5475/5477] Add a helper to make case-insensitive globs




commit 7ccf9d159b23d15619fffc247e96c27b4f671565
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Jun 3 23:50:09 2021 -0400

    Add a helper to make case-insensitive globs
    
    This will be used in GtkFileFilter in the future.

 gtk/fnmatch.c    | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 gtk/gtkprivate.h |  3 +++
 2 files changed, 50 insertions(+)
---
diff --git a/gtk/fnmatch.c b/gtk/fnmatch.c
index 0ad1cd8992..4fe826fefa 100644
--- a/gtk/fnmatch.c
+++ b/gtk/fnmatch.c
@@ -253,3 +253,50 @@ _gtk_fnmatch (const char *pattern,
 {
   return gtk_fnmatch_intern (pattern, string, TRUE, no_leading_period, casefold);
 }
+
+/* Turn a glob pattern into a case-insensitive one, by replacing
+ * alphabetic characters by [xX] ranges.
+ */
+char *
+_gtk_make_ci_glob_pattern (const char *pattern)
+{
+  GString *s;
+  gboolean in_range = FALSE;
+
+  s = g_string_new ("");
+  for (const char *p = pattern; *p; p = g_utf8_next_char (p))
+    {
+      gunichar c = g_utf8_get_char (p);
+      if (in_range)
+        {
+          g_string_append_unichar (s, c);
+          if (c == ']')
+            in_range = FALSE;
+          continue;
+        }
+
+      if (c == '[')
+        {
+          g_string_append (s, "[");
+          p = g_utf8_next_char (p);
+          c = g_utf8_get_char (p);
+          if (c == ']')
+            g_string_append (s, "]");
+          in_range = TRUE;
+          continue;
+        }
+      else if (g_unichar_isalpha (c))
+        {
+          g_string_append (s, "[");
+          g_string_append_unichar (s, g_unichar_tolower (c));
+          g_string_append_unichar (s, g_unichar_toupper (c));
+          g_string_append (s, "]");
+        }
+      else
+        {
+          g_string_append_unichar (s, c);
+        }
+    }
+
+  return g_string_free (s, FALSE);
+}
diff --git a/gtk/gtkprivate.h b/gtk/gtkprivate.h
index d24c272065..8cb11c761c 100644
--- a/gtk/gtkprivate.h
+++ b/gtk/gtkprivate.h
@@ -64,6 +64,9 @@ gboolean      _gtk_fnmatch                (const char *pattern,
                                            gboolean    no_leading_period,
                                            gboolean    casefold);
 
+char *        _gtk_make_ci_glob_pattern   (const char *pattern);
+
+
 char        * _gtk_get_lc_ctype           (void);
 
 void          _gtk_ensure_resources       (void);


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