Re: Why no g_string_append_vprintf() ?

On Wed, Dec 21, 2005 at 10:28:50AM -0800, Alan M. Evans wrote:
> A patch was already submitted in January. Still UNCONFIRMED.

Yes, I've had a look at that patch. It's not very efficient; it
allocates a new string buffer big enough to hold the string, then fills
it, then strdup()s it into the GString, then frees the first buffer.
That's unnecessary.

My method is:

void g_string_append_vprintf(GString *str, const gchar *fmt, va_list args)
  gsize len = g_printf_string_upper_bound(fmt, args) + 1;
  g_string_maybe_expand(str, len);
  g_vsnprintf(str->str + str->len, len, fmt, args);
  str->len += len;

This doesn't double-allocate the buffer and then incur an unnecessary

We can then trivially wrap

void g_string_vprintf(GString *str, const gchar* fmt, va_list args)
  g_string_truncate(str, 0);
  g_string_append_vprintf(str, fmt, args);

Finally this would allow another function I added, because I noticed I
keep doing:

  GString *str = g_string_new(NULL);
  g_string_append_printf(str, fmt, ...);
  return str;

Instead, we just

  return g_string_new_printf(fmt, ...);

Any thoughts on these? Should I submit a patch containing that lot?

Paul "LeoNerd" Evans

leonerd leonerd org uk
ICQ# 4135350       |  Registered Linux# 179460

Attachment: signature.asc
Description: Digital signature

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