From 753ceac3272a8c4f150ace8b4695991a85345c3c Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 22 Sep 2023 15:28:36 +0000 Subject: Revamp case functions, add strcasestr() fallback Signed-off-by: Laurent Bercot --- src/include/skalibs/bytestr.h | 1 - src/include/skalibs/posixplz.h | 1 + src/libposixplz/strcasestr.c | 28 ++++++++++++++++++++++++++++ src/libstddjb/case_lowerb.c | 7 ++++--- src/libstddjb/case_lowers.c | 7 ++++--- src/libstddjb/case_startb.c | 1 + src/libstddjb/case_str.c | 33 --------------------------------- src/libstddjb/case_upperb.c | 7 ++++--- src/libstddjb/case_uppers.c | 7 ++++--- 9 files changed, 46 insertions(+), 46 deletions(-) create mode 100644 src/libposixplz/strcasestr.c delete mode 100644 src/libstddjb/case_str.c (limited to 'src') diff --git a/src/include/skalibs/bytestr.h b/src/include/skalibs/bytestr.h index 78fb8a1..30695f8 100644 --- a/src/include/skalibs/bytestr.h +++ b/src/include/skalibs/bytestr.h @@ -49,6 +49,5 @@ extern void case_upperb (char *, size_t) ; #define case_equalb(a, n, b) (!strncasecmp(a, (b), n)) #define case_starts(s, t) case_startb(s, strlen(s), t) extern int case_startb (char const *, size_t, char const *) gccattr_pure ; -extern size_t case_str (char const *, char const *) gccattr_pure ; #endif diff --git a/src/include/skalibs/posixplz.h b/src/include/skalibs/posixplz.h index 6dfe954..9eef116 100644 --- a/src/include/skalibs/posixplz.h +++ b/src/include/skalibs/posixplz.h @@ -24,6 +24,7 @@ extern char **environ ; */ extern void *memmem (void const *, size_t, void const *, size_t) gccattr_pure ; +extern char *strcasestr (char const *, char const *) gccattr_pure ; extern int getpeereid (int, uid_t *, gid_t *) ; diff --git a/src/libposixplz/strcasestr.c b/src/libposixplz/strcasestr.c new file mode 100644 index 0000000..2347267 --- /dev/null +++ b/src/libposixplz/strcasestr.c @@ -0,0 +1,28 @@ +/* ISC license. */ + +#include + +#ifndef SKALIBS_HASSTRCASESTR + +#include + +#include + + /* XXX: copies strings on the stack, careful with long strings */ + +char *strcasestr (char const *haystack, char const *needle) +{ + size_t nlen = strlen(needle) ; + size_t hlen = strlen(haystack) ; + char *p ; + char lneedle[nlen + 1] ; + char lhaystack[hlen + 1] ; + memcpy(lneedle, needle, nlen + 1) ; + memcpy(lhaystack, haystack, hlen + 1) ; + case_lowerb(lneedle, nlen) ; + case_lowerb(lhaystack, hlen) ; + p = strstr(lhaystack, lneedle) ; + return p ? haystack + (p - lhaystack) : 0 ; +} + +#endif diff --git a/src/libstddjb/case_lowerb.c b/src/libstddjb/case_lowerb.c index 66284df..7cc5331 100644 --- a/src/libstddjb/case_lowerb.c +++ b/src/libstddjb/case_lowerb.c @@ -1,13 +1,14 @@ /* ISC license. */ +#include + #include void case_lowerb (char *s, size_t len) { - unsigned char const d = 'a' - 'A' ; while (len--) { - if (('A' <= *s) && (*s <= 'Z')) *s += d ; - ++s ; + int c = tolower(*s) ; + *s++ = (unsigned char)c ; } } diff --git a/src/libstddjb/case_lowers.c b/src/libstddjb/case_lowers.c index 349eade..a674f28 100644 --- a/src/libstddjb/case_lowers.c +++ b/src/libstddjb/case_lowers.c @@ -1,13 +1,14 @@ /* ISC license. */ +#include + #include void case_lowers (char *s) { - unsigned char const d = 'a' - 'A' ; while (*s) { - if (('A' <= *s) && (*s <= 'Z')) *s += d ; - ++s ; + int c = tolower(*s) ; + *s++ = (unsigned char)c ; } } diff --git a/src/libstddjb/case_startb.c b/src/libstddjb/case_startb.c index 42e12f2..8b98631 100644 --- a/src/libstddjb/case_startb.c +++ b/src/libstddjb/case_startb.c @@ -2,6 +2,7 @@ #include #include + #include int case_startb (char const *s, size_t slen, char const *t) diff --git a/src/libstddjb/case_str.c b/src/libstddjb/case_str.c deleted file mode 100644 index 59f157f..0000000 --- a/src/libstddjb/case_str.c +++ /dev/null @@ -1,33 +0,0 @@ -/* ISC license. */ - -#include - -#ifdef SKALIBS_HASSTRCASESTR - -#include -#include -#include - -size_t case_str (char const *haystack, char const *needle) -{ - char *p = strcasestr(haystack, needle) ; - return p ? p - haystack : strlen(haystack) ; -} - -#else - -#include -#include -#include - -size_t case_str (char const *haystack, char const *needle) -{ - size_t nlen = strlen(needle) ; - char const *p = haystack ; - if (!nlen) return 0 ; - for (; *p ; p++) - if (!strncasecmp(p, needle, nlen)) return p - haystack ; - return strlen(haystack) ; -} - -#endif diff --git a/src/libstddjb/case_upperb.c b/src/libstddjb/case_upperb.c index 78ece8f..f245f4d 100644 --- a/src/libstddjb/case_upperb.c +++ b/src/libstddjb/case_upperb.c @@ -1,13 +1,14 @@ /* ISC license. */ +#include + #include void case_upperb (char *s, size_t len) { - unsigned char const d = 'a' - 'A' ; while (len--) { - if (('a' <= *s) && (*s <= 'z')) *s -= d ; - ++s ; + int c = toupper(*s) ; + *s++ = (unsigned char)c ; } } diff --git a/src/libstddjb/case_uppers.c b/src/libstddjb/case_uppers.c index da0837c..4191711 100644 --- a/src/libstddjb/case_uppers.c +++ b/src/libstddjb/case_uppers.c @@ -1,13 +1,14 @@ /* ISC license. */ +#include + #include void case_uppers (char *s) { - unsigned char const d = 'a' - 'A' ; while (*s) { - if (('a' <= *s) && (*s <= 'z')) *s -= d ; - ++s ; + int c = toupper(*s) ; + *s++ = (unsigned char)c ; } } -- cgit v1.2.3