From d578f8d136fd0c0ce7eea57509d277dcf442e920 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Thu, 4 Feb 2021 16:02:40 -0800 Subject: [PATCH] separated_buffer_t to accept strings by rvalue reference This saves a copy in some cases. --- src/io.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/io.h b/src/io.h index 8ed25cb6a..44b6274cb 100644 --- a/src/io.h +++ b/src/io.h @@ -152,9 +152,15 @@ class separated_buffer_t { } /// Append a string \p str with the given separation type \p sep. - void append(const std::string &str, separation_type_t sep = separation_type_t::inferred) { - const char *cstr = str.c_str(); - append(cstr, cstr + str.size(), sep); + void append(std::string &&str, separation_type_t sep = separation_type_t::inferred) { + if (!try_add_size(str.size())) return; + // Try merging with the last element. + if (sep == separation_type_t::inferred && !elements_.empty() && + !elements_.back().is_explicitly_separated()) { + elements_.back().contents.append(str); + } else { + elements_.emplace_back(std::move(str), sep); + } } };