diff --git a/src/base/system.cpp b/src/base/system.cpp index 3d97b3fef..7820f6d5e 100644 --- a/src/base/system.cpp +++ b/src/base/system.cpp @@ -1237,7 +1237,7 @@ static int parse_int(int *out, const char **str) { int i = 0; *out = 0; - if(**str < '0' || **str > '9') + if(!str_isnum(**str)) return -1; i = **str - '0'; @@ -1245,7 +1245,7 @@ static int parse_int(int *out, const char **str) while(true) { - if(**str < '0' || **str > '9') + if(!str_isnum(**str)) { *out = i; return 0; @@ -2950,7 +2950,7 @@ int str_comp_filenames(const char *a, const char *b) for(; *a && *b; ++a, ++b) { - if(*a >= '0' && *a <= '9' && *b >= '0' && *b <= '9') + if(str_isnum(*a) && str_isnum(*b)) { result = 0; do @@ -2959,11 +2959,11 @@ int str_comp_filenames(const char *a, const char *b) result = *a - *b; ++a; ++b; - } while(*a >= '0' && *a <= '9' && *b >= '0' && *b <= '9'); + } while(str_isnum(*a) && str_isnum(*b)); - if(*a >= '0' && *a <= '9') + if(str_isnum(*a)) return 1; - else if(*b >= '0' && *b <= '9') + else if(str_isnum(*b)) return -1; else if(result || *a == '\0' || *b == '\0') return result; @@ -3563,11 +3563,16 @@ char str_uppercase(char c) return c; } +bool str_isnum(char c) +{ + return c >= '0' && c <= '9'; +} + int str_isallnum(const char *str) { while(*str) { - if(!(*str >= '0' && *str <= '9')) + if(!str_isnum(*str)) return 0; str++; } @@ -3578,7 +3583,7 @@ int str_isallnum_hex(const char *str) { while(*str) { - if(!(*str >= '0' && *str <= '9') && !(*str >= 'a' && *str <= 'f') && !(*str >= 'A' && *str <= 'F')) + if(!str_isnum(*str) && !(*str >= 'a' && *str <= 'f') && !(*str >= 'A' && *str <= 'F')) return 0; str++; } @@ -4546,7 +4551,7 @@ void os_locale_str(char *locale, size_t length) { locale[i] = '-'; } - else if(locale[i] != '-' && !(locale[i] >= 'a' && locale[i] <= 'z') && !(locale[i] >= 'A' && locale[i] <= 'Z') && !(locale[i] >= '0' && locale[i] <= '9')) + else if(locale[i] != '-' && !(locale[i] >= 'a' && locale[i] <= 'z') && !(locale[i] >= 'A' && locale[i] <= 'Z') && !(str_isnum(locale[i]))) { locale[i] = '\0'; break; diff --git a/src/base/system.h b/src/base/system.h index 85127f112..5e09879cb 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -2121,6 +2121,8 @@ int str_isspace(char c); char str_uppercase(char c); +bool str_isnum(char c); + int str_isallnum(const char *str); int str_isallnum_hex(const char *str); diff --git a/src/engine/client/backend/opengl/backend_opengl.cpp b/src/engine/client/backend/opengl/backend_opengl.cpp index ca2de6ab6..ddf23f060 100644 --- a/src/engine/client/backend/opengl/backend_opengl.cpp +++ b/src/engine/client/backend/opengl/backend_opengl.cpp @@ -5,6 +5,7 @@ #include #include +#include #if defined(BACKEND_AS_OPENGL_ES) || !defined(CONF_BACKEND_OPENGL_ES) @@ -194,7 +195,7 @@ static void ParseVersionString(EBackendType BackendType, const char *pStr, int & bool LastWasNumber = false; while(*pStr && TotalNumbersPassed < 3) { - if(*pStr >= '0' && *pStr <= '9') + if(str_isnum(*pStr)) { aCurNumberStr[CurNumberStrLen++] = (char)*pStr; LastWasNumber = true; diff --git a/src/test/str.cpp b/src/test/str.cpp index 4b4efb1c0..dde5edd27 100644 --- a/src/test/str.cpp +++ b/src/test/str.cpp @@ -4,6 +4,34 @@ #include +TEST(Str, StrIsNum) +{ + EXPECT_EQ(str_isnum('/'), false); + EXPECT_EQ(str_isnum('0'), true); + EXPECT_EQ(str_isnum('1'), true); + EXPECT_EQ(str_isnum('2'), true); + EXPECT_EQ(str_isnum('8'), true); + EXPECT_EQ(str_isnum('9'), true); + EXPECT_EQ(str_isnum(':'), false); + EXPECT_EQ(str_isnum(' '), false); +} + +TEST(Str, StrIsAllNum) +{ + EXPECT_EQ(str_isallnum("/"), 0); + EXPECT_EQ(str_isallnum("0"), 1); + EXPECT_EQ(str_isallnum("1"), 1); + EXPECT_EQ(str_isallnum("2"), 1); + EXPECT_EQ(str_isallnum("8"), 1); + EXPECT_EQ(str_isallnum("9"), 1); + EXPECT_EQ(str_isallnum(":"), 0); + EXPECT_EQ(str_isallnum(" "), 0); + + EXPECT_EQ(str_isallnum("123"), 1); + EXPECT_EQ(str_isallnum("123/"), 0); + EXPECT_EQ(str_isallnum("123:"), 0); +} + TEST(Str, Dist) { EXPECT_EQ(str_utf8_dist("aaa", "aaa"), 0);