diff options
author | Mattias Andrée <maandree@kth.se> | 2018-10-21 15:11:23 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2018-10-21 15:11:23 +0200 |
commit | 2504bc9ef9e54cd600cec086ece9a5df4cbe8dd8 (patch) | |
tree | 1255ae1d208e549adf63b134581b250c84a86f22 /strcaseeqlen.c | |
parent | Add strrcasechr (diff) | |
download | libsimple-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 '')
-rw-r--r-- | strcaseeqlen.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/strcaseeqlen.c b/strcaseeqlen.c new file mode 100644 index 0000000..625a3ad --- /dev/null +++ b/strcaseeqlen.c @@ -0,0 +1,43 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" +#ifndef TEST + + +size_t +libsimple_strcaseeqlen(const char *a, const char *b) +{ + size_t i = 0; + for (; tolower(a[i]) == tolower(b[i]) && a[i]; i++); + return i; +} + + +#else +#include "test.h" + +int +main(void) +{ + char s[] = "abcdefgh", t[] = "ABCDEFGH"; + size_t i, j; + assert(libsimple_strcaseeqlen("", "") == 0); + assert(libsimple_strcaseeqlen("x", "") == 0); + assert(libsimple_strcaseeqlen("x", "y") == 0); + assert(libsimple_strcaseeqlen("", "y") == 0); + for (i = 0; i <= 8; i++) { + for (j = 0; j <= 8; j++) { + assert(libsimple_strcaseeqlen(&s[i], &t[j]) == (i == j ? 8 - i : 0)); + assert(libsimple_strcaseeqlen(&t[i], &s[j]) == (i == j ? 8 - i : 0)); + s[i] = t[j] = '\0'; + assert(libsimple_strcaseeqlen(s, t) == (i < j ? i : j)); + assert(libsimple_strcaseeqlen(t, s) == (i < j ? i : j)); + s[i] = "abcdefgh"[i]; + t[j] = "ABCDEFGH"[j]; + } + } + assert(libsimple_strcaseeqlen("abc", "abc") == 3); + assert(libsimple_strcaseeqlen("123", "123") == 3); + return 0; +} + +#endif |