1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-10 13:00:37 +00:00

VariantParser: Fix reading negated identifiers, replace inf_neg with -inf

This commit is contained in:
Aaron Franke
2025-01-15 16:43:30 -08:00
parent f418603522
commit 4d75c92225

View File

@@ -148,7 +148,8 @@ const char *VariantParser::tk_name[TK_MAX] = {
static double stor_fix(const String &p_str) { static double stor_fix(const String &p_str) {
if (p_str == "inf") { if (p_str == "inf") {
return INFINITY; return INFINITY;
} else if (p_str == "inf_neg") { } else if (p_str == "-inf" || p_str == "inf_neg") {
// inf_neg kept for compatibility.
return -INFINITY; return -INFINITY;
} else if (p_str == "nan") { } else if (p_str == "nan") {
return NAN; return NAN;
@@ -411,11 +412,13 @@ Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, Stri
if (cchar <= 32) { if (cchar <= 32) {
break; break;
} }
StringBuffer<> token_text;
if (cchar == '-' || (cchar >= '0' && cchar <= '9')) { if (cchar == '-') {
token_text += '-';
cchar = p_stream->get_char();
}
if (cchar >= '0' && cchar <= '9') {
//a number //a number
StringBuffer<> num;
#define READING_SIGN 0 #define READING_SIGN 0
#define READING_INT 1 #define READING_INT 1
#define READING_DEC 2 #define READING_DEC 2
@@ -423,11 +426,6 @@ Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, Stri
#define READING_DONE 4 #define READING_DONE 4
int reading = READING_INT; int reading = READING_INT;
if (cchar == '-') {
num += '-';
cchar = p_stream->get_char();
}
char32_t c = cchar; char32_t c = cchar;
bool exp_sign = false; bool exp_sign = false;
bool exp_beg = false; bool exp_beg = false;
@@ -474,7 +472,7 @@ Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, Stri
if (reading == READING_DONE) { if (reading == READING_DONE) {
break; break;
} }
num += c; token_text += c;
c = p_stream->get_char(); c = p_stream->get_char();
} }
@@ -483,17 +481,16 @@ Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, Stri
r_token.type = TK_NUMBER; r_token.type = TK_NUMBER;
if (is_float) { if (is_float) {
r_token.value = num.as_double(); r_token.value = token_text.as_double();
} else { } else {
r_token.value = num.as_int(); r_token.value = token_text.as_int();
} }
return OK; return OK;
} else if (is_ascii_alphabet_char(cchar) || is_underscore(cchar)) { } else if (is_ascii_alphabet_char(cchar) || is_underscore(cchar)) {
StringBuffer<> id;
bool first = true; bool first = true;
while (is_ascii_alphabet_char(cchar) || is_underscore(cchar) || (!first && is_digit(cchar))) { while (is_ascii_alphabet_char(cchar) || is_underscore(cchar) || (!first && is_digit(cchar))) {
id += cchar; token_text += cchar;
cchar = p_stream->get_char(); cchar = p_stream->get_char();
first = false; first = false;
} }
@@ -501,7 +498,7 @@ Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, Stri
p_stream->saved = cchar; p_stream->saved = cchar;
r_token.type = TK_IDENTIFIER; r_token.type = TK_IDENTIFIER;
r_token.value = id.as_string(); r_token.value = token_text.as_string();
return OK; return OK;
} else { } else {
r_err_str = "Unexpected character"; r_err_str = "Unexpected character";
@@ -699,7 +696,8 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream,
value = Variant(); value = Variant();
} else if (id == "inf") { } else if (id == "inf") {
value = INFINITY; value = INFINITY;
} else if (id == "inf_neg") { } else if (id == "-inf" || id == "inf_neg") {
// inf_neg kept for compatibility.
value = -INFINITY; value = -INFINITY;
} else if (id == "nan") { } else if (id == "nan") {
value = NAN; value = NAN;
@@ -1941,7 +1939,7 @@ static String rtos_fix(double p_value) {
if (p_value > 0) { if (p_value > 0) {
return "inf"; return "inf";
} else { } else {
return "inf_neg"; return "-inf";
} }
} else { } else {
return rtoss(p_value); return rtoss(p_value);
@@ -1961,7 +1959,7 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str
} break; } break;
case Variant::FLOAT: { case Variant::FLOAT: {
String s = rtos_fix(p_variant.operator double()); String s = rtos_fix(p_variant.operator double());
if (s != "inf" && s != "inf_neg" && s != "nan") { if (s != "inf" && s != "-inf" && s != "nan") {
if (!s.contains_char('.') && !s.contains_char('e') && !s.contains_char('E')) { if (!s.contains_char('.') && !s.contains_char('e') && !s.contains_char('E')) {
s += ".0"; s += ".0";
} }