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 | |
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>
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | libsimple.c | 46 | ||||
-rw-r--r-- | libsimple/str.h | 4 | ||||
-rw-r--r-- | libsimple/strn.h | 36 | ||||
-rw-r--r-- | strncaseeqlen.c | 45 | ||||
-rw-r--r-- | strneqlen.c | 45 |
6 files changed, 170 insertions, 8 deletions
@@ -97,6 +97,7 @@ OBJ =\ strncasechr.o\ strncasechrnul.o\ strncaseends.o\ + strncaseeqlen.o\ strncasestarts.o\ strncasestr.o\ strnchr.o\ @@ -104,6 +105,7 @@ OBJ =\ strndup.o\ strnend.o\ strnends.o\ + strneqlen.o\ strnstarts.o\ strnstr.o\ strrcasechr.o\ diff --git a/libsimple.c b/libsimple.c index 4aa2368..1cf1cfb 100644 --- a/libsimple.c +++ b/libsimple.c @@ -1278,6 +1278,52 @@ main(void) assert(libsimple_strrcaseeqlen("123", "123") == 3); } + for (n = 0; n < 10; n++) { + char a[] = "abcdefgh", b[] = "abcdefgh"; + size_t I, J; + assert(libsimple_strrneqlen("", "", n) == 0); + assert(libsimple_strrneqlen("x", "", n) == 0); + assert(libsimple_strrneqlen("x", "y", n) == 0); + assert(libsimple_strrneqlen("", "y", n) == 0); + for (i = 0; i <= 8; i++) { + for (j = 0; j <= 8; j++) { + I = 8 - i; + J = 8 - j; + assert(libsimple_strrneqlen(&a[i], &b[j], n) == (I == J ? MIN(I,n) : MIN(I,J) * (n >= MAX(I,J)))); + a[i] = b[j] = '\0'; + assert(libsimple_strrneqlen(a, b, n) == (MIN(i, n) == MIN(j, n) ? MIN(i, n) : 0)); + a[i] = "abcdefgh"[i]; + b[j] = "abcdefgh"[j]; + } + } + assert(libsimple_strrneqlen("abc", "ABC", n) == 0); + assert(libsimple_strrneqlen("123", "123", n) == MIN(3, n)); + } + + for (n = 0; n < 10; n++) { + char a[] = "abcdefgh", b[] = "ABCDEFGH"; + size_t I, J; + assert(libsimple_strrncaseeqlen("", "", n) == 0); + assert(libsimple_strrncaseeqlen("x", "", n) == 0); + assert(libsimple_strrncaseeqlen("x", "y", n) == 0); + assert(libsimple_strrncaseeqlen("", "y", n) == 0); + for (i = 0; i <= 8; i++) { + for (j = 0; j <= 8; j++) { + I = 8 - i; + J = 8 - j; + assert(libsimple_strrncaseeqlen(&a[i], &b[j], n) == (I == J ? MIN(I,n) : MIN(I,J) * (n >= MAX(I,J)))); + assert(libsimple_strrncaseeqlen(&b[i], &a[j], n) == (I == J ? MIN(I,n) : MIN(I,J) * (n >= MAX(I,J)))); + a[i] = b[j] = '\0'; + assert(libsimple_strrncaseeqlen(a, b, n) == (MIN(i, n) == MIN(j, n) ? MIN(i, n) : 0)); + assert(libsimple_strrncaseeqlen(b, a, n) == (MIN(i, n) == MIN(j, n) ? MIN(i, n) : 0)); + a[i] = "abcdefgh"[i]; + b[j] = "ABCDEFGH"[j]; + } + } + assert(libsimple_strrncaseeqlen("abc", "abc", n) == MIN(3, n)); + assert(libsimple_strrncaseeqlen("123", "123", n) == MIN(3, n)); + } + if (!have_custom_malloc()) { stderr_real = 1; fprintf(stderr, "\nSome tests have not been ran because malloc(3) was not " diff --git a/libsimple/str.h b/libsimple/str.h index 522062e..928eb95 100644 --- a/libsimple/str.h +++ b/libsimple/str.h @@ -320,7 +320,7 @@ size_t libsimple_strcaseeqlen(const char *, const char *); */ _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) static inline size_t libsimple_strreqlen(const char *__a, const char *__b) -{ return libsimple_memreqlen(__a, (strlen)(__a), __b, (strlen)(__b)); } +{ return memreqlen(__a, (strlen)(__a), __b, (strlen)(__b)); } #ifndef strreqlen # define strreqlen libsimple_strreqlen #endif @@ -336,7 +336,7 @@ static inline size_t libsimple_strreqlen(const char *__a, const char *__b) */ _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) static inline size_t libsimple_strrcaseeqlen(const char *__a, const char *__b) -{ return libsimple_memrcaseeqlen(__a, (strlen)(__a), __b, (strlen)(__b)); } +{ return memrcaseeqlen(__a, (strlen)(__a), __b, (strlen)(__b)); } #ifndef strrcaseeqlen # define strrcaseeqlen libsimple_strrcaseeqlen #endif diff --git a/libsimple/strn.h b/libsimple/strn.h index 97d42cb..a69e1ba 100644 --- a/libsimple/strn.h +++ b/libsimple/strn.h @@ -1,12 +1,6 @@ /* See LICENSE file for copyright and license details. */ -/* TODO strneqlen */ -/* TODO strncaseeqlen */ -/* TODO strrneqlen */ -/* TODO strrncaseeqlen */ - - _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) char *libsimple_strnchr(const char *, int, size_t); #ifndef strnchr @@ -158,3 +152,33 @@ static inline int libsimple_strncaseeqnul(const char *__a, const char *__b, size #ifndef strncaseeqnul # define strncaseeqnul libsimple_strncaseeqnul #endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +size_t libsimple_strneqlen(const char *, const char *, size_t); +#ifndef strneqlen +# define strneqlen libsimple_strneqlen +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +size_t libsimple_strncaseeqlen(const char *, const char *, size_t); +#ifndef strncaseeqlen +# define strncaseeqlen libsimple_strncaseeqlen +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +static inline size_t libsimple_strrneqlen(const char *__a, const char *__b, size_t __n) +{ return memreqlen(__a, (strnlen)(__a, __n), __b, (strnlen)(__b, __n)); } +#ifndef strrneqlen +# define strrneqlen libsimple_strrneqlen +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +static inline size_t libsimple_strrncaseeqlen(const char *__a, const char *__b, size_t __n) +{ return memrcaseeqlen(__a, (strnlen)(__a, __n), __b, (strnlen)(__b, __n)); } +#ifndef strrncaseeqlen +# define strrncaseeqlen libsimple_strrncaseeqlen +#endif 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 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 |