This commit is contained in:
Valk Richard Li 2020-04-06 07:06:33 -07:00 committed by GitHub
parent 8477a5662f
commit 8519b134be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 160 additions and 100 deletions

View File

@ -12,52 +12,85 @@
'0xdeadbeef'
'0xDEADBEEF'
'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)
{
if(str.length()>1 && str[0]=='-')
if(!str.length())
return false;
if(str[0]=='-' && str.length()>1)
{
// in lexer this statements are useless
// because lexer judge a number that begins with 0~9 (or 0x for hex & 0o for oct)
std::string temp="";
int str_len=str.length();
for(int i=1;i<str_len;++i)
temp+=str[i];
str=temp;
std::string tmp="";
for(int i=1;i<str.length();++i)
tmp.push_back(str[i]);
str=tmp;
}
else if(str[0]=='-' && str.length()==1)
return false;
if(str.length()==1 && '0'<=str[0] && str[0]<='9')
return true;
else if(str.length()>2 && str[0]=='0' && str[1]=='x')
{
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;
}
return check_hex_string(str);
else if(str.length()>2 && str[0]=='0' && str[1]=='o')
{
int str_len=str.length();
for(int i=2;i<str_len;++i)
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 check_oct_string(str);
else if('0'<=str[0] && str[0]<='9')
return check_dec_string(str);
return false;
}
@ -65,75 +98,102 @@ bool check_numerable_string(std::string str)
trans_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)
{
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;
std::string tmp="";
for(int i=1;i<str.length();++i)
tmp.push_back(str[i]);
str=tmp;
}
double trans_str_number=0;
if(str.length()==1)
trans_str_number=(double)(str[0]-'0');
else if(str[0]=='0' && str[1]=='x')
{
long long int hex_number=0,bit_pow=1;
for(int i=str.length()-1;i>1;--i)
{
if('0'<=str[i] && str[i]<='9')
hex_number+=bit_pow*(str[i]-'0');
else if('a'<=str[i] && str[i]<='f')
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;
else if(str[0]=='-' && str.length()==1)
return 0;
if(str.length()==1 && '0'<=str[0] && str[0]<='9')
ret_num=(double)(str[0]-'0');
else if(str.length()>2 && str[0]=='0' && str[1]=='x')
ret_num=hex_to_double(str);
else if(str.length()>2 && str[0]=='0' && str[1]=='o')
ret_num=oct_to_double(str);
else if('0'<=str[0] && str[0]<='9')
ret_num=dec_to_double(str);
return is_negative?-ret_num:ret_num;
}
/*