Re: Some new GString functions - constructors



On Tue, Dec 20, 2005 at 06:31:01PM -0500, Owen Taylor wrote:
> At a high-level, I think there is a question of "what GString is" - 
> should there be GString versions of everything you want to do 
> with a char *?
> 
> My opinion is no ... a GString is the equivalent of a Java 
> StringBuffer .. it is a useful way to build and work with strings,
> but it isn't *a string*. That's a char *.
> 
> It would be for example, wrong to pass a GString as an argument
> to a function that takes a string as an input parameter.

Yes; I do see your point here... There is after all, no point in
defining an operation to printf() a GString, when you might as well just
use %s with s->str.

> That implies to me, for example:
> 
>  GList *g_string_split(GString *str, gchar c);
> 
> Is wrong ... we already have g_strsplit():

Ahh.. That would miss half the point of my intention with this function.

It is true that this function takes, as input, a GString. But that
wasn't its raison d'etre. The point of this new function is its return
value. Yes, we already have g_strsplit(). This returns a NULL-terminated
array of NULL-terminated gchar* strings. My intended point of
g_string_split() would be that it would return a collection of GString*
objects. The convenience is that it wraps up the strings as GString*s.

That said, I don't see why we need a new split function, on reflection.
Maybe a:

  GString** g_string_new_strv(gchar** a);

would be useful; it would return a NULL-terminated array of new
GString*s, each one wrapping a string from the original array. Thus, to
form a GString** one could

  gchar** sv = g_strsplit(original_string->str, delim);
  GString** gsv = g_string_new_strv(sv);
  g_strfreev(sv);

So easily providing what I intended for g_string_split(), but being less
added code, and more useful.

That said, I'm still not 100% happy with a NULL-terminated GString*
array. Other threads give alternative ideas I have for these.

> (Sidenote: g_strsplit() takes a char * delimiter. This has 
> the big benefit of handling UTF-8 and ascii equally well.)

Ah yes; a most useful property.

-- 
Paul "LeoNerd" Evans

leonerd leonerd org uk
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/

Attachment: signature.asc
Description: Digital signature



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