[gnome-control-center] shell: Use g_autofree for strings



commit c6965d9e309169fab179e38a92623240b7e3ffaf
Author: Robert Ancell <robert ancell canonical com>
Date:   Fri Sep 27 11:50:17 2019 +1200

    shell: Use g_autofree for strings

 shell/cc-log.c         |   4 +-
 shell/cc-panel-list.c  |  28 +++++---------
 shell/cc-shell-model.c | 100 ++++++++++++++-----------------------------------
 3 files changed, 39 insertions(+), 93 deletions(-)
---
diff --git a/shell/cc-log.c b/shell/cc-log.c
index 95633bb97..dba4f5a26 100644
--- a/shell/cc-log.c
+++ b/shell/cc-log.c
@@ -57,7 +57,7 @@ log_handler (const gchar    *domain,
   g_autoptr(GDateTime) now = NULL;
   const gchar *level;
   g_autofree gchar *ftime = NULL;
-  gchar *buffer;
+  g_autofree gchar *buffer = NULL;
 
   /* Skip ignored log domains */
   if (domain && g_strv_contains (ignored_domains, domain))
@@ -80,8 +80,6 @@ log_handler (const gchar    *domain,
   g_io_channel_flush (standard_channel, NULL);
 
   G_UNLOCK (channel_lock);
-
-  g_free (buffer);
 }
 
 void
diff --git a/shell/cc-panel-list.c b/shell/cc-panel-list.c
index e5134ac6b..e2850ae0d 100644
--- a/shell/cc-panel-list.c
+++ b/shell/cc-panel-list.c
@@ -335,7 +335,9 @@ filter_func (GtkListBoxRow *row,
 {
   CcPanelList *self;
   RowData *data;
-  gchar *search_text, *panel_text, *panel_description;
+  g_autofree gchar *search_text = NULL;
+  g_autofree gchar *panel_text = NULL;
+  g_autofree gchar *panel_description = NULL;
   gboolean retval = FALSE;
   gint i;
 
@@ -365,10 +367,6 @@ filter_func (GtkListBoxRow *row,
   retval = retval || g_strstr_len (panel_text, -1, search_text) != NULL ||
            g_strstr_len (panel_description, -1, search_text) != NULL;
 
-  g_free (panel_text);
-  g_free (search_text);
-  g_free (panel_description);
-
   return retval;
 }
 
@@ -462,9 +460,11 @@ search_sort_function (GtkListBoxRow *a,
 {
   CcPanelList *self;
   RowData *a_data, *b_data;
-  gchar *a_name, *b_name, *search, *a_strstr, *b_strstr;
+  g_autofree gchar *a_name = NULL;
+  g_autofree gchar *b_name = NULL;
+  g_autofree gchar *search = NULL;
+  gchar *a_strstr, *b_strstr;
   gint a_distance, b_distance;
-  gint retval;
 
   self = CC_PANEL_LIST (user_data);
   search = NULL;
@@ -486,10 +486,7 @@ search_sort_function (GtkListBoxRow *a,
 
   /* Default result for empty search */
   if (!search || g_utf8_strlen (search, -1) == 0)
-    {
-      retval = g_strcmp0 (a_name, b_name);
-      goto out;
-    }
+    return g_strcmp0 (a_name, b_name);
 
   a_strstr = g_strstr_len (a_name, -1, search);
   b_strstr = g_strstr_len (b_name, -1, search);
@@ -500,14 +497,7 @@ search_sort_function (GtkListBoxRow *a,
   if (b_strstr)
     b_distance = g_strstr_len (b_name, -1, search) - b_name;
 
-  retval = a_distance - b_distance;
-
-out:
-  g_free (a_name);
-  g_free (b_name);
-  g_free (search);
-
-  return retval;
+  return a_distance - b_distance;
 }
 
 static void
diff --git a/shell/cc-shell-model.c b/shell/cc-shell-model.c
index a84de3ee2..ff3aef312 100644
--- a/shell/cc-shell-model.c
+++ b/shell/cc-shell-model.c
@@ -44,19 +44,13 @@ sort_by_name (GtkTreeModel *model,
               GtkTreeIter  *a,
               GtkTreeIter  *b)
 {
-  gchar *a_name = NULL;
-  gchar *b_name = NULL;
-  gint rval = 0;
+  g_autofree gchar *a_name = NULL;
+  g_autofree gchar *b_name = NULL;
 
   gtk_tree_model_get (model, a, COL_CASEFOLDED_NAME, &a_name, -1);
   gtk_tree_model_get (model, b, COL_CASEFOLDED_NAME, &b_name, -1);
 
-  rval = g_strcmp0 (a_name, b_name);
-
-  g_free (a_name);
-  g_free (b_name);
-
-  return rval;
+  return g_strcmp0 (a_name, b_name);
 }
 
 static gint
@@ -66,9 +60,8 @@ sort_by_name_with_terms (GtkTreeModel  *model,
                          gchar        **terms)
 {
   gboolean a_match, b_match;
-  gchar *a_name = NULL;
-  gchar *b_name = NULL;
-  gint rval = 0;
+  g_autofree gchar *a_name = NULL;
+  g_autofree gchar *b_name = NULL;
   gint i;
 
   gtk_tree_model_get (model, a, COL_CASEFOLDED_NAME, &a_name, -1);
@@ -80,21 +73,12 @@ sort_by_name_with_terms (GtkTreeModel  *model,
       b_match = strstr (b_name, terms[i]) != NULL;
 
       if (a_match && !b_match)
-        {
-          rval = -1;
-          break;
-        }
+        return -1;
       else if (!a_match && b_match)
-        {
-          rval = 1;
-          break;
-        }
+        return 1;
     }
 
-  g_free (a_name);
-  g_free (b_name);
-
-  return rval;
+  return 0;
 }
 
 static gint
@@ -123,9 +107,8 @@ sort_by_keywords_with_terms (GtkTreeModel  *model,
                              gchar        **terms)
 {
   gint a_matches, b_matches;
-  gchar **a_keywords = NULL;
-  gchar **b_keywords = NULL;
-  gint rval = 0;
+  g_auto(GStrv) a_keywords = NULL;
+  g_auto(GStrv) b_keywords = NULL;
 
   gtk_tree_model_get (model, a, COL_KEYWORDS, &a_keywords, -1);
   gtk_tree_model_get (model, b, COL_KEYWORDS, &b_keywords, -1);
@@ -134,14 +117,11 @@ sort_by_keywords_with_terms (GtkTreeModel  *model,
   b_matches = count_matches (b_keywords, terms);
 
   if (a_matches > b_matches)
-    rval = -1;
+    return -1;
   else if (a_matches < b_matches)
-    rval = 1;
-
-  g_strfreev (a_keywords);
-  g_strfreev (b_keywords);
+    return 1;
 
-  return rval;
+  return 0;
 }
 
 static gint
@@ -151,30 +131,20 @@ sort_by_description_with_terms (GtkTreeModel  *model,
                                 gchar        **terms)
 {
   gint a_matches, b_matches;
-  gchar *a_description = NULL;
-  gchar *b_description = NULL;
-  gchar **a_description_split = NULL;
-  gchar **b_description_split = NULL;
-  gint rval = 0;
+  g_autofree gchar *a_description = NULL;
+  g_autofree gchar *b_description = NULL;
+  g_auto(GStrv) a_description_split = NULL;
+  g_auto(GStrv) b_description_split = NULL;
 
   gtk_tree_model_get (model, a, COL_DESCRIPTION, &a_description, -1);
   gtk_tree_model_get (model, b, COL_DESCRIPTION, &b_description, -1);
 
   if (a_description && !b_description)
-    {
-      rval = -1;
-      goto out;
-    }
+    return -1;
   else if (!a_description && b_description)
-    {
-      rval = 1;
-      goto out;
-    }
+    return 1;
   else if (!a_description && !b_description)
-    {
-      rval = 0;
-      goto out;
-    }
+    return 0;
 
   a_description_split = g_strsplit (a_description, " ", -1);
   b_description_split = g_strsplit (b_description, " ", -1);
@@ -183,17 +153,11 @@ sort_by_description_with_terms (GtkTreeModel  *model,
   b_matches = count_matches (b_description_split, terms);
 
   if (a_matches > b_matches)
-    rval = -1;
+    return -1;
   else if (a_matches < b_matches)
-    rval = 1;
+    return 1;
 
- out:
-  g_free (a_description);
-  g_free (b_description);
-  g_strfreev (a_description_split);
-  g_strfreev (b_description_split);
-
-  return rval;
+  return 0;
 }
 
 static gint
@@ -319,8 +283,9 @@ cc_shell_model_add_item (CcShellModel    *model,
   g_autoptr(GIcon) icon = NULL;
   const gchar *name = g_app_info_get_name (appinfo);
   const gchar *comment = g_app_info_get_description (appinfo);
-  char **keywords;
-  char *casefolded_name, *casefolded_description;
+  g_auto(GStrv) keywords = NULL;
+  g_autofree gchar *casefolded_name = NULL;
+  g_autofree gchar *casefolded_description = NULL;
 
   casefolded_name = cc_util_normalize_casefold_and_unaccent (name);
   casefolded_description = cc_util_normalize_casefold_and_unaccent (comment);
@@ -339,10 +304,6 @@ cc_shell_model_add_item (CcShellModel    *model,
                                      COL_KEYWORDS, keywords,
                                      COL_VISIBILITY, CC_PANEL_VISIBLE,
                                      -1);
-
-  g_free (casefolded_name);
-  g_free (casefolded_description);
-  g_strfreev (keywords);
 }
 
 gboolean
@@ -374,9 +335,10 @@ cc_shell_model_iter_matches_search (CcShellModel *model,
                                     GtkTreeIter  *iter,
                                     const char   *term)
 {
-  gchar *name, *description;
+  g_autofree gchar *name = NULL;
+  g_autofree gchar *description = NULL;
   gboolean result;
-  gchar **keywords;
+  g_auto(GStrv) keywords = NULL;
 
   gtk_tree_model_get (GTK_TREE_MODEL (model), iter,
                       COL_CASEFOLDED_NAME, &name,
@@ -397,10 +359,6 @@ cc_shell_model_iter_matches_search (CcShellModel *model,
         result = (strstr (keywords[i], term) == keywords[i]);
     }
 
-  g_free (name);
-  g_free (description);
-  g_strfreev (keywords);
-
   return result;
 }
 


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