optimize the SDL_str(case)cmp functions

main
pionere 2022-02-05 11:01:25 +01:00 committed by Sam Lantinga
parent aec58d8208
commit 25203222f5
1 changed files with 27 additions and 25 deletions

View File

@ -1070,13 +1070,16 @@ SDL_strcmp(const char *str1, const char *str2)
#if defined(HAVE_STRCMP) #if defined(HAVE_STRCMP)
return strcmp(str1, str2); return strcmp(str1, str2);
#else #else
while (*str1 && *str2) { int result;
if (*str1 != *str2)
while(1) {
result = (int)((unsigned char) *str1 - (unsigned char) *str2);
if (result != 0 || (*str1 == '\0'/* && *str2 == '\0'*/))
break; break;
++str1; ++str1;
++str2; ++str2;
} }
return (int)((unsigned char) *str1 - (unsigned char) *str2); return result;
#endif /* HAVE_STRCMP */ #endif /* HAVE_STRCMP */
} }
@ -1086,17 +1089,20 @@ SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
#if defined(HAVE_STRNCMP) #if defined(HAVE_STRNCMP)
return strncmp(str1, str2, maxlen); return strncmp(str1, str2, maxlen);
#else #else
while (*str1 && *str2 && maxlen) { int result;
if (*str1 != *str2)
while (maxlen) {
result = (int) (unsigned char) *str1 - (unsigned char) *str2;
if (result != 0 || *str1 == '\0'/* && *str2 == '\0'*/)
break; break;
++str1; ++str1;
++str2; ++str2;
--maxlen; --maxlen;
} }
if (!maxlen) { if (!maxlen) {
return 0; result = 0;
} }
return (int) ((unsigned char) *str1 - (unsigned char) *str2); return result;
#endif /* HAVE_STRNCMP */ #endif /* HAVE_STRNCMP */
} }
@ -1108,19 +1114,18 @@ SDL_strcasecmp(const char *str1, const char *str2)
#elif defined(HAVE__STRICMP) #elif defined(HAVE__STRICMP)
return _stricmp(str1, str2); return _stricmp(str1, str2);
#else #else
char a = 0; int a, b, result;
char b = 0;
while (*str1 && *str2) { while (1) {
a = SDL_toupper((unsigned char) *str1); a = SDL_toupper((unsigned char) *str1);
b = SDL_toupper((unsigned char) *str2); b = SDL_toupper((unsigned char) *str2);
if (a != b) result = a - b;
if (result != 0 || a == 0 /*&& b == 0*/)
break; break;
++str1; ++str1;
++str2; ++str2;
} }
a = SDL_toupper((unsigned char) *str1); return result;
b = SDL_toupper((unsigned char) *str2);
return (int) ((unsigned char) a - (unsigned char) b);
#endif /* HAVE_STRCASECMP */ #endif /* HAVE_STRCASECMP */
} }
@ -1132,24 +1137,21 @@ SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
#elif defined(HAVE__STRNICMP) #elif defined(HAVE__STRNICMP)
return _strnicmp(str1, str2, maxlen); return _strnicmp(str1, str2, maxlen);
#else #else
char a = 0; int a, b, result;
char b = 0;
while (*str1 && *str2 && maxlen) { while (maxlen) {
a = SDL_tolower((unsigned char) *str1); a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2); b = SDL_tolower((unsigned char) *str2);
if (a != b) result = a - b;
if (result != 0 || a == 0 /*&& b == 0*/)
break; break;
++str1; ++str1;
++str2; ++str2;
--maxlen; --maxlen;
} }
if (maxlen == 0) { if (maxlen == 0)
return 0; result = 0;
} else { return result;
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
return (int) ((unsigned char) a - (unsigned char) b);
}
#endif /* HAVE_STRNCASECMP */ #endif /* HAVE_STRNCASECMP */
} }