summaryrefslogtreecommitdiff
path: root/libc/string/strstr.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2000-07-06 07:16:59 +0000
committerEric Andersen <andersen@codepoet.org>2000-07-06 07:16:59 +0000
commitafb85e9d6ca1de8f1ecb267e8c30b88ba4382820 (patch)
treee6b9c1e2d2b9ec890cf832dc0973231dace674c2 /libc/string/strstr.c
parent6811c064139574e2e2e07fdeb10218428cd68b36 (diff)
Rework all the string handling. Make const stuff be constified.
-Erik
Diffstat (limited to 'libc/string/strstr.c')
-rw-r--r--libc/string/strstr.c155
1 files changed, 113 insertions, 42 deletions
diff --git a/libc/string/strstr.c b/libc/string/strstr.c
index 3c853ac52..03d6c8e5f 100644
--- a/libc/string/strstr.c
+++ b/libc/string/strstr.c
@@ -1,54 +1,125 @@
-/* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
- * This file is part of the Linux-8086 C library and is distributed
- * under the GNU Library General Public License.
- */
+/* Return the offset of one string within another.
+ Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
-#include <string.h>
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
-#if 1
-/* We've now got a nice fast strchr and memcmp use them */
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+/*
+ * My personal strstr() implementation that beats most other algorithms.
+ * Until someone tells me otherwise, I assume that this is the
+ * fastest implementation of strstr() in C.
+ * I deliberately chose not to comment it. You should have at least
+ * as much fun trying to understand it, as I had to write it :-).
+ *
+ * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#if defined _LIBC || defined HAVE_STRING_H
+# include <string.h>
+#endif
+
+typedef unsigned chartype;
+
+#undef strstr
char *
-strstr(s1, s2)
-char *s1; char *s2;
+strstr (phaystack, pneedle)
+ const char *phaystack;
+ const char *pneedle;
{
- register int l = strlen(s2);
- register char * p = s1;
-
- if( l==0 ) return p;
-
- while ((p = strchr(p, *s2)))
- {
- if( memcmp(p, s2, l) == 0 )
- return p;
- p++;
- }
- return (char *) 0;
-}
+ register const unsigned char *haystack, *needle;
+ register chartype b, c;
-#else
-/* This is a nice simple self contained strstr,
- now go and work out why the GNU one is faster :-) */
+ haystack = (const unsigned char *) phaystack;
+ needle = (const unsigned char *) pneedle;
-char *strstr(str1, str2)
-char *str1, *str2;
-{
- register char *Sptr, *Tptr;
- int len = strlen(str1) -strlen(str2) + 1;
+ b = *needle;
+ if (b != '\0')
+ {
+ haystack--; /* possible ANSI violation */
+ do
+ {
+ c = *++haystack;
+ if (c == '\0')
+ goto ret0;
+ }
+ while (c != b);
- if (*str2)
- for (; len > 0; len--, str1++){
- if (*str1 != *str2)
- continue;
+ c = *++needle;
+ if (c == '\0')
+ goto foundneedle;
+ ++needle;
+ goto jin;
- for (Sptr = str1, Tptr = str2; *Tptr != '\0'; Sptr++, Tptr++)
- if (*Sptr != *Tptr)
- break;
+ for (;;)
+ {
+ register chartype a;
+ register const unsigned char *rhaystack, *rneedle;
- if (*Tptr == '\0')
- return (char*) str1;
- }
+ do
+ {
+ a = *++haystack;
+ if (a == '\0')
+ goto ret0;
+ if (a == b)
+ break;
+ a = *++haystack;
+ if (a == '\0')
+ goto ret0;
+shloop: }
+ while (a != b);
+
+jin: a = *++haystack;
+ if (a == '\0')
+ goto ret0;
+
+ if (a != c)
+ goto shloop;
- return (char*)0;
+ rhaystack = haystack-- + 1;
+ rneedle = needle;
+ a = *rneedle;
+
+ if (*rhaystack == a)
+ do
+ {
+ if (a == '\0')
+ goto foundneedle;
+ ++rhaystack;
+ a = *++needle;
+ if (*rhaystack != a)
+ break;
+ if (a == '\0')
+ goto foundneedle;
+ ++rhaystack;
+ a = *++needle;
+ }
+ while (*rhaystack == a);
+
+ needle = rneedle; /* took the register-poor approach */
+
+ if (a == '\0')
+ break;
+ }
+ }
+foundneedle:
+ return (char*) haystack;
+ret0:
+ return 0;
}
-#endif