Optimize TlStorerToString.

GitOrigin-RevId: fe43727ec9a0ec777860c16fd6fcbd73a9fd4c09
This commit is contained in:
levlam 2020-04-29 21:49:11 +03:00
parent 1fe3b9809d
commit 7fd495a69c
2 changed files with 11 additions and 15 deletions

View File

@ -32,7 +32,7 @@ std::string TD_TL_writer_cpp::gen_output_begin() const {
"std::string to_string(const BaseObject &value) {\n"
" TlStorerToString storer;\n"
" value.store(storer, \"\");\n"
" return storer.str();\n"
" return storer.move_as_str();\n"
"}\n";
}

View File

@ -142,12 +142,10 @@ class TlStorerCalcLength {
class TlStorerToString {
std::string result;
int shift = 0;
size_t shift = 0;
void store_field_begin(const char *name) {
for (int i = 0; i < shift; i++) {
result += ' ';
}
result.append(shift, ' ');
if (name && name[0]) {
result += name;
result += " = ";
@ -155,7 +153,7 @@ class TlStorerToString {
}
void store_field_end() {
result += "\n";
result += '\n';
}
void store_long(int64 value) {
@ -165,14 +163,14 @@ class TlStorerToString {
void store_binary(Slice data) {
static const char *hex = "0123456789ABCDEF";
result.append("{ ");
result.append("{ ", 2);
for (auto c : data) {
unsigned char byte = c;
result += hex[byte >> 4];
result += hex[byte & 15];
result += ' ';
}
result.append("}");
result += '}';
}
public:
@ -211,7 +209,7 @@ class TlStorerToString {
void store_field(const char *name, const string &value) {
store_field_begin(name);
result += '"';
result.append(value.data(), value.size());
result += value;
result += '"';
store_field_end();
}
@ -265,16 +263,14 @@ class TlStorerToString {
}
void store_class_end() {
CHECK(shift >= 2);
shift -= 2;
for (int i = 0; i < shift; i++) {
result += ' ';
}
result.append(shift, ' ');
result += "}\n";
CHECK(shift >= 0);
}
std::string str() const {
return result;
std::string move_as_str() {
return std::move(result);
}
};