Update from main archive 961219
[glibc.git] / sysdeps / generic / strstr.c
blobd8d0b84813b6cd70e1ecc16606982f15d93f104b
1 /* Copyright (C) 1994, 1996 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 not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, 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 */
28 #include <string.h>
29 #include <sys/types.h>
31 typedef unsigned chartype;
33 char *
34 strstr (phaystack, pneedle)
35 const char *phaystack;
36 const char *pneedle;
38 register const unsigned char *haystack, *needle;
39 register chartype b, c;
41 haystack = (const unsigned char *) phaystack;
42 needle = (const unsigned char *) pneedle;
44 b = *needle;
45 if (b != '\0')
47 haystack--; /* possible ANSI violation */
50 c = *++haystack;
51 if (c == '\0')
52 goto ret0;
54 while (c != b);
56 c = *++needle;
57 if (c == '\0')
58 goto foundneedle;
59 ++needle;
60 goto jin;
62 for (;;)
64 register chartype a;
65 register const unsigned char *rhaystack, *rneedle;
69 a = *++haystack;
70 if (a == '\0')
71 goto ret0;
72 if (a == b)
73 break;
74 a = *++haystack;
75 if (a == '\0')
76 goto ret0;
77 shloop: }
78 while (a != b);
80 jin: a = *++haystack;
81 if (a == '\0')
82 goto ret0;
84 if (a != c)
85 goto shloop;
87 rhaystack = haystack-- + 1;
88 rneedle = needle;
89 a = *rneedle;
91 if (*rhaystack == a)
94 if (a == '\0')
95 goto foundneedle;
96 ++rhaystack;
97 a = *++needle;
98 if (*rhaystack != a)
99 break;
100 if (a == '\0')
101 goto foundneedle;
102 ++rhaystack;
103 a = *++needle;
105 while (*rhaystack == a);
107 needle = rneedle; /* took the register-poor approach */
109 if (a == '\0')
110 break;
113 foundneedle:
114 return (char*) haystack;
115 ret0:
116 return 0;