]> git.ipfire.org Git - thirdparty/glibc.git/blob - wcsmbs/wcsstr.c
advisories: Add Reported-By
[thirdparty/glibc.git] / wcsmbs / wcsstr.c
1 /* Locate a substring in a wide-character string.
2 Copyright (C) 1995-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <wchar.h>
20 #include <string.h>
21
22 #define AVAILABLE(h, h_l, j, n_l) \
23 (((j) + (n_l) <= (h_l)) \
24 || ((h_l) += __wcsnlen ((void*)((h) + (h_l)), (n_l) + 128), \
25 (j) + (n_l) <= (h_l)))
26 #include "wcs-two-way.h"
27
28 #ifndef WCSSTR
29 # define WCSSTR wcsstr
30 #endif
31
32 wchar_t *
33 WCSSTR (const wchar_t *haystack, const wchar_t *needle)
34 {
35 /* Ensure haystack length is at least as long as needle length.
36 Since a match may occur early on in a huge haystack, use strnlen
37 and read ahead a few cachelines for improved performance. */
38 size_t ne_len = __wcslen (needle);
39 size_t hs_len = __wcsnlen (haystack, ne_len | 128);
40 if (hs_len < ne_len)
41 return NULL;
42
43 /* Check whether we have a match. This improves performance since we
44 avoid initialization overheads. */
45 if (__wmemcmp (haystack, needle, ne_len) == 0)
46 return (wchar_t *) haystack;
47
48 return two_way_short_needle (haystack, hs_len, needle, ne_len);
49 }
50 /* This alias is for backward compatibility with drafts of the ISO C
51 standard. Unfortunately the Unix(TM) standard requires this name. */
52 weak_alias (wcsstr, wcswcs)