g_str_has_prefix: don't call strlen(str)

There's no reason to check the length of @str in g_str_has_prefix(),
since if it's shorter than @prefix, the strncmp() will fail anyway.
And besides making the function less efficient, it also breaks code
like:

    if (buf->len >=3 && g_str_has_prefix (buf->data, "foo"))
      ...

which really looks like it ought to work whether buf->data is
nul-terminated or not.

https://bugzilla.gnome.org/show_bug.cgi?id=727890
This commit is contained in:
Dan Winship 2014-04-09 09:57:46 -04:00
parent 0e44b29340
commit eec507c159

View File

@ -2794,19 +2794,10 @@ 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;
return strncmp (str, prefix, strlen (prefix)) == 0;
}
/**