1 /* strstr.c - search for a substring in string.
3 Copyright 2008 Karl Berry.
4 Copyright 1994, 1995 Free Software Foundation, Inc.
5 This file was part of the GNU C Library.
6 Modified for kpathsea by Karl Berry.
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with this library; 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 */
30 #if !defined (__STDC__) || !__STDC__
31 /* This is a separate conditional since some stdc systems
32 reject `defined (const)'. */
38 typedef unsigned chartype;
41 strstr (phaystack, pneedle)
42 const char *phaystack;
45 register const unsigned char *haystack, *needle;
46 register chartype b, c;
48 haystack = (const unsigned char *) phaystack;
49 needle = (const unsigned char *) pneedle;
54 haystack--; /* possible ANSI violation */
72 register const unsigned char *rhaystack, *rneedle;
94 rhaystack = haystack-- + 1;
112 while (*rhaystack == a);
114 needle = rneedle; /* took the register-poor aproach */
121 return (char*) haystack;