(fgetwc, getwc, getwchar, fputwc, putwc, putwchar, fgetws, fputws, ungetwc, getwc_unl...
[platform/upstream/glibc.git] / wcsmbs / wcsstr.c
1 /* Copyright (C) 1995, 1996, 1997 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 Library General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    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    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public
15    License along with the GNU C Library; see the file COPYING.LIB.  If not,
16    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17    Boston, MA 02111-1307, USA.  */
18
19 /*
20  * The original strstr() file contains the following comment:
21  *
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 :-).
27  *
28  * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
29
30 #include <wchar.h>
31
32 wchar_t *
33 wcsstr (haystack, needle)
34      const wchar_t *haystack;
35      const wchar_t *needle;
36 {
37   register wchar_t b, c;
38
39   if ((b = *needle) != L'\0')
40     {
41       haystack--;                               /* possible ANSI violation */
42       do
43         if ((c = *++haystack) == L'\0')
44           goto ret0;
45       while (c != b);
46
47       if (!(c = *++needle))
48         goto foundneedle;
49       ++needle;
50       goto jin;
51
52       for (;;)
53         {
54           register wchar_t a;
55           register const wchar_t *rhaystack, *rneedle;
56
57           do
58             {
59               if (!(a = *++haystack))
60                 goto ret0;
61               if (a == b)
62                 break;
63               if ((a = *++haystack) == L'\0')
64                 goto ret0;
65 shloop:       ;
66             }
67           while (a != b);
68
69 jin:      if (!(a = *++haystack))
70             goto ret0;
71
72           if (a != c)
73             goto shloop;
74
75           if (*(rhaystack = haystack-- + 1) == (a = *(rneedle = needle)))
76             do
77               {
78                 if (a == L'\0')
79                   goto foundneedle;
80                 if (*++rhaystack != (a = *++needle))
81                   break;
82                 if (a == L'\0')
83                   goto foundneedle;
84               }
85             while (*++rhaystack == (a = *++needle));
86
87           needle = rneedle;               /* took the register-poor approach */
88
89           if (a == L'\0')
90             break;
91         }
92     }
93 foundneedle:
94   return (wchar_t*) haystack;
95 ret0:
96   return NULL;
97 }
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)