ref: 232f814cbdfd8ab842db3419844f1988cbf8d367
parent: 4c38694f5a5d742b1640a5d6add30ace501efa69
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Dec 6 19:35:11 EST 2017
[lib/c] Fix strstr() This function tried a very optimized algorithm, that was wrong. In this version we try a new one that is more classical and we know that works.
--- a/lib/c/src/strstr.c
+++ b/lib/c/src/strstr.c
@@ -5,22 +5,22 @@
strstr(const char *s1, const char *s2)
{
const char *p, *q;
- int c;
+ int c0, c;
- c = *s2++;
- if (c == '\0')
+ c0 = *s2;
+ if (c0 == '\0')
return (char *) s1;
-
- while (*s1) {
- if (*s1 != c) {
- ++s1;
- } else {
- p = s1++;
- for (q = s2; *q && *s1 == *q; ++s1, ++q)
- ;
- if (*q == '\0')
- return (char *) p;
+ --s1;
+ while ((s1 = strchr(s1 + 1, c0)) != NULL) {
+ p = s1;
+ q = s2;
+ for (;;) {
+ if ((c = *++p) == '\0')
+ return (char *) s1;
+ if (c != *++q)
+ break;
}
}
+
return NULL;
}