diff --git a/td/generate/tl_writer_cpp.cpp b/td/generate/tl_writer_cpp.cpp index bfaeb2eb..f9c5029a 100644 --- a/td/generate/tl_writer_cpp.cpp +++ b/td/generate/tl_writer_cpp.cpp @@ -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"; } diff --git a/tdutils/td/utils/tl_storers.h b/tdutils/td/utils/tl_storers.h index f4c6d614..4476115b 100644 --- a/tdutils/td/utils/tl_storers.h +++ b/tdutils/td/utils/tl_storers.h @@ -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); } };