Bug 548612 – g_strstr_len() should use memmem when available
authorBastien Nocera <hadess@hadess.net>
Wed, 27 Aug 2008 23:23:23 +0000 (23:23 +0000)
committerBastien Nocera <hadess@src.gnome.org>
Wed, 27 Aug 2008 23:23:23 +0000 (23:23 +0000)
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

ChangeLog
configure.in
glib/gstrfuncs.c
tests/string-test.c

index 17e779acc9013845870a163d3d41e9b1d13106f8..24089ff3ee7d77d1c45893fa81d24f683bf7ba9f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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>
 
        * glib/giowin32.c: Stylistic changes. Plug an unlikely memory leak
index 2f59af1b7004173fa646ff858b0faa353127c064..a04ef3da6ea8e75072c23371e58c18e37b83bde7 100644 (file)
@@ -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)
 
index ee07cc356a1e66d7f7bc7c2d2f8eb7b00cf9c1df..a1e8c4c4af119327068341c4de2488f522916607 100644 (file)
@@ -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 */
     }
 }
 
index 7da4128f16a5c6019fb283609889e9e2e5a76a69..d8aa728d3464aeb079a087307cb7028d6da32ae3 100644 (file)
@@ -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;
 }