aboutsummaryrefslogtreecommitdiffstats
path: root/memcasemem.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2018-10-21 20:12:31 +0200
committerMattias Andrée <maandree@kth.se>2018-10-21 20:12:31 +0200
commit2215a474e340c3f8f58efe35fc96d62693eacef7 (patch)
tree87505305fb28e285b02082f07cdb2760b53ff01d /memcasemem.c
parentAdd memcasechr (diff)
downloadlibsimple-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 'memcasemem.c')
-rw-r--r--memcasemem.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/memcasemem.c b/memcasemem.c
new file mode 100644
index 0000000..244042b
--- /dev/null
+++ b/memcasemem.c
@@ -0,0 +1,58 @@
+/* See LICENSE file for copyright and license details. */
+#include "libsimple.h"
+#ifndef TEST
+
+
+void *
+libsimple_memcasemem(const void *hay_, size_t hayn, const void *sub_, size_t subn)
+{
+ char *hay = (void *)hay_, *end;
+ const char *sub = sub_;
+
+ if (!subn)
+ return hay;
+ if (hayn < subn)
+ return NULL;
+ if (subn == 1)
+ return libsimple_memcasechr(hay, *sub, hayn);
+
+ for (end = &hay[hayn - subn + 1]; hay != end; hay++)
+ if (tolower(*hay) == tolower(*sub) && !libsimple_memcasecmp(hay, sub, subn))
+ return hay;
+
+ return NULL;
+}
+
+
+#else
+#include "test.h"
+
+int
+main(void)
+{
+ assert(!strcmpnul(libsimple_memcasemem("test", 4, "test", 4), "test"));
+ assert(!strcmpnul(libsimple_memcasemem("", 0, "", 0), ""));
+ assert(!strcmpnul(libsimple_memcasemem("test", 4, "", 0), "test"));
+ assert(!libsimple_memcasemem("", 0, "test", 4));
+ assert(!libsimple_memcasemem("t", 1, "test", 4));
+ assert(!strcmpnul(libsimple_memcasemem("test", 4, "t", 1), "test"));
+ assert(!strcmpnul(libsimple_memcasemem("test", 4, "e", 1), "est"));
+ assert(!strcmpnul(libsimple_memcasemem("test", 4, "s", 1), "st"));
+ assert(!libsimple_memcasemem("test", 4, "x", 1));
+ assert(!strcmpnul(libsimple_memcasemem("test", 4, "te", 2), "test"));
+ assert(!strcmpnul(libsimple_memcasemem("test", 4, "es", 2), "est"));
+ assert(!strcmpnul(libsimple_memcasemem("test", 4, "st", 2), "st"));
+ assert(!strcmpnul(libsimple_memcasemem("test", 5, "t", 2), "t"));
+ assert(!libsimple_memcasemem("test", 4, "xx", 2));
+ assert(!strcmpnul(libsimple_memcasemem("abc", 3, "c", 1), "c"));
+ assert(!strcmpnul(libsimple_memcasemem("abc", 3, "bc", 2), "bc"));
+ assert(!strcmpnul(libsimple_memcasemem("abc", 3, "abc", 3), "abc"));
+ assert(!libsimple_memcasemem("abc", 3, "abc", 4));
+ assert(!strcmpnul(libsimple_memcasemem("abcabc", 6, "bc", 2), "bcabc"));
+ assert(!strcmpnul(libsimple_memcasemem("abcabc", 6, "BC", 2), "bcabc"));
+ assert(!strcmpnul(libsimple_memcasemem("ABCABC", 6, "bc", 2), "BCABC"));
+ assert(!strcmpnul(libsimple_memcasemem("ABCABC", 6, "BC", 2), "BCABC"));
+ return 0;
+}
+
+#endif