AmendHub

Download:

jcs

/

wallops

/

amendments

/

124

util: Add an implementation of strcasestr


jcs made amendment 124 3 months ago
--- util.c Fri Sep 20 20:58:32 2024 +++ util.c Fri Sep 20 21:00:58 2024 @@ -657,6 +657,43 @@ strncasecmp(const char *s1, const char *s2, size_t n) return (0); } +char * +strcasestr(const char *s, const char *find) +{ + const unsigned char *cm = strcasecmp_charmap; + const unsigned char *us = (const unsigned char *)s; + const unsigned char *ufind = (const unsigned char *)find; + const unsigned char *tus, *tufind; + + if (find == NULL) + return NULL; + if (find[0] == '\0') + return (char *)s; + + while (*us) { + if (cm[*us] == cm[*ufind]) { + tus = us; + tufind = ufind; + + while (*tufind) { + if (*tus == '\0') + goto advance_us; + if (cm[*tus] != cm[*tufind]) + goto advance_us; + tus++; + tufind++; + } + + return (char *)us; + } + +advance_us: + us++; + } + + return NULL; +} + /* * BSD warn(3) functions */ --- util.h Fri Sep 13 14:03:05 2024 +++ util.h Fri Sep 20 17:30:10 2024 @@ -153,6 +153,7 @@ long strpos_quoted(char *str, char c); size_t hexify(unsigned char *input, size_t len, unsigned char *output); size_t unhexify(unsigned char *input, size_t len, unsigned char *output); char * OSTypeToString(OSType type); +char * strcasestr(const char *s, const char *find); short strcasecmp(const char *s1, const char *s2); short strncasecmp(const char *s1, const char *s2, size_t n);