aboutsummaryrefslogtreecommitdiffstats
path: root/strnend.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 /strnend.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 '')
-rw-r--r--strnend.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/strnend.c b/strnend.c
new file mode 100644
index 0000000..df950cf
--- /dev/null
+++ b/strnend.c
@@ -0,0 +1,38 @@
+/* See LICENSE file for copyright and license details. */
+#include "libsimple.h"
+#ifndef TEST
+
+
+char *
+libsimple_strnend(const char *s_, size_t n)
+{
+ char *s = *(char **)(void *)&s_;
+ char *end = &s[n];
+ for (; s != end && *s; s++);
+ return s;
+}
+
+
+#else
+#include "test.h"
+
+int
+main(void)
+{
+ assert(!strcmpnul(libsimple_strnend("123456789", 0), "123456789"));
+ assert(!strcmpnul(libsimple_strnend("123456789", 1), "23456789"));
+ assert(!strcmpnul(libsimple_strnend("123456789", 2), "3456789"));
+ assert(!strcmpnul(libsimple_strnend("123456789", 3), "456789"));
+ assert(!strcmpnul(libsimple_strnend("123456789", 4), "56789"));
+ assert(!strcmpnul(libsimple_strnend("123456789", 5), "6789"));
+ assert(!strcmpnul(libsimple_strnend("123456789", 6), "789"));
+ assert(!strcmpnul(libsimple_strnend("123456789", 7), "89"));
+ assert(!strcmpnul(libsimple_strnend("123456789", 8), "9"));
+ assert(!strcmpnul(libsimple_strnend("123456789", 9), ""));
+ assert(!strcmpnul(libsimple_strnend("123456789", 10), ""));
+ assert(!strcmpnul(libsimple_strnend("123456789", 11), ""));
+ assert(!strcmpnul(libsimple_strnend("123456789", 12), ""));
+ return 0;
+}
+
+#endif