scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 9fdc79d8cddf62db618edfa7dd30e2413dc1ecb9
parent 610a73fa975fbb6981dc1aa383b2d92567060a98
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu,  7 Dec 2017 00:35:11 +0100

[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.

Diffstat:
Mlib/c/src/strstr.c | 26+++++++++++++-------------
1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/lib/c/src/strstr.c b/lib/c/src/strstr.c @@ -5,22 +5,22 @@ char * 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; }