From bf3440c6c9a26644d77d2ebf0d24ac1583588ab7 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 19 Aug 2018 00:27:04 +0200 Subject: Add str[r]n[case]str and str[n][case]cmpnul MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 4 +++ TODO | 6 ----- libsimple.h | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- strncasestr.c | 17 ++++++++++++ strnstr.c | 17 ++++++++++++ strrncasestr.c | 23 ++++++++++++++++ strrnstr.c | 23 ++++++++++++++++ test.h | 9 +------ 8 files changed, 168 insertions(+), 16 deletions(-) delete mode 100644 TODO create mode 100644 strncasestr.c create mode 100644 strnstr.c create mode 100644 strrncasestr.c create mode 100644 strrnstr.c diff --git a/Makefile b/Makefile index 591d62a..19cebc6 100644 --- a/Makefile +++ b/Makefile @@ -39,8 +39,12 @@ OBJ =\ strchrnul.o\ strends.o\ strndup.o\ + strnstr.o\ + strncasestr.o\ strrcasestr.o\ strrstr.o\ + strrncasestr.o\ + strrnstr.o\ strstarts.o\ strtotimespec.o\ strtotimeval.o\ diff --git a/TODO b/TODO deleted file mode 100644 index f0a33fb..0000000 --- a/TODO +++ /dev/null @@ -1,6 +0,0 @@ -strnstr -strrnstr -strncasestr -strrncasestr -strcmpnul -strcasecmpnul diff --git a/libsimple.h b/libsimple.h index 9766404..602d766 100644 --- a/libsimple.h +++ b/libsimple.h @@ -701,6 +701,34 @@ char *libsimple_strrstr(const char *, const char *); #endif +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +char *libsimple_strrnstr(const char *, const char *, size_t); /* TODO test */ +#ifndef strrnstr +# define strrnstr libsimple_strrnstr +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +char *libsimple_strnstr(const char *, const char *, size_t); /* TODO test */ +#ifndef strnstr +# define strnstr libsimple_strnstr +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +char *libsimple_strrncasestr(const char *, const char *, size_t); /* TODO test */ +#ifndef strrncasestr +# define strrncasestr libsimple_strrncasestr +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +char *libsimple_strnstr(const char *, const char *, size_t); /* TODO test */ +#ifndef strncasestr +# define strncasestr libsimple_strncasestr +#endif + + _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) int libsimple_strstarts(const char *, const char *); #ifndef strstarts @@ -743,12 +771,65 @@ char *libsimple_strrcasestr(const char *, const char *); #endif +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __warn_unused_result__))) +static inline int libsimple_strcmpnul(const char *__a, const char *__b) /* TODO test */ +{ return (!__a || !__b) ? !__b - !__a : strcmp(__a, __b); } +#ifndef strcmpnul +# define strcmpnul libsimple_strcmpnul +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __warn_unused_result__))) +static inline int libsimple_strcasecmpnul(const char *__a, const char *__b) /* TODO test */ +{ return (!__a || !__b) ? !__b - !__a : strcasecmp(__a, __b); } +#ifndef strcasecmpnul +# define strcasecmpnul libsimple_strcasecmpnul +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __warn_unused_result__))) +static inline int libsimple_strncmpnul(const char *__a, const char *__b, size_t __n) /* TODO test */ +{ return (!__a || !__b) ? !__b - !__a : strncmp(__a, __b, __n); } +#ifndef strncmpnul +# define strncmpnul libsimple_strncmpnul +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __warn_unused_result__))) +static inline int libsimple_strncasecmpnul(const char *__a, const char *__b, size_t __n) /* TODO test */ +{ return (!__a || !__b) ? !__b - !__a : strncasecmp(__a, __b, __n); } +#ifndef strncasecmpnul +# define strncasecmpnul libsimple_strncasecmpnul +#endif + + _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) -static inline int streq(const char *__a, const char *__b) { return !strcmp(__a, __b); } /* TODO test */ +static inline int libsimple_streq(const char *__a, const char *__b) { return !strcmp(__a, __b); } /* TODO test */ +#ifndef streq +# define streq libsimple_streq +#endif _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) -static inline int strneq(const char *__a, const char *__b, size_t __n) { return !strncmp(__a, __b, __n); } /* TODO test */ +static inline int libsimple_strneq(const char *__a, const char *__b, size_t __n) { return !strncmp(__a, __b, __n); } /* TODO test */ +#ifndef strneq +# define strneq libsimple_strneq +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __warn_unused_result__))) +static inline int libsimple_streqnul(const char *__a, const char *__b) { return !strcmpnul(__a, __b); } /* TODO test */ +#ifndef streqnul +# define streqnul libsimple_streqnul +#endif + + +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __warn_unused_result__))) +static inline int libsimple_strneqnul(const char *__a, const char *__b, size_t __n) /* TODO test */ +{ return !strncmpnul(__a, __b, __n); } +#ifndef strneqnul +# define strneqnul libsimple_strneqnul +#endif #define malloczn(CLEAR, ...) _libsimple_malloczn((CLEAR), __VA_ARGS__, (size_t)0) /* TODO test */ diff --git a/strncasestr.c b/strncasestr.c new file mode 100644 index 0000000..cd7fd8a --- /dev/null +++ b/strncasestr.c @@ -0,0 +1,17 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" + + +char * +libsimple_strncasestr(const char *h_, const char *n, size_t len) +{ + char *h = *(char **)(void *)&h_; + size_t nn = strlen(n); + nn = MIN(nn, len); + if (!nn) + return h; + for (; *h && len--; h++) + if (!strncasecmp(h, n, nn)) + return h; + return NULL; +} diff --git a/strnstr.c b/strnstr.c new file mode 100644 index 0000000..df8708b --- /dev/null +++ b/strnstr.c @@ -0,0 +1,17 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" + + +char * +libsimple_strnstr(const char *h_, const char *n, size_t len) +{ + char *h = *(char **)(void *)&h_; + size_t nn = strlen(n); + nn = MIN(nn, len); + if (!nn) + return h; + for (; *h && len--; h++) + if (!strncmp(h, n, nn)) + return h; + return NULL; +} diff --git a/strrncasestr.c b/strrncasestr.c new file mode 100644 index 0000000..8fb0778 --- /dev/null +++ b/strrncasestr.c @@ -0,0 +1,23 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" + + +char * +libsimple_strrncasestr(const char *h_, const char *n, size_t len) +{ + char *h = *(char **)(void *)&h_; + size_t hn = strlen(h); + size_t nn = strlen(n); + hn = MIN(hn, len); + nn = MIN(nn, len); + if (!nn) + return &h[hn]; + if (hn < nn) + return NULL; + for (h += hn -= nn; hn--; h--) + if (!strncasecmp(h, n, nn)) + return h; + if (!strncasecmp(h, n, nn)) + return h; + return NULL; +} diff --git a/strrnstr.c b/strrnstr.c new file mode 100644 index 0000000..6a7b06e --- /dev/null +++ b/strrnstr.c @@ -0,0 +1,23 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" + + +char * +libsimple_strrnstr(const char *h_, const char *n, size_t len) +{ + char *h = *(char **)(void *)&h_; + size_t hn = strlen(h); + size_t nn = strlen(n); + hn = MIN(hn, len); + nn = MIN(nn, len); + if (!nn) + return &h[hn]; + if (hn < nn) + return NULL; + for (h += hn -= nn; hn--; h--) + if (!strncmp(h, n, nn)) + return h; + if (!strncmp(h, n, nn)) + return h; + return NULL; +} diff --git a/test.h b/test.h index 2a1c656..a622cba 100644 --- a/test.h +++ b/test.h @@ -9,11 +9,4 @@ exit(1);\ } while (0) - - -#define strcmp(...) test_strcmp(__VA_ARGS__) -static inline int -test_strcmp(const char *a, const char *b) -{ - return (!a || !b) ? !b - !a : (strcmp)(a, b); -} +#define strcmp(...) strcmpnul(__VA_ARGS__) -- cgit v1.2.3-70-g09d2