Re: Some new GString functions - constructors



On Tue, Dec 20, 2005 at 03:26:45PM -0800, Alan M. Evans wrote:
> > Not entirely certain I like this. See below.
> > 
> > >   GStringList* g_string_split(GString *str, gchar c);
> > 
> > Seems to me this should return a gchar** a-la g_strsplit().
> 
> I take that back. I can always use g_strsplit() on the GString contents.
> On further consideration, I suppose that your GList contains GStrings,
> not gchar pointers. Still not certain that g_string_split() is the best
> name. And not certain that GList is the best container for the returned
> data. Do you really not mind 24 bytes of overhead for every returned
> token? (12 bytes for GList + 12 bytes for GString)

Yes; I realised this would be quite a contentious issue; I wanted to
throw it open for debate.

My gut feeling, as I said in the other thread, is that a raw gchar**
doesn't really feel right. We're dealing with a GString "nice shiney
wrapped type" here...

Though I do take your point of the overhead of a GList.

Any number of possible ways to do this now strike me:

  GPtrArray* g_string_split_array();
  
  GString** g_string_split_strv();  // returns NULL-terminated array
  
Or, we could at this juncture try something "new and funky".

Suppose instead we could do this:

  GString str;
  g_string_init(&str);
  g_string_append_len(...);

I.e. keep the GString struct itself as a real variable, rather than just
throwing pointers to it about the place, then we could construct a

  GArray* g_string_split_array();

To return an array of GString structs themselves, rather than GString*
pointers. This would also have other benefits, like being able to
contain a plain GString struct inside a larger struct type.

Compare

  struct {
    ...
    GString* name;
  };

  printf("My name is %s\n", me->name->str);

vs:

  struct {
    ...
    GString name;
  };

  printf("My name is %s\n", me.name->str);


If you could use a bare GString inside a larger structure like that, it
would have immediate savings in terms of memory allocation overheads,
pointer dereference overheads, and general code neatness.

This idea would require two new functions:

  void g_string_init(GString *str);
  void g_string_fini(GString *str);

Which would do most of the work of g_string_new() and g_string_free()
respectively, apart from the initial struct allocation / final struct
free().

Thoughts on that one?

-- 
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]