scc

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

commit c3e16c112ab5c52986db7a90dddf8b20b78436a8
parent 3392e2335ae20127606a01f6878f16c597dee087
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 21 Sep 2018 18:42:09 +0100

[lib/c] Fix str?spn functions

Diffstat:
Mlib/c/strcspn.c | 10++++++----
Mlib/c/strpbrk.c | 12+++++++-----
Mlib/c/strspn.c | 10++++++----
Mtests/libc/execute/0016-strcspn.c | 2+-
Mtests/libc/execute/0025-strspn.c | 4++--
5 files changed, 22 insertions(+), 16 deletions(-)

diff --git a/lib/c/strcspn.c b/lib/c/strcspn.c @@ -4,15 +4,17 @@ size_t strcspn(const char *s1, const char *s2) { - char buf[__NUMCHARS]; - unsigned char ch; + const unsigned char *s = s1; + const unsigned char *accept = s2; + unsigned ch; size_t n; + char buf[__NUMCHARS]; memset(buf, 0, sizeof(buf)); - while (ch = *s2++) + while (ch = *accept++) buf[ch] = 1; - for (n = 0; (ch = *s1++) && !buf[ch]; ++n) + for (n = 0; (ch = *s++) && !buf[ch]; ++n) ; return n; diff --git a/lib/c/strpbrk.c b/lib/c/strpbrk.c @@ -4,15 +4,17 @@ char * strpbrk(const char *s1, const char *s2) { - char buf[__NUMCHARS]; + const unsigned char *s = s1; + const unsigned char *accept = s2; unsigned ch; + char buf[__NUMCHARS]; memset(buf, 0, sizeof(buf)); - while (ch = *s2++) + while (ch = *accept++) buf[ch] = 1; - while ((ch = *s1) && !buf[ch]) - s1++; + while ((ch = *s) && !buf[ch]) + s++; - return (ch == '\0') ? NULL : (char *) s1; + return (ch == '\0') ? NULL : (char *) s; } diff --git a/lib/c/strspn.c b/lib/c/strspn.c @@ -4,15 +4,17 @@ size_t strspn(const char *s1, const char *s2) { - char buf[__NUMCHARS]; - unsigned char ch; + const unsigned char *s = s1; + const unsigned char *accept = s2; + unsigned ch; size_t n; + char buf[__NUMCHARS]; memset(buf, 0, sizeof(buf)); - while (ch = *s2++) + while (ch = *accept++) buf[ch] = 1; - for (n = 0; (ch = *s1++) && buf[ch]; ++n) + for (n = 0; (ch = *s++) && buf[ch]; ++n) ; return n; diff --git a/tests/libc/execute/0016-strcspn.c b/tests/libc/execute/0016-strcspn.c @@ -14,7 +14,7 @@ main() { puts("testing"); - assert(strcspn("012", "56789") == 3); + assert(strcspn("0125", "56789") == 3); assert(strcspn("", "56789") == 0); assert(strcspn("01234", "") == 5); assert(strcspn("", "") == 0); diff --git a/tests/libc/execute/0025-strspn.c b/tests/libc/execute/0025-strspn.c @@ -13,8 +13,8 @@ int main() { puts("testing"); - assert(strspn("abcdef", "cba") == 3); - assert(strspn("abc", "cba0") == 3); + assert(strspn("abccdef", "cba") == 4); + assert(strspn("abcg", "cba0") == 3); assert(strspn("", "abc") == 0); assert(strspn("abc", "") == 0); assert(strspn("", "") == 0);