diff options
author | Mattias Andrée <maandree@kth.se> | 2018-10-22 21:24:05 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2018-10-22 21:24:05 +0200 |
commit | e82a1defcf86d9ed5ef38748c156cdd7d545914d (patch) | |
tree | 85c10426cac9f75b4aaf15fe2281bd44de08ee1c /strncaseeqlen.c | |
parent | Add strn{case}{starts,ends} (diff) | |
download | libsimple-e82a1defcf86d9ed5ef38748c156cdd7d545914d.tar.gz libsimple-e82a1defcf86d9ed5ef38748c156cdd7d545914d.tar.bz2 libsimple-e82a1defcf86d9ed5ef38748c156cdd7d545914d.tar.xz |
Add strn[case]eqlen
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'strncaseeqlen.c')
-rw-r--r-- | strncaseeqlen.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/strncaseeqlen.c b/strncaseeqlen.c new file mode 100644 index 0000000..56ace80 --- /dev/null +++ b/strncaseeqlen.c @@ -0,0 +1,45 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" +#ifndef TEST + + +size_t +libsimple_strncaseeqlen(const char *a, const char *b, size_t n) +{ + size_t i = 0; + for (; i < n && a[i] && tolower(a[i]) == tolower(b[i]); i++); + return i; +} + + +#else +#include "test.h" + +int +main(void) +{ + char s[] = "abcdefgh", t[] = "abcdefgh"; + size_t i, j, n; + for (n = 0; n < 10; n++) { + assert(libsimple_strncaseeqlen("", "", n) == 0); + assert(libsimple_strncaseeqlen("x", "", n) == 0); + assert(libsimple_strncaseeqlen("x", "y", n) == 0); + assert(libsimple_strncaseeqlen("", "y", n) == 0); + assert(libsimple_strncaseeqlen("abc", "ABC", n) == MIN(n, 3)); + assert(libsimple_strncaseeqlen("ABC", "abc", n) == MIN(n, 3)); + assert(libsimple_strncaseeqlen("ABC", "ABC", n) == MIN(n, 3)); + assert(libsimple_strncaseeqlen("123", "123", n) == MIN(n, 3)); + for (i = 0; i <= 8; i++) { + for (j = 0; j <= 8; j++) { + assert(libsimple_strncaseeqlen(&s[i], &t[j], n) == MIN(i == j ? 8 - i : 0, n)); + s[i] = t[j] = '\0'; + assert(libsimple_strncaseeqlen(s, t, n) == MIN(i < j ? i : j, n)); + s[i] = "abcdefgh"[i]; + t[j] = "abcdefgh"[j]; + } + } + } + return 0; +} + +#endif |