[glib: 1/2] g_strsplit: Use a pre-allocated GArray when max_tokens is provided




commit 0618f5eb82efc3233e8a592975f452f3db3f162e
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Fri Sep 2 21:31:34 2022 +0200

    g_strsplit: Use a pre-allocated GArray when max_tokens is provided
    
    In case max_tokens is provided, we can safely pre-allocate the GArray to
    the max_tokens value plus one for the NULL terminating value.

 glib/gstrfuncs.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
---
diff --git a/glib/gstrfuncs.c b/glib/gstrfuncs.c
index 0386e8e45b..ee934e5d76 100644
--- a/glib/gstrfuncs.c
+++ b/glib/gstrfuncs.c
@@ -2425,9 +2425,15 @@ g_strsplit (const gchar *string,
   g_return_val_if_fail (delimiter[0] != '\0', NULL);
 
   if (max_tokens < 1)
-    max_tokens = G_MAXINT;
+    {
+      max_tokens = G_MAXINT;
+      string_list = g_ptr_array_new ();
+    }
+  else
+    {
+      string_list = g_ptr_array_new_full (max_tokens + 1, NULL);
+    }
 
-  string_list = g_ptr_array_new ();
   remainder = string;
   s = strstr (remainder, delimiter);
   if (s)


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