gnucash maint: Multiple changes pushed

Christopher Lam clam at code.gnucash.org
Sat Oct 30 10:40:34 EDT 2021


Updated	 via  https://github.com/Gnucash/gnucash/commit/b5f5129f (commit)
	 via  https://github.com/Gnucash/gnucash/commit/e94ee3bf (commit)
	from  https://github.com/Gnucash/gnucash/commit/f813f7cd (commit)



commit b5f5129f7d658c296a323b6e5deb3c31c4a3b3ca
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Tue Oct 19 19:21:59 2021 +0800

    Shortcut g_list_length comparison against small numbers
    
    It's more efficient to test node && node->next etc when testing GList
    length against small numbers

diff --git a/gnucash/gnome-utils/gnc-main-window.c b/gnucash/gnome-utils/gnc-main-window.c
index 1567e3a39..385a216d2 100644
--- a/gnucash/gnome-utils/gnc-main-window.c
+++ b/gnucash/gnome-utils/gnc-main-window.c
@@ -1446,7 +1446,7 @@ gnc_main_window_delete_event (GtkWidget *window,
     if (already_dead)
         return TRUE;
 
-    if (g_list_length (active_windows) > 1)
+    if (active_windows && active_windows->next)
     {
         gint response;
         GtkWidget *dialog;
@@ -1478,7 +1478,7 @@ gnc_main_window_delete_event (GtkWidget *window,
         return TRUE;
     }
 
-    if (g_list_length(active_windows) > 1)
+    if (active_windows && active_windows->next)
         return FALSE;
 
     already_dead = gnc_main_window_quit(GNC_MAIN_WINDOW(window));
@@ -3348,7 +3348,7 @@ gnc_main_window_close_page (GncPluginPage *page)
             /* remove the preference callbacks from the main window */
             gnc_main_window_remove_prefs (window);
         }
-        if (window && g_list_length (active_windows) > 1)
+        if (window && active_windows && active_windows->next)
             gtk_widget_destroy (GTK_WIDGET(window));
     }
 }
diff --git a/gnucash/gnome/dialog-imap-editor.c b/gnucash/gnome/dialog-imap-editor.c
index cd4a70bef..966d60391 100644
--- a/gnucash/gnome/dialog-imap-editor.c
+++ b/gnucash/gnome/dialog-imap-editor.c
@@ -251,7 +251,7 @@ gnc_imap_dialog_delete (ImapDialog *imap_dialog)
     list = gtk_tree_selection_get_selected_rows (selection, &fmodel);
 
     // Make sure we have some rows selected
-    if (g_list_length (list) == 0)
+    if (list == NULL)
         return;
 
     // reset the invalid map total
