More on GStrings



On Mon, Jul 31, 2000 at 10:40:48AM -0700, Darin Adler wrote:

> As someone spending the last few months learning glib and gtk I think the
> boolean to g_string_free is one of the more confusing interfaces in glib
> (even though it's a tiny minor point). I *hate* functions where you pass a
> boolean to make it do two different jobs. I think making a better interface
> here would be a nice little improvement.
> 
> Yes, glib could use a function that frees the GString and returns the
> pointer. I'd prefer a new function (perhaps we can eventually phase out
> g_string_free(FALSE) completely), but adding the return value to
> g_string_free would be OK. What I don't like about a return value for
> g_string_free is that it confuses the issue even further by introducing the
> question of what g_string_free returns when you pass TRUE.

The boolean argument in g_string_free is strange at first,
but is and orthogonal with the free arguments for GArray and GPtrArray.


But while we're on the subject GString...  I've been preallocating big
GStrings and sprintfa'ing into them for awhile.  While reading the source
for gtk+ 1.2.8 the other day, I noticed that it does a g_strdup under the
covers.  This seems unnecessary: once we know the sting buffer is big
enough, we can vsprintf directly to the buffer.

So instead of

	static void
	g_string_sprintfa_int (GString     *string,
			       const gchar *fmt,
			       va_list      args)
	{
		gchar *buffer;

		buffer = g_strdup_vprintf (fmt, args);
		g_string_append (string, buffer);
		g_free (buffer);
	}

we would have something like

	static void
	g_string_sprintfa_int (GString     * fstring,
			       const gchar * fmt,
			       va_list       fargs)
	{
		GRealString * string = (GRealString*)fstring;
		guint upper_bound;
		va_list args2;

		G_VA_COPY (args2, args);
		upper_bound = g_printf_string_upper_bound (fmt, args);
		g_string_maybe_expand (string, string->len + upper_bound);

		vsprintf (buffer + buffer->len, format, args2);
		va_end (args2);
	}

which I haven't tested but should give the idea.

cheers,
Charles




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