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.
This commit is contained in:
Marco Trevisan (Treviño) 2022-09-02 21:31:34 +02:00
parent 88d4b3b365
commit 0618f5eb82

View File

@ -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)