Better command builder format output like ts3 expects it

This commit is contained in:
WolverinDEV 2020-01-28 21:00:53 +01:00
parent d13c1e6d68
commit 4aec154ff1

View File

@ -143,7 +143,7 @@ namespace ts {
return command_builder_impl<std::vector<std::string>>{this->expected_bulk_size, this->_identifier, this->bulks.begin(), this->bulks.end()}; return command_builder_impl<std::vector<std::string>>{this->expected_bulk_size, this->_identifier, this->bulks.begin(), this->bulks.end()};
} }
inline std::string build() const { inline std::string build(bool with_empty = false) const {
if(this->builded.has_value()) if(this->builded.has_value())
return this->builded.value(); return this->builded.value();
@ -154,20 +154,24 @@ namespace ts {
result.append(this->_identifier); result.append(this->_identifier);
for(auto it = this->bulks.begin(); it != this->bulks.end(); it++) { for(auto it = this->bulks.begin(); it != this->bulks.end(); it++) {
result.append(*it); if(it->empty() && !with_empty) continue;
result.append(*it, 0, it->length() - 1);
if(it + 1 != this->bulks.end()) if(it + 1 != this->bulks.end())
result.append("|"); result.append("|");
} }
if(!with_empty && result.ends_with('|'))
this->builded = result.substr(0, result.length() - 1);
else
this->builded = result; this->builded = result;
return result; return this->builded.value();
} }
inline void reserve_bulks(size_t count) { this->bulks.reserve(count); } inline void reserve_bulks(size_t count) { this->bulks.reserve(count); }
inline void put(size_t index, const std::string_view& key, const std::string_view& value) { inline void put(size_t index, const std::string_view& key, const std::string_view& value) {
while(this->bulks.size() <= index) while(this->bulks.size() <= index)
this->bulks.emplace_back(" ").reserve(expected_bulk_size); this->bulks.emplace_back("").reserve(expected_bulk_size);
auto& data = this->bulks[index]; auto& data = this->bulks[index];
size_t begin, end; size_t begin, end;
@ -193,7 +197,7 @@ namespace ts {
/* directly puts data without checking for duplicates */ /* directly puts data without checking for duplicates */
inline void put_unchecked(size_t index, const std::string_view& key, const std::string_view& value) { inline void put_unchecked(size_t index, const std::string_view& key, const std::string_view& value) {
while(this->bulks.size() <= index) while(this->bulks.size() <= index)
this->bulks.emplace_back(" ").reserve(expected_bulk_size); this->bulks.emplace_back("").reserve(expected_bulk_size);
this->impl_put_unchecked(this->bulks[index], index, key, value); this->impl_put_unchecked(this->bulks[index], index, key, value);
} }
@ -217,8 +221,10 @@ namespace ts {
data.reserve(data.length() + key.size() + escaped_value.size() + 2); data.reserve(data.length() + key.size() + escaped_value.size() + 2);
data.append(key); data.append(key);
if(!escaped_value.empty()) {
data.append("="); data.append("=");
data.append(escaped_value); data.append(escaped_value);
}
data.append(" "); data.append(" ");
this->builded.reset(); this->builded.reset();