Also check for the log_user method, to avoid
[coreutils.git] / lib / strstr.c
blob42ffcfa8e3d65e187da9f0da461a94705bb437e9
1 /* Copyright (C) 1994, 1999, 2002 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 #if HAVE_CONFIG_H
28 # include <config.h>
29 #endif
31 #if defined _LIBC || defined HAVE_STRING_H
32 # include <string.h>
33 #endif
35 typedef unsigned chartype;
37 #undef strstr
39 char *
40 strstr (const char *phaystack, const char *pneedle)
42 register const unsigned char *haystack, *needle;
43 register chartype b, c;
45 haystack = (const unsigned char *) phaystack;
46 needle = (const unsigned char *) pneedle;
48 b = *needle;
49 if (b != '\0')
51 haystack--; /* possible ANSI violation */
54 c = *++haystack;
55 if (c == '\0')
56 goto ret0;
58 while (c != b);
60 c = *++needle;
61 if (c == '\0')
62 goto foundneedle;
63 ++needle;
64 goto jin;
66 for (;;)
68 register chartype a;
69 register const unsigned char *rhaystack, *rneedle;
73 a = *++haystack;
74 if (a == '\0')
75 goto ret0;
76 if (a == b)
77 break;
78 a = *++haystack;
79 if (a == '\0')
80 goto ret0;
81 shloop:; }
82 while (a != b);
84 jin: a = *++haystack;
85 if (a == '\0')
86 goto ret0;
88 if (a != c)
89 goto shloop;
91 rhaystack = haystack-- + 1;
92 rneedle = needle;
93 a = *rneedle;
95 if (*rhaystack == a)
98 if (a == '\0')
99 goto foundneedle;
100 ++rhaystack;
101 a = *++needle;
102 if (*rhaystack != a)
103 break;
104 if (a == '\0')
105 goto foundneedle;
106 ++rhaystack;
107 a = *++needle;
109 while (*rhaystack == a);
111 needle = rneedle; /* took the register-poor approach */
113 if (a == '\0')
114 break;
117 foundneedle:
118 return (char*) haystack;
119 ret0:
120 return 0;