diff --git a/td/telegram/ClientJson.cpp b/td/telegram/ClientJson.cpp index 2f218d03095b..e67f620333cb 100644 --- a/td/telegram/ClientJson.cpp +++ b/td/telegram/ClientJson.cpp @@ -58,21 +58,18 @@ static string from_response(const td_api::Object &object, const string &extra, i auto buf = StackAllocator::alloc(1 << 18); JsonBuilder jb(StringBuilder(buf.as_slice(), true), -1); jb.enter_value() << ToJson(object); - auto slice = jb.string_builder().as_cslice(); + auto &sb = jb.string_builder(); + auto slice = sb.as_cslice(); CHECK(!slice.empty() && slice.back() == '}'); - string str; - str.reserve(slice.size() + (extra.empty() ? 0 : 10 + extra.size()) + (client_id == 0 ? 0 : 14 + 10)); - str.append(slice.begin(), slice.size() - 1); + sb.pop_back(); if (!extra.empty()) { - str += ",\"@extra\":"; - str += extra; + sb << ",\"@extra\":" << extra; } if (client_id != 0) { - str += ",\"@client_id\":"; - str += to_string(client_id); + sb << ",\"@client_id\":" << client_id; } - str += '}'; - return str; + sb << '}'; + return sb.as_cslice().str(); } static TD_THREAD_LOCAL string *current_output; diff --git a/tdutils/td/utils/StringBuilder.h b/tdutils/td/utils/StringBuilder.h index 1d7da54ffe67..1a1987caa108 100644 --- a/tdutils/td/utils/StringBuilder.h +++ b/tdutils/td/utils/StringBuilder.h @@ -27,6 +27,11 @@ class StringBuilder { error_flag_ = false; } + void pop_back() { + CHECK(current_ptr_ > begin_ptr_); + current_ptr_--; + } + MutableCSlice as_cslice() { if (current_ptr_ >= end_ptr_ + RESERVED_SIZE) { std::abort(); // shouldn't happen