aboutsummaryrefslogtreecommitdiffstats
path: root/strrnchr.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2018-10-22 15:52:32 +0200
committerMattias Andrée <maandree@kth.se>2018-10-22 15:52:32 +0200
commitd69fd77060515537f9b6cba0daa12c1155b2c02f (patch)
tree49720d25c5ec376808369c43d9e9afedf5eec898 /strrnchr.c
parentAdd memcase{cmp,ends,starts,eq} and mem[r]casemem (diff)
downloadlibsimple-d69fd77060515537f9b6cba0daa12c1155b2c02f.tar.gz
libsimple-d69fd77060515537f9b6cba0daa12c1155b2c02f.tar.bz2
libsimple-d69fd77060515537f9b6cba0daa12c1155b2c02f.tar.xz
Add strn{,case}chr{,nul}, strnend, and strrn{,case}chr
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'strrnchr.c')
-rw-r--r--strrnchr.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/strrnchr.c b/strrnchr.c
new file mode 100644
index 0000000..d67775e
--- /dev/null
+++ b/strrnchr.c
@@ -0,0 +1,47 @@
+/* See LICENSE file for copyright and license details. */
+#include "libsimple.h"
+#ifndef TEST
+
+
+char *
+libsimple_strrnchr(const char *s_, int c_, size_t n)
+{
+ char *s = *(char **)(void *)&s_, c = (char)c_;
+ char *end = &s[n], *r = NULL;
+ for (; s != end; s++) {
+ if (*s == c)
+ r = s;
+ if (!*s)
+ break;
+ }
+ return r;
+}
+
+
+#else
+#include "test.h"
+
+int
+main(void)
+{
+ assert(!strcmpnul(libsimple_strrnchr("abcABCabcABC", 'a', 13), "abcABC"));
+ assert(!strcmpnul(libsimple_strrnchr("abcABCabcABC", 'c', 13), "cABC"));
+ assert(!strcmpnul(libsimple_strrnchr("abcABCabcABC", 'A', 13), "ABC"));
+ assert(!strcmpnul(libsimple_strrnchr("abcABCabcABC", 'C', 13), "C"));
+ assert(!strcmpnul(libsimple_strrnchr("ABCabcABCabc", 'a', 13), "abc"));
+ assert(!strcmpnul(libsimple_strrnchr("ABCabcABCabc", 'c', 13), "c"));
+ assert(!strcmpnul(libsimple_strrnchr("ABCabcABCabc", 'A', 13), "ABCabc"));
+ assert(!strcmpnul(libsimple_strrnchr("ABCabcABCabc", 'C', 13), "Cabc"));
+ assert(!libsimple_strrnchr("abcABCabcABC", 'x', 13));
+ assert(!libsimple_strrnchr("abcABCabcABC", 'x', 20));
+ assert(!strcmpnul(libsimple_strrnchr("abcABCabcABC", '\0', 13), ""));
+ assert(!strcmpnul(libsimple_strrnchr("abcABCabcABC", '\0', 20), ""));
+ assert(!libsimple_strrnchr("abcABCabcABC", '\0', 3));
+ assert(!libsimple_strrnchr("abcdef", 'd', 3));
+ assert(!libsimple_strrnchr("abcdef", 'e', 3));
+ assert(!strcmpnul(libsimple_strrnchr("123123", '1', 7), "123"));
+ assert(!strcmpnul(libsimple_strrnchr("123123", '3', 7), "3"));
+ return 0;
+}
+
+#endif