optimize the SDL_str(case)cmp functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c
index e23f14a..1331930 100644
--- a/src/stdlib/SDL_string.c
+++ b/src/stdlib/SDL_string.c
@@ -1070,13 +1070,16 @@ SDL_strcmp(const char *str1, const char *str2)
#if defined(HAVE_STRCMP)
return strcmp(str1, str2);
#else
- while (*str1 && *str2) {
- if (*str1 != *str2)
+ int result;
+
+ while(1) {
+ result = (int)((unsigned char) *str1 - (unsigned char) *str2);
+ if (result != 0 || (*str1 == '\0'/* && *str2 == '\0'*/))
break;
++str1;
++str2;
}
- return (int)((unsigned char) *str1 - (unsigned char) *str2);
+ return result;
#endif /* HAVE_STRCMP */
}
@@ -1086,17 +1089,20 @@ SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
#if defined(HAVE_STRNCMP)
return strncmp(str1, str2, maxlen);
#else
- while (*str1 && *str2 && maxlen) {
- if (*str1 != *str2)
+ int result;
+
+ while (maxlen) {
+ result = (int) (unsigned char) *str1 - (unsigned char) *str2;
+ if (result != 0 || *str1 == '\0'/* && *str2 == '\0'*/)
break;
++str1;
++str2;
--maxlen;
}
if (!maxlen) {
- return 0;
+ result = 0;
}
- return (int) ((unsigned char) *str1 - (unsigned char) *str2);
+ return result;
#endif /* HAVE_STRNCMP */
}
@@ -1108,19 +1114,18 @@ SDL_strcasecmp(const char *str1, const char *str2)
#elif defined(HAVE__STRICMP)
return _stricmp(str1, str2);
#else
- char a = 0;
- char b = 0;
- while (*str1 && *str2) {
+ int a, b, result;
+
+ while (1) {
a = SDL_toupper((unsigned char) *str1);
b = SDL_toupper((unsigned char) *str2);
- if (a != b)
+ result = a - b;
+ if (result != 0 || a == 0 /*&& b == 0*/)
break;
++str1;
++str2;
}
- a = SDL_toupper((unsigned char) *str1);
- b = SDL_toupper((unsigned char) *str2);
- return (int) ((unsigned char) a - (unsigned char) b);
+ return result;
#endif /* HAVE_STRCASECMP */
}
@@ -1132,24 +1137,21 @@ SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
#elif defined(HAVE__STRNICMP)
return _strnicmp(str1, str2, maxlen);
#else
- char a = 0;
- char b = 0;
- while (*str1 && *str2 && maxlen) {
+ int a, b, result;
+
+ while (maxlen) {
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
- if (a != b)
+ result = a - b;
+ if (result != 0 || a == 0 /*&& b == 0*/)
break;
++str1;
++str2;
--maxlen;
}
- if (maxlen == 0) {
- return 0;
- } else {
- a = SDL_tolower((unsigned char) *str1);
- b = SDL_tolower((unsigned char) *str2);
- return (int) ((unsigned char) a - (unsigned char) b);
- }
+ if (maxlen == 0)
+ result = 0;
+ return result;
#endif /* HAVE_STRNCASECMP */
}