Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / lib / strstr.c
blobdd4622297a2559b5cb3b136d534c07c278cdeaa3
1 /* Copyright (C) 1994, 1999, 2002-2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 * My personal strstr() implementation that beats most other algorithms.
20 * Until someone tells me otherwise, I assume that this is the
21 * fastest implementation of strstr() in C.
22 * I deliberately chose not to comment it. You should have at least
23 * as much fun trying to understand it, as I had to write it :-).
25 * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
27 #if HAVE_CONFIG_H
28 # include <config.h>
29 #endif
31 #include <string.h>
33 typedef unsigned chartype;
35 #undef strstr
37 char *
38 strstr (const char *phaystack, const char *pneedle)
40 register const unsigned char *haystack, *needle;
41 register chartype b, c;
43 haystack = (const unsigned char *) phaystack;
44 needle = (const unsigned char *) pneedle;
46 b = *needle;
47 if (b != '\0')
49 haystack--; /* possible ANSI violation */
52 c = *++haystack;
53 if (c == '\0')
54 goto ret0;
56 while (c != b);
58 c = *++needle;
59 if (c == '\0')
60 goto foundneedle;
61 ++needle;
62 goto jin;
64 for (;;)
66 register chartype a;
67 register const unsigned char *rhaystack, *rneedle;
71 a = *++haystack;
72 if (a == '\0')
73 goto ret0;
74 if (a == b)
75 break;
76 a = *++haystack;
77 if (a == '\0')
78 goto ret0;
79 shloop:; }
80 while (a != b);
82 jin: a = *++haystack;
83 if (a == '\0')
84 goto ret0;
86 if (a != c)
87 goto shloop;
89 rhaystack = haystack-- + 1;
90 rneedle = needle;
91 a = *rneedle;
93 if (*rhaystack == a)
96 if (a == '\0')
97 goto foundneedle;
98 ++rhaystack;
99 a = *++needle;
100 if (*rhaystack != a)
101 break;
102 if (a == '\0')
103 goto foundneedle;
104 ++rhaystack;
105 a = *++needle;
107 while (*rhaystack == a);
109 needle = rneedle; /* took the register-poor approach */
111 if (a == '\0')
112 break;
115 foundneedle:
116 return (char*) haystack;
117 ret0:
118 return 0;