Replace my shitty fallbacks with those from FreeBSD

This commit is contained in:
Elizabeth Myers 2016-04-10 17:25:32 -05:00
parent 3eb5fee4f1
commit 8b813d3060

View file

@ -140,20 +140,20 @@ rb_string_to_array(char *string, char **parv, int maxpara)
#ifndef HAVE_STRCASECMP #ifndef HAVE_STRCASECMP
#ifndef _WIN32 #ifndef _WIN32
/* Crummy fallback impl by me. --Elizafox */ /* Fallback taken from FreeBSD. --Elizafox */
int int
rb_strcasecmp(const char *s1, const char *s2) rb_strcasecmp(const char *s1, const char *s2)
{ {
for(; *s1 != '\0' || *s2 != '\0'; s1++, s2++) const unsigned char *us1 = (const unsigned char *)s1,
const unsigned char *us2 = (const unsigned char *)s2;
while (tolower(*us1) == tolower(*us2++))
{ {
if(tolower(*s1) != tolower(*s2)) if (*us1++ == '\0')
return tolower(*s1) - tolower(*s2); return 0;
} }
if(*s1 == '\0' && *s2 == '\0') return (tolower(*us1) - tolower(*--us2));
return 0;
return tolower(*(--s1)) - tolower(*(--s2));
} }
#else /* _WIN32 */ #else /* _WIN32 */
int int
@ -172,23 +172,24 @@ rb_strcasecmp(const char *s1, const char *s2)
#ifndef HAVE_STRNCASECMP #ifndef HAVE_STRNCASECMP
#ifndef _WIN32 #ifndef _WIN32
/* Crummy fallback impl by me. --Elizafox */ /* Fallback taken from FreeBSD. --Elizafox */
int int
rb_strncasecmp(const char *s1, const char *s2, size_t n) strncasecmp(const char *s1, const char *s2, size_t n)
{ {
if(n == 0) if (n != 0)
return 0;
for(; *s1 != '\0' || *s2 != '\0' || n; s1++, s2++, n--)
{ {
if(tolower(*s1) != tolower(*s2)) const unsigned char *us1 = (const unsigned char *)s1;
return tolower(*s1) - tolower(*s2); const unsigned char *us2 = (const unsigned char *)s2;
do
{
if (tolower(*us1) != tolower(*us2++))
return (tolower(*us1) - tolower(*--us2));
if (*us1++ == '\0')
break;
} while (--n != 0);
} }
return 0;
if(*s1 == '\0' && *s2 == '\0')
return 0;
return tolower(*(--s1)) - tolower(*(--s2));
} }
#else /* _WIN32 */ #else /* _WIN32 */
int int
@ -206,22 +207,25 @@ rb_strncasecmp(const char *s1, const char *s2, size_t n)
#endif #endif
#ifndef HAVE_STRCASESTR #ifndef HAVE_STRCASESTR
/* Crummy fallback impl by me. --Elizafox */ /* Fallback taken from FreeBSD. --Elizafox */
char * char *
rb_strcasestr(const char *s, const char *find) rb_strcasestr(cnst char *s, const char *find)
{ {
size_t len_f = strlen(find); char c, sc;
size_t len;
if(*s == '\0') if ((c = *find++) != 0) {
return (char *)s; c = tolower((unsigned char)c);
len = strlen(find);
for(char *c = (char *)s; *c != '\0'; c++) do {
{ do {
if(*c == *find && strncasecmp(c, find, len_f) == 0) if ((sc = *s++) == 0)
return c; return (NULL);
} while ((char)tolower((unsigned char)sc) != c);
} while (rb_strncasecmp(s, find, len) != 0);
s--;
} }
return ((char *)s);
return NULL;
} }
#else #else
char * char *