From: Tim Janik Date: Wed, 21 Oct 1998 17:18:22 +0000 (+0000) Subject: API cleanups of the new g_str* functions for consistency with the existing X-Git-Tag: GLIB_1_1_4~5 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2009313501ca210189c7adeefd6a4687a21b0269;p=platform%2Fupstream%2Fglib.git API cleanups of the new g_str* functions for consistency with the existing Wed Oct 21 17:03:05 1998 Tim Janik * glib.h: * gstrfuncs.c: API cleanups of the new g_str* functions for consistency with the existing naming scheme for g_str* functions. grouped the g_str* functions that will return a newly allocated string seperatedly. all of the in_place arguments were skipped, the caller is supposed to pass a g_strdup()ed string if he wants to retrive a new copy. indentation and coding style fixups. added some g_return_if_fail() statements. string array functions are prefixed with g_str_array_. (g_strdelimit): return the modified string like all other g_str* functions, that operate in place. (g_strchug): renamed from g_str_chug(), removed in_place argument. (g_strchomp): renamed from g_str_chomp(), removed in_place argument. (g_strstrip): renamed from g_str_strip(), removed in_place argument. (g_str_array_join): renamed from g_strconcatv(), since it actually operates on a string array and has totaly different semantics from g_strconcat(). check for separator != NULL, don't segfault if the first string is NULL. removed the `const' from the string array that's passed, so users can operate on gchar** string arrays. (g_str_array_split): renamed from g_str_split() because we actually produce a string array. reimplemented this function for efficiency. removed macro aliases g_str_joinv and g_str_join. --- diff --git a/ChangeLog b/ChangeLog index 2223719..db364d4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,27 @@ +Wed Oct 21 17:03:05 1998 Tim Janik + + * glib.h: + * gstrfuncs.c: API cleanups of the new g_str* functions for consistency + with the existing naming scheme for g_str* functions. grouped the g_str* + functions that will return a newly allocated string seperatedly. all of + the in_place arguments were skipped, the caller is supposed to pass a + g_strdup()ed string if he wants to retrive a new copy. indentation and + coding style fixups. added some g_return_if_fail() statements. + string array functions are prefixed with g_str_array_. + (g_strdelimit): return the modified string like all other g_str* + functions, that operate in place. + (g_strchug): renamed from g_str_chug(), removed in_place argument. + (g_strchomp): renamed from g_str_chomp(), removed in_place argument. + (g_strstrip): renamed from g_str_strip(), removed in_place argument. + (g_str_array_join): renamed from g_strconcatv(), since it actually + operates on a string array and has totaly different semantics from + g_strconcat(). check for separator != NULL, don't segfault if the first + string is NULL. removed the `const' from the string array that's passed, + so users can operate on gchar** string arrays. + (g_str_array_split): renamed from g_str_split() because we actually + produce a string array. reimplemented this function for efficiency. + removed macro aliases g_str_joinv and g_str_join. + 1998-10-20 Elliot Lee * glib.h, gstrfuncs.c: Move string join/split/free routines from diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index 2223719..db364d4 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,27 @@ +Wed Oct 21 17:03:05 1998 Tim Janik + + * glib.h: + * gstrfuncs.c: API cleanups of the new g_str* functions for consistency + with the existing naming scheme for g_str* functions. grouped the g_str* + functions that will return a newly allocated string seperatedly. all of + the in_place arguments were skipped, the caller is supposed to pass a + g_strdup()ed string if he wants to retrive a new copy. indentation and + coding style fixups. added some g_return_if_fail() statements. + string array functions are prefixed with g_str_array_. + (g_strdelimit): return the modified string like all other g_str* + functions, that operate in place. + (g_strchug): renamed from g_str_chug(), removed in_place argument. + (g_strchomp): renamed from g_str_chomp(), removed in_place argument. + (g_strstrip): renamed from g_str_strip(), removed in_place argument. + (g_str_array_join): renamed from g_strconcatv(), since it actually + operates on a string array and has totaly different semantics from + g_strconcat(). check for separator != NULL, don't segfault if the first + string is NULL. removed the `const' from the string array that's passed, + so users can operate on gchar** string arrays. + (g_str_array_split): renamed from g_str_split() because we actually + produce a string array. reimplemented this function for efficiency. + removed macro aliases g_str_joinv and g_str_join. + 1998-10-20 Elliot Lee * glib.h, gstrfuncs.c: Move string join/split/free routines from diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 2223719..db364d4 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,27 @@ +Wed Oct 21 17:03:05 1998 Tim Janik + + * glib.h: + * gstrfuncs.c: API cleanups of the new g_str* functions for consistency + with the existing naming scheme for g_str* functions. grouped the g_str* + functions that will return a newly allocated string seperatedly. all of + the in_place arguments were skipped, the caller is supposed to pass a + g_strdup()ed string if he wants to retrive a new copy. indentation and + coding style fixups. added some g_return_if_fail() statements. + string array functions are prefixed with g_str_array_. + (g_strdelimit): return the modified string like all other g_str* + functions, that operate in place. + (g_strchug): renamed from g_str_chug(), removed in_place argument. + (g_strchomp): renamed from g_str_chomp(), removed in_place argument. + (g_strstrip): renamed from g_str_strip(), removed in_place argument. + (g_str_array_join): renamed from g_strconcatv(), since it actually + operates on a string array and has totaly different semantics from + g_strconcat(). check for separator != NULL, don't segfault if the first + string is NULL. removed the `const' from the string array that's passed, + so users can operate on gchar** string arrays. + (g_str_array_split): renamed from g_str_split() because we actually + produce a string array. reimplemented this function for efficiency. + removed macro aliases g_str_joinv and g_str_join. + 1998-10-20 Elliot Lee * glib.h, gstrfuncs.c: Move string join/split/free routines from diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 2223719..db364d4 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,27 @@ +Wed Oct 21 17:03:05 1998 Tim Janik + + * glib.h: + * gstrfuncs.c: API cleanups of the new g_str* functions for consistency + with the existing naming scheme for g_str* functions. grouped the g_str* + functions that will return a newly allocated string seperatedly. all of + the in_place arguments were skipped, the caller is supposed to pass a + g_strdup()ed string if he wants to retrive a new copy. indentation and + coding style fixups. added some g_return_if_fail() statements. + string array functions are prefixed with g_str_array_. + (g_strdelimit): return the modified string like all other g_str* + functions, that operate in place. + (g_strchug): renamed from g_str_chug(), removed in_place argument. + (g_strchomp): renamed from g_str_chomp(), removed in_place argument. + (g_strstrip): renamed from g_str_strip(), removed in_place argument. + (g_str_array_join): renamed from g_strconcatv(), since it actually + operates on a string array and has totaly different semantics from + g_strconcat(). check for separator != NULL, don't segfault if the first + string is NULL. removed the `const' from the string array that's passed, + so users can operate on gchar** string arrays. + (g_str_array_split): renamed from g_str_split() because we actually + produce a string array. reimplemented this function for efficiency. + removed macro aliases g_str_joinv and g_str_join. + 1998-10-20 Elliot Lee * glib.h, gstrfuncs.c: Move string join/split/free routines from diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index 2223719..db364d4 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,27 @@ +Wed Oct 21 17:03:05 1998 Tim Janik + + * glib.h: + * gstrfuncs.c: API cleanups of the new g_str* functions for consistency + with the existing naming scheme for g_str* functions. grouped the g_str* + functions that will return a newly allocated string seperatedly. all of + the in_place arguments were skipped, the caller is supposed to pass a + g_strdup()ed string if he wants to retrive a new copy. indentation and + coding style fixups. added some g_return_if_fail() statements. + string array functions are prefixed with g_str_array_. + (g_strdelimit): return the modified string like all other g_str* + functions, that operate in place. + (g_strchug): renamed from g_str_chug(), removed in_place argument. + (g_strchomp): renamed from g_str_chomp(), removed in_place argument. + (g_strstrip): renamed from g_str_strip(), removed in_place argument. + (g_str_array_join): renamed from g_strconcatv(), since it actually + operates on a string array and has totaly different semantics from + g_strconcat(). check for separator != NULL, don't segfault if the first + string is NULL. removed the `const' from the string array that's passed, + so users can operate on gchar** string arrays. + (g_str_array_split): renamed from g_str_split() because we actually + produce a string array. reimplemented this function for efficiency. + removed macro aliases g_str_joinv and g_str_join. + 1998-10-20 Elliot Lee * glib.h, gstrfuncs.c: Move string join/split/free routines from diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 2223719..db364d4 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,27 @@ +Wed Oct 21 17:03:05 1998 Tim Janik + + * glib.h: + * gstrfuncs.c: API cleanups of the new g_str* functions for consistency + with the existing naming scheme for g_str* functions. grouped the g_str* + functions that will return a newly allocated string seperatedly. all of + the in_place arguments were skipped, the caller is supposed to pass a + g_strdup()ed string if he wants to retrive a new copy. indentation and + coding style fixups. added some g_return_if_fail() statements. + string array functions are prefixed with g_str_array_. + (g_strdelimit): return the modified string like all other g_str* + functions, that operate in place. + (g_strchug): renamed from g_str_chug(), removed in_place argument. + (g_strchomp): renamed from g_str_chomp(), removed in_place argument. + (g_strstrip): renamed from g_str_strip(), removed in_place argument. + (g_str_array_join): renamed from g_strconcatv(), since it actually + operates on a string array and has totaly different semantics from + g_strconcat(). check for separator != NULL, don't segfault if the first + string is NULL. removed the `const' from the string array that's passed, + so users can operate on gchar** string arrays. + (g_str_array_split): renamed from g_str_split() because we actually + produce a string array. reimplemented this function for efficiency. + removed macro aliases g_str_joinv and g_str_join. + 1998-10-20 Elliot Lee * glib.h, gstrfuncs.c: Move string join/split/free routines from diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 2223719..db364d4 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,27 @@ +Wed Oct 21 17:03:05 1998 Tim Janik + + * glib.h: + * gstrfuncs.c: API cleanups of the new g_str* functions for consistency + with the existing naming scheme for g_str* functions. grouped the g_str* + functions that will return a newly allocated string seperatedly. all of + the in_place arguments were skipped, the caller is supposed to pass a + g_strdup()ed string if he wants to retrive a new copy. indentation and + coding style fixups. added some g_return_if_fail() statements. + string array functions are prefixed with g_str_array_. + (g_strdelimit): return the modified string like all other g_str* + functions, that operate in place. + (g_strchug): renamed from g_str_chug(), removed in_place argument. + (g_strchomp): renamed from g_str_chomp(), removed in_place argument. + (g_strstrip): renamed from g_str_strip(), removed in_place argument. + (g_str_array_join): renamed from g_strconcatv(), since it actually + operates on a string array and has totaly different semantics from + g_strconcat(). check for separator != NULL, don't segfault if the first + string is NULL. removed the `const' from the string array that's passed, + so users can operate on gchar** string arrays. + (g_str_array_split): renamed from g_str_split() because we actually + produce a string array. reimplemented this function for efficiency. + removed macro aliases g_str_joinv and g_str_join. + 1998-10-20 Elliot Lee * glib.h, gstrfuncs.c: Move string join/split/free routines from diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 2223719..db364d4 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,27 @@ +Wed Oct 21 17:03:05 1998 Tim Janik + + * glib.h: + * gstrfuncs.c: API cleanups of the new g_str* functions for consistency + with the existing naming scheme for g_str* functions. grouped the g_str* + functions that will return a newly allocated string seperatedly. all of + the in_place arguments were skipped, the caller is supposed to pass a + g_strdup()ed string if he wants to retrive a new copy. indentation and + coding style fixups. added some g_return_if_fail() statements. + string array functions are prefixed with g_str_array_. + (g_strdelimit): return the modified string like all other g_str* + functions, that operate in place. + (g_strchug): renamed from g_str_chug(), removed in_place argument. + (g_strchomp): renamed from g_str_chomp(), removed in_place argument. + (g_strstrip): renamed from g_str_strip(), removed in_place argument. + (g_str_array_join): renamed from g_strconcatv(), since it actually + operates on a string array and has totaly different semantics from + g_strconcat(). check for separator != NULL, don't segfault if the first + string is NULL. removed the `const' from the string array that's passed, + so users can operate on gchar** string arrays. + (g_str_array_split): renamed from g_str_split() because we actually + produce a string array. reimplemented this function for efficiency. + removed macro aliases g_str_joinv and g_str_join. + 1998-10-20 Elliot Lee * glib.h, gstrfuncs.c: Move string join/split/free routines from diff --git a/glib.h b/glib.h index 8ce902f..2d37a01 100644 --- a/glib.h +++ b/glib.h @@ -1334,24 +1334,13 @@ gdouble g_timer_elapsed (GTimer *timer, gulong *microseconds); -/* String utility functions +/* String utility functions that modify a string argument or + * return a constant string that must not be freed. */ #define G_STR_DELIMITERS "_-|> <." -void g_strdelimit (gchar *string, +gchar* g_strdelimit (gchar *string, const gchar *delimiters, gchar new_delimiter); -gchar* g_strdup (const gchar *str); -gchar* g_strdup_printf (const gchar *format, - ...) G_GNUC_PRINTF (1, 2); -gchar* g_strdup_vprintf (const gchar *format, - va_list args); -gchar* g_strndup (const gchar *str, - guint n); -gchar* g_strnfill (guint length, - gchar fill_char); -gchar* g_strconcat (const gchar *string1, - ...); /* NULL terminated */ -#define g_str_join g_strconcat gdouble g_strtod (const gchar *nptr, gchar **endptr); gchar* g_strerror (gint errnum); @@ -1363,26 +1352,42 @@ void g_strup (gchar *string); void g_strreverse (gchar *string); gpointer g_memdup (gconstpointer mem, guint byte_size); - /* removes leading spaces */ -gchar * g_str_chug (gchar *astring, - gboolean in_place); +gchar* g_strchug (gchar *string); /* removes trailing spaces */ -gchar * g_str_chomp (gchar *astring, - gboolean in_place); +gchar* g_strchomp (gchar *string); /* removes leading & trailing spaces */ -#define g_str_strip(astring, in_place) \ - g_str_chomp(g_str_chug(astring, in_place), FALSE) - -/* these routines that work with string arrays in which the last - element is NULL */ -gchar ** g_str_split (const gchar *string, - const gchar *delim, - gint max_tokens); -gchar* g_strconcatv (const gchar *separator, - const gchar **strarray); -#define g_str_joinv g_strconcatv -void g_str_array_free (gchar **strarray); +#define g_strstrip( string ) g_strchomp (g_strchug (string)) + +/* String utility functions that return a newly allocated string which + * ought to be freed from the caller at some point. + */ +gchar* g_strdup (const gchar *str); +gchar* g_strdup_printf (const gchar *format, + ...) G_GNUC_PRINTF (1, 2); +gchar* g_strdup_vprintf (const gchar *format, + va_list args); +gchar* g_strndup (const gchar *str, + guint n); +gchar* g_strnfill (guint length, + gchar fill_char); +gchar* g_strconcat (const gchar *string1, + ...); /* NULL terminated */ + +/* NULL terminated string arrays. + * g_str_array_split() splits up string into max_tokens tokens at delim and + * returns a newly allocated string array. + * g_str_array_join() concatenates all of str_array's strings, sliding in an + * optional separator, the returned string is newly allocated. + * g_str_array_free() frees the array itself and all of its strings. + */ +gchar** g_str_array_split (const gchar *string, + const gchar *delimiter, + gint max_tokens); +gchar* g_str_array_join (gchar **str_array, + const gchar *separator); +void g_str_array_free (gchar **str_array); + /* calculate a string size, guarranteed to fit format + args. diff --git a/glib/glib.h b/glib/glib.h index 8ce902f..2d37a01 100644 --- a/glib/glib.h +++ b/glib/glib.h @@ -1334,24 +1334,13 @@ gdouble g_timer_elapsed (GTimer *timer, gulong *microseconds); -/* String utility functions +/* String utility functions that modify a string argument or + * return a constant string that must not be freed. */ #define G_STR_DELIMITERS "_-|> <." -void g_strdelimit (gchar *string, +gchar* g_strdelimit (gchar *string, const gchar *delimiters, gchar new_delimiter); -gchar* g_strdup (const gchar *str); -gchar* g_strdup_printf (const gchar *format, - ...) G_GNUC_PRINTF (1, 2); -gchar* g_strdup_vprintf (const gchar *format, - va_list args); -gchar* g_strndup (const gchar *str, - guint n); -gchar* g_strnfill (guint length, - gchar fill_char); -gchar* g_strconcat (const gchar *string1, - ...); /* NULL terminated */ -#define g_str_join g_strconcat gdouble g_strtod (const gchar *nptr, gchar **endptr); gchar* g_strerror (gint errnum); @@ -1363,26 +1352,42 @@ void g_strup (gchar *string); void g_strreverse (gchar *string); gpointer g_memdup (gconstpointer mem, guint byte_size); - /* removes leading spaces */ -gchar * g_str_chug (gchar *astring, - gboolean in_place); +gchar* g_strchug (gchar *string); /* removes trailing spaces */ -gchar * g_str_chomp (gchar *astring, - gboolean in_place); +gchar* g_strchomp (gchar *string); /* removes leading & trailing spaces */ -#define g_str_strip(astring, in_place) \ - g_str_chomp(g_str_chug(astring, in_place), FALSE) - -/* these routines that work with string arrays in which the last - element is NULL */ -gchar ** g_str_split (const gchar *string, - const gchar *delim, - gint max_tokens); -gchar* g_strconcatv (const gchar *separator, - const gchar **strarray); -#define g_str_joinv g_strconcatv -void g_str_array_free (gchar **strarray); +#define g_strstrip( string ) g_strchomp (g_strchug (string)) + +/* String utility functions that return a newly allocated string which + * ought to be freed from the caller at some point. + */ +gchar* g_strdup (const gchar *str); +gchar* g_strdup_printf (const gchar *format, + ...) G_GNUC_PRINTF (1, 2); +gchar* g_strdup_vprintf (const gchar *format, + va_list args); +gchar* g_strndup (const gchar *str, + guint n); +gchar* g_strnfill (guint length, + gchar fill_char); +gchar* g_strconcat (const gchar *string1, + ...); /* NULL terminated */ + +/* NULL terminated string arrays. + * g_str_array_split() splits up string into max_tokens tokens at delim and + * returns a newly allocated string array. + * g_str_array_join() concatenates all of str_array's strings, sliding in an + * optional separator, the returned string is newly allocated. + * g_str_array_free() frees the array itself and all of its strings. + */ +gchar** g_str_array_split (const gchar *string, + const gchar *delimiter, + gint max_tokens); +gchar* g_str_array_join (gchar **str_array, + const gchar *separator); +void g_str_array_free (gchar **str_array); + /* calculate a string size, guarranteed to fit format + args. diff --git a/glib/gstrfuncs.c b/glib/gstrfuncs.c index 31283ce..b40bafd 100644 --- a/glib/gstrfuncs.c +++ b/glib/gstrfuncs.c @@ -967,14 +967,14 @@ g_strcasecmp (const gchar *s1, #endif } -void +gchar* g_strdelimit (gchar *string, const gchar *delimiters, gchar new_delim) { register gchar *c; - g_return_if_fail (string != NULL); + g_return_val_if_fail (string != NULL, NULL); if (!delimiters) delimiters = G_STR_DELIMITERS; @@ -984,139 +984,138 @@ g_strdelimit (gchar *string, if (strchr (delimiters, *c)) *c = new_delim; } + + return string; } /* blame Elliot for these next five routines */ -char ** -g_str_split(const gchar *string, const gchar *delim, gint max_tokens) +gchar* +g_strchug (gchar *string) { - /* this could more easily be implemented using a GPtrArray */ - gchar **retval = NULL; - GList *items = NULL, *anode = NULL; - gint numitems = 0, dlen, i; - gchar *src, *cur, *nxt; + gchar *start; - g_return_val_if_fail(string != NULL, NULL); - g_return_val_if_fail(delim != NULL, NULL); + g_return_val_if_fail (string != NULL, NULL); - if(max_tokens < 0) - max_tokens = INT_MAX; + for (start = string; *start && isspace (*start); start++) + ; - dlen = strlen(delim); - nxt = strstr(string, delim); - if(!nxt) { - retval = g_malloc(sizeof(gchar *) * 2); - retval[0] = g_strdup(string); - retval[1] = NULL; - return retval; - } - src = cur = g_strdup(string); - nxt = strstr(src, delim); - - while(nxt && numitems < (max_tokens - 1)) { - *nxt = '\0'; - items = g_list_append(items, g_strdup(cur)); - cur = nxt + dlen; - nxt = strstr(cur, delim); - numitems++; - } - /* We have to take the rest of the string and put it as last token */ - if(*cur) { - items = g_list_append(items, g_strdup(cur)); - numitems++; - } - g_free(src); + strcpy (string, start); - retval = g_malloc(sizeof(gchar *) * (numitems + 1)); - for(anode = items, i = 0; anode; anode = anode->next, i++) - retval[i] = anode->data; - retval[i] = NULL; - g_list_free(items); - - return retval; + return string; } -gchar * -g_str_chug(gchar *astring, gboolean in_place) +gchar* +g_strchomp (gchar *string) { - gchar *retval, *start; + gchar *s; - g_return_val_if_fail(astring != NULL, NULL); + g_return_val_if_fail (string != NULL, NULL); - for(start = astring; *start && isspace(*start); start++) - /* */; + if (!*string) + return string; - if(in_place) { - retval = astring; - g_memmove(retval, start, strlen(start) + 1); - } else - retval = g_strdup(start); + for (s = string + strlen (string) - 1; s >= string && isspace (*s); s--) + *s = '\0'; - return retval; + return string; } -gchar * -g_str_chomp(gchar *astring, gboolean in_place) +gchar** +g_str_array_split (const gchar *string, + const gchar *delimiter, + gint max_tokens) { - int i; - gchar *retval, *end; + GSList *string_list = NULL, *slist; + gchar **str_array, **as, *s; + guint n = 1; - g_return_val_if_fail(astring != NULL, NULL); - - if(in_place) - retval = astring; - else - retval = g_strdup(astring); + g_return_val_if_fail (string != NULL, NULL); + g_return_val_if_fail (delimiter != NULL, NULL); - i = strlen (retval); - if (!i) - return retval; + if (max_tokens < 1) + max_tokens = G_MAXINT; - end = retval + i - 1; - for (; end >= retval && isspace (*end); end--) - *end = '\0'; + s = strstr (string, delimiter); + if (s) + { + guint delimiter_len = strlen (delimiter); + + do + { + guint len; + gchar *new_string; + + len = s - string; + new_string = g_new (gchar, len + 1); + strncpy (new_string, string, len); + new_string[len] = 0; + string_list = g_slist_prepend (string_list, new_string); + n++; + string = s + delimiter_len; + s = strstr (string, delimiter); + } + while (--max_tokens && s); + } + if (*string) + { + n++; + string_list = g_slist_prepend (string_list, g_strdup (string)); + } + + str_array = g_new (gchar*, n); + as = str_array + n - 1; + *(as--) = NULL; + for (slist = string_list; slist; slist = slist->next) + *(as--) = slist->data; + g_slist_free (string_list); - return retval; + return str_array; } void -g_str_array_free(gchar **strarray) +g_str_array_free (gchar **str_array) { - int i; - - if(strarray == NULL) return; /* Don't use g_return_if_fail, - because this is legal */ - - for(i = 0; strarray[i]; i++) - g_free(strarray[i]); + if (str_array) + { + gchar **as; - g_free(strarray); + for (as = str_array; *as; as++) + g_free (*as); + g_free (str_array); + } } -gchar* g_strconcatv (const gchar *separator, - const gchar **strarray) +gchar* +g_str_array_join (gchar **str_array, + const gchar *separator) { - guint l, sepl; - gchar *concat; - int i; + gchar *string; - g_return_val_if_fail (strarray != NULL, NULL); + g_return_val_if_fail (str_array != NULL, NULL); + g_return_val_if_fail (separator != NULL, NULL); - l = strlen(strarray[0]) + 1; - sepl = strlen(separator); - for(i = 1; strarray[i]; i++) { - l += sepl; - l += strlen(strarray[i]); - } - - concat = g_new (gchar, l); - *concat = '\0'; + if (*str_array) + { + guint len; + guint seperator_len; + gchar **as; - strcat (concat, strarray[0]); - for(i = 1; strarray[i]; i++) { - strcat (concat, separator); - strcat (concat, strarray[i]); - } + seperator_len = strlen (separator); + len = 1 + strlen (*str_array); + for (as = str_array + 1; *as; as++) + len += seperator_len + strlen (*as); + + string = g_new (gchar, len); + *string = 0; + strcat (string, *str_array); + for (as = str_array + 1; *as; as++) + { + strcat (string, separator); + strcat (string, *as); + } + } + else + string = g_strdup (""); - return concat; + return string; } diff --git a/gstrfuncs.c b/gstrfuncs.c index 31283ce..b40bafd 100644 --- a/gstrfuncs.c +++ b/gstrfuncs.c @@ -967,14 +967,14 @@ g_strcasecmp (const gchar *s1, #endif } -void +gchar* g_strdelimit (gchar *string, const gchar *delimiters, gchar new_delim) { register gchar *c; - g_return_if_fail (string != NULL); + g_return_val_if_fail (string != NULL, NULL); if (!delimiters) delimiters = G_STR_DELIMITERS; @@ -984,139 +984,138 @@ g_strdelimit (gchar *string, if (strchr (delimiters, *c)) *c = new_delim; } + + return string; } /* blame Elliot for these next five routines */ -char ** -g_str_split(const gchar *string, const gchar *delim, gint max_tokens) +gchar* +g_strchug (gchar *string) { - /* this could more easily be implemented using a GPtrArray */ - gchar **retval = NULL; - GList *items = NULL, *anode = NULL; - gint numitems = 0, dlen, i; - gchar *src, *cur, *nxt; + gchar *start; - g_return_val_if_fail(string != NULL, NULL); - g_return_val_if_fail(delim != NULL, NULL); + g_return_val_if_fail (string != NULL, NULL); - if(max_tokens < 0) - max_tokens = INT_MAX; + for (start = string; *start && isspace (*start); start++) + ; - dlen = strlen(delim); - nxt = strstr(string, delim); - if(!nxt) { - retval = g_malloc(sizeof(gchar *) * 2); - retval[0] = g_strdup(string); - retval[1] = NULL; - return retval; - } - src = cur = g_strdup(string); - nxt = strstr(src, delim); - - while(nxt && numitems < (max_tokens - 1)) { - *nxt = '\0'; - items = g_list_append(items, g_strdup(cur)); - cur = nxt + dlen; - nxt = strstr(cur, delim); - numitems++; - } - /* We have to take the rest of the string and put it as last token */ - if(*cur) { - items = g_list_append(items, g_strdup(cur)); - numitems++; - } - g_free(src); + strcpy (string, start); - retval = g_malloc(sizeof(gchar *) * (numitems + 1)); - for(anode = items, i = 0; anode; anode = anode->next, i++) - retval[i] = anode->data; - retval[i] = NULL; - g_list_free(items); - - return retval; + return string; } -gchar * -g_str_chug(gchar *astring, gboolean in_place) +gchar* +g_strchomp (gchar *string) { - gchar *retval, *start; + gchar *s; - g_return_val_if_fail(astring != NULL, NULL); + g_return_val_if_fail (string != NULL, NULL); - for(start = astring; *start && isspace(*start); start++) - /* */; + if (!*string) + return string; - if(in_place) { - retval = astring; - g_memmove(retval, start, strlen(start) + 1); - } else - retval = g_strdup(start); + for (s = string + strlen (string) - 1; s >= string && isspace (*s); s--) + *s = '\0'; - return retval; + return string; } -gchar * -g_str_chomp(gchar *astring, gboolean in_place) +gchar** +g_str_array_split (const gchar *string, + const gchar *delimiter, + gint max_tokens) { - int i; - gchar *retval, *end; + GSList *string_list = NULL, *slist; + gchar **str_array, **as, *s; + guint n = 1; - g_return_val_if_fail(astring != NULL, NULL); - - if(in_place) - retval = astring; - else - retval = g_strdup(astring); + g_return_val_if_fail (string != NULL, NULL); + g_return_val_if_fail (delimiter != NULL, NULL); - i = strlen (retval); - if (!i) - return retval; + if (max_tokens < 1) + max_tokens = G_MAXINT; - end = retval + i - 1; - for (; end >= retval && isspace (*end); end--) - *end = '\0'; + s = strstr (string, delimiter); + if (s) + { + guint delimiter_len = strlen (delimiter); + + do + { + guint len; + gchar *new_string; + + len = s - string; + new_string = g_new (gchar, len + 1); + strncpy (new_string, string, len); + new_string[len] = 0; + string_list = g_slist_prepend (string_list, new_string); + n++; + string = s + delimiter_len; + s = strstr (string, delimiter); + } + while (--max_tokens && s); + } + if (*string) + { + n++; + string_list = g_slist_prepend (string_list, g_strdup (string)); + } + + str_array = g_new (gchar*, n); + as = str_array + n - 1; + *(as--) = NULL; + for (slist = string_list; slist; slist = slist->next) + *(as--) = slist->data; + g_slist_free (string_list); - return retval; + return str_array; } void -g_str_array_free(gchar **strarray) +g_str_array_free (gchar **str_array) { - int i; - - if(strarray == NULL) return; /* Don't use g_return_if_fail, - because this is legal */ - - for(i = 0; strarray[i]; i++) - g_free(strarray[i]); + if (str_array) + { + gchar **as; - g_free(strarray); + for (as = str_array; *as; as++) + g_free (*as); + g_free (str_array); + } } -gchar* g_strconcatv (const gchar *separator, - const gchar **strarray) +gchar* +g_str_array_join (gchar **str_array, + const gchar *separator) { - guint l, sepl; - gchar *concat; - int i; + gchar *string; - g_return_val_if_fail (strarray != NULL, NULL); + g_return_val_if_fail (str_array != NULL, NULL); + g_return_val_if_fail (separator != NULL, NULL); - l = strlen(strarray[0]) + 1; - sepl = strlen(separator); - for(i = 1; strarray[i]; i++) { - l += sepl; - l += strlen(strarray[i]); - } - - concat = g_new (gchar, l); - *concat = '\0'; + if (*str_array) + { + guint len; + guint seperator_len; + gchar **as; - strcat (concat, strarray[0]); - for(i = 1; strarray[i]; i++) { - strcat (concat, separator); - strcat (concat, strarray[i]); - } + seperator_len = strlen (separator); + len = 1 + strlen (*str_array); + for (as = str_array + 1; *as; as++) + len += seperator_len + strlen (*as); + + string = g_new (gchar, len); + *string = 0; + strcat (string, *str_array); + for (as = str_array + 1; *as; as++) + { + strcat (string, separator); + strcat (string, *as); + } + } + else + string = g_strdup (""); - return concat; + return string; }