diff options
author | Mattias Andrée <maandree@kth.se> | 2018-10-21 20:12:31 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2018-10-21 20:12:31 +0200 |
commit | 2215a474e340c3f8f58efe35fc96d62693eacef7 (patch) | |
tree | 87505305fb28e285b02082f07cdb2760b53ff01d /memrcasemem.c | |
parent | Add memcasechr (diff) | |
download | libsimple-2215a474e340c3f8f58efe35fc96d62693eacef7.tar.gz libsimple-2215a474e340c3f8f58efe35fc96d62693eacef7.tar.bz2 libsimple-2215a474e340c3f8f58efe35fc96d62693eacef7.tar.xz |
Add memcase{cmp,ends,starts,eq} and mem[r]casemem
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'memrcasemem.c')
-rw-r--r-- | memrcasemem.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/memrcasemem.c b/memrcasemem.c new file mode 100644 index 0000000..e7672f9 --- /dev/null +++ b/memrcasemem.c @@ -0,0 +1,61 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" +#ifndef TEST + + +void * +libsimple_memrcasemem(const void *hay_, size_t hayn, const void *sub_, size_t subn) +{ + char *hay = (void *)hay_, *start; + const char *sub = sub_; + + if (!subn) + return &hay[hayn]; + if (hayn < subn) + return NULL; + if (subn == 1) + return libsimple_memrcasechr(hay, *sub, hayn); + + start = hay; + hay = &hay[hayn - subn]; + do { + if (tolower(*hay) == tolower(*sub) && !libsimple_memcasecmp(hay, sub, subn)) + return hay; + } while (hay-- != start); + + return NULL; +} + + +#else +#include "test.h" + +int +main(void) +{ + assert(!strcmpnul(libsimple_memrcasemem("test", 4, "test", 4), "test")); + assert(!strcmpnul(libsimple_memrcasemem("", 0, "", 0), "")); + assert(!strcmpnul(libsimple_memrcasemem("test", 4, "", 0), "")); + assert(!libsimple_memrcasemem("", 0, "test", 4)); + assert(!libsimple_memrcasemem("t", 1, "test", 4)); + assert(!strcmpnul(libsimple_memrcasemem("test", 4, "t", 1), "t")); + assert(!strcmpnul(libsimple_memrcasemem("test", 4, "e", 1), "est")); + assert(!strcmpnul(libsimple_memrcasemem("test", 4, "s", 1), "st")); + assert(!libsimple_memrcasemem("test", 4, "x", 1)); + assert(!strcmpnul(libsimple_memrcasemem("test", 4, "te", 2), "test")); + assert(!strcmpnul(libsimple_memrcasemem("test", 4, "es", 2), "est")); + assert(!strcmpnul(libsimple_memrcasemem("test", 4, "st", 2), "st")); + assert(!strcmpnul(libsimple_memrcasemem("test", 5, "t", 2), "t")); + assert(!libsimple_memrcasemem("test", 4, "xx", 2)); + assert(!strcmpnul(libsimple_memrcasemem("abc", 3, "c", 1), "c")); + assert(!strcmpnul(libsimple_memrcasemem("abc", 3, "bc", 2), "bc")); + assert(!strcmpnul(libsimple_memrcasemem("abc", 3, "abc", 3), "abc")); + assert(!libsimple_memrcasemem("abc", 3, "abc", 4)); + assert(!strcmpnul(libsimple_memrcasemem("abcabc", 6, "bc", 2), "bc")); + assert(!strcmpnul(libsimple_memrcasemem("abcabc", 6, "BC", 2), "bc")); + assert(!strcmpnul(libsimple_memrcasemem("ABCABC", 6, "bc", 2), "BC")); + assert(!strcmpnul(libsimple_memrcasemem("ABCABC", 6, "BC", 2), "BC")); + return 0; +} + +#endif |