[devhelp] search: Strip leading, trailing and multiple whitespaces in the search string



commit 9b77d5af2d7c5d33ff91ddecd8fb595789f73aa6
Author: Aleksander Morgado <aleksander lanedo com>
Date:   Wed Dec 15 00:29:28 2010 +0100

    search: Strip leading, trailing and multiple whitespaces in the search string
    
     So that stringv array is only filled in with valid keywords, and not with empty
     strings

 src/dh-keyword-model.c |   19 ++++++++++++++++---
 1 files changed, 16 insertions(+), 3 deletions(-)
---
diff --git a/src/dh-keyword-model.c b/src/dh-keyword-model.c
index 6f249fd..71f066f 100644
--- a/src/dh-keyword-model.c
+++ b/src/dh-keyword-model.c
@@ -454,6 +454,8 @@ dh_keyword_model_filter (DhKeywordModel *model,
         gint                 i;
         GtkTreePath         *path;
         GtkTreeIter          iter;
+        gchar               *processed_string;
+        gchar               *aux_str;
 
         g_return_val_if_fail (DH_IS_KEYWORD_MODEL (model), NULL);
         g_return_val_if_fail (string != NULL, NULL);
@@ -467,11 +469,20 @@ dh_keyword_model_filter (DhKeywordModel *model,
         new_list = NULL;
         hits = 0;
 
-        if (string[0] != '\0') {
+        /* Remove all leading and trailing whitespaces in the search string */
+        processed_string = g_strdup (string);
+        g_strstrip (processed_string);
+        /* Avoid words being separated by more than one whitespace */
+        aux_str = processed_string;
+        while ((aux_str = strchr (aux_str, ' ')) != NULL) {
+                g_strchug (++aux_str);
+        }
+
+        if (processed_string[0] != '\0') {
                 gchar    **stringv;
                 gboolean   case_sensitive;
 
-                stringv = g_strsplit (string, " ", -1);
+                stringv = g_strsplit (processed_string, " ", -1);
 
                 case_sensitive = FALSE;
 
@@ -495,7 +506,7 @@ dh_keyword_model_filter (DhKeywordModel *model,
                 }
 
                 new_list = keyword_model_search (model,
-                                                 string,
+                                                 processed_string,
                                                  stringv,
                                                  book_id,
                                                  case_sensitive,
@@ -539,6 +550,8 @@ dh_keyword_model_filter (DhKeywordModel *model,
                 }
         }
 
+        g_free (processed_string);
+
         if (hits == 1) {
                 return priv->keyword_words->data;
         }



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