From e82a1defcf86d9ed5ef38748c156cdd7d545914d Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 22 Oct 2018 21:24:05 +0200 Subject: Add strn[case]eqlen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- strneqlen.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 strneqlen.c (limited to 'strneqlen.c') diff --git a/strneqlen.c b/strneqlen.c new file mode 100644 index 0000000..5ef50e3 --- /dev/null +++ b/strneqlen.c @@ -0,0 +1,45 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" +#ifndef TEST + + +size_t +libsimple_strneqlen(const char *a, const char *b, size_t n) +{ + size_t i = 0; + for (; i < n && a[i] && a[i] == 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_strneqlen("", "", n) == 0); + assert(libsimple_strneqlen("x", "", n) == 0); + assert(libsimple_strneqlen("x", "y", n) == 0); + assert(libsimple_strneqlen("", "y", n) == 0); + assert(libsimple_strneqlen("abc", "ABC", n) == 0); + assert(libsimple_strneqlen("ABC", "abc", n) == 0); + assert(libsimple_strneqlen("ABC", "ABC", n) == MIN(n, 3)); + assert(libsimple_strneqlen("123", "123", n) == MIN(n, 3)); + for (i = 0; i <= 8; i++) { + for (j = 0; j <= 8; j++) { + assert(libsimple_strneqlen(&s[i], &t[j], n) == MIN(i == j ? 8 - i : 0, n)); + s[i] = t[j] = '\0'; + assert(libsimple_strneqlen(s, t, n) == MIN(i < j ? i : j, n)); + s[i] = "abcdefgh"[i]; + t[j] = "abcdefgh"[j]; + } + } + } + return 0; +} + +#endif -- cgit v1.2.3-70-g09d2