Re: has_prefix and has_suffix proposal for glib 2.2
- From: Alex Larsson <alexl redhat com>
- To: Evan Martin <martine cs washington edu>
- Cc: gtk-devel-list gnome org
- Subject: Re: has_prefix and has_suffix proposal for glib 2.2
- Date: Mon, 6 May 2002 10:11:59 -0400 (EDT)
On Sun, 5 May 2002, Evan Martin wrote:
> On Fri, May 03, 2002 at 05:15:40PM -0400, Alex Larsson wrote:
> > + str_len = strlen (str);
> > + suffix_len = strlen (suffix);
> > +
> > + if (str_len < suffix_len)
> > + return FALSE;
> > +
> > + return strcmp (str + str_len - suffix_len, suffix) == 0;
>
> Isn't this sort of arithmetic not UTF-8 safe?
> Or are of none the GTK functions of this nature intended to be?
It is safe if both of the strings are valid UTF-8. In fact it is correct
for all sane encodings if the strings passed are valid strings of the
same encoding.
> > +gboolean
> > +g_str_has_prefix (const gchar *str,
> > + const gchar *prefix)
> > +{
> > + int str_len;
> > + int prefix_len;
> > +
> > + g_return_val_if_fail (str != NULL, FALSE);
> > + g_return_val_if_fail (prefix != NULL, FALSE);
> > +
> > + str_len = strlen (str);
> > + prefix_len = strlen (prefix);
> > +
> > + if (str_len < prefix_len)
> > + return FALSE;
> > +
> > + return strncmp (str, prefix, prefix_len) == 0;
>
> Wouldn't it be faster to not calculate the lengths? That requires
> iterating through both of the strings once before even comparing them.
> I guess if you're worried about speed you wouldn't use strncmp because
> you're only concerned with whether they match...
>
> Well, I stand by my first comment, at least. ;)
Perhaps we should not calculate the length of the first string. That's not
strictly necessary, and it may be long. But I doubt it will matter much
for real life usage of this function.
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Alexander Larsson Red Hat, Inc
alexl redhat com alla lysator liu se
He's an all-American Amish messiah on the run. She's a strong-willed impetuous
research scientist from aristocratic European stock. They fight crime!
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]