aboutsummaryrefslogtreecommitdiffstats
path: root/memrmem.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2018-08-12 22:02:07 +0200
committerMattias Andrée <maandree@kth.se>2018-08-12 22:04:32 +0200
commit1054a875c7f96e5799c8bfbf8a73e41dd064b606 (patch)
tree48184e8d2f0c15bae01aee97eaf57247c34e968a /memrmem.c
parentAdd tests (diff)
downloadlibsimple-1054a875c7f96e5799c8bfbf8a73e41dd064b606.tar.gz
libsimple-1054a875c7f96e5799c8bfbf8a73e41dd064b606.tar.bz2
libsimple-1054a875c7f96e5799c8bfbf8a73e41dd064b606.tar.xz
Add memrmem, strrstr, and strrcasestr
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'memrmem.c')
-rw-r--r--memrmem.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/memrmem.c b/memrmem.c
new file mode 100644
index 0000000..713ba34
--- /dev/null
+++ b/memrmem.c
@@ -0,0 +1,57 @@
+/* See LICENSE file for copyright and license details. */
+#include "libsimple.h"
+
+
+void *
+libsimple_memrmem(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;
+ if (hayn < subn)
+ return NULL;
+ if (subn == 1)
+ return libsimple_memrchr(hay, *sub, hayn);
+
+ start = hay;
+ hay = &hay[hayn - subn];
+ do {
+ if (*hay == *sub && !memcmp(hay, sub, subn))
+ return hay;
+ } while (hay-- != start);
+
+ return NULL;
+}
+
+
+#ifdef TEST
+#include <assert.h>
+
+int
+main(void)
+{
+ assert(!strcmp(libsimple_memrmem("test", 4, "test", 4), "test"));
+ assert(!strcmp(libsimple_memrmem("", 0, "", 0), ""));
+ assert(!strcmp(libsimple_memrmem("test", 4, "", 0), ""));
+ assert(!libsimple_memrmem("", 0, "test", 4));
+ assert(!libsimple_memrmem("t", 1, "test", 4));
+ assert(!strcmp(libsimple_memrmem("test", 4, "t", 1), "t"));
+ assert(!strcmp(libsimple_memrmem("test", 4, "e", 1), "est"));
+ assert(!strcmp(libsimple_memrmem("test", 4, "s", 1), "st"));
+ assert(!libsimple_memrmem("test", 4, "x", 1));
+ assert(!strcmp(libsimple_memrmem("test", 4, "te", 2), "test"));
+ assert(!strcmp(libsimple_memrmem("test", 4, "es", 2), "est"));
+ assert(!strcmp(libsimple_memrmem("test", 4, "st", 2), "st"));
+ assert(!strcmp(libsimple_memrmem("test", 5, "t", 2), "t"));
+ assert(!libsimple_memrmem("test", 4, "xx", 2));
+ assert(!strcmp(libsimple_memrmem("abc", 3, "c", 1), "c"));
+ assert(!strcmp(libsimple_memrmem("abc", 3, "bc", 2), "bc"));
+ assert(!strcmp(libsimple_memrmem("abc", 3, "abc", 3), "abc"));
+ assert(!libsimple_memrmem("abc", 3, "abc", 4));
+ assert(!strcmp(libsimple_memrmem("abcabc", 6, "bc", 2), "bc"));
+ return 0;
+}
+
+#endif