]> git.ipfire.org Git - thirdparty/glibc.git/blob - wcsmbs/wcsstr.c
wcsmbs: Add test-wcsstr
[thirdparty/glibc.git] / wcsmbs / wcsstr.c
1 /* Copyright (C) 1995-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18 /*
19 * The original strstr() file contains the following comment:
20 *
21 * My personal strstr() implementation that beats most other algorithms.
22 * Until someone tells me otherwise, I assume that this is the
23 * fastest implementation of strstr() in C.
24 * I deliberately chose not to comment it. You should have at least
25 * as much fun trying to understand it, as I had to write it :-).
26 *
27 * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
28
29 #include <wchar.h>
30
31 #ifndef WCSSTR
32 # define WCSSTR wcsstr
33 #endif
34
35 wchar_t *
36 WCSSTR (const wchar_t *haystack, const wchar_t *needle)
37 {
38 wchar_t b, c;
39
40 if ((b = *needle) != L'\0')
41 {
42 haystack--; /* possible ANSI violation */
43 do
44 if ((c = *++haystack) == L'\0')
45 goto ret0;
46 while (c != b);
47
48 if (!(c = *++needle))
49 goto foundneedle;
50 ++needle;
51 goto jin;
52
53 for (;;)
54 {
55 wchar_t a;
56 const wchar_t *rhaystack, *rneedle;
57
58 do
59 {
60 if (!(a = *++haystack))
61 goto ret0;
62 if (a == b)
63 break;
64 if ((a = *++haystack) == L'\0')
65 goto ret0;
66 shloop: ;
67 }
68 while (a != b);
69
70 jin: if (!(a = *++haystack))
71 goto ret0;
72
73 if (a != c)
74 goto shloop;
75
76 if (*(rhaystack = haystack-- + 1) == (a = *(rneedle = needle)))
77 do
78 {
79 if (a == L'\0')
80 goto foundneedle;
81 if (*++rhaystack != (a = *++needle))
82 break;
83 if (a == L'\0')
84 goto foundneedle;
85 }
86 while (*++rhaystack == (a = *++needle));
87
88 needle = rneedle; /* took the register-poor approach */
89
90 if (a == L'\0')
91 break;
92 }
93 }
94 foundneedle:
95 return (wchar_t*) haystack;
96 ret0:
97 return NULL;
98 }
99 /* This alias is for backward compatibility with drafts of the ISO C
100 standard. Unfortunately the Unix(TM) standard requires this name. */
101 weak_alias (wcsstr, wcswcs)