Move StrToI32 to lib/

Günther Brammer 2011-05-01 18:55:02 +02:00
parent 117cbe2b81
commit fadacd3bb4
3 changed files with 55 additions and 47 deletions

View File

@ -137,8 +137,55 @@ bool IsIdentifier(char cChar)
return false;
}
static bool IsNumber(char c, int base)
{
return (c >= '0' && c <= '9' && c < ('0' + base)) ||
(c >= 'a' && c <= 'z' && c < ('a' + base - 10)) ||
(c >= 'A' && c <= 'Z' && c < ('A' + base - 10));
}
static int ToNumber(char c)
{
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'z') return 10 + c - 'a';
if (c >= 'A' && c <= 'Z') return 10 + c - 'A';
assert(false);
return 0;
}
//------------------------------- Strings ------------------------------------------------
int32_t StrToI32(const char *s, int base, const char **scan_end)
{
int sign = 1;
int32_t result = 0;
if (*s == '-')
{
sign = -1;
s++;
}
else if (*s == '+')
{
s++;
}
while (IsNumber(*s,base))
{
int value = ToNumber(*s++);
assert (value < base && value >= 0);
result *= base;
result += value;
}
if (scan_end != 0L) *scan_end = s;
if (result < 0)
{
//overflow
// we need 2147483648 (2^31) to be -2147483648 in order for -2147483648 to work
//result = INT_MAX;
}
result *= sign;
return result;
}
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
{
if (szSource == sTarget) return;

View File

@ -47,6 +47,7 @@ inline int DWordAligned(int val)
int32_t Distance(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2);
int Angle(int iX1, int iY1, int iX2, int iY2);
int Pow(int base, int exponent);
int32_t StrToI32(const char *s, int base, const char **scan_end);
#include <cstring>
inline void ZeroMem(void *lpMem, size_t dwSize)

View File

@ -471,6 +471,13 @@ static C4ScriptOpDef C4ScriptOpMap[] =
{ 0, NULL, AB_ERR, AB_ERR, 0, 0, 0, C4V_Any, C4V_Any, C4V_Any}
};
static C4V_Type GetOperatorRetType(C4AulBCCType Code)
{
for (int i = 0; C4ScriptOpMap[i].Identifier; i++)
if (C4ScriptOpMap[i].Code == Code)
return C4ScriptOpMap[i].RetType;
}
int C4AulParseState::GetOperator(const char* pScript)
{
// return value:
@ -502,53 +509,6 @@ int C4AulParseState::GetOperator(const char* pScript)
return -1;
}
static bool IsNumber(char c, int base)
{
return (c >= '0' && c <= '9' && c < ('0' + base)) ||
(c >= 'a' && c <= 'z' && c < ('a' + base - 10)) ||
(c >= 'A' && c <= 'Z' && c < ('A' + base - 10));
}
static int ToNumber(char c)
{
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'z') return 10 + c - 'a';
if (c >= 'A' && c <= 'Z') return 10 + c - 'A';
assert(false);
return 0;
}
static int32_t StrToI32(const char *s, int base, const char **scan_end)
{
int sign = 1;
int32_t result = 0;
if (*s == '-')
{
sign = -1;
s++;
}
else if (*s == '+')
{
s++;
}
while (IsNumber(*s,base))
{
int value = ToNumber(*s++);
assert (value < base && value >= 0);
result *= base;
result += value;
}
if (scan_end != 0L) *scan_end = s;
if (result < 0)
{
//overflow
// we need 2147483648 (2^31) to be -2147483648 in order for -2147483648 to work
//result = INT_MAX;
}
result *= sign;
return result;
}
void C4AulParseState::ClearToken()
{
// if last token was a string, make sure its ref is deleted