Add str_isnum

This commit is contained in:
ChillerDragon 2024-04-30 17:13:12 +08:00
parent f0e804c575
commit 740aada779
4 changed files with 46 additions and 10 deletions

View file

@ -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;

View file

@ -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);

View file

@ -5,6 +5,7 @@
#include <engine/client/backend_sdl.h>
#include <base/detect.h>
#include <base/system.h>
#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;

View file

@ -4,6 +4,34 @@
#include <game/gamecore.h>
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);