[glib] g_str_has_prefix: don't call strlen(str)
- From: Dan Winship <danw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] g_str_has_prefix: don't call strlen(str)
- Date: Thu, 10 Apr 2014 14:10:52 +0000 (UTC)
commit eec507c15987388ae624eada236032e700bf8c50
Author: Dan Winship <danw gnome org>
Date: Wed Apr 9 09:57:46 2014 -0400
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
glib/gstrfuncs.c | 11 +----------
1 files changed, 1 insertions(+), 10 deletions(-)
---
diff --git a/glib/gstrfuncs.c b/glib/gstrfuncs.c
index e9b13c5..0c50dfb 100644
--- a/glib/gstrfuncs.c
+++ b/glib/gstrfuncs.c
@@ -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;
}
/**
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]