strfuncs: a few g_strsplit_set() improvements

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 <philip@tecnocode.co.uk>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This commit is contained in:
Marc-André Lureau 2020-07-07 13:34:35 +04:00
parent 1ee22d0ae9
commit db9987d269

View File

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