Merge branch 'wip/baedert/arrays' into 'master'

strfuncs: Use a GPtrArray in strsplit()

See merge request GNOME/glib!1518
This commit is contained in:
Sebastian Dröge 2020-06-05 07:31:34 +00:00
commit c573e21191

View File

@ -35,6 +35,7 @@
#include <string.h> #include <string.h>
#include <locale.h> #include <locale.h>
#include <errno.h> #include <errno.h>
#include <garray.h>
#include <ctype.h> /* For tolower() */ #include <ctype.h> /* For tolower() */
#ifdef HAVE_XLOCALE_H #ifdef HAVE_XLOCALE_H
@ -2352,10 +2353,9 @@ g_strsplit (const gchar *string,
const gchar *delimiter, const gchar *delimiter,
gint max_tokens) gint max_tokens)
{ {
GSList *string_list = NULL, *slist; char *s;
gchar **str_array, *s;
guint n = 0;
const gchar *remainder; const gchar *remainder;
GPtrArray *string_list;
g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (string != NULL, NULL);
g_return_val_if_fail (delimiter != NULL, NULL); g_return_val_if_fail (delimiter != NULL, NULL);
@ -2364,6 +2364,7 @@ g_strsplit (const gchar *string,
if (max_tokens < 1) if (max_tokens < 1)
max_tokens = G_MAXINT; max_tokens = G_MAXINT;
string_list = g_ptr_array_new ();
remainder = string; remainder = string;
s = strstr (remainder, delimiter); s = strstr (remainder, delimiter);
if (s) if (s)
@ -2375,28 +2376,17 @@ g_strsplit (const gchar *string,
gsize len; gsize len;
len = s - remainder; len = s - remainder;
string_list = g_slist_prepend (string_list, g_ptr_array_add (string_list, g_strndup (remainder, len));
g_strndup (remainder, len));
n++;
remainder = s + delimiter_len; remainder = s + delimiter_len;
s = strstr (remainder, delimiter); s = strstr (remainder, delimiter);
} }
} }
if (*string) if (*string)
{ g_ptr_array_add (string_list, g_strdup (remainder));
n++;
string_list = g_slist_prepend (string_list, g_strdup (remainder));
}
str_array = g_new (gchar*, n + 1); g_ptr_array_add (string_list, NULL);
str_array[n--] = NULL; return (char **) g_ptr_array_free (string_list, FALSE);
for (slist = string_list; slist; slist = slist->next)
str_array[n--] = slist->data;
g_slist_free (string_list);
return str_array;
} }
/** /**