[c22e1a4acbd2d996ff19a852585f9434883c30124f6b118eb9152fe4e5ee7994: 1/8] strfuncs: a few g_strsplit_set() improvements



commit db9987d269b80e2fdfc6539ee51187910034629f
Author: Marc-André Lureau <marcandre lureau redhat com>
Date:   Tue Jul 7 13:34:35 2020 +0400

    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>

 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;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]