[devhelp] KeywordModel: delegate case_sensitive to DhSearchContext



commit 800a20119d62717925db6a414e74ddf3eb0d4a63
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu Jan 18 13:02:43 2018 +0100

    KeywordModel: delegate case_sensitive to DhSearchContext
    
    The implementation is different, now the book_id and page_id are not
    taken into account, only remaining keywords, which is more logical.

 src/dh-keyword-model.c  |   18 +-----------------
 src/dh-search-context.c |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 src/dh-search-context.h |    3 +++
 3 files changed, 51 insertions(+), 17 deletions(-)
---
diff --git a/src/dh-keyword-model.c b/src/dh-keyword-model.c
index 18fac66..9b5bed9 100644
--- a/src/dh-keyword-model.c
+++ b/src/dh-keyword-model.c
@@ -630,7 +630,6 @@ static GQueue *
 keyword_model_search (DhKeywordModel   *model,
                       const gchar      *search_string,
                       DhSearchContext  *search_context,
-                      gboolean          case_sensitive,
                       DhLink          **exact_link)
 {
         DhKeywordModelPrivate *priv = dh_keyword_model_get_instance_private (model);
@@ -648,7 +647,7 @@ keyword_model_search (DhKeywordModel   *model,
         settings.book_id = priv->current_book_id;
         settings.skip_book_id = NULL;
         settings.page_id = _dh_search_context_get_page_id (search_context);
-        settings.case_sensitive = case_sensitive;
+        settings.case_sensitive = _dh_search_context_get_case_sensitive (search_context);
         settings.prefix = TRUE;
 
         if (settings.page_id != NULL) {
@@ -850,21 +849,7 @@ dh_keyword_model_filter (DhKeywordModel *model,
         search_context = _dh_search_context_new (search_string);
 
         if (search_context != NULL) {
-                gboolean case_sensitive;
                 const gchar *book_id_in_search_string;
-                gint i;
-
-                /* Searches are case sensitive when any uppercase
-                 * letter is used in the search terms, matching vim
-                 * smartcase behaviour.
-                 */
-                case_sensitive = FALSE;
-                for (i = 0; search_string[i] != '\0'; i++) {
-                        if (g_ascii_isupper (search_string[i])) {
-                                case_sensitive = TRUE;
-                                break;
-                        }
-                }
 
                 book_id_in_search_string = _dh_search_context_get_book_id (search_context);
 
@@ -878,7 +863,6 @@ dh_keyword_model_filter (DhKeywordModel *model,
                 new_list = keyword_model_search (model,
                                                  search_string,
                                                  search_context,
-                                                 case_sensitive,
                                                  &exact_link);
         } else {
                 new_list = g_queue_new ();
diff --git a/src/dh-search-context.c b/src/dh-search-context.c
index 03d5b36..671a727 100644
--- a/src/dh-search-context.c
+++ b/src/dh-search-context.c
@@ -23,6 +23,7 @@ struct _DhSearchContext {
         gchar *book_id;
         gchar *page_id;
         GStrv keywords;
+        guint case_sensitive : 1;
 };
 
 /* Process the input search string and extract:
@@ -139,6 +140,42 @@ out:
         return ret;
 }
 
+static gboolean
+contains_uppercase_letter (const gchar *str)
+{
+        const gchar *p;
+
+        for (p = str; *p != '\0'; p++) {
+                if (g_ascii_isupper (*p))
+                        return TRUE;
+        }
+
+        return FALSE;
+}
+
+static void
+set_case_sensitive (DhSearchContext *search)
+{
+        gint i;
+
+        search->case_sensitive = FALSE;
+
+        if (search->keywords == NULL)
+                return;
+
+        /* Searches are case sensitive when any uppercase letter is used in the
+         * search terms, matching Vim smartcase behaviour.
+         */
+        for (i = 0; search->keywords[i] != NULL; i++) {
+                const gchar *cur_keyword = search->keywords[i];
+
+                if (contains_uppercase_letter (cur_keyword)) {
+                        search->case_sensitive = TRUE;
+                        break;
+                }
+        }
+}
+
 /* Returns: (transfer full) (nullable): a new #DhSearchContext, or %NULL if
  * @search_string is invalid.
  */
@@ -156,6 +193,8 @@ _dh_search_context_new (const gchar *search_string)
                 return NULL;
         }
 
+        set_case_sensitive (search);
+
         return search;
 }
 
@@ -194,3 +233,11 @@ _dh_search_context_get_keywords (DhSearchContext *search)
 
         return search->keywords;
 }
+
+gboolean
+_dh_search_context_get_case_sensitive (DhSearchContext *search)
+{
+        g_return_val_if_fail (search != NULL, FALSE);
+
+        return search->case_sensitive;
+}
diff --git a/src/dh-search-context.h b/src/dh-search-context.h
index 14fc34f..2f748a1 100644
--- a/src/dh-search-context.h
+++ b/src/dh-search-context.h
@@ -40,6 +40,9 @@ const gchar *           _dh_search_context_get_page_id          (DhSearchContext
 G_GNUC_INTERNAL
 GStrv                   _dh_search_context_get_keywords         (DhSearchContext *search);
 
+G_GNUC_INTERNAL
+gboolean                _dh_search_context_get_case_sensitive   (DhSearchContext *search);
+
 G_END_DECLS
 
 #endif /* DH_SEARCH_CONTEXT_H */


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