Sun Jul 14 01:51:39 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / wcsmbs / wcsstr.c
blobd2c863fff30e6a06966a24691738109668d6971c
1 /* Copyright (C) 1995, 1996 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 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.
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.
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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, 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;