This commit is contained in:
blueloveTH 2024-03-04 17:11:56 +08:00
parent d4cf8b8c03
commit 6cf2f950cd

View File

@ -289,7 +289,7 @@ static bool is_unicode_Lo_char(uint32_t c) {
add_token(TK("@num"), int_out); add_token(TK("@num"), int_out);
return; return;
case IntParsingResult::Overflow: case IntParsingResult::Overflow:
SyntaxError("integer literal too large"); SyntaxError("int literal is too large");
return; return;
case IntParsingResult::Failure: case IntParsingResult::Failure:
break; // do nothing break; // do nothing
@ -502,8 +502,9 @@ IntParsingResult parse_int(std::string_view text, i64* out, int base){
if(text.length() == 0) return IntParsingResult::Failure; if(text.length() == 0) return IntParsingResult::Failure;
for(char c : text){ for(char c : text){
if(c >= '0' && c <= '9'){ if(c >= '0' && c <= '9'){
i64 prev_out = *out;
*out = (*out * 10) + (c - '0'); *out = (*out * 10) + (c - '0');
if(*out < 0) return IntParsingResult::Overflow; if(*out < prev_out) return IntParsingResult::Overflow;
}else{ }else{
return IntParsingResult::Failure; return IntParsingResult::Failure;
} }
@ -515,8 +516,9 @@ IntParsingResult parse_int(std::string_view text, i64* out, int base){
if(text.length() == 0) return IntParsingResult::Failure; if(text.length() == 0) return IntParsingResult::Failure;
for(char c : text){ for(char c : text){
if(c == '0' || c == '1'){ if(c == '0' || c == '1'){
i64 prev_out = *out;
*out = (*out << 1) | (c - '0'); *out = (*out << 1) | (c - '0');
if(*out < 0) return IntParsingResult::Overflow; if(*out < prev_out) return IntParsingResult::Overflow;
}else{ }else{
return IntParsingResult::Failure; return IntParsingResult::Failure;
} }
@ -528,8 +530,9 @@ IntParsingResult parse_int(std::string_view text, i64* out, int base){
if(text.length() == 0) return IntParsingResult::Failure; if(text.length() == 0) return IntParsingResult::Failure;
for(char c : text){ for(char c : text){
if(c >= '0' && c <= '7'){ if(c >= '0' && c <= '7'){
i64 prev_out = *out;
*out = (*out << 3) | (c - '0'); *out = (*out << 3) | (c - '0');
if(*out < 0) return IntParsingResult::Overflow; if(*out < prev_out) return IntParsingResult::Overflow;
}else{ }else{
return IntParsingResult::Failure; return IntParsingResult::Failure;
} }
@ -540,15 +543,16 @@ IntParsingResult parse_int(std::string_view text, i64* out, int base){
if(f_startswith_2(text, "0x")) text.remove_prefix(2); if(f_startswith_2(text, "0x")) text.remove_prefix(2);
if(text.length() == 0) return IntParsingResult::Failure; if(text.length() == 0) return IntParsingResult::Failure;
for(char c : text){ for(char c : text){
i64 prev_out = *out;
if(c >= '0' && c <= '9'){ if(c >= '0' && c <= '9'){
*out = (*out << 4) | (c - '0'); *out = (*out << 4) | (c - '0');
if(*out < 0) return IntParsingResult::Overflow; if(*out < prev_out) return IntParsingResult::Overflow;
}else if(c >= 'a' && c <= 'f'){ }else if(c >= 'a' && c <= 'f'){
*out = (*out << 4) | (c - 'a' + 10); *out = (*out << 4) | (c - 'a' + 10);
if(*out < 0) return IntParsingResult::Overflow; if(*out < prev_out) return IntParsingResult::Overflow;
}else if(c >= 'A' && c <= 'F'){ }else if(c >= 'A' && c <= 'F'){
*out = (*out << 4) | (c - 'A' + 10); *out = (*out << 4) | (c - 'A' + 10);
if(*out < 0) return IntParsingResult::Overflow; if(*out < prev_out) return IntParsingResult::Overflow;
}else{ }else{
return IntParsingResult::Failure; return IntParsingResult::Failure;
} }