[BZ #3112]
[glibc.git] / wcsmbs / wcsstr.c
blobf785c152182bef6d7b57f96a143be8e91eab73a4
1 /* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
20 * The original strstr() file contains the following comment:
22 * My personal strstr() implementation that beats most other algorithms.
23 * Until someone tells me otherwise, I assume that this is the
24 * fastest implementation of strstr() in C.
25 * I deliberately chose not to comment it. You should have at least
26 * as much fun trying to understand it, as I had to write it :-).
28 * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
30 #include <wchar.h>
32 wchar_t *
33 wcsstr (haystack, needle)
34 const wchar_t *haystack;
35 const wchar_t *needle;
37 register wchar_t b, c;
39 if ((b = *needle) != L'\0')
41 haystack--; /* possible ANSI violation */
43 if ((c = *++haystack) == L'\0')
44 goto ret0;
45 while (c != b);
47 if (!(c = *++needle))
48 goto foundneedle;
49 ++needle;
50 goto jin;
52 for (;;)
54 register wchar_t a;
55 register const wchar_t *rhaystack, *rneedle;
59 if (!(a = *++haystack))
60 goto ret0;
61 if (a == b)
62 break;
63 if ((a = *++haystack) == L'\0')
64 goto ret0;
65 shloop: ;
67 while (a != b);
69 jin: if (!(a = *++haystack))
70 goto ret0;
72 if (a != c)
73 goto shloop;
75 if (*(rhaystack = haystack-- + 1) == (a = *(rneedle = needle)))
78 if (a == L'\0')
79 goto foundneedle;
80 if (*++rhaystack != (a = *++needle))
81 break;
82 if (a == L'\0')
83 goto foundneedle;
85 while (*++rhaystack == (a = *++needle));
87 needle = rneedle; /* took the register-poor approach */
89 if (a == L'\0')
90 break;
93 foundneedle:
94 return (wchar_t*) haystack;
95 ret0:
96 return NULL;
98 /* This alias is for backward compatibility with drafts of the ISO C
99 standard. Unfortunately the Unix(TM) standard requires this name. */
100 weak_alias (wcsstr, wcswcs)