Re: has_prefix and has_suffix proposal for glib 2.2



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?


> +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. ;)

-- 
      Evan Martin
martine cs washington edu
  http://neugierig.org



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