From db9987d269b80e2fdfc6539ee51187910034629f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 7 Jul 2020 13:34:35 +0400 Subject: [PATCH] strfuncs: a few g_strsplit_set() improvements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit gboolean is secretly actually typedef gint gboolean, so the delim_table is going to take 1KB of stack all by itself. That’s fine, but it could be smaller. This strnpbrk()-like block could do with a comment to make it a bit clearer what it’s doing. Suggested-by: Philip Withnall Signed-off-by: Marc-André Lureau --- glib/gstrfuncs.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/glib/gstrfuncs.c b/glib/gstrfuncs.c index 31e4cf2f4..01f4fe3a8 100644 --- a/glib/gstrfuncs.c +++ b/glib/gstrfuncs.c @@ -2393,7 +2393,8 @@ g_strsplit (const gchar *string, * g_strsplit_set: * @string: The string to be tokenized * @delimiters: A nul-terminated string containing bytes that are used - * to split the string. + * to split the string (it can accept an empty string, which will result + * in no string splitting). * @max_tokens: The maximum number of tokens to split @string into. * If this is less than 1, the string is split completely * @@ -2429,7 +2430,7 @@ g_strsplit_set (const gchar *string, const gchar *delimiters, gint max_tokens) { - gboolean delim_table[256]; + guint8 delim_table[256]; /* 1 = index is a separator; 0 otherwise */ GSList *tokens, *list; gint n_tokens; const gchar *s; @@ -2450,6 +2451,9 @@ g_strsplit_set (const gchar *string, return result; } + /* Check if each character in @string is a separator, by indexing by the + * character value into the @delim_table, which has value 1 stored at an index + * if that index is a separator. */ memset (delim_table, FALSE, sizeof (delim_table)); for (s = delimiters; *s != '\0'; ++s) delim_table[*(guchar *)s] = TRUE;