glib String function suggestion



This has nothing to do with GTK as such, but I can't find a more appropriate
place to post about glib.

Trying to hammer out some code using a bunch of GStrings, I ran across a
case where I wanted to concatenate them into a new GString, separated by a
delimiter - much like g_strjoin.  To my astonishment, this function did not
yet exist, so I hacked it up myself (okay, more cut and paste than true
hacking, but what the heck).  Here it is... I humbly recommend it to be
added to glib in future releases.  But then again, I'm just a lowly
neo-hacker, so what do I know?  ;)

/* g_string_join - concatenates zero or more GStrings 
   into a new GString, separated by an optional separator */
GString* g_string_join (const gchar *separator, ...) {
  GString *string, *s;
  va_list args;

  if (separator == NULL)
    separator = "";

  string = g_string_new ("");

  va_start (args, separator);
  s = va_arg (args, GString*);
  if (s) {
    g_string_append (string, s->str);
    s = va_arg (args, GString*);
    while (s) {
      g_string_append (string, separator);
      g_string_append (string, s->str);
      s = va_arg (args, GString*);
    }
  }

  va_end (args);

  return string;
}



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