[gnome-todo] utils: Optimize gtd_str_replace()



commit ac7105aade8757b8e08f0384da9779e5c759d0ed
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Fri Feb 2 13:31:46 2018 -0200

    utils: Optimize gtd_str_replace()
    
    The complexity is the same - O(n) - but we avoid
    iterating over the source and new string.

 src/gtd-utils.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
---
diff --git a/src/gtd-utils.c b/src/gtd-utils.c
index 434306a..0e33d02 100644
--- a/src/gtd-utils.c
+++ b/src/gtd-utils.c
@@ -31,6 +31,7 @@ gtd_str_replace (const gchar *source,
   gint64 n_ocurrences;
   gint64 replacement_len;
   gint64 search_len;
+  gint64 source_len;
   gint64 final_size;
   gint64 diff;
 
@@ -39,6 +40,7 @@ gtd_str_replace (const gchar *source,
   g_assert_nonnull (replacement);
 
   /* Count the number of ocurrences of "search" inside "source" */
+  source_len = strlen (source);
   search_len = strlen (search);
   replacement_len = strlen (replacement);
   n_ocurrences = 0;
@@ -52,10 +54,11 @@ gtd_str_replace (const gchar *source,
 
   /* Calculate size of the new string */
   diff = replacement_len - search_len;
-  final_size = strlen (source) + diff * n_ocurrences + 1;
+  final_size = source_len + diff * n_ocurrences + 1;
 
   /* Create the new string */
-  new_string = g_malloc0 (final_size);
+  new_string = g_malloc (final_size);
+  new_string[final_size - 1] = '\0';
 
   /*
    * And copy the contents of the source string into the new string,
@@ -83,7 +86,7 @@ gtd_str_replace (const gchar *source,
     }
 
   /* Copy the last chunk of string if any */
-  diff = strlen (source) - (source_aux2 - source);
+  diff = source_len - (source_aux2 - source);
   strncpy (new_aux, source_aux2, diff);
 
   return new_string;


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