gettext: Sync with gettext 0.23.
[gnulib.git] / lib / wcsstr.c
bloba6ec1db2dd301f6e974227e63b1901082de0b59e
1 /* Locate a substring in a wide string.
2 Copyright (C) 2011-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2011.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Bruno Haible, 1999. */
20 #include <config.h>
22 /* Specification. */
23 #include <wchar.h>
25 #if NEED_LINEAR_WCSSTR
27 /* Use Two-Way algorithm, worst-case O(n+m). */
29 # define FUNC wcsstr
30 # define UNIT wchar_t
31 # define RETURN_TYPE wchar_t *
32 # define MEMCHR0(s, n) wmemchr (s, 0, n)
33 # define STRCHR wcschr
34 # include "wcsstr-impl.h"
36 #else
38 /* Use simple implementation, worst-case O(nm). */
40 wchar_t *
41 wcsstr (const wchar_t *haystack, const wchar_t *needle)
43 wchar_t n = needle[0];
45 /* Is needle empty? */
46 if (n == (wchar_t)'\0')
47 return (wchar_t *) haystack;
49 /* Is needle nearly empty? */
50 if (needle[1] == (wchar_t)'\0')
51 return wcschr (haystack, n);
53 /* Search for needle's first character. */
54 for (; *haystack != (wchar_t)'\0'; haystack++)
56 if (*haystack == n)
58 /* Compare with needle's remaining characters. */
59 const wchar_t *hptr = haystack + 1;
60 const wchar_t *nptr = needle + 1;
61 for (;;)
63 if (*hptr != *nptr)
64 break;
65 hptr++; nptr++;
66 if (*nptr == (wchar_t)'\0')
67 return (wchar_t *) haystack;
72 return NULL;
75 #endif