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
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); }
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);
40 /* Check for quadratic performance. */
41 if (haystack && needle)
44 for (i = 0; i < 2 * m; i++)
46 haystack[2 * m] = L'B';
47 haystack[2 * m + 1] = 0;
48 for (i = 0; i < m; i++)
52 if (!wcsstr (haystack, needle))
55 /* Free allocated memory, in case some sanitizer is watching. */
60 [gl_cv_func_wcsstr_linear=yes], [gl_cv_func_wcsstr_linear=no],
61 [gl_cv_func_wcsstr_linear="$gl_cross_guess_normal"])
63 case "$gl_cv_func_wcsstr_linear" in
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.])