diff options
author | Mattias Andrée <maandree@kth.se> | 2018-10-22 19:57:13 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2018-10-22 19:57:13 +0200 |
commit | b6f3b8d1e7161441303e518febabcb5a7958f1f3 (patch) | |
tree | 703f9b353518ab7bffb344d38251106ddd477534 | |
parent | Add strn{,case}chr{,nul}, strnend, and strrn{,case}chr (diff) | |
download | libsimple-b6f3b8d1e7161441303e518febabcb5a7958f1f3.tar.gz libsimple-b6f3b8d1e7161441303e518febabcb5a7958f1f3.tar.bz2 libsimple-b6f3b8d1e7161441303e518febabcb5a7958f1f3.tar.xz |
Add strn{case}{starts,ends}
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | libsimple/strn.h | 44 | ||||
-rw-r--r-- | strncaseends.c | 49 | ||||
-rw-r--r-- | strncasestarts.c | 49 | ||||
-rw-r--r-- | strnends.c | 49 | ||||
-rw-r--r-- | strnstarts.c | 49 |
6 files changed, 234 insertions, 10 deletions
@@ -96,11 +96,15 @@ OBJ =\ streqlen.o\ strncasechr.o\ strncasechrnul.o\ + strncaseends.o\ + strncasestarts.o\ strncasestr.o\ strnchr.o\ strnchrnul.o\ strndup.o\ strnend.o\ + strnends.o\ + strnstarts.o\ strnstr.o\ strrcasechr.o\ strrcasestr.o\ diff --git a/libsimple/strn.h b/libsimple/strn.h index 66b1e10..97d42cb 100644 --- a/libsimple/strn.h +++ b/libsimple/strn.h @@ -1,10 +1,6 @@ /* See LICENSE file for copyright and license details. */ -/* TODO strnstarts */ -/* TODO strncasestarts */ -/* TODO strnends */ -/* TODO strncaseends */ /* TODO strneqlen */ /* TODO strncaseeqlen */ /* TODO strrneqlen */ @@ -68,16 +64,16 @@ char *libsimple_strnstr(const char *, const char *, size_t); _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) -char *libsimple_strrnstr(const char *, const char *, size_t); -#ifndef strrnstr -# define strrnstr libsimple_strrnstr +char *libsimple_strncasestr(const char *, const char *, size_t); +#ifndef strncasestr +# define strncasestr libsimple_strncasestr #endif _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) -char *libsimple_strncasestr(const char *, const char *, size_t); -#ifndef strncasestr -# define strncasestr libsimple_strncasestr +char *libsimple_strrnstr(const char *, const char *, size_t); +#ifndef strrnstr +# define strrnstr libsimple_strrnstr #endif @@ -88,6 +84,34 @@ char *libsimple_strrncasestr(const char *, const char *, size_t); #endif +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +int libsimple_strnstarts(const char *, const char *, size_t); +#ifndef strnstarts +# define strnstarts libsimple_strnstarts +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +int libsimple_strncasestarts(const char *, const char *, size_t); +#ifndef strncasestarts +# define strncasestarts libsimple_strncasestarts +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +int libsimple_strnends(const char *, const char *, size_t); +#ifndef strnends +# define strnends libsimple_strnends +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +int libsimple_strncaseends(const char *, const char *, size_t); +#ifndef strncaseends +# define strncaseends libsimple_strncaseends +#endif + + _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __warn_unused_result__))) static inline int libsimple_strncmpnul(const char *__a, const char *__b, size_t __n) { return (!__a || !__b) ? !__b - !__a : strncmp(__a, __b, __n); } diff --git a/strncaseends.c b/strncaseends.c new file mode 100644 index 0000000..708c16a --- /dev/null +++ b/strncaseends.c @@ -0,0 +1,49 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" +#ifndef TEST + + +int +libsimple_strncaseends(const char *s_, const char *t, size_t n) +{ + char *s = *(char **)(void *)&s_; + size_t len = strlen(t); + n = strnlen(s, n); + if (n < len) + return 0; + return !strncasecmp(s, t, len); +} + + +#else +#include "test.h" + +int +main(void) +{ + size_t n, i, j; + char buf[10]; + for (n = 0; n <= 8; n++) { + assert(libsimple_strncaseends("abcABC", "", n) == 1); + assert(libsimple_strncaseends("ABCabc", "", n) == 1); + assert(libsimple_strncaseends("123456", "", n) == 1); + for (i = 0; i < 7; i++) { + for (j = 1; j < 8 - i; j++) { + strcpy(buf, &"abcABCx"[i])[j] = '\0'; + assert(libsimple_strncaseends("abcABC", buf, n) == ((!i || i == 3) && j <= MIN(n, 6 - i))); + assert(libsimple_strncaseends("ABCabc", buf, n) == ((!i || i == 3) && j <= MIN(n, 6 - i))); + strcpy(buf, &"ABCabcx"[i])[j] = '\0'; + assert(libsimple_strncaseends("abcABC", buf, n) == ((!i || i == 3) && j <= MIN(n, 6 - i))); + assert(libsimple_strncaseends("ABCabc", buf, n) == ((!i || i == 3) && j <= MIN(n, 6 - i))); + strcpy(buf, &"1234567"[i])[j] = '\0'; + assert(libsimple_strncaseends("123456", buf, n) == (!i && j >= 1 && j <= MIN(n, 6))); + assert(libsimple_strncaseends("zzzzzz", buf, n) == 0); + strcpy(buf, &"aaaaaaa"[i])[j] = '\0'; + assert(libsimple_strncaseends("zzzzzz", buf, n) == 0); + } + } + } + return 0; +} + +#endif diff --git a/strncasestarts.c b/strncasestarts.c new file mode 100644 index 0000000..d7c6350 --- /dev/null +++ b/strncasestarts.c @@ -0,0 +1,49 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" +#ifndef TEST + + +int +libsimple_strncasestarts(const char *s_, const char *t, size_t n) +{ + char *s = *(char **)(void *)&s_; + size_t len = strlen(t); + n = strnlen(s, n); + if (n < len) + return 0; + return !strncasecmp(s, t, len); +} + + +#else +#include "test.h" + +int +main(void) +{ + size_t n, i, j; + char buf[10]; + for (n = 0; n <= 8; n++) { + assert(libsimple_strncasestarts("abcABC", "", n) == 1); + assert(libsimple_strncasestarts("ABCabc", "", n) == 1); + assert(libsimple_strncasestarts("123456", "", n) == 1); + for (i = 0; i < 7; i++) { + for (j = 1; j < 8 - i; j++) { + strcpy(buf, &"abcABCx"[i])[j] = '\0'; + assert(libsimple_strncasestarts("abcABC", buf, n) == ((i == 0 || i == 3) && i + j < 7 && n >= j)); + assert(libsimple_strncasestarts("ABCabc", buf, n) == ((i == 0 || i == 3) && i + j < 7 && n >= j)); + strcpy(buf, &"ABCabcx"[i])[j] = '\0'; + assert(libsimple_strncasestarts("abcABC", buf, n) == ((i == 0 || i == 3) && i + j < 7 && n >= j)); + assert(libsimple_strncasestarts("ABCabc", buf, n) == ((i == 0 || i == 3) && i + j < 7 && n >= j)); + strcpy(buf, &"1234567"[i])[j] = '\0'; + assert(libsimple_strncasestarts("123456", buf, n) == (i == 0 && j < 7 && n >= j)); + assert(libsimple_strncasestarts("zzzzzz", buf, n) == 0); + strcpy(buf, &"aaaaaaa"[i])[j] = '\0'; + assert(libsimple_strncasestarts("zzzzzz", buf, n) == 0); + } + } + } + return 0; +} + +#endif diff --git a/strnends.c b/strnends.c new file mode 100644 index 0000000..3d92ca0 --- /dev/null +++ b/strnends.c @@ -0,0 +1,49 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" +#ifndef TEST + + +int +libsimple_strnends(const char *s_, const char *t, size_t n) +{ + char *s = *(char **)(void *)&s_; + size_t len = strlen(t); + n = strnlen(s, n); + if (n < len) + return 0; + return !strncmp(s, t, len); +} + + +#else +#include "test.h" + +int +main(void) +{ + size_t n, i, j; + char buf[10]; + for (n = 0; n <= 8; n++) { + assert(libsimple_strnends("abcABC", "", n) == 1); + assert(libsimple_strnends("ABCabc", "", n) == 1); + assert(libsimple_strnends("123456", "", n) == 1); + for (i = 0; i < 7; i++) { + for (j = 1; j < 8 - i; j++) { + strcpy(buf, &"abcABCx"[i])[j] = '\0'; + assert(libsimple_strnends("abcABC", buf, n) == (!i && j >= 1 && j <= MIN(n, 6))); + assert(libsimple_strnends("ABCabc", buf, n) == (i == 3 && n >= 1 && j <= MIN(n, 3))); + strcpy(buf, &"ABCabcx"[i])[j] = '\0'; + assert(libsimple_strnends("abcABC", buf, n) == (i == 3 && n >= 1 && j <= MIN(n, 3))); + assert(libsimple_strnends("ABCabc", buf, n) == (!i && j >= 1 && j <= MIN(n, 6))); + strcpy(buf, &"1234567"[i])[j] = '\0'; + assert(libsimple_strnends("123456", buf, n) == (!i && j >= 1 && j <= MIN(n, 6))); + assert(libsimple_strnends("zzzzzz", buf, n) == 0); + strcpy(buf, &"aaaaaaa"[i])[j] = '\0'; + assert(libsimple_strnends("zzzzzz", buf, n) == 0); + } + } + } + return 0; +} + +#endif diff --git a/strnstarts.c b/strnstarts.c new file mode 100644 index 0000000..727cf3e --- /dev/null +++ b/strnstarts.c @@ -0,0 +1,49 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" +#ifndef TEST + + +int +libsimple_strnstarts(const char *s_, const char *t, size_t n) +{ + char *s = *(char **)(void *)&s_; + size_t len = strlen(t); + n = strnlen(s, n); + if (n < len) + return 0; + return !strncmp(s, t, len); +} + + +#else +#include "test.h" + +int +main(void) +{ + size_t n, i, j; + char buf[10]; + for (n = 0; n <= 8; n++) { + assert(libsimple_strnstarts("abcABC", "", n) == 1); + assert(libsimple_strnstarts("ABCabc", "", n) == 1); + assert(libsimple_strnstarts("123456", "", n) == 1); + for (i = 0; i < 7; i++) { + for (j = 1; j < 8 - i; j++) { + strcpy(buf, &"abcABCx"[i])[j] = '\0'; + assert(libsimple_strnstarts("abcABC", buf, n) == (i == 0 && j < 7 && n >= j)); + assert(libsimple_strnstarts("ABCabc", buf, n) == (i == 3 && j < 4 && n >= j)); + strcpy(buf, &"ABCabcx"[i])[j] = '\0'; + assert(libsimple_strnstarts("abcABC", buf, n) == (i == 3 && j < 4 && n >= j)); + assert(libsimple_strnstarts("ABCabc", buf, n) == (i == 0 && j < 7 && n >= j)); + strcpy(buf, &"1234567"[i])[j] = '\0'; + assert(libsimple_strnstarts("123456", buf, n) == (i == 0 && j < 7 && n >= j)); + assert(libsimple_strnstarts("zzzzzz", buf, n) == 0); + strcpy(buf, &"aaaaaaa"[i])[j] = '\0'; + assert(libsimple_strnstarts("zzzzzz", buf, n) == 0); + } + } + } + return 0; +} + +#endif |