@@ -645,7 +645,7 @@ get_imap_info (ImapDialog *imap_dialog, Account *acc, const gchar *category, con
     else
         head = IMAP_FRAME;
 
-    if (g_list_length (imap_list) > 0)
+    if (imap_list != NULL)
     {
         PINFO("List length is %d", g_list_length (imap_list));
 
diff --git a/gnucash/gnome/dialog-invoice.c b/gnucash/gnome/dialog-invoice.c
index c25735f01..4faa55df1 100644
--- a/gnucash/gnome/dialog-invoice.c
+++ b/gnucash/gnome/dialog-invoice.c
@@ -3237,7 +3237,7 @@ multi_post_invoice_cb (GtkWindow *dialog, GList *invoice_list, gpointer user_dat
     gboolean test;
     InvoiceWindow *iw;
 
-    if (g_list_length(invoice_list) == 0)
+    if (invoice_list == NULL)
         return;
     // Get the posting parameters for these invoices
     iw = gnc_ui_invoice_edit(dialog, invoice_list->data);
@@ -3287,7 +3287,7 @@ multi_print_invoice_cb (GtkWindow *dialog, GList *invoice_list, gpointer user_da
 {
     struct multi_edit_invoice_data meid;
 
-    if (g_list_length(invoice_list) == 0)
+    if (invoice_list == NULL)
         return;
 
     meid.user_data = user_data;
diff --git a/gnucash/gnome/dialog-payment.c b/gnucash/gnome/dialog-payment.c
index 6a8cd48ca..20bb58c50 100644
--- a/gnucash/gnome/dialog-payment.c
+++ b/gnucash/gnome/dialog-payment.c
@@ -1735,7 +1735,7 @@ static GList *select_txn_lots (GtkWindow *parent, Transaction *txn, Account **po
     /* If the txn has both APAR splits linked to a business lot and
      * splits that are not, issue a warning some will be discarded.
      */
-    if (has_no_lot_apar_splits && (g_list_length (txn_lots) > 0))
+    if (has_no_lot_apar_splits && (txn_lots != NULL))
     {
         GtkWidget *dialog;
         char *split_str = g_strdup ("");
diff --git a/gnucash/gnome/dialog-price-edit-db.c b/gnucash/gnome/dialog-price-edit-db.c
index 70ef0078f..62511ee8e 100644
--- a/gnucash/gnome/dialog-price-edit-db.c
+++ b/gnucash/gnome/dialog-price-edit-db.c
@@ -366,7 +366,7 @@ selection_changed_cb (GtkTreeSelection *selection, gpointer data)
     PricesDialog *pdb_dialog = data;
     GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(pdb_dialog->remove_view));
     GList *rows = gtk_tree_selection_get_selected_rows (selection, &model);
-    gboolean have_rows = (g_list_length (rows) > 0 ? TRUE : FALSE);
+    gboolean have_rows = (rows != NULL);
 
     change_source_flag (PRICE_REMOVE_SOURCE_COMM, have_rows, pdb_dialog);
     g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
@@ -547,7 +547,7 @@ gnc_prices_dialog_add_clicked (GtkWidget *widget, gpointer data)
     }
     else if (comm_list) // selection contains price parent rows
     {
-        if (g_list_length (comm_list) == 1) // make sure it is only one parent
+        if (!comm_list->next) // make sure it is only one parent
         {
             price = gnc_price_create (pdb_dialog->book);
             gnc_price_set_commodity (price, comm_list->data);
diff --git a/gnucash/gnome/dialog-sx-editor.c b/gnucash/gnome/dialog-sx-editor.c
index b855046a4..d3d5308cb 100644
--- a/gnucash/gnome/dialog-sx-editor.c
+++ b/gnucash/gnome/dialog-sx-editor.c
@@ -579,7 +579,7 @@ gnc_sxed_check_endpoint (GncSxEditorDialog *sxed)
 
     g_date_clear (&nextDate, 1);
     gnc_frequency_save_to_recurrence (sxed->gncfreq, &schedule, &startDate);
-    if (g_list_length (schedule) > 0)
+    if (schedule != NULL)
     {
         g_date_subtract_days (&startDate, 1);
         recurrenceListNextInstance (schedule, &startDate, &nextDate);
@@ -1766,7 +1766,7 @@ _sx_engine_event_handler (QofInstance *ent, QofEventId event_type, gpointer user
     book = qof_instance_get_book (QOF_INSTANCE (acct));
     affected_sxes = gnc_sx_get_sxes_referencing_account (book, acct);
 
-    if (g_list_length (affected_sxes) == 0)
+    if (affected_sxes == NULL)
         return;
 
     {
diff --git a/gnucash/gnome/dialog-sx-editor2.c b/gnucash/gnome/dialog-sx-editor2.c
index 5a271e4a8..6c95643ad 100644
--- a/gnucash/gnome/dialog-sx-editor2.c
+++ b/gnucash/gnome/dialog-sx-editor2.c
@@ -836,7 +836,7 @@ gnc_sxed_check_consistent (GncSxEditorDialog2 *sxed)
 
         g_date_clear (&nextDate, 1);
         gnc_frequency_save_to_recurrence (sxed->gncfreq, &schedule, &startDate);
-        if (g_list_length (schedule) > 0)
+        if (schedule != NULL)
         {
             g_date_subtract_days (&startDate, 1);
             recurrenceListNextInstance (schedule, &startDate, &nextDate);
@@ -1704,7 +1704,7 @@ _sx_engine_event_handler (QofInstance *ent, QofEventId event_type, gpointer user
     book = qof_instance_get_book (QOF_INSTANCE (acct));
     affected_sxes = gnc_sx_get_sxes_referencing_account (book, acct);
 
-    if (g_list_length (affected_sxes) == 0)
+    if (affected_sxes == NULL)
         return;
 
     {
diff --git a/gnucash/gnome/dialog-sx-since-last-run.c b/gnucash/gnome/dialog-sx-since-last-run.c
index 49992a530..5aba06020 100644
--- a/gnucash/gnome/dialog-sx-since-last-run.c
+++ b/gnucash/gnome/dialog-sx-since-last-run.c
@@ -1154,9 +1154,11 @@ dialog_response_cb (GtkDialog *dialog, gint response_id, GncSxSinceLastRunDialog
         // - [?] ability to create transactions
     {
         GList *unbound_variables;
+        gint unbound_len;
         unbound_variables = gnc_sx_instance_model_check_variables (app_dialog->editing_model->instances);
-        PINFO ("%d variables unbound", g_list_length (unbound_variables));
-        if (g_list_length (unbound_variables) > 0)
+        unbound_len = g_list_length (unbound_variables);
+        PINFO ("%d variables unbound", unbound_len);
+        if (unbound_len > 0)
         {
             // focus first variable
             GncSxVariableNeeded *first_unbound;
diff --git a/gnucash/gnome/gnc-plugin-page-account-tree.c b/gnucash/gnome/gnc-plugin-page-account-tree.c
index 3c323480a..1a6b7ff76 100644
--- a/gnucash/gnome/gnc-plugin-page-account-tree.c
+++ b/gnucash/gnome/gnc-plugin-page-account-tree.c
@@ -593,7 +593,7 @@ gnc_plugin_page_account_tree_open (Account *account, GtkWindow *win)
     page_list = gnc_gobject_tracking_get_list(GNC_PLUGIN_PAGE_ACCOUNT_TREE_NAME);
 
     // If we have a window, look for account page in that window
-    if (g_list_length ((GList*)page_list) != 0)
+    if (page_list != NULL)
     {
         if (win != NULL)
         {
@@ -1500,7 +1500,7 @@ account_subaccount (Account* account)
 {
     Account* subaccount = NULL;
     GList *subs = gnc_account_get_children (account);
-    if (g_list_length (subs) == 1)
+    if (subs && !subs->next)
         subaccount = subs->data;
     g_list_free (subs);
     return subaccount;
diff --git a/gnucash/gnome/gnc-plugin-page-sx-list.c b/gnucash/gnome/gnc-plugin-page-sx-list.c
index e9ce4b685..ed8507b5c 100644
--- a/gnucash/gnome/gnc-plugin-page-sx-list.c
+++ b/gnucash/gnome/gnc-plugin-page-sx-list.c
@@ -756,7 +756,7 @@ gnc_plugin_page_sx_list_cmd_edit (GtkAction *action, GncPluginPageSxList *page)
 
     selection = gtk_tree_view_get_selection (priv->tree_view);
     selected_paths = gtk_tree_selection_get_selected_rows (selection, &model);
-    if (g_list_length (selected_paths) == 0)
+    if (selected_paths == NULL)
     {
         g_warning ("no selection edit.");
         return;
@@ -792,7 +792,7 @@ gnc_plugin_page_sx_list_cmd_edit2 (GtkAction *action, GncPluginPageSxList *page)
 
     selection = gtk_tree_view_get_selection (priv->tree_view);
     selected_paths = gtk_tree_selection_get_selected_rows (selection, &model);
-    if (g_list_length (selected_paths) == 0)
+    if (selected_paths == NULL)
     {
         g_warning ("no selection edit.");
         return;
@@ -853,7 +853,7 @@ gnc_plugin_page_sx_list_cmd_delete (GtkAction *action, GncPluginPageSxList *page
 
     selection = gtk_tree_view_get_selection (priv->tree_view);
     selected_paths = gtk_tree_selection_get_selected_rows (selection, &model);
-    if (g_list_length (selected_paths) == 0)
+    if (selected_paths == NULL)
     {
         g_warning ("no selection for delete.");
         return;
diff --git a/gnucash/register/ledger-core/gnc-ledger-display.c b/gnucash/register/ledger-core/gnc-ledger-display.c
index f292c29e1..3326ddeae 100644
--- a/gnucash/register/ledger-core/gnc-ledger-display.c
+++ b/gnucash/register/ledger-core/gnc-ledger-display.c
@@ -429,7 +429,7 @@ gnc_ledger_display_gl (void)
         tRoot = gnc_book_get_template_root (gnc_get_current_book());
         al = gnc_account_get_descendants (tRoot);
 
-        if (g_list_length (al) != 0)
+        if (al != NULL)
             xaccQueryAddAccountMatch (query, al, QOF_GUID_MATCH_NONE, QOF_QUERY_AND);
 
         g_list_free (al);
diff --git a/libgnucash/engine/Recurrence.c b/libgnucash/engine/Recurrence.c
index 1f8647fc3..9e2e33e8f 100644
--- a/libgnucash/engine/Recurrence.c
+++ b/libgnucash/engine/Recurrence.c
@@ -556,7 +556,7 @@ recurrenceWeekendAdjustFromString(const gchar *str)
 gboolean
 recurrenceListIsSemiMonthly(GList *recurrences)
 {
-    if (g_list_length(recurrences) != 2)
+    if (!(recurrences && recurrences->next && !recurrences->next->next))
         return FALSE;
 
     // should be a "semi-monthly":
@@ -879,9 +879,10 @@ recurrenceListCmp(GList *a, GList *b)
 {
     Recurrence *most_freq_a, *most_freq_b;
 
-    g_return_val_if_fail(g_list_length(a) != 0 && g_list_length(b) != 0, 0);
-    g_return_val_if_fail(g_list_length(a) != 0, -1);
-    g_return_val_if_fail(g_list_length(b) != 0, 1);
+    if (!a)
+        return (b ? -1 : 0);
+    else if (!b)
+        return 1;
 
     most_freq_a = (Recurrence*)g_list_nth_data(g_list_sort(a, (GCompareFunc)recurrenceCmp), 0);
     most_freq_b = (Recurrence*)g_list_nth_data(g_list_sort(b, (GCompareFunc)recurrenceCmp), 0);
diff --git a/libgnucash/engine/gncIDSearch.c b/libgnucash/engine/gncIDSearch.c
index 058474ec8..449732c87 100644
--- a/libgnucash/engine/gncIDSearch.c
+++ b/libgnucash/engine/gncIDSearch.c
@@ -89,7 +89,6 @@ static void * search(QofBook * book, const gchar *id, void * object, GncSearchTy
     void *c;
     GList *result;
     QofQuery *q;
-    gint len;
     QofQueryPredData* string_pred_data;
     
     PINFO("Type = %d", type);
@@ -123,8 +122,7 @@ static void * search(QofBook * book, const gchar *id, void * object, GncSearchTy
     result = qof_query_run (q);
 
     // now compare _exactly_
-    len = g_list_length (result);
-    if (result && (len > 0))
+    if (result != NULL)
     {
         result = g_list_first (result);
 
diff --git a/libgnucash/engine/qofquery.cpp b/libgnucash/engine/qofquery.cpp
index 22793bf43..f531857f0 100644
--- a/libgnucash/engine/qofquery.cpp
+++ b/libgnucash/engine/qofquery.cpp
@@ -676,7 +676,7 @@ void qof_query_add_term (QofQuery *q, QofQueryParamList *param_list,
     qs = qof_query_create ();
     query_init (qs, qt);
 
-    if (qof_query_has_terms (q))
+    if (q->terms != NULL)
         qr = qof_query_merge (q, qs, op);
     else
         qr = qof_query_merge (q, qs, QOF_QUERY_OR);
@@ -701,7 +701,8 @@ void qof_query_purge_terms (QofQuery *q, QofQueryParamList *param_list)
             qt = static_cast<QofQueryTerm*>(_and_->data);
             if (!param_list_cmp (qt->param_list, param_list))
             {
-                if (g_list_length (static_cast<GList*>(_or_->data)) == 1)
+                auto or_list = static_cast<GList*> (_or_->data);
+                if (or_list && !or_list->next)
                 {
                     q->terms = g_list_remove_link (static_cast<GList*>(q->terms), _or_);
                     g_list_free_1 (_or_);
@@ -710,7 +711,7 @@ void qof_query_purge_terms (QofQuery *q, QofQueryParamList *param_list)
                 }
                 else
                 {
-                    _or_->data = g_list_remove_link (static_cast<GList*>(_or_->data), _and_);
+                    _or_->data = g_list_remove_link (or_list, _and_);
                     g_list_free_1 (_and_);
                     _and_ = static_cast<GList*>(_or_->data);
                     if (!_and_) break;
@@ -1137,7 +1138,7 @@ qof_query_merge(QofQuery *q1, QofQuery *q2, QofQueryOp op)
      * so that the first term added to an empty query doesn't screw up.
      */
     if ((QOF_QUERY_AND == op) &&
-            ( (0 == qof_query_has_terms (q1)) || (0 == qof_query_has_terms (q2)) ))
+        (q1->terms == NULL || q2->terms == NULL))
     {
         op = QOF_QUERY_OR;
     }

commit e94ee3bfada59b7181eb0de3445ee1ea7004c47f
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Thu Oct 14 22:09:18 2021 +0800

    Don't g_strdup char* from scm_to_locale|utf8_string
    
    Both scm_to_locale_string and scm_to_utf8_string return newly
    allocated char*. It is not necessary to return a strdup'd char* and
    free the original.

diff --git a/bindings/guile/glib-guile.c b/bindings/guile/glib-guile.c
index 6a36178fd..992df69a1 100644
--- a/bindings/guile/glib-guile.c
+++ b/bindings/guile/glib-guile.c
@@ -164,8 +164,7 @@ gnc_scm_to_glist_string(SCM list)
 
             str = gnc_scm_to_utf8_string (SCM_CAR(list));
             if (str)
-                glist = g_list_prepend (glist, g_strdup (str));
-            g_free (str);
+                glist = g_list_prepend (glist, str);
         }
         list = SCM_CDR (list);
     }
@@ -186,8 +185,7 @@ gnc_scm_to_gslist_string(SCM list)
 
             str = gnc_scm_to_utf8_string (SCM_CAR(list));
             if (str)
-                gslist = g_slist_prepend (gslist, g_strdup (str));
-            g_free (str);
+                gslist = g_slist_prepend (gslist, str);
         }
         list = SCM_CDR (list);
     }
diff --git a/bindings/guile/gnc-guile-utils.c b/bindings/guile/gnc-guile-utils.c
index 99e46d7d2..de00ed33c 100644
--- a/bindings/guile/gnc-guile-utils.c
+++ b/bindings/guile/gnc-guile-utils.c
@@ -39,15 +39,7 @@
 gchar *gnc_scm_to_utf8_string(SCM scm_string)
 {
     if (scm_is_string (scm_string))
-    {
-        gchar* s;
-        char * str;
-
-        str = scm_to_utf8_stringn(scm_string, NULL);
-        s = g_strdup(str);
-        free (str);
-        return s;
-    }
+        return scm_to_utf8_stringn(scm_string, NULL);
 
     /* Unable to extract string from the symbol...*/
     g_error ("bad value\n");
@@ -71,15 +63,7 @@ gchar *gnc_scm_to_utf8_string(SCM scm_string)
 gchar *gnc_scm_to_locale_string(SCM scm_string)
 {
     if (scm_is_string (scm_string))
-    {
-        gchar* s;
-        char * str;
-
-        str = scm_to_locale_string(scm_string);
-        s = g_strdup(str);
-        free (str);
-        return s;
-    }
+        return scm_to_locale_string(scm_string);
 
     /* Unable to extract string from the symbol...*/
     g_error ("bad value\n");
@@ -104,12 +88,7 @@ gnc_scm_symbol_to_locale_string(SCM symbol_value)
     {
         SCM string_value = scm_symbol_to_string (symbol_value);
         if (scm_is_string (string_value))
-        {
-            char  *tmp = scm_to_utf8_string (string_value);
-            gchar *str = g_strdup (tmp);
-            free (tmp);
-            return str;
-        }
+            return scm_to_utf8_string (string_value);
     }
 
     /* Unable to extract string from the symbol...*/



Summary of changes:
 bindings/guile/glib-guile.c                       |  6 ++---
 bindings/guile/gnc-guile-utils.c                  | 27 +++--------------------
 gnucash/gnome-utils/gnc-main-window.c             |  6 ++---
 gnucash/gnome/dialog-imap-editor.c                |  4 ++--
 gnucash/gnome/dialog-invoice.c                    |  4 ++--
 gnucash/gnome/dialog-payment.c                    |  2 +-
 gnucash/gnome/dialog-price-edit-db.c              |  4 ++--
 gnucash/gnome/dialog-sx-editor.c                  |  4 ++--
 gnucash/gnome/dialog-sx-editor2.c                 |  4 ++--
 gnucash/gnome/dialog-sx-since-last-run.c          |  6 +++--
 gnucash/gnome/gnc-plugin-page-account-tree.c      |  4 ++--
 gnucash/gnome/gnc-plugin-page-sx-list.c           |  6 ++---
 gnucash/register/ledger-core/gnc-ledger-display.c |  2 +-
 libgnucash/engine/Recurrence.c                    |  9 ++++----
 libgnucash/engine/gncIDSearch.c                   |  4 +---
 libgnucash/engine/qofquery.cpp                    |  9 ++++----
 16 files changed, 40 insertions(+), 61 deletions(-)



More information about the gnucash-changes mailing list