Re: Some string parsing functions



Hongli Lai <hongli@telekabel.nl> writes:

> When I was developing The Garp (garp.sourceforge.net), I encountered
> a problem.  There are no functions in Glib or Glibc to find, delete,
> insert and replace substrings.  I asked the people at
> gtk-app-devel-list@gnome.org, but they say there isn't such thing in
> Glib yet.  Havoc told me this would be nice to add to the TODO list
> of Glib 1.4.

Thanks a lot for the contributions - I'm not sure that we'd
want to add all of these functions - I'd rather not duplicate
standard C functionality with only a small change in interface.

But there are most likely some things here we should add. GLib's
string manipulation is still rather weak.

> But here's the good news: no need to add that to TODO :-)
> I implemented all those functions and tested them.
> I attached the file in this email. Please take a look at it.
 
> I hope my functions will be included in Glib.
 
> gint g_str_get_length (const gchar *str);

strlen()

> gchar *g_str_find_substr (const gchar *str, const gchar *str_to_find);

I don't quite understand the point of the way you implemented - your
semantics seem to be:

 if (strstr(str, str_to_find))
   return g_strdup (str_to_find);

I'd say, just have people use strstr().

> gchar *g_str_find_substr_right (const gchar *str, const gchar *str_to_find);

Since it isn't in ANSI C a g_strrstr() would be useful.

> gint g_str_find_substr_index (const gchar *str, const gchar *str_to_find);

strstr() is close enough and standard. 

> gint g_str_find_substr_index_start_from (const gchar *str, guint startpos, const gchar *str_to_find);

strstr (str + starpos, str_to_find)

> gint g_str_find_substr_index_right (const gchar *str, const gchar *str_to_find);

> gchar *g_str_copy_substr (const gchar *str, guint index, guint count);

g_strndup (str + index, count);

> gchar *g_str_copy_substr_index (const gchar *str, guint startpos, guint endpos);
 
g_strndup (str + starpos, endpos - startpos);

> /* String manipulation functions */
> gchar *g_str_delete_substr (const gchar *str, guint index, guint count);
> gchar *g_str_insert_substr (const gchar *str, guint index, const gchar *substr_to_insert);

This might be useful, but I think I would just encourage people to use
GString for this sort of thing. GString can be more efficient for
changing strings in place, especially if a number of substitutions are
done in sequence.

> gchar *g_str_replace_substr (const gchar *str, const gchar *substr_to_replace, const gchar *replace_with);
> gchar *g_str_replace_substr_start_from (const gchar *str, guint startpos, const gchar *substr_to_replace, const gchar *replace_with);
> gchar *g_str_replace_substr_right (const gchar *str, const gchar *substr_to_replace, const gchar *replace_with);
> gchar *g_str_replace_substr_all (const gchar *str, const gchar *substr_to_replace, const gchar *replace_with);

I'd prefer to see these as operations on GString. I'm not sure
about the _start_from() variant - if it is useful, then it would
be better to make all the functions take a start_pos, and maybe
an end_pos argument. 

Actually, I'm a little hesitant to add these functions at all- 
we will eventually have regular expressions in GLib, so this
will just be a small special case. But it may be useful, since
at this point, lacking a good utf-8 capable regular expression
library, we probably aren't going to get regular expressions
into GLib-2.0. 

What do other people think?

Regards,
                                        Owen 







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