*** empty log message ***
[coreutils.git] / lib / strstr.c
blobb16799a2e6486f4bfd94749474cdf5afe3889735
1 /* Copyright (C) 1994 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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 :-).
25 * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
27 #include <string.h>
28 #include <sys/types.h>
30 typedef unsigned chartype;
32 char *
33 strstr (const char *phaystack, const char *pneedle)
35 register const unsigned char *haystack, *needle;
36 register chartype b, c;
38 haystack = (const unsigned char *) phaystack;
39 needle = (const unsigned char *) pneedle;
41 b = *needle;
42 if (b != '\0')
44 haystack--; /* possible ANSI violation */
47 c = *++haystack;
48 if (c == '\0')
49 goto ret0;
51 while (c != b);
53 c = *++needle;
54 if (c == '\0')
55 goto foundneedle;
56 ++needle;
57 goto jin;
59 for (;;)
61 register chartype a;
62 register const unsigned char *rhaystack, *rneedle;
66 a = *++haystack;
67 if (a == '\0')
68 goto ret0;
69 if (a == b)
70 break;
71 a = *++haystack;
72 if (a == '\0')
73 goto ret0;
74 shloop: }
75 while (a != b);
77 jin: a = *++haystack;
78 if (a == '\0')
79 goto ret0;
81 if (a != c)
82 goto shloop;
84 rhaystack = haystack-- + 1;
85 rneedle = needle;
86 a = *rneedle;
88 if (*rhaystack == a)
91 if (a == '\0')
92 goto foundneedle;
93 ++rhaystack;
94 a = *++needle;
95 if (*rhaystack != a)
96 break;
97 if (a == '\0')
98 goto foundneedle;
99 ++rhaystack;
100 a = *++needle;
102 while (*rhaystack == a);
104 needle = rneedle; /* took the register-poor aproach */
106 if (a == '\0')
107 break;
110 foundneedle:
111 return (char*) haystack;
112 ret0:
113 return 0;