summaryrefslogtreecommitdiff
path: root/glib
diff options
context:
space:
mode:
authorMatthias Clasen <maclas@gmx.de>2004-01-28 00:37:46 +0000
committerMatthias Clasen <matthiasc@src.gnome.org>2004-01-28 00:37:46 +0000
commit326c025205a0b1beebf3cfb9c07d9b510fedc108 (patch)
tree13f04d59e4a627cc204372a2149f071861b9fda6 /glib
parentfc8c5a9233fc3b74a6891819744acfde79cfd59f (diff)
downloadglib-326c025205a0b1beebf3cfb9c07d9b510fedc108.tar.gz
New function, a cross between g_strsplit() and strtok(). (#88329, Soeren
Wed Jan 28 01:39:21 2004 Matthias Clasen <maclas@gmx.de> * glib/gstrfuncs.h: * glib/gstrfuncs.c (g_strsplit_set): New function, a cross between g_strsplit() and strtok(). (#88329, Soeren Sandmann) * tests/strfunc-test.c (main): Add g_strsplit_set() tests.
Diffstat (limited to 'glib')
-rw-r--r--glib/gstrfuncs.c100
-rw-r--r--glib/gstrfuncs.h7
2 files changed, 105 insertions, 2 deletions
diff --git a/glib/gstrfuncs.c b/glib/gstrfuncs.c
index e1662125d..c4854942e 100644
--- a/glib/gstrfuncs.c
+++ b/glib/gstrfuncs.c
@@ -2172,6 +2172,106 @@ 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.
+ * @max_tokens: The maximum number of tokens to split @string into.
+ * If this is less than 1, the string is split completely
+ *
+ * Splits @string into a number of tokens not containing any of the characters
+ * in @delimiter. A token is the (possibly empty) longest string that does not
+ * contain any of the characters in @delimiters. If @max_tokens is reached, the
+ * remainder is appended to the last token.
+ *
+ * For example the result of g_strtokenize ("abc:def/ghi", ":/", -1) is a
+ * %NULL-terminated vector containing the three strings "abc", "def",
+ * and "ghi".
+ *
+ * The result if g_strtokenize (":def/ghi:", ":/", -1) is a %NULL-terminated
+ * vector containing the four strings "", "def", "ghi", and "".
+ *
+ * As a special case, the result of splitting the empty string "" is an empty
+ * vector, not a vector containing a single string. The reason for this
+ * special case is that being able to represent a empty vector is typically
+ * more useful than consistent handling of empty elements. If you do need
+ * to represent empty elements, you'll need to check for the empty string
+ * before calling g_strsplit().
+ *
+ * Note that this function works on bytes not characters, so it can't be used
+ * to delimit UTF-8 strings for anything but ASCII characters.
+ *
+ * Return value: a newly-allocated %NULL-terminated array of strings. Use
+ * g_strfreev() to free it.
+ *
+ * Since: 2.4
+ **/
+gchar **
+g_strsplit_set (const gchar *string,
+ const gchar *delimiters,
+ gint max_tokens)
+{
+ gboolean delim_table[256];
+ GSList *tokens, *list;
+ gint n_tokens;
+ const gchar *s;
+ const gchar *current;
+ gchar *token;
+ gchar **result;
+
+ g_return_val_if_fail (string != NULL, NULL);
+ g_return_val_if_fail (delimiters != NULL, NULL);
+
+ if (max_tokens < 1)
+ max_tokens = G_MAXINT;
+
+ if (*string == '\0')
+ {
+ result = g_new (char *, 1);
+ result[0] = NULL;
+ return result;
+ }
+
+ memset (delim_table, FALSE, sizeof (delim_table));
+ for (s = delimiters; *s != '\0'; ++s)
+ delim_table[*(guchar *)s] = TRUE;
+
+ tokens = NULL;
+ n_tokens = 0;
+
+ s = current = string;
+ while (*s != '\0')
+ {
+ if (delim_table[*(guchar *)s] && n_tokens + 1 < max_tokens)
+ {
+ gchar *token;
+
+ token = g_strndup (current, s - current);
+ tokens = g_slist_prepend (tokens, token);
+ ++n_tokens;
+
+ current = s + 1;
+ }
+
+ ++s;
+ }
+
+ token = g_strndup (current, s - current);
+ tokens = g_slist_prepend (tokens, token);
+ ++n_tokens;
+
+ result = g_new (gchar *, n_tokens + 1);
+
+ result[n_tokens] = NULL;
+ for (list = tokens; list != NULL; list = list->next)
+ result[--n_tokens] = list->data;
+
+ g_slist_free (tokens);
+
+ return result;
+}
+
+/**
* g_strfreev:
* @str_array: a %NULL-terminated array of strings to free.
diff --git a/glib/gstrfuncs.h b/glib/gstrfuncs.h
index 785bee6f8..928106897 100644
--- a/glib/gstrfuncs.h
+++ b/glib/gstrfuncs.h
@@ -213,8 +213,8 @@ gpointer g_memdup (gconstpointer mem,
guint byte_size);
/* NULL terminated string arrays.
- * g_strsplit() splits up string into max_tokens tokens at delim and
- * returns a newly allocated string array.
+ * g_strsplit(), g_strsplit_set() split up string into max_tokens tokens
+ * at delim and return a newly allocated string array.
* g_strjoinv() concatenates all of str_array's strings, sliding in an
* optional separator, the returned string is newly allocated.
* g_strfreev() frees the array itself and all of its strings.
@@ -223,6 +223,9 @@ gpointer g_memdup (gconstpointer mem,
gchar** g_strsplit (const gchar *string,
const gchar *delimiter,
gint max_tokens);
+gchar ** g_strsplit_set (const gchar *string,
+ const gchar *delimiters,
+ gint max_tokens);
gchar* g_strjoinv (const gchar *separator,
gchar **str_array);
void g_strfreev (gchar **str_array);