update
This commit is contained in:
parent
8477a5662f
commit
8519b134be
|
@ -12,52 +12,85 @@
|
||||||
'0xdeadbeef'
|
'0xdeadbeef'
|
||||||
'0xDEADBEEF'
|
'0xDEADBEEF'
|
||||||
'0o71230'
|
'0o71230'
|
||||||
|
'1e23'
|
||||||
|
'1E-123'
|
||||||
|
'1.34E10'
|
||||||
*/
|
*/
|
||||||
|
inline bool check_hex_string(std::string str)
|
||||||
|
{
|
||||||
|
int len=str.length();
|
||||||
|
for(int i=2;i<len;++i)
|
||||||
|
if(!(('0'<=str[i] && str[i]<='9') || ('a'<=str[i] && str[i]<='f') || ('A'<=str[i] && str[i]<='F')))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
inline bool check_oct_string(std::string str)
|
||||||
|
{
|
||||||
|
int len=str.length();
|
||||||
|
for(int i=2;i<len;++i)
|
||||||
|
if(str[i]<'0' || str[i]>'7')
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
inline bool check_dec_string(std::string str)
|
||||||
|
{
|
||||||
|
int len=str.length();
|
||||||
|
int dot_cnt=0;
|
||||||
|
int i=0;
|
||||||
|
for(;i<len;++i)
|
||||||
|
{
|
||||||
|
if(str[i]=='.')
|
||||||
|
{
|
||||||
|
if(i==len-1)
|
||||||
|
return false;
|
||||||
|
++dot_cnt;
|
||||||
|
}
|
||||||
|
else if(str[i]=='e' || str[i]=='E')
|
||||||
|
break;
|
||||||
|
else if(str[i]<'0' || str[i]>'9')
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(str[i]=='e' || str[i]=='E')
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
if(i==len)
|
||||||
|
return false;
|
||||||
|
if(str[i]=='-')
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
if(i==len)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for(;i<len;++i)
|
||||||
|
if(str[i]<'0' || str[i]>='9')
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(dot_cnt>1 || str[0]=='.' || (!dot_cnt && str[0]=='0'))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool check_numerable_string(std::string str)
|
bool check_numerable_string(std::string str)
|
||||||
{
|
{
|
||||||
if(str.length()>1 && str[0]=='-')
|
if(!str.length())
|
||||||
|
return false;
|
||||||
|
if(str[0]=='-' && str.length()>1)
|
||||||
{
|
{
|
||||||
// in lexer this statements are useless
|
std::string tmp="";
|
||||||
// because lexer judge a number that begins with 0~9 (or 0x for hex & 0o for oct)
|
for(int i=1;i<str.length();++i)
|
||||||
std::string temp="";
|
tmp.push_back(str[i]);
|
||||||
int str_len=str.length();
|
str=tmp;
|
||||||
for(int i=1;i<str_len;++i)
|
|
||||||
temp+=str[i];
|
|
||||||
str=temp;
|
|
||||||
}
|
}
|
||||||
|
else if(str[0]=='-' && str.length()==1)
|
||||||
|
return false;
|
||||||
if(str.length()==1 && '0'<=str[0] && str[0]<='9')
|
if(str.length()==1 && '0'<=str[0] && str[0]<='9')
|
||||||
return true;
|
return true;
|
||||||
else if(str.length()>2 && str[0]=='0' && str[1]=='x')
|
else if(str.length()>2 && str[0]=='0' && str[1]=='x')
|
||||||
{
|
return check_hex_string(str);
|
||||||
int str_len=str.length();
|
|
||||||
for(int i=2;i<str_len;++i)
|
|
||||||
if(!(('0'<=str[i] && str[i]<='9') || ('a'<=str[i] && str[i]<='f') || ('A'<=str[i] && str[i]<='F')))
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if(str.length()>2 && str[0]=='0' && str[1]=='o')
|
else if(str.length()>2 && str[0]=='0' && str[1]=='o')
|
||||||
{
|
return check_oct_string(str);
|
||||||
int str_len=str.length();
|
else if('0'<=str[0] && str[0]<='9')
|
||||||
for(int i=2;i<str_len;++i)
|
return check_dec_string(str);
|
||||||
if(!('0'<=str[i] && str[i]<='7'))
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int str_len=str.length();
|
|
||||||
int float_dot_cnt=0;
|
|
||||||
for(int i=0;i<str_len;++i)
|
|
||||||
{
|
|
||||||
if(str[i]=='.')
|
|
||||||
++float_dot_cnt;
|
|
||||||
else if(!('0'<=str[i] && str[i]<='9'))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if((float_dot_cnt>1) || (str[0]=='.') || (!float_dot_cnt && str[0]=='0'))
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,75 +98,102 @@ bool check_numerable_string(std::string str)
|
||||||
trans_string_to_number:
|
trans_string_to_number:
|
||||||
convert string to number
|
convert string to number
|
||||||
*/
|
*/
|
||||||
|
inline double hex_to_double(std::string str)
|
||||||
|
{
|
||||||
|
double ret=0;
|
||||||
|
int len=str.length();
|
||||||
|
double num_pow=1;
|
||||||
|
for(int i=len-1;i>1;--i)
|
||||||
|
{
|
||||||
|
if('0'<=str[i] && str[i]<='9')
|
||||||
|
ret+=num_pow*(str[i]-'0');
|
||||||
|
else if('a'<=str[i] && str[i]<='f')
|
||||||
|
ret+=num_pow*(str[i]-'a'+10);
|
||||||
|
else if('A'<=str[i] && str[i]<='F')
|
||||||
|
ret+=num_pow*(str[i]-'A'+10);
|
||||||
|
num_pow*=16;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
inline double oct_to_double(std::string str)
|
||||||
|
{
|
||||||
|
double ret=0;
|
||||||
|
int len=str.length();
|
||||||
|
double num_pow=1;
|
||||||
|
for(int i=len-1;i>1;--i)
|
||||||
|
{
|
||||||
|
ret+=num_pow*(str[i]-'0');
|
||||||
|
num_pow*=8;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
inline double dec_to_double(std::string str)
|
||||||
|
{
|
||||||
|
double ret=0;
|
||||||
|
int len=str.length();
|
||||||
|
int i=0;
|
||||||
|
for(;i<len;++i)
|
||||||
|
{
|
||||||
|
if('0'<=str[i] && str[i]<='9')
|
||||||
|
ret=ret*10+(str[i]-'0');
|
||||||
|
else if(str[i]=='.' || str[i]=='e' || str[i]=='E')
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(str[i]=='.')
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
double num_pow=0.1;
|
||||||
|
for(;i<len;++i)
|
||||||
|
{
|
||||||
|
if('0'<=str[i] && str[i]<='9')
|
||||||
|
{
|
||||||
|
ret+=num_pow*(str[i]-'0');
|
||||||
|
num_pow*=0.1;
|
||||||
|
}
|
||||||
|
else if(str[i]=='e' || str[i]=='E')
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(str[i]=='e' || str[i]=='E')
|
||||||
|
{
|
||||||
|
|
||||||
|
++i;
|
||||||
|
bool is_negative=str[i]=='-';
|
||||||
|
if(is_negative)
|
||||||
|
++i;
|
||||||
|
double num_pow=0;
|
||||||
|
for(;i<len;++i)
|
||||||
|
num_pow=num_pow*10+(str[i]-'0');
|
||||||
|
num_pow=std::pow(10,is_negative?-num_pow:num_pow);
|
||||||
|
ret*=num_pow;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
double trans_string_to_number(std::string str)
|
double trans_string_to_number(std::string str)
|
||||||
{
|
{
|
||||||
bool is_negative=false;
|
bool is_negative=false;
|
||||||
if(str.length()>1 && str[0]=='-')
|
double ret_num=0;
|
||||||
|
if(!str.length())
|
||||||
|
return 0;
|
||||||
|
if(str[0]=='-' && str.length()>1)
|
||||||
{
|
{
|
||||||
// in parse this statements are useless
|
|
||||||
// because lexer recognizes a number that begins with a '0'~'9' char
|
|
||||||
std::string temp="";
|
|
||||||
int str_len=str.length();
|
|
||||||
for(int i=1;i<str_len;++i)
|
|
||||||
temp+=str[i];
|
|
||||||
str=temp;
|
|
||||||
is_negative=true;
|
is_negative=true;
|
||||||
|
std::string tmp="";
|
||||||
|
for(int i=1;i<str.length();++i)
|
||||||
|
tmp.push_back(str[i]);
|
||||||
|
str=tmp;
|
||||||
}
|
}
|
||||||
double trans_str_number=0;
|
else if(str[0]=='-' && str.length()==1)
|
||||||
if(str.length()==1)
|
return 0;
|
||||||
trans_str_number=(double)(str[0]-'0');
|
if(str.length()==1 && '0'<=str[0] && str[0]<='9')
|
||||||
else if(str[0]=='0' && str[1]=='x')
|
ret_num=(double)(str[0]-'0');
|
||||||
{
|
else if(str.length()>2 && str[0]=='0' && str[1]=='x')
|
||||||
long long int hex_number=0,bit_pow=1;
|
ret_num=hex_to_double(str);
|
||||||
for(int i=str.length()-1;i>1;--i)
|
else if(str.length()>2 && str[0]=='0' && str[1]=='o')
|
||||||
{
|
ret_num=oct_to_double(str);
|
||||||
if('0'<=str[i] && str[i]<='9')
|
else if('0'<=str[0] && str[0]<='9')
|
||||||
hex_number+=bit_pow*(str[i]-'0');
|
ret_num=dec_to_double(str);
|
||||||
else if('a'<=str[i] && str[i]<='f')
|
return is_negative?-ret_num:ret_num;
|
||||||
hex_number+=bit_pow*(10+str[i]-'a');
|
|
||||||
else
|
|
||||||
hex_number+=bit_pow*(10+str[i]-'A');
|
|
||||||
bit_pow<<=4;
|
|
||||||
}
|
|
||||||
trans_str_number=(double)hex_number;
|
|
||||||
}
|
|
||||||
else if(str[0]=='0' && str[1]=='o')
|
|
||||||
{
|
|
||||||
long long int oct_number=0,bit_pow=1;
|
|
||||||
for(int i=str.length()-1;i>1;--i)
|
|
||||||
{
|
|
||||||
oct_number+=bit_pow*(str[i]-'0');
|
|
||||||
bit_pow<<=3;
|
|
||||||
}
|
|
||||||
trans_str_number=(double)oct_number;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int float_dot_place=str.length();
|
|
||||||
int str_len=str.length();
|
|
||||||
double pow;
|
|
||||||
for(int i=0;i<str_len;++i)
|
|
||||||
if(str[i]=='.')
|
|
||||||
{
|
|
||||||
float_dot_place=i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
pow=0.1;
|
|
||||||
for(int i=float_dot_place+1;i<str_len;++i)
|
|
||||||
{
|
|
||||||
trans_str_number+=pow*(double(str[i]-'0'));
|
|
||||||
pow/=10;
|
|
||||||
}
|
|
||||||
pow=1;
|
|
||||||
for(int i=float_dot_place-1;i>=0;--i)
|
|
||||||
{
|
|
||||||
trans_str_number+=pow*(double(str[i]-'0'));
|
|
||||||
pow*=10;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(is_negative)
|
|
||||||
trans_str_number*=-1;
|
|
||||||
return trans_str_number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in New Issue