aboutsummaryrefslogtreecommitdiffstats
path: root/memrcaseeqlen.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2018-10-21 15:11:23 +0200
committerMattias Andrée <maandree@kth.se>2018-10-21 15:11:23 +0200
commit2504bc9ef9e54cd600cec086ece9a5df4cbe8dd8 (patch)
tree1255ae1d208e549adf63b134581b250c84a86f22 /memrcaseeqlen.c
parentAdd strrcasechr (diff)
downloadlibsimple-2504bc9ef9e54cd600cec086ece9a5df4cbe8dd8.tar.gz
libsimple-2504bc9ef9e54cd600cec086ece9a5df4cbe8dd8.tar.bz2
libsimple-2504bc9ef9e54cd600cec086ece9a5df4cbe8dd8.tar.xz
Add {str,mem}[r][case]eqlen
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'memrcaseeqlen.c')
-rw-r--r--memrcaseeqlen.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/memrcaseeqlen.c b/memrcaseeqlen.c
new file mode 100644
index 0000000..3f586b7
--- /dev/null
+++ b/memrcaseeqlen.c
@@ -0,0 +1,41 @@
+/* See LICENSE file for copyright and license details. */
+#include "libsimple.h"
+#ifndef TEST
+
+
+size_t
+libsimple_memrcaseeqlen(const void *a_, size_t n, const void *b_, size_t m)
+{
+ const char *a = &((char *)a_)[n], *b = &((char *)b_)[m];
+ size_t i = 0, len = n < m ? n : m;
+ for (; i < len && (--a, --b, tolower(*a) == tolower(*b)); i++);
+ return i;
+}
+
+
+#else
+#include "test.h"
+
+int
+main(void)
+{
+ char s[] = "abcdefgh", t[] = "ABCDEFGH";
+ size_t i, j;
+ assert(libsimple_memrcaseeqlen("x", 0, "x", 0) == 0);
+ assert(libsimple_memrcaseeqlen("x", 1, "x", 0) == 0);
+ assert(libsimple_memrcaseeqlen("x", 1, "y", 1) == 0);
+ assert(libsimple_memrcaseeqlen("y", 0, "y", 1) == 0);
+ for (i = 0; i <= 8; i++) {
+ for (j = 0; j <= 8; j++) {
+ assert(libsimple_memrcaseeqlen(&s[i], 8 - i, &t[j], 8 - j) == 8 - (i > j ? i : j));
+ assert(libsimple_memrcaseeqlen(s, i, t, j) == (i == j ? i : 0));
+ assert(libsimple_memrcaseeqlen(&t[i], 8 - i, &s[j], 8 - j) == 8 - (i > j ? i : j));
+ assert(libsimple_memrcaseeqlen(t, i, s, j) == (i == j ? i : 0));
+ }
+ }
+ assert(libsimple_memrcaseeqlen("abc", 3, "abc", 3) == 3);
+ assert(libsimple_memrcaseeqlen("123", 3, "123", 3) == 3);
+ return 0;
+}
+
+#endif