]> git.ozlabs.org Git - yaboot.git/blob - lib/strstr.c
Remove some DEBUG code.
[yaboot.git] / lib / strstr.c
1 /* Return the offset of one string within another.
2    Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 /*
21  * My personal strstr() implementation that beats most other algorithms.
22  * Until someone tells me otherwise, I assume that this is the
23  * fastest implementation of strstr() in C.
24  * I deliberately chose not to comment it.  You should have at least
25  * as much fun trying to understand it, as I had to write it :-).
26  *
27  * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
28
29 #include <string.h>
30
31 typedef unsigned chartype;
32
33 #undef strstr
34
35 char *
36 strstr (phaystack, pneedle)
37      const char *phaystack;
38      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             }
81           while (a != b);
82
83 jin:      a = *++haystack;
84           if (a == '\0')
85             goto ret0;
86
87           if (a != c)
88             goto shloop;
89
90           rhaystack = haystack-- + 1;
91           rneedle = needle;
92           a = *rneedle;
93
94           if (*rhaystack == a)
95             do
96               {
97                 if (a == '\0')
98                   goto foundneedle;
99                 ++rhaystack;
100                 a = *++needle;
101                 if (*rhaystack != a)
102                   break;
103                 if (a == '\0')
104                   goto foundneedle;
105                 ++rhaystack;
106                 a = *++needle;
107               }
108             while (*rhaystack == a);
109
110           needle = rneedle;             /* took the register-poor approach */
111
112           if (a == '\0')
113             break;
114         }
115     }
116 foundneedle:
117   return (char*) haystack;
118 ret0:
119   return 0;
120 }