diff --git a/ChangeLog b/ChangeLog index 17e779acc..24089ff3e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2008-08-28 Bastien Nocera + + 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 * glib/giowin32.c: Stylistic changes. Plug an unlikely memory leak diff --git a/configure.in b/configure.in index 2f59af1b7..a04ef3da6 100644 --- a/configure.in +++ b/configure.in @@ -559,6 +559,7 @@ AC_CHECK_FUNCS(mmap) AC_CHECK_FUNCS(posix_memalign) AC_CHECK_FUNCS(memalign) AC_CHECK_FUNCS(valloc) +AC_CHECK_FUNCS(memmem) AC_CHECK_FUNCS(atexit on_exit) diff --git a/glib/gstrfuncs.c b/glib/gstrfuncs.c index ee07cc356..a1e8c4c4a 100644 --- a/glib/gstrfuncs.c +++ b/glib/gstrfuncs.c @@ -2578,7 +2578,9 @@ g_strjoin (const gchar *separator, /** * 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 @@ -2595,11 +2597,14 @@ g_strstr_len (const gchar *haystack, { 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; @@ -2626,6 +2631,7 @@ g_strstr_len (const gchar *haystack, } return NULL; +#endif /* HAVE_MEMMEM */ } } diff --git a/tests/string-test.c b/tests/string-test.c index 7da4128f1..d8aa728d3 100644 --- a/tests/string-test.c +++ b/tests/string-test.c @@ -307,6 +307,11 @@ main (int argc, 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; }