Optimize memory allocations in VariantParser::get_token

This commit is contained in:
Evgeny Zuev 2017-09-05 11:04:33 +07:00
parent cae0059d5f
commit a0bdb6e2f6

View file

@ -29,6 +29,7 @@
/*************************************************************************/ /*************************************************************************/
#include "variant_parser.h" #include "variant_parser.h"
#include "core/string_buffer.h"
#include "io/resource_loader.h" #include "io/resource_loader.h"
#include "os/input_event.h" #include "os/input_event.h"
#include "os/keyboard.h" #include "os/keyboard.h"
@ -176,14 +177,15 @@ Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, Stri
}; };
case '#': { case '#': {
String color_str = "#"; StringBuffer color_str;
color_str += '#';
while (true) { while (true) {
CharType ch = p_stream->get_char(); CharType ch = p_stream->get_char();
if (p_stream->is_eof()) { if (p_stream->is_eof()) {
r_token.type = TK_EOF; r_token.type = TK_EOF;
return OK; return OK;
} else if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) { } else if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) {
color_str += String::chr(ch); color_str += ch;
} else { } else {
p_stream->saved = ch; p_stream->saved = ch;
@ -191,7 +193,7 @@ Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, Stri
} }
} }
r_token.value = Color::html(color_str); r_token.value = Color::html(color_str.as_string());
r_token.type = TK_COLOR; r_token.type = TK_COLOR;
return OK; return OK;
}; };
@ -296,7 +298,7 @@ Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, Stri
if (cchar == '-' || (cchar >= '0' && cchar <= '9')) { if (cchar == '-' || (cchar >= '0' && cchar <= '9')) {
//a number //a number
String num; 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
@ -359,7 +361,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 += String::chr(c); num += c;
c = p_stream->get_char(); c = p_stream->get_char();
} }
@ -368,19 +370,19 @@ 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.to_double(); r_token.value = num.as_double();
else else
r_token.value = num.to_int(); r_token.value = num.as_int();
return OK; return OK;
} else if ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_') { } else if ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_') {
String id; StringBuffer id;
bool first = true; bool first = true;
while ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_' || (!first && cchar >= '0' && cchar <= '9')) { while ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_' || (!first && cchar >= '0' && cchar <= '9')) {
id += String::chr(cchar); id += cchar;
cchar = p_stream->get_char(); cchar = p_stream->get_char();
first = false; first = false;
} }
@ -388,7 +390,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; r_token.value = id.as_string();
return OK; return OK;
} else { } else {
r_err_str = "Unexpected character."; r_err_str = "Unexpected character.";