From 2215a474e340c3f8f58efe35fc96d62693eacef7 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 21 Oct 2018 20:12:31 +0200 Subject: Add memcase{cmp,ends,starts,eq} and mem[r]casemem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- memcasestarts.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 memcasestarts.c (limited to 'memcasestarts.c') diff --git a/memcasestarts.c b/memcasestarts.c new file mode 100644 index 0000000..f9c30ee --- /dev/null +++ b/memcasestarts.c @@ -0,0 +1,58 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" +#ifndef TEST + + +int +libsimple_memcasestarts(const void *s_, size_t n, const void *t_, size_t m) +{ + const char *s = s_, *t = t_; + size_t i = 0; + if (n < m) + return 0; + while (i < m && tolower(s[i]) == tolower(t[i])) i++; + return i == m; +} + + +#else +#include "test.h" + +int +main(void) +{ + assert(libsimple_memcasestarts("", 0, "", 0) == 1); + assert(libsimple_memcasestarts("test", 4, "", 0) == 1); + assert(libsimple_memcasestarts("", 0, "test", 4) == 0); + assert(libsimple_memcasestarts("test", 4, "test", 4) == 1); + assert(libsimple_memcasestarts("test", 4, "test", 5) == 0); + assert(libsimple_memcasestarts("test", 4, "testx", 5) == 0); + assert(libsimple_memcasestarts("test", 4, "tes", 3) == 1); + assert(libsimple_memcasestarts("test", 4, "te", 2) == 1); + assert(libsimple_memcasestarts("test", 4, "t", 1) == 1); + assert(libsimple_memcasestarts("test", 4, "x", 1) == 0); + assert(libsimple_memcasestarts("test", 4, "xx", 2) == 0); + + assert(libsimple_memcasestarts("TEST", 4, "", 0) == 1); + assert(libsimple_memcasestarts("TEST", 4, "test", 4) == 1); + assert(libsimple_memcasestarts("TEST", 4, "test", 5) == 0); + assert(libsimple_memcasestarts("TEST", 4, "testx", 5) == 0); + assert(libsimple_memcasestarts("TEST", 4, "tes", 3) == 1); + assert(libsimple_memcasestarts("TEST", 4, "te", 2) == 1); + assert(libsimple_memcasestarts("TEST", 4, "t", 1) == 1); + assert(libsimple_memcasestarts("TEST", 4, "x", 1) == 0); + assert(libsimple_memcasestarts("TEST", 4, "xx", 2) == 0); + + assert(libsimple_memcasestarts("", 0, "TEST", 4) == 0); + assert(libsimple_memcasestarts("test", 4, "TEST", 4) == 1); + assert(libsimple_memcasestarts("test", 4, "TEST", 5) == 0); + assert(libsimple_memcasestarts("test", 4, "TESTX", 5) == 0); + assert(libsimple_memcasestarts("test", 4, "TES", 3) == 1); + assert(libsimple_memcasestarts("test", 4, "TE", 2) == 1); + assert(libsimple_memcasestarts("test", 4, "T", 1) == 1); + assert(libsimple_memcasestarts("test", 4, "X", 1) == 0); + assert(libsimple_memcasestarts("test", 4, "XX", 2) == 0); + return 0; +} + +#endif -- cgit v1.2.3-70-g09d2