memmem: rearrange memmem and expand memmem-simple modules
authorPádraig Brady <P@draigBrady.com>
Tue, 22 Jun 2010 23:42:54 +0000 (00:42 +0100)
committerPádraig Brady <P@draigBrady.com>
Wed, 15 Dec 2010 22:14:31 +0000 (22:14 +0000)
Move all functional checks to memmem-simple so that one has
a fully functional memmem by using just this module.
Restrict the memmem module to performance checks only.
Document exactly how the memmem and memmem-simple modules
relate to each other.

* m4/memmem.m4 (gl_FUNC_MEMMEM_SIMPLE, gl_FUNC_MEMMEM): Move the
empty needle check from the memmem module to memmem-simple.
Also expand the empty needle check to ensure the correct
pointer is returned, not just a non NULL pointer.
In gl_FUNC_MEMMEM, make the cross compilation check only
consider the versions with performance issues.
* doc/glibc-functions/memmem.texi: Rearrange the portability
documentation to correlate with the rearranged checks.

ChangeLog
doc/glibc-functions/memmem.texi
m4/memmem.m4

index 5a65773..4d83a57 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,18 @@
 2010-12-15  Pádraig Brady <P@draigBrady.com>
+
+       memmem, memmem-simple: reorganize and expand empty needle check
+       * m4/memmem.m4 (gl_FUNC_MEMMEM_SIMPLE, gl_FUNC_MEMMEM): Move all
+       functional checks to memmem-simple so that one has a fully functional
+       memmem by using just this module.
+       Restrict the performance only check to the memmem module.
+       Also expand the empty needle check to ensure the correct
+       pointer is returned, not just a non NULL pointer.
+       * doc/glibc-functions/memmem.texi: Rearrange the portability
+       documentation to correlate with the rearranged checks.
+       Clarify exactly how the memmem and memmem-simple modules
+       relate to each other.
+
+2010-12-15  Pádraig Brady <P@draigBrady.com>
             Bruno Haible  <bruno@clisp.org>
 
        Improve cross-compilation guesses for uClibc.
index 60cb174..8911756 100644 (file)
@@ -4,6 +4,10 @@
 
 Gnulib module: memmem or memmem-simple
 
+Both modules implement the same replacement for the @code{memmem} function
+with the memmem module providing a replacement on more platforms where
+the existing @code{memmem} function has a quadratic worst-case complexity.
+
 Portability problems fixed by either Gnulib module @code{memmem-simple}
 or @code{memmem}:
 @itemize
@@ -17,18 +21,20 @@ Linux libc 5.0.9
 This function can trigger false positives for long periodic needles on
 some platforms:
 glibc 2.12, Cygwin 1.7.7.
-@end itemize
 
-Portability problems fixed by Gnulib module @code{memmem}:
-@itemize
 @item
 This function returns incorrect values in some cases, such as when
 given an empty needle:
 glibc <= 2.0, Cygwin 1.5.x.
+@end itemize
+
+Performance problems fixed by Gnulib module @code{memmem}:
+@itemize
 @item
 This function has quadratic instead of linear worst-case complexity on some
 platforms:
 glibc 2.8, FreeBSD 6.2, NetBSD 5.0, AIX 5.1, Solaris 11 2010-11, Cygwin 1.5.x.
+Note for small needles the replacement may be slower.
 @end itemize
 
 Portability problems not fixed by Gnulib:
index e3bb83b..0ad18e1 100644 (file)
@@ -5,7 +5,7 @@ dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
 
-dnl Check that memmem is present.
+dnl Check that memmem is present and functional.
 AC_DEFUN([gl_FUNC_MEMMEM_SIMPLE],
 [
   dnl Persuade glibc <string.h> to declare memmem().
@@ -18,6 +18,7 @@ AC_DEFUN([gl_FUNC_MEMMEM_SIMPLE],
     HAVE_DECL_MEMMEM=0
   else
     dnl Detect http://sourceware.org/bugzilla/show_bug.cgi?id=12092.
+    dnl Also check that we handle empty needles correctly.
     AC_CACHE_CHECK([whether memmem works],
       [gl_cv_func_memmem_works_always],
       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
@@ -25,21 +26,31 @@ AC_DEFUN([gl_FUNC_MEMMEM_SIMPLE],
 #define P "_EF_BF_BD"
 #define HAYSTACK "F_BD_CE_BD" P P P P "_C3_88_20" P P P "_C3_A7_20" P
 #define NEEDLE P P P P P
-]], [[return !!memmem (HAYSTACK, strlen (HAYSTACK), NEEDLE, strlen (NEEDLE));
+]], [[
+    int result = 0;
+    if (memmem (HAYSTACK, strlen (HAYSTACK), NEEDLE, strlen (NEEDLE)))
+      result |= 1;
+    /* Check for empty needle behavior.  */
+    {
+      char* haystack="AAA";
+      if (memmem (haystack, 3, 0, 0) != haystack)
+        result |= 2;
+    }
+    return result;
     ]])],
         [gl_cv_func_memmem_works_always=yes],
         [gl_cv_func_memmem_works_always=no],
-        [dnl glibc 2.12 and cygwin 1.7.7 have a known bug.  uClibc is not
-         dnl affected, since it uses different source code for memmem than
-         dnl glibc.
-         dnl Assume that it works on all other platforms, even if it is not
-         dnl linear.
+        [dnl glibc 2.12 and cygwin 1.7.7 have issue #12092 above.
+         dnl Also empty needles work on glibc >= 2.1 and cygwin >= 1.7.0
+         dnl uClibc is not affected, since it uses different source code.
+         dnl Assume that it works on all other platforms (even if not linear).
          AC_EGREP_CPP([Lucky user],
            [
 #ifdef __GNU_LIBRARY__
  #include <features.h>
- #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \
-     || defined __UCLIBC__
+ #if ((__GLIBC__ == 2 && ((__GLIBC_MINOR > 0 && __GLIBC_MINOR__ < 9) \
+                          || __GLIBC_MINOR__ > 12)) \
+     || (__GLIBC__ > 2)) || defined __UCLIBC__
   Lucky user
  #endif
 #elif defined __CYGWIN__
@@ -63,7 +74,7 @@ AC_DEFUN([gl_FUNC_MEMMEM_SIMPLE],
   gl_PREREQ_MEMMEM
 ]) # gl_FUNC_MEMMEM_SIMPLE
 
-dnl Additionally, check that memmem is efficient and handles empty needles.
+dnl Additionally, check that memmem has linear performance characteristics
 AC_DEFUN([gl_FUNC_MEMMEM],
 [
   AC_REQUIRE([gl_FUNC_MEMMEM_SIMPLE])
@@ -95,26 +106,23 @@ static void quit (int sig) { exit (sig + 128); }
         if (!memmem (haystack, 2 * m + 1, needle, m + 1))
           result |= 1;
       }
-    /* Check for empty needle behavior.  */
-    if (!memmem ("a", 1, 0, 0))
-      result |= 2;
     return result;
     ]])],
         [gl_cv_func_memmem_works_fast=yes], [gl_cv_func_memmem_works_fast=no],
-        [dnl Only glibc > 2.12 and cygwin > 1.7.7 are known to have a
-         dnl bug-free memmem that works in linear time.
+        [dnl Only glibc >= 2.9 and cygwin > 1.7.0 are known to have a
+         dnl memmem that works in linear time.
          AC_EGREP_CPP([Lucky user],
            [
 #include <features.h>
 #ifdef __GNU_LIBRARY__
- #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \
+ #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9) || (__GLIBC__ > 2)) \
      && !defined __UCLIBC__
   Lucky user
  #endif
 #endif
 #ifdef __CYGWIN__
  #include <cygwin/version.h>
- #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
+ #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 0)
   Lucky user
  #endif
 #endif