aboutsummaryrefslogtreecommitdiffstats
path: root/strrcasechr.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2018-10-21 11:59:01 +0200
committerMattias Andrée <maandree@kth.se>2018-10-21 11:59:01 +0200
commit167ddc64a60bf8360ef67c675b97a517dc0e85d1 (patch)
tree52cd6c2977f86668dab82ef90cb979c8869c07e5 /strrcasechr.c
parentAdd strcasechr and strcasechrnul (diff)
downloadlibsimple-167ddc64a60bf8360ef67c675b97a517dc0e85d1.tar.gz
libsimple-167ddc64a60bf8360ef67c675b97a517dc0e85d1.tar.bz2
libsimple-167ddc64a60bf8360ef67c675b97a517dc0e85d1.tar.xz
Add strrcasechr
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--strrcasechr.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/strrcasechr.c b/strrcasechr.c
new file mode 100644
index 0000000..979e61b
--- /dev/null
+++ b/strrcasechr.c
@@ -0,0 +1,46 @@
+/* See LICENSE file for copyright and license details. */
+#include "libsimple.h"
+#ifndef TEST
+
+
+char *
+libsimple_strrcasechr(const char *s_, int c)
+{
+ char *s = *(char **)(void *)&s_, lc = (char)tolower(c), uc = (char)toupper(c), *r = NULL;
+ if (lc != uc) {
+ for (; *s; s++)
+ if (*s == lc || *s == uc)
+ r = s;
+ } else {
+ for (; *s; s++)
+ if (*s == lc)
+ r = s;
+ }
+ return r;
+}
+
+
+#else
+#include "test.h"
+
+int
+main(void)
+{
+ assert(!strcmpnul(libsimple_strrcasechr("abcABCabcABCx123123", 'c'), "Cx123123"));
+ assert(!strcmpnul(libsimple_strrcasechr("abcABCabcABCx123123", 'a'), "ABCx123123"));
+ assert(!strcmpnul(libsimple_strrcasechr("abcABCabcABCx123123", 'x'), "x123123"));
+ assert(!strcmpnul(libsimple_strrcasechr("abcABCabcABCx123123", 'C'), "Cx123123"));
+ assert(!strcmpnul(libsimple_strrcasechr("abcABCabcABCx123123", 'A'), "ABCx123123"));
+ assert(!strcmpnul(libsimple_strrcasechr("abcABCabcABCx123123", 'X'), "x123123"));
+ assert(!strcmpnul(libsimple_strrcasechr("ABCabcABCabcX123123", 'c'), "cX123123"));
+ assert(!strcmpnul(libsimple_strrcasechr("ABCabcABCabcX123123", 'a'), "abcX123123"));
+ assert(!strcmpnul(libsimple_strrcasechr("ABCabcABCabcX123123", 'x'), "X123123"));
+ assert(!strcmpnul(libsimple_strrcasechr("ABCabcABCabcX123123", 'C'), "cX123123"));
+ assert(!strcmpnul(libsimple_strrcasechr("ABCabcABCabcX123123", 'A'), "abcX123123"));
+ assert(!strcmpnul(libsimple_strrcasechr("ABCabcABCabcX123123", 'X'), "X123123"));
+ assert(!libsimple_strrcasechr("ABCabcABCabcX123123", '0'));
+ assert(!strcmpnul(libsimple_strrcasechr("ABCabcABCabcX123123", '1'), "123"));
+ return 0;
+}
+
+#endif