1 /* strstr - find a substring within a string */
3 /* Copyright (C) 1994, 1999 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
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 */
34 #if defined _LIBC || defined HAVE_STRING_H
37 #include <sys/types.h>
39 typedef unsigned chartype;
44 strstr (const char *phaystack, const char *pneedle)
46 register const unsigned char *haystack, *needle;
47 register chartype b, c;
49 haystack = (const unsigned char *) phaystack;
50 needle = (const unsigned char *) pneedle;
55 haystack--; /* possible ANSI violation */
73 register const unsigned char *rhaystack, *rneedle;
95 rhaystack = haystack-- + 1;
113 while (*rhaystack == a);
115 needle = rneedle; /* took the register-poor approach */
122 return (char*) haystack;