1 /* Copyright (C) 1994 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 * My personal strstr() implementation that beats most other algorithms.
21 * Until someone tells me otherwise, I assume that this is the
22 * fastest implementation of strstr() in C.
23 * I deliberately chose not to comment it. You should have at least
24 * as much fun trying to understand it, as I had to write it :-).
26 * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
29 #include <sys/types.h>
31 typedef unsigned chartype
;
34 strstr (phaystack
, pneedle
)
35 const char *phaystack
;
38 register const unsigned char *haystack
, *needle
;
39 register chartype b
, c
;
41 haystack
= (const unsigned char *) phaystack
;
42 needle
= (const unsigned char *) pneedle
;
47 haystack
--; /* possible ANSI violation */
65 register const unsigned char *rhaystack
, *rneedle
;
87 rhaystack
= haystack
-- + 1;
105 while (*rhaystack
== a
);
107 needle
= rneedle
; /* took the register-poor aproach */
114 return (char*) haystack
;