gnucash stable: avoid strlen where possible

Christopher Lam clam at code.gnucash.org
Wed Nov 1 10:45:08 EDT 2023


Updated	 via  https://github.com/Gnucash/gnucash/commit/d617129d (commit)
	from  https://github.com/Gnucash/gnucash/commit/f30fcc2a (commit)



commit d617129db83b37cc12b3c2434c55e73efb4d98d6
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Wed Nov 1 20:57:05 2023 +0800

    avoid strlen where possible
    
    if testing emptiness, test *str instead.

diff --git a/libgnucash/backend/xml/gnc-account-xml-v2.cpp b/libgnucash/backend/xml/gnc-account-xml-v2.cpp
index 95f54ab5be..126c591f7c 100644
--- a/libgnucash/backend/xml/gnc-account-xml-v2.cpp
+++ b/libgnucash/backend/xml/gnc-account-xml-v2.cpp
@@ -121,13 +121,13 @@ gnc_account_dom_tree_create (Account* act,
     }
 
     str = xaccAccountGetCode (act);
-    if (str && strlen (str) > 0)
+    if (str && *str)
     {
         xmlAddChild (ret, text_to_dom_tree (act_code_string, str));
     }
 
     str = xaccAccountGetDescription (act);
-    if (str && strlen (str) > 0)
+    if (str && *str)
     {
         xmlAddChild (ret, text_to_dom_tree (act_description_string, str));
     }
diff --git a/libgnucash/backend/xml/gnc-address-xml-v2.cpp b/libgnucash/backend/xml/gnc-address-xml-v2.cpp
index 5e5481593d..bf53fa3c20 100644
--- a/libgnucash/backend/xml/gnc-address-xml-v2.cpp
+++ b/libgnucash/backend/xml/gnc-address-xml-v2.cpp
@@ -60,7 +60,7 @@ const gchar* address_version_string = "2.0.0";
 static void
 maybe_add_string (xmlNodePtr ptr, const char* tag, const char* str)
 {
-    if (str && strlen (str) > 0)
+    if (str && *str)
         xmlAddChild (ptr, text_to_dom_tree (tag, str));
 }
 
diff --git a/libgnucash/backend/xml/gnc-commodity-xml-v2.cpp b/libgnucash/backend/xml/gnc-commodity-xml-v2.cpp
index 25503d9adb..f943965a7f 100644
--- a/libgnucash/backend/xml/gnc-commodity-xml-v2.cpp
+++ b/libgnucash/backend/xml/gnc-commodity-xml-v2.cpp
@@ -85,12 +85,10 @@ gnc_commodity_dom_tree_create (const gnc_commodity* com)
                                                 gnc_commodity_get_fullname (com)));
         }
 
-        if (gnc_commodity_get_cusip (com) &&
-            strlen (gnc_commodity_get_cusip (com)) > 0)
+        const char* cusip = gnc_commodity_get_cusip (com);
+        if (cusip && *cusip)
         {
-            xmlAddChild (ret, text_to_dom_tree (
-                             cmdty_xcode,
-                             gnc_commodity_get_cusip (com)));
+            xmlAddChild (ret, text_to_dom_tree (cmdty_xcode, cusip));
         }
 
         xmlAddChild (ret, int_to_dom_tree (cmdty_fraction,
diff --git a/libgnucash/backend/xml/gnc-employee-xml-v2.cpp b/libgnucash/backend/xml/gnc-employee-xml-v2.cpp
index e7e2bf613e..14d3892ae7 100644
--- a/libgnucash/backend/xml/gnc-employee-xml-v2.cpp
+++ b/libgnucash/backend/xml/gnc-employee-xml-v2.cpp
@@ -66,7 +66,7 @@ const gchar* employee_version_string = "2.0.0";
 static void
 maybe_add_string (xmlNodePtr ptr, const char* tag, const char* str)
 {
-    if (str && strlen (str) > 0)
+    if (str && *str)
         xmlAddChild (ptr, text_to_dom_tree (tag, str));
 }
 
diff --git a/libgnucash/backend/xml/gnc-entry-xml-v2.cpp b/libgnucash/backend/xml/gnc-entry-xml-v2.cpp
index 25e3c77822..3397c405bb 100644
--- a/libgnucash/backend/xml/gnc-entry-xml-v2.cpp
+++ b/libgnucash/backend/xml/gnc-entry-xml-v2.cpp
@@ -93,7 +93,7 @@ const gchar* entry_version_string = "2.0.0";
 static void
 maybe_add_string (xmlNodePtr ptr, const char* tag, const char* str)
 {
-    if (str && strlen (str) > 0)
+    if (str && *str)
         xmlAddChild (ptr, text_to_dom_tree (tag, str));
 }
 
diff --git a/libgnucash/backend/xml/gnc-invoice-xml-v2.cpp b/libgnucash/backend/xml/gnc-invoice-xml-v2.cpp
index c79937ee22..3faaddf5df 100644
--- a/libgnucash/backend/xml/gnc-invoice-xml-v2.cpp
+++ b/libgnucash/backend/xml/gnc-invoice-xml-v2.cpp
@@ -73,7 +73,7 @@ const gchar* invoice_version_string = "2.0.0";
 static void
 maybe_add_string (xmlNodePtr ptr, const char* tag, const char* str)
 {
-    if (str && strlen (str) > 0)
+    if (str && *str)
         xmlAddChild (ptr, text_to_dom_tree (tag, str));
 }
 
diff --git a/libgnucash/backend/xml/gnc-order-xml-v2.cpp b/libgnucash/backend/xml/gnc-order-xml-v2.cpp
index 4160f2f427..eddc344dd5 100644
--- a/libgnucash/backend/xml/gnc-order-xml-v2.cpp
+++ b/libgnucash/backend/xml/gnc-order-xml-v2.cpp
@@ -64,7 +64,7 @@ const gchar* order_version_string = "2.0.0";
 static void
 maybe_add_string (xmlNodePtr ptr, const char* tag, const char* str)
 {
-    if (str && strlen (str) > 0)
+    if (str && *str)
         xmlAddChild (ptr, text_to_dom_tree (tag, str));
 }
 
diff --git a/libgnucash/backend/xml/gnc-pricedb-xml-v2.cpp b/libgnucash/backend/xml/gnc-pricedb-xml-v2.cpp
index 2c9894782f..958496ee1b 100644
--- a/libgnucash/backend/xml/gnc-pricedb-xml-v2.cpp
+++ b/libgnucash/backend/xml/gnc-pricedb-xml-v2.cpp
@@ -436,14 +436,14 @@ gnc_price_to_dom_tree (const xmlChar* tag, GNCPrice* price)
     if (!add_child_or_kill_parent (price_xml, tmpnode)) return NULL;
 
     sourcestr = gnc_price_get_source_string (price);
-    if (sourcestr && (strlen (sourcestr) != 0))
+    if (sourcestr && *sourcestr)
     {
         tmpnode = text_to_dom_tree ("price:source", sourcestr);
         if (!add_child_or_kill_parent (price_xml, tmpnode)) return NULL;
     }
 
     typestr = gnc_price_get_typestr (price);
-    if (typestr && (strlen (typestr) != 0))
+    if (typestr && *typestr)
     {
         tmpnode = text_to_dom_tree ("price:type", typestr);
         if (!add_child_or_kill_parent (price_xml, tmpnode)) return NULL;
diff --git a/libgnucash/backend/xml/gnc-xml-backend.cpp b/libgnucash/backend/xml/gnc-xml-backend.cpp
index b1c7deba54..7894d02f57 100644
--- a/libgnucash/backend/xml/gnc-xml-backend.cpp
+++ b/libgnucash/backend/xml/gnc-xml-backend.cpp
@@ -775,7 +775,7 @@ GncXmlBackend::remove_old_files ()
              * juggling, but considering the above tests, this should always
              * be safe */
             regex_t pattern;
-            gchar* stamp_start = name + strlen (m_fullpath.c_str());
+            gchar* stamp_start = name + m_fullpath.size();
             gchar* expression = g_strdup_printf ("^\\.[[:digit:]]{14}(\\%s|\\%s|\\.xac)$",
                                                  GNC_DATAFILE_EXT, GNC_LOGFILE_EXT);
             gboolean got_date_stamp = FALSE;
diff --git a/libgnucash/backend/xml/xml-helpers.h b/libgnucash/backend/xml/xml-helpers.h
index 782f56b4c6..71b99dbe9b 100644
--- a/libgnucash/backend/xml/xml-helpers.h
+++ b/libgnucash/backend/xml/xml-helpers.h
@@ -40,7 +40,7 @@ maybe_add_numeric (xmlNodePtr ptr, const char* tag, gnc_numeric val)
 static inline void
 maybe_add_string (xmlNodePtr ptr, const char* tag, const char* str)
 {
-    if (str && strlen (str) > 0)
+    if (str && *str)
         xmlAddChild (ptr, text_to_dom_tree (tag, str));
 }
 



Summary of changes:
 libgnucash/backend/xml/gnc-account-xml-v2.cpp   | 4 ++--
 libgnucash/backend/xml/gnc-address-xml-v2.cpp   | 2 +-
 libgnucash/backend/xml/gnc-commodity-xml-v2.cpp | 8 +++-----
 libgnucash/backend/xml/gnc-employee-xml-v2.cpp  | 2 +-
 libgnucash/backend/xml/gnc-entry-xml-v2.cpp     | 2 +-
 libgnucash/backend/xml/gnc-invoice-xml-v2.cpp   | 2 +-
 libgnucash/backend/xml/gnc-order-xml-v2.cpp     | 2 +-
 libgnucash/backend/xml/gnc-pricedb-xml-v2.cpp   | 4 ++--
 libgnucash/backend/xml/gnc-xml-backend.cpp      | 2 +-
 libgnucash/backend/xml/xml-helpers.h            | 2 +-
 10 files changed, 14 insertions(+), 16 deletions(-)



More information about the gnucash-changes mailing list