Fix new strfuncs back up (again) - No, incrementing pointers in a loop is

Fix new strfuncs back up (again)
	- No, incrementing pointers in a loop is not any faster than
	  indexing an array in a loop with a good compiler, but it is
	  harder to read.
	- strconcat doesn't allow a separator - added g_str_array_join,
	  renamed g_str_array_join to g_str_array_joinv
	- join routines take separator as first argument, as is customary.
This commit is contained in:
Elliot Lee 1998-10-21 20:14:16 +00:00
parent 634e4a58c6
commit daf46f9550
4 changed files with 158 additions and 46 deletions

8
glib.h
View File

@ -1377,15 +1377,17 @@ gchar* g_strconcat (const gchar *string1,
/* NULL terminated string arrays. /* NULL terminated string arrays.
* g_str_array_split() splits up string into max_tokens tokens at delim and * g_str_array_split() splits up string into max_tokens tokens at delim and
* returns a newly allocated string array. * returns a newly allocated string array.
* g_str_array_join() concatenates all of str_array's strings, sliding in an * g_str_array_joinv() concatenates all of str_array's strings, sliding in an
* optional separator, the returned string is newly allocated. * optional separator, the returned string is newly allocated.
* g_str_array_free() frees the array itself and all of its strings. * g_str_array_free() frees the array itself and all of its strings.
*/ */
gchar** g_str_array_split (const gchar *string, gchar** g_str_array_split (const gchar *string,
const gchar *delimiter, const gchar *delimiter,
gint max_tokens); gint max_tokens);
gchar* g_str_array_join (gchar **str_array, gchar* g_str_array_joinv (const gchar *separator,
const gchar *separator); const gchar **str_array);
gchar* g_str_array_join (const gchar *separator,
...);
void g_str_array_free (gchar **str_array); void g_str_array_free (gchar **str_array);

View File

@ -1377,15 +1377,17 @@ gchar* g_strconcat (const gchar *string1,
/* NULL terminated string arrays. /* NULL terminated string arrays.
* g_str_array_split() splits up string into max_tokens tokens at delim and * g_str_array_split() splits up string into max_tokens tokens at delim and
* returns a newly allocated string array. * returns a newly allocated string array.
* g_str_array_join() concatenates all of str_array's strings, sliding in an * g_str_array_joinv() concatenates all of str_array's strings, sliding in an
* optional separator, the returned string is newly allocated. * optional separator, the returned string is newly allocated.
* g_str_array_free() frees the array itself and all of its strings. * g_str_array_free() frees the array itself and all of its strings.
*/ */
gchar** g_str_array_split (const gchar *string, gchar** g_str_array_split (const gchar *string,
const gchar *delimiter, const gchar *delimiter,
gint max_tokens); gint max_tokens);
gchar* g_str_array_join (gchar **str_array, gchar* g_str_array_joinv (const gchar *separator,
const gchar *separator); const gchar **str_array);
gchar* g_str_array_join (const gchar *separator,
...);
void g_str_array_free (gchar **str_array); void g_str_array_free (gchar **str_array);

View File

@ -1026,8 +1026,8 @@ g_str_array_split (const gchar *string,
gint max_tokens) gint max_tokens)
{ {
GSList *string_list = NULL, *slist; GSList *string_list = NULL, *slist;
gchar **str_array, **as, *s; gchar **str_array, *s;
guint n = 1; guint i, n = 1;
g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (string != NULL, NULL);
g_return_val_if_fail (delimiter != NULL, NULL); g_return_val_if_fail (delimiter != NULL, NULL);
@ -1063,10 +1063,13 @@ g_str_array_split (const gchar *string,
} }
str_array = g_new (gchar*, n); str_array = g_new (gchar*, n);
as = str_array + n - 1;
*(as--) = NULL; i = n - 1;
str_array[i--] = NULL;
for (slist = string_list; slist; slist = slist->next) for (slist = string_list; slist; slist = slist->next)
*(as--) = slist->data; str_array[i--] = slist->data;
g_slist_free (string_list); g_slist_free (string_list);
return str_array; return str_array;
@ -1077,41 +1080,43 @@ g_str_array_free (gchar **str_array)
{ {
if (str_array) if (str_array)
{ {
gchar **as; int i;
for(i = 0; str_array[i] != NULL; i++)
g_free(str_array[i]);
for (as = str_array; *as; as++)
g_free (*as);
g_free (str_array); g_free (str_array);
} }
} }
gchar* gchar*
g_str_array_join (gchar **str_array, g_str_array_joinv (const gchar *separator,
const gchar *separator) const gchar **str_array)
{ {
gchar *string; gchar *string;
g_return_val_if_fail (str_array != NULL, NULL); g_return_val_if_fail (str_array != NULL, NULL);
g_return_val_if_fail (separator != NULL, NULL);
if(separator == NULL)
separator = "";
if (*str_array) if (*str_array)
{ {
guint len; guint i, len;
guint seperator_len; guint separator_len;
gchar **as;
seperator_len = strlen (separator); separator_len = strlen (separator);
len = 1 + strlen (*str_array); len = 1 + strlen (str_array[0]);
for (as = str_array + 1; *as; as++) for(i = 1; str_array[i] != NULL; i++)
len += seperator_len + strlen (*as); len += separator_len + strlen(str_array[i]);
string = g_new (gchar, len); string = g_new (gchar, len);
*string = 0; *string = 0;
strcat (string, *str_array); strcat (string, *str_array);
for (as = str_array + 1; *as; as++) for (i = 1; str_array[i] != NULL; i++)
{ {
strcat (string, separator); strcat (string, separator);
strcat (string, *as); strcat (string, str_array[i]);
} }
} }
else else
@ -1119,3 +1124,52 @@ g_str_array_join (gchar **str_array,
return string; return string;
} }
gchar*
g_str_array_join (const gchar *separator,
...)
{
gchar *string, *s;
va_list args;
guint len;
guint separator_len;
if(separator == NULL)
separator = "";
separator_len = strlen (separator);
va_start(args, separator);
s = va_arg(args, gchar *);
if(s) {
len = strlen(s) + 1;
while((s = va_arg(args, gchar*)))
{
len += separator_len + strlen(s);
}
va_end(args);
string = g_new (gchar, len);
va_start(args, separator);
*string = 0;
s = va_arg(args, gchar*);
strcat (string, s);
while((s = va_arg(args, gchar*)))
{
strcat(string, separator);
strcat(string, s);
}
} else
string = g_strdup("");
va_end(args);
return string;
}

View File

@ -1026,8 +1026,8 @@ g_str_array_split (const gchar *string,
gint max_tokens) gint max_tokens)
{ {
GSList *string_list = NULL, *slist; GSList *string_list = NULL, *slist;
gchar **str_array, **as, *s; gchar **str_array, *s;
guint n = 1; guint i, n = 1;
g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (string != NULL, NULL);
g_return_val_if_fail (delimiter != NULL, NULL); g_return_val_if_fail (delimiter != NULL, NULL);
@ -1063,10 +1063,13 @@ g_str_array_split (const gchar *string,
} }
str_array = g_new (gchar*, n); str_array = g_new (gchar*, n);
as = str_array + n - 1;
*(as--) = NULL; i = n - 1;
str_array[i--] = NULL;
for (slist = string_list; slist; slist = slist->next) for (slist = string_list; slist; slist = slist->next)
*(as--) = slist->data; str_array[i--] = slist->data;
g_slist_free (string_list); g_slist_free (string_list);
return str_array; return str_array;
@ -1077,41 +1080,43 @@ g_str_array_free (gchar **str_array)
{ {
if (str_array) if (str_array)
{ {
gchar **as; int i;
for(i = 0; str_array[i] != NULL; i++)
g_free(str_array[i]);
for (as = str_array; *as; as++)
g_free (*as);
g_free (str_array); g_free (str_array);
} }
} }
gchar* gchar*
g_str_array_join (gchar **str_array, g_str_array_joinv (const gchar *separator,
const gchar *separator) const gchar **str_array)
{ {
gchar *string; gchar *string;
g_return_val_if_fail (str_array != NULL, NULL); g_return_val_if_fail (str_array != NULL, NULL);
g_return_val_if_fail (separator != NULL, NULL);
if(separator == NULL)
separator = "";
if (*str_array) if (*str_array)
{ {
guint len; guint i, len;
guint seperator_len; guint separator_len;
gchar **as;
seperator_len = strlen (separator); separator_len = strlen (separator);
len = 1 + strlen (*str_array); len = 1 + strlen (str_array[0]);
for (as = str_array + 1; *as; as++) for(i = 1; str_array[i] != NULL; i++)
len += seperator_len + strlen (*as); len += separator_len + strlen(str_array[i]);
string = g_new (gchar, len); string = g_new (gchar, len);
*string = 0; *string = 0;
strcat (string, *str_array); strcat (string, *str_array);
for (as = str_array + 1; *as; as++) for (i = 1; str_array[i] != NULL; i++)
{ {
strcat (string, separator); strcat (string, separator);
strcat (string, *as); strcat (string, str_array[i]);
} }
} }
else else
@ -1119,3 +1124,52 @@ g_str_array_join (gchar **str_array,
return string; return string;
} }
gchar*
g_str_array_join (const gchar *separator,
...)
{
gchar *string, *s;
va_list args;
guint len;
guint separator_len;
if(separator == NULL)
separator = "";
separator_len = strlen (separator);
va_start(args, separator);
s = va_arg(args, gchar *);
if(s) {
len = strlen(s) + 1;
while((s = va_arg(args, gchar*)))
{
len += separator_len + strlen(s);
}
va_end(args);
string = g_new (gchar, len);
va_start(args, separator);
*string = 0;
s = va_arg(args, gchar*);
strcat (string, s);
while((s = va_arg(args, gchar*)))
{
strcat(string, separator);
strcat(string, s);
}
} else
string = g_strdup("");
va_end(args);
return string;
}