[gtk/prop-list: 1/2] string filter: Add a match mode



commit dbae1469be4d877b1a1b1b581347825c91a71844
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Dec 2 20:27:09 2019 -0500

    string filter: Add a match mode
    
    This replaces the match-substring property and
    generalizes it.

 gtk/gtkstringfilter.c      | 72 +++++++++++++++++++++++++++-------------------
 gtk/gtkstringfilter.h      | 14 +++++++--
 testsuite/gtk/expression.c |  8 +++---
 testsuite/gtk/filter.c     |  4 +--
 4 files changed, 60 insertions(+), 38 deletions(-)
---
diff --git a/gtk/gtkstringfilter.c b/gtk/gtkstringfilter.c
index ae29bee7e5..3b3443f621 100644
--- a/gtk/gtkstringfilter.c
+++ b/gtk/gtkstringfilter.c
@@ -32,7 +32,7 @@ struct _GtkStringFilter
   char *search_prepared;
 
   gboolean ignore_case;
-  gboolean match_substring;
+  GtkStringFilterMatchMode match_mode;
 
   GtkExpression *expression;
 };
@@ -41,7 +41,7 @@ enum {
   PROP_0,
   PROP_EXPRESSION,
   PROP_IGNORE_CASE,
-  PROP_MATCH_SUBSTRING,
+  PROP_MATCH_MODE,
   PROP_SEARCH,
   NUM_PROPERTIES
 };
@@ -92,12 +92,25 @@ gtk_string_filter_filter (GtkFilter *filter,
     return FALSE;
   prepared = gtk_string_filter_prepare (self, s);
 
-  if (self->match_substring)
-    result = strstr (prepared, self->search_prepared) != NULL;
-  else
-    result = strcmp (prepared, self->search_prepared) == 0;
+  switch (self->match_mode)
+    {
+    case GTK_STRING_FILTER_MATCH_EXACT:
+      result = strcmp (prepared, self->search_prepared) == 0;
+      break;
+    case GTK_STRING_FILTER_MATCH_SUBSTRING:
+      result = strstr (prepared, self->search_prepared) != NULL;
+      break;
+    case GTK_STRING_FILTER_MATCH_PREFIX:
+      result = g_str_has_prefix (prepared, self->search_prepared);
+      break;
+    case GTK_STRING_FILTER_MATCH_SUFFIX:
+      result = g_str_has_suffix (prepared, self->search_prepared);
+      break;
+    default:
+      g_assert_not_reached ();
+    }
 
-#if 1
+#if 0
   g_print ("%s (%s) %s %s (%s)\n", s, prepared, result ? "==" : "!=", self->search, self->search_prepared);
 #endif
 
@@ -125,8 +138,8 @@ gtk_string_filter_set_property (GObject      *object,
       gtk_string_filter_set_ignore_case (self, g_value_get_boolean (value));
       break;
 
-    case PROP_MATCH_SUBSTRING:
-      gtk_string_filter_set_match_substring (self, g_value_get_boolean (value));
+    case PROP_MATCH_MODE:
+      gtk_string_filter_set_match_mode (self, g_value_get_enum (value));
       break;
 
     case PROP_SEARCH:
@@ -157,8 +170,8 @@ gtk_string_filter_get_property (GObject     *object,
       g_value_set_boolean (value, self->ignore_case);
       break;
 
-    case PROP_MATCH_SUBSTRING:
-      g_value_set_boolean (value, self->match_substring);
+    case PROP_MATCH_MODE:
+      g_value_set_enum (value, self->match_mode);
       break;
 
     case PROP_SEARCH:
@@ -220,16 +233,17 @@ gtk_string_filter_class_init (GtkStringFilterClass *class)
                             G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
 
   /**
-   * GtkStringFilter:match-substring:
+   * GtkStringFilter:match-mode:
    *
    * If exact matches are necessary or if substrings are allowed
    */
-  properties[PROP_MATCH_SUBSTRING] =
-      g_param_spec_boolean ("match-substring",
-                            P_("Match substring"),
-                            P_("If exact matches are necessary or if substrings are allowed"),
-                            TRUE,
-                            G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
+  properties[PROP_MATCH_MODE] =
+      g_param_spec_enum ("match-mode",
+                         P_("Match mode"),
+                         P_("If exact matches are necessary or if substrings are allowed"),
+                         GTK_TYPE_STRING_FILTER_MATCH_MODE,
+                         GTK_STRING_FILTER_MATCH_EXACT,
+                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
 
   /**
    * GtkStringFilter:search:
@@ -251,7 +265,7 @@ static void
 gtk_string_filter_init (GtkStringFilter *self)
 {
   self->ignore_case = TRUE;
-  self->match_substring = TRUE;
+  self->match_mode = GTK_STRING_FILTER_MATCH_EXACT;
 
   gtk_filter_changed (GTK_FILTER (self), GTK_FILTER_CHANGE_MATCH_ALL);
 }
@@ -388,30 +402,30 @@ gtk_string_filter_set_ignore_case (GtkStringFilter *self,
   g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_IGNORE_CASE]);
 }
 
-gboolean
-gtk_string_filter_get_match_substring (GtkStringFilter *self)
+GtkStringFilterMatchMode
+gtk_string_filter_get_match_mode (GtkStringFilter *self)
 {
-  g_return_val_if_fail (GTK_IS_STRING_FILTER (self), TRUE);
+  g_return_val_if_fail (GTK_IS_STRING_FILTER (self), GTK_STRING_FILTER_MATCH_EXACT);
 
-  return self->match_substring;
+  return self->match_mode;
 }
 
 
 void
-gtk_string_filter_set_match_substring (GtkStringFilter *self,
-                                       gboolean         match_substring)
+gtk_string_filter_set_match_mode (GtkStringFilter *self,
+                                  GtkStringFilterMatchMode mode)
 {
   g_return_if_fail (GTK_IS_STRING_FILTER (self));
 
-  if (self->match_substring == match_substring)
+  if (self->match_mode == mode)
     return;
 
-  self->match_substring = match_substring;
+  self->match_mode = mode;
 
   if (self->search)
-    gtk_filter_changed (GTK_FILTER (self), match_substring ? GTK_FILTER_CHANGE_LESS_STRICT : 
GTK_FILTER_CHANGE_MORE_STRICT);
+    gtk_filter_changed (GTK_FILTER (self), mode == GTK_STRING_FILTER_MATCH_EXACT ? 
GTK_FILTER_CHANGE_MORE_STRICT : GTK_FILTER_CHANGE_LESS_STRICT);
 
-  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MATCH_SUBSTRING]);
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MATCH_MODE]);
 }
 
                                                                  
diff --git a/gtk/gtkstringfilter.h b/gtk/gtkstringfilter.h
index 3571fad2f2..e5e6cb719f 100644
--- a/gtk/gtkstringfilter.h
+++ b/gtk/gtkstringfilter.h
@@ -51,11 +51,19 @@ gboolean                gtk_string_filter_get_ignore_case       (GtkStringFilter
 GDK_AVAILABLE_IN_ALL
 void                    gtk_string_filter_set_ignore_case       (GtkStringFilter        *self,
                                                                  gboolean                ignore_case);
+
+typedef enum {
+  GTK_STRING_FILTER_MATCH_EXACT,
+  GTK_STRING_FILTER_MATCH_SUBSTRING,
+  GTK_STRING_FILTER_MATCH_PREFIX,
+  GTK_STRING_FILTER_MATCH_SUFFIX
+} GtkStringFilterMatchMode;
+
 GDK_AVAILABLE_IN_ALL
-gboolean                gtk_string_filter_get_match_substring   (GtkStringFilter        *self);
+GtkStringFilterMatchMode gtk_string_filter_get_match_mode   (GtkStringFilter          *self);
 GDK_AVAILABLE_IN_ALL
-void                    gtk_string_filter_set_match_substring   (GtkStringFilter        *self,
-                                                                 gboolean                match_substring);
+void                     gtk_string_filter_set_match_mode   (GtkStringFilter          *self,
+                                                             GtkStringFilterMatchMode  mode);
 
 
 
diff --git a/testsuite/gtk/expression.c b/testsuite/gtk/expression.c
index 697ee9b947..822ce64670 100644
--- a/testsuite/gtk/expression.c
+++ b/testsuite/gtk/expression.c
@@ -65,11 +65,11 @@ static char *
 print_filter_info (GtkStringFilter *filter,
                    const char      *search,
                    gboolean         ignore_case,
-                   gboolean         match_substring)
+                   GtkStringFilterMatchMode match_mode)
 {
   g_assert_cmpstr (search, ==, gtk_string_filter_get_search (filter));
   g_assert_cmpint (ignore_case, ==, gtk_string_filter_get_ignore_case (filter));
-  g_assert_cmpint (match_substring, ==, gtk_string_filter_get_match_substring (filter));
+  g_assert_cmpint (match_mode, ==, gtk_string_filter_get_match_mode (filter));
 
   return g_strdup ("OK");
 }
@@ -86,7 +86,7 @@ test_closure (void)
   filter = GTK_STRING_FILTER (gtk_string_filter_new ());
   pexpr[0] = gtk_property_expression_new (GTK_TYPE_STRING_FILTER, NULL, "search");
   pexpr[1] = gtk_property_expression_new (GTK_TYPE_STRING_FILTER, NULL, "ignore-case");
-  pexpr[2] = gtk_property_expression_new (GTK_TYPE_STRING_FILTER, NULL, "match-substring");
+  pexpr[2] = gtk_property_expression_new (GTK_TYPE_STRING_FILTER, NULL, "match-mode");
   expr = gtk_cclosure_expression_new (G_TYPE_STRING,
                                       NULL,
                                       3,
@@ -114,7 +114,7 @@ test_closure (void)
 
   gtk_string_filter_set_search (filter, "Hello");
   gtk_string_filter_set_ignore_case (filter, TRUE);
-  gtk_string_filter_set_match_substring (filter, FALSE);
+  gtk_string_filter_set_match_mode (filter, GTK_STRING_FILTER_MATCH_EXACT);
   g_assert_cmpint (counter, ==, 5);
   g_assert (gtk_expression_evaluate (expr, filter , &value));
   g_assert_cmpstr (g_value_get_string (&value), ==, "OK");
diff --git a/testsuite/gtk/filter.c b/testsuite/gtk/filter.c
index b7aa741828..dbe8cc4602 100644
--- a/testsuite/gtk/filter.c
+++ b/testsuite/gtk/filter.c
@@ -302,13 +302,13 @@ test_string_properties (void)
   gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "Thirteen");
   assert_model (model, "13");
 
-  gtk_string_filter_set_match_substring (GTK_STRING_FILTER (filter), FALSE);
+  gtk_string_filter_set_match_mode (GTK_STRING_FILTER (filter), GTK_STRING_FILTER_MATCH_EXACT);
   assert_model (model, "13");
 
   gtk_string_filter_set_ignore_case (GTK_STRING_FILTER (filter), TRUE);
   assert_model (model, "13");
 
-  gtk_string_filter_set_match_substring (GTK_STRING_FILTER (filter), TRUE);
+  gtk_string_filter_set_match_mode (GTK_STRING_FILTER (filter), GTK_STRING_FILTER_MATCH_SUBSTRING);
   assert_model (model, "13 113 213 313 413 513 613 713 813 913");
 
   g_object_unref (model);


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