2 dnl Copyright (C) 2008 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
7 dnl Check that strstr is efficient.
8 AC_DEFUN([gl_FUNC_STRSTR],
10 AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
11 AC_CACHE_CHECK([whether strstr works in linear time],
12 [gl_cv_func_strstr_linear],
13 [AC_RUN_IFELSE([AC_LANG_PROGRAM([
14 #include <string.h> /* for memmem */
15 #include <stdlib.h> /* for malloc */
16 #include <unistd.h> /* for alarm */
17 ], [[size_t m = 1000000;
18 char *haystack = (char *) malloc (2 * m + 2);
19 char *needle = (char *) malloc (m + 2);
21 /* Failure to compile this test due to missing alarm is okay,
22 since all such platforms (mingw) also have quadratic strstr. */
24 /* Check for quadratic performance. */
25 if (haystack && needle)
27 memset (haystack, 'A', 2 * m);
28 haystack[2 * m] = 'B';
29 haystack[2 * m + 1] = 0;
30 memset (needle, 'A', m);
33 result = strstr (haystack, needle);
36 [gl_cv_func_strstr_linear=yes], [gl_cv_func_strstr_linear=no],
37 [dnl pessimistically assume the worst, since even glibc 2.6.1
38 dnl has quadratic complexity in its strstr
39 gl_cv_func_strstr_linear="guessing no"])])
40 if test "$gl_cv_func_strstr_linear" != yes; then