aboutsummaryrefslogtreecommitdiffstats
path: root/strncasechrnul.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 /strncasechrnul.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--strncasechrnul.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/strncasechrnul.c b/strncasechrnul.c
new file mode 100644
index 0000000..608f097
--- /dev/null
+++ b/strncasechrnul.c
@@ -0,0 +1,43 @@
+/* See LICENSE file for copyright and license details. */
+#include "libsimple.h"
+#ifndef TEST
+
+
+char *
+libsimple_strncasechrnul(const char *s_, int c, size_t n)
+{
+ char *s = *(char **)(void *)&s_;
+ char *end = &s[n];
+ c = tolower(c);
+ for (; s != end && *s && tolower(*s) != c; s++);
+ return s;
+}
+
+
+#else
+#include "test.h"
+
+int
+main(void)
+{
+ assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", 'a', 13), "abcABCabcABC"));
+ assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", 'c', 13), "cABCabcABC"));
+ assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", 'A', 13), "abcABCabcABC"));
+ assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", 'C', 13), "cABCabcABC"));
+ assert(!strcmpnul(libsimple_strncasechrnul("ABCabcABCabc", 'a', 13), "ABCabcABCabc"));
+ assert(!strcmpnul(libsimple_strncasechrnul("ABCabcABCabc", 'c', 13), "CabcABCabc"));
+ assert(!strcmpnul(libsimple_strncasechrnul("ABCabcABCabc", 'A', 13), "ABCabcABCabc"));
+ assert(!strcmpnul(libsimple_strncasechrnul("ABCabcABCabc", 'C', 13), "CabcABCabc"));
+ assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", 'x', 13), ""));
+ assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", 'x', 20), ""));
+ assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", '\0', 13), ""));
+ assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", '\0', 20), ""));
+ assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", '\0', 3), "ABCabcABC"));
+ assert(!strcmpnul(libsimple_strncasechrnul("abcdef", 'd', 3), "def"));
+ assert(!strcmpnul(libsimple_strncasechrnul("abcdef", 'e', 3), "def"));
+ assert(!strcmpnul(libsimple_strncasechrnul("123123", '1', 7), "123123"));
+ assert(!strcmpnul(libsimple_strncasechrnul("123123", '3', 7), "3123"));
+ return 0;
+}
+
+#endif