c-vasnprintf: Relicense under LGPLv2+.
[gnulib.git] / m4 / wcsstr.m4
blobeb43b9f35466a5b5238d31024432e94406ba7492
1 # wcsstr.m4
2 # serial 3
3 dnl Copyright (C) 2011-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 dnl Check that wcsstr exists and works.
9 AC_DEFUN([gl_FUNC_WCSSTR_SIMPLE],
11   AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
12   AC_CHECK_FUNCS_ONCE([wcsstr])
13   if test $ac_cv_func_wcsstr = no; then
14     HAVE_WCSSTR=0
15   fi
18 dnl Additionally, check that wcsstr is efficient.
19 AC_DEFUN([gl_FUNC_WCSSTR],
21   AC_REQUIRE([gl_FUNC_WCSSTR_SIMPLE])
22   if test $HAVE_WCSSTR = 1; then
23     AC_CACHE_CHECK([whether wcsstr works in linear time],
24       [gl_cv_func_wcsstr_linear],
25       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
26 #include <signal.h> /* for signal */
27 #include <wchar.h> /* for wcsstr */
28 #include <stdlib.h> /* for malloc */
29 #include <unistd.h> /* for alarm */
30 static void quit (int sig) { _exit (sig + 128); }
31 ]], [[
32     int result = 0;
33     size_t m = 1000000;
34     wchar_t *haystack = (wchar_t *) malloc ((2 * m + 2) * sizeof (wchar_t));
35     wchar_t *needle = (wchar_t *) malloc ((m + 2) * sizeof (wchar_t));
36     /* Failure to compile this test due to missing alarm is okay,
37        since all such platforms (mingw) also have quadratic strstr.  */
38     signal (SIGALRM, quit);
39     alarm (5);
40     /* Check for quadratic performance.  */
41     if (haystack && needle)
42       {
43         size_t i;
44         for (i = 0; i < 2 * m; i++)
45           haystack[i] = L'A';
46         haystack[2 * m] = L'B';
47         haystack[2 * m + 1] = 0;
48         for (i = 0; i < m; i++)
49           needle[i] = L'A';
50         needle[m] = L'B';
51         needle[m + 1] = 0;
52         if (!wcsstr (haystack, needle))
53           result |= 1;
54       }
55     /* Free allocated memory, in case some sanitizer is watching.  */
56     free (haystack);
57     free (needle);
58     return result;
59     ]])],
60         [gl_cv_func_wcsstr_linear=yes], [gl_cv_func_wcsstr_linear=no],
61         [gl_cv_func_wcsstr_linear="$gl_cross_guess_normal"])
62       ])
63     case "$gl_cv_func_wcsstr_linear" in
64       *yes) ;;
65       *)
66         REPLACE_WCSSTR=1
67         ;;
68     esac
69   fi
70   if test $HAVE_WCSSTR = 0 || test $REPLACE_WCSSTR = 1; then
71     AC_DEFINE([NEED_LINEAR_WCSSTR], [1],
72       [Define to 1 to get a worst-case linear time implementation of wcsstr.])
73   fi