*** empty log message ***
[gnulib.git] / lib / strstr.c
1 /* Copyright (C) 1994, 1999, 2002-2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /*
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 :-).
24  *
25  * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
26
27 #if HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30
31 #include <string.h>
32
33 typedef unsigned chartype;
34
35 #undef strstr
36
37 char *
38 strstr (const char *phaystack, const char *pneedle)
39 {
40   register const unsigned char *haystack, *needle;
41   register chartype b, c;
42
43   haystack = (const unsigned char *) phaystack;
44   needle = (const unsigned char *) pneedle;
45
46   b = *needle;
47   if (b != '\0')
48     {
49       haystack--;                               /* possible ANSI violation */
50       do
51         {
52           c = *++haystack;
53           if (c == '\0')
54             goto ret0;
55         }
56       while (c != b);
57
58       c = *++needle;
59       if (c == '\0')
60         goto foundneedle;
61       ++needle;
62       goto jin;
63
64       for (;;)
65         {
66           register chartype a;
67           register const unsigned char *rhaystack, *rneedle;
68
69           do
70             {
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);
81
82 jin:      a = *++haystack;
83           if (a == '\0')
84             goto ret0;
85
86           if (a != c)
87             goto shloop;
88
89           rhaystack = haystack-- + 1;
90           rneedle = needle;
91           a = *rneedle;
92
93           if (*rhaystack == a)
94             do
95               {
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;
106               }
107             while (*rhaystack == a);
108
109           needle = rneedle;                /* took the register-poor approach */
110
111           if (a == '\0')
112             break;
113         }
114     }
115 foundneedle:
116   return (char*) haystack;
117 ret0:
118   return 0;
119 }