mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-03-02 20:10:32 +00:00
Merge pull request #1532 from GarageGames/pr/1143
[CLONE] More consolefunctions #1143
This commit is contained in:
commit
794f1b9536
7 changed files with 706 additions and 0 deletions
|
|
@ -152,11 +152,20 @@ inline U32 dAtoui(const char *str, U32 base = 10)
|
|||
return strtoul(str, NULL, base);
|
||||
}
|
||||
|
||||
inline U16 dAtous(const char *str, U32 base = 10)
|
||||
{
|
||||
return strtoul(str, NULL, base);
|
||||
}
|
||||
|
||||
inline F32 dAtof(const char *str)
|
||||
{
|
||||
return strtof(str, NULL);
|
||||
}
|
||||
|
||||
inline F64 dAtod(const char *str)
|
||||
{
|
||||
return strtod(str, NULL);
|
||||
}
|
||||
|
||||
inline char dToupper(const char c)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1628,3 +1628,103 @@ String String::GetTrailingNumber(const char* str, S32& number)
|
|||
|
||||
return base.substr(0, p - base.c_str());
|
||||
}
|
||||
|
||||
String String::GetFirstNumber(const char* str, U32& startPos, U32& endPos)
|
||||
{
|
||||
// Check for trivial strings
|
||||
if (!str || !str[0])
|
||||
return String::EmptyString;
|
||||
|
||||
// Find the number at the end of the string
|
||||
String base(str);
|
||||
const char* p = base.c_str();
|
||||
const char* end = base.c_str() + base.length() - 1;
|
||||
bool dec = false;
|
||||
startPos = 0;
|
||||
|
||||
//Check if we are just a digit
|
||||
if(p == end && isdigit(*p))
|
||||
return base;
|
||||
|
||||
//Look for the first digit
|
||||
while ((p != end) && (dIsspace(*p) || !isdigit(*p)))
|
||||
{
|
||||
p++;
|
||||
startPos++;
|
||||
}
|
||||
|
||||
//Handle if we are at the end and found nothing
|
||||
if(p == end && !isdigit(*p))
|
||||
return "";
|
||||
|
||||
//update our end position at least to the start of our number
|
||||
endPos = startPos;
|
||||
|
||||
//Backup our ptr
|
||||
const char* backup = p;
|
||||
|
||||
//Check for any negative or decimal values
|
||||
if(startPos > 0)
|
||||
{
|
||||
p--;
|
||||
startPos--;
|
||||
if(*p == '.')
|
||||
{
|
||||
dec = true;
|
||||
|
||||
//ignore any duplicate periods
|
||||
while ((p != base.c_str()) && (*p == '.'))
|
||||
{
|
||||
p--;
|
||||
startPos--;
|
||||
}
|
||||
|
||||
//Found a decimal lets still check for negative sign
|
||||
if(startPos > 0)
|
||||
{
|
||||
p--;
|
||||
startPos--;
|
||||
if((*p != '-') && (*p != '_'))
|
||||
{
|
||||
startPos++;
|
||||
p++;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if((*p != '-') && (*p != '_'))
|
||||
{
|
||||
//go back to where we where cause no decimal or negative sign found
|
||||
startPos++;
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
//Restore where we were
|
||||
p = backup;
|
||||
|
||||
//look for the end of the digits
|
||||
bool justFoundDec = false;
|
||||
while (p != end)
|
||||
{
|
||||
if(*p == '.')
|
||||
{
|
||||
if(dec && !justFoundDec)
|
||||
break;
|
||||
else
|
||||
{
|
||||
dec = true;
|
||||
justFoundDec = true;
|
||||
}
|
||||
}
|
||||
else if(!isdigit(*p))
|
||||
break;
|
||||
else if(justFoundDec)
|
||||
justFoundDec = false;
|
||||
|
||||
p++;
|
||||
endPos++;
|
||||
}
|
||||
|
||||
U32 len = (!isdigit(*p)) ? endPos - startPos : (endPos + 1) - startPos;
|
||||
return base.substr(startPos, len);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -191,6 +191,7 @@ public:
|
|||
static String ToUpper(const String &string);
|
||||
|
||||
static String GetTrailingNumber(const char* str, S32& number);
|
||||
static String GetFirstNumber(const char* str, U32& startPos, U32& endPos);
|
||||
|
||||
/// @}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue