+2008-08-28 Bastien Nocera <hadess@hadess.net>
+
+ Bug 548612 – g_strstr_len() should use memmem when available
+
+ * configure.in: detect whether memmem is available in the C library
+ * glib/gstrfuncs.c (g_strstr_len): use memmem for g_strstr_len() if
+ available in it's available, as it could be optimised by the C library
+ * tests/string-test.c (main): Add a few tests for g_strstr_len()
+
2008-08-27 Tor Lillqvist <tml@novell.com>
* glib/giowin32.c: Stylistic changes. Plug an unlikely memory leak
/**
* g_strstr_len:
* @haystack: a string.
- * @haystack_len: the maximum length of @haystack.
+ * @haystack_len: the maximum length of @haystack. Note that -1 is
+ * a valid length, if @haystack is nul-terminated, meaning it will
+ * search through the whole string.
* @needle: the string to search for.
*
* Searches the string @haystack for the first occurrence
{
g_return_val_if_fail (haystack != NULL, NULL);
g_return_val_if_fail (needle != NULL, NULL);
-
+
if (haystack_len < 0)
return strstr (haystack, needle);
else
{
+#ifdef HAVE_MEMMEM
+ return memmem (haystack, haystack_len, needle, strlen (needle));
+#else
const gchar *p = haystack;
gsize needle_len = strlen (needle);
const gchar *end;
}
return NULL;
+#endif /* HAVE_MEMMEM */
}
}
g_assert (strcmp (tmp_string, "b a") == 0);
g_free (tmp_string);
+ tmp_string = g_strdup (GLIB_TEST_STRING);
+ g_assert (g_strstr_len (tmp_string, 4, "rado") == NULL);
+ g_assert (g_strstr_len (tmp_string, -1, "rado") == tmp_string + 5);
+ g_free (tmp_string);
+
return 0;
}