glib r7407 - in trunk: . glib tests



Author: hadess
Date: Wed Aug 27 23:23:23 2008
New Revision: 7407
URL: http://svn.gnome.org/viewvc/glib?rev=7407&view=rev

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



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

Modified: trunk/configure.in
==============================================================================
--- trunk/configure.in	(original)
+++ trunk/configure.in	Wed Aug 27 23:23:23 2008
@@ -559,6 +559,7 @@
 AC_CHECK_FUNCS(posix_memalign)
 AC_CHECK_FUNCS(memalign)
 AC_CHECK_FUNCS(valloc)
+AC_CHECK_FUNCS(memmem)
 
 AC_CHECK_FUNCS(atexit on_exit)
 

Modified: trunk/glib/gstrfuncs.c
==============================================================================
--- trunk/glib/gstrfuncs.c	(original)
+++ trunk/glib/gstrfuncs.c	Wed Aug 27 23:23:23 2008
@@ -2578,7 +2578,9 @@
 /**
  * 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_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 @@
 	}
       
       return NULL;
+#endif /* HAVE_MEMMEM */
     }
 }
 

Modified: trunk/tests/string-test.c
==============================================================================
--- trunk/tests/string-test.c	(original)
+++ trunk/tests/string-test.c	Wed Aug 27 23:23:23 2008
@@ -307,6 +307,11 @@
   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;
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]