Bug 548612 – g_strstr_len() should use memmem when available

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()


svn path=/trunk/; revision=7407
This commit is contained in:
Bastien Nocera 2008-08-27 23:23:23 +00:00 committed by Bastien Nocera
parent 32947a3b4a
commit 5e2a6047ea
4 changed files with 23 additions and 2 deletions

View File

@ -1,3 +1,12 @@
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> 2008-08-27 Tor Lillqvist <tml@novell.com>
* glib/giowin32.c: Stylistic changes. Plug an unlikely memory leak * glib/giowin32.c: Stylistic changes. Plug an unlikely memory leak

View File

@ -559,6 +559,7 @@ AC_CHECK_FUNCS(mmap)
AC_CHECK_FUNCS(posix_memalign) AC_CHECK_FUNCS(posix_memalign)
AC_CHECK_FUNCS(memalign) AC_CHECK_FUNCS(memalign)
AC_CHECK_FUNCS(valloc) AC_CHECK_FUNCS(valloc)
AC_CHECK_FUNCS(memmem)
AC_CHECK_FUNCS(atexit on_exit) AC_CHECK_FUNCS(atexit on_exit)

View File

@ -2578,7 +2578,9 @@ g_strjoin (const gchar *separator,
/** /**
* g_strstr_len: * g_strstr_len:
* @haystack: a string. * @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. * @needle: the string to search for.
* *
* Searches the string @haystack for the first occurrence * 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 (haystack != NULL, NULL);
g_return_val_if_fail (needle != NULL, NULL); g_return_val_if_fail (needle != NULL, NULL);
if (haystack_len < 0) if (haystack_len < 0)
return strstr (haystack, needle); return strstr (haystack, needle);
else else
{ {
#ifdef HAVE_MEMMEM
return memmem (haystack, haystack_len, needle, strlen (needle));
#else
const gchar *p = haystack; const gchar *p = haystack;
gsize needle_len = strlen (needle); gsize needle_len = strlen (needle);
const gchar *end; const gchar *end;
@ -2626,6 +2631,7 @@ g_strstr_len (const gchar *haystack,
} }
return NULL; return NULL;
#endif /* HAVE_MEMMEM */
} }
} }

View File

@ -307,6 +307,11 @@ main (int argc,
g_assert (strcmp (tmp_string, "b a") == 0); g_assert (strcmp (tmp_string, "b a") == 0);
g_free (tmp_string); 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; return 0;
} }