|
From: Daniel K. O. <dan...@ya...> - 2003-03-31 03:12:50
|
> Hi fellows, > I'm developing a low-level C-based string manipulation API ... > in my Implementation of the function "explode" which splits a > string into > pieces (with a specified separator) and returns these pieces > into an array. While you are reinventing the wheel, you can take a look at GLib's sources at www.gtk.org. More specifically the funciton g_strsplit(): http://developer.gnome.org/doc/API/2.0/glib/glib-String-Utility-Functions.ht ml#g-strsplit Here is the code: Note: "GSList" is a single linked list pointer. - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< /** * g_strsplit: * @string: a string to split. * @delimiter: a string which specifies the places at which to split the string. * The delimiter is not included in any of the resulting strings, unless * @max_tokens is reached. * @max_tokens: the maximum number of pieces to split @string into. If this is * less than 1, the string is split completely. * * Splits a string into a maximum of @max_tokens pieces, using the given * @delimiter. If @max_tokens is reached, the remainder of @string is appended * to the last token. * * 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(). * * Return value: a newly-allocated %NULL-terminated array of strings. Use * g_strfreev() to free it. **/ gchar** g_strsplit (const gchar *string, const gchar *delimiter, gint max_tokens) { GSList *string_list = NULL, *slist; gchar **str_array, *s; guint n = 0; const gchar *remainder; g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (delimiter != NULL, NULL); g_return_val_if_fail (delimiter[0] != '\0', NULL); if (max_tokens < 1) max_tokens = G_MAXINT; remainder = string; s = strstr (remainder, delimiter); if (s) { gsize delimiter_len = strlen (delimiter); while (--max_tokens && s) { gsize len; gchar *new_string; len = s - remainder; new_string = g_new (gchar, len + 1); strncpy (new_string, remainder, len); new_string[len] = 0; string_list = g_slist_prepend (string_list, new_string); n++; remainder = s + delimiter_len; s = strstr (remainder, delimiter); } } if (*string) { n++; string_list = g_slist_prepend (string_list, g_strdup (remainder)); } str_array = g_new (gchar*, n + 1); str_array[n--] = NULL; for (slist = string_list; slist; slist = slist->next) str_array[n--] = slist->data; g_slist_free (string_list); return str_array; } - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< Daniel K. O. Only ask questions with yes/no answers if you want "yes" or "no" as the answer. http://homepages.tesco.net/~J.deBoynePollard/FGA/questions-with-yes-or-no-an swers.html |