glib-aux: don't use GPtrArray in nm_utils_strsplit_quoted()

GPtrArray requires an additional heap allocation for the GPtrArray.
Utterly useless in the majority of cases.

Anyway. Allocating (and exponentially grown) a buffer is not too hard,
just slightly more cumbersome. Since nm_utils_strsplit_quoted() is
heavily unit tested and entirely self-contained, let's opt for the
more complicated implementation and avoid the extra allocation.
This commit is contained in:
Thomas Haller 2022-07-13 17:18:29 +02:00
parent e122df6005
commit 4dd4d5b759
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -2288,9 +2288,11 @@ nm_utils_escaped_tokens_options_split(char *str, const char **out_key, const cha
char **
nm_utils_strsplit_quoted(const char *str)
{
gs_unref_ptrarray GPtrArray *arr = NULL;
gs_free char *str_out = NULL;
CharLookupTable ch_lookup;
char **arr = NULL;
gsize arr_len = 0;
gsize arr_alloc = 0;
gs_free char *str_out = NULL;
CharLookupTable ch_lookup;
nm_assert(str);
@ -2347,19 +2349,32 @@ nm_utils_strsplit_quoted(const char *str)
str++;
}
if (!arr)
arr = g_ptr_array_new();
g_ptr_array_add(arr, g_strndup(str_out, j));
if (arr_len >= arr_alloc) {
if (arr_alloc == 0)
arr_alloc = 4;
else
arr_alloc *= 2;
arr = g_realloc(arr, sizeof(char *) * arr_alloc);
}
arr[arr_len++] = g_strndup(str_out, j);
}
if (!arr)
return g_new0(char *, 1);
g_ptr_array_add(arr, NULL);
/* We want to return an optimally sized strv array, with no excess
* memory allocated. Hence, clone once more. */
return nm_memdup(arr->pdata, sizeof(char *) * arr->len);
if (arr_len + 1u != arr_alloc) {
gs_free char **arr_old = arr;
arr = g_new(char *, arr_len + 1u);
memcpy(arr, arr_old, sizeof(char *) * arr_len);
}
arr[arr_len] = NULL;
return arr;
}
/*****************************************************************************/