diff options
Diffstat (limited to 'Source/cmStringAlgorithms.cxx')
| -rw-r--r-- | Source/cmStringAlgorithms.cxx | 44 |
1 files changed, 36 insertions, 8 deletions
diff --git a/Source/cmStringAlgorithms.cxx b/Source/cmStringAlgorithms.cxx index f73c854..66bf383 100644 --- a/Source/cmStringAlgorithms.cxx +++ b/Source/cmStringAlgorithms.cxx @@ -203,17 +203,45 @@ cmAlphaNum::cmAlphaNum(double val) MakeDigits(this->View_, this->Digits_, "%g", val); } -std::string cmCatViews(std::initializer_list<cm::string_view> views) +std::string cmCatViews( + std::initializer_list<std::pair<cm::string_view, std::string*>> views) { - std::size_t total_size = 0; - for (cm::string_view const& view : views) { - total_size += view.size(); + std::size_t totalSize = 0; + std::string* rvalueString = nullptr; + std::size_t rvalueStringLength = 0; + std::size_t rvalueStringOffset = 0; + for (auto const& view : views) { + // Find the rvalue string with the largest capacity. + if (view.second && + (!rvalueString || + view.second->capacity() > rvalueString->capacity())) { + rvalueString = view.second; + rvalueStringLength = rvalueString->length(); + rvalueStringOffset = totalSize; + } + totalSize += view.first.size(); } - std::string result(total_size, '\0'); - std::string::iterator sit = result.begin(); - for (cm::string_view const& view : views) { - sit = std::copy_n(view.data(), view.size(), sit); + std::string result; + std::string::size_type initialLen = 0; + if (rvalueString && rvalueString->capacity() >= totalSize) { + result = std::move(*rvalueString); + } else { + rvalueString = nullptr; + } + result.resize(totalSize); + if (rvalueString && rvalueStringOffset > 0) { + std::copy_backward(result.begin(), result.begin() + rvalueStringLength, + result.begin() + rvalueStringOffset + + rvalueStringLength); + } + std::string::iterator sit = result.begin() + initialLen; + for (auto const& view : views) { + if (rvalueString && view.second == rvalueString) { + sit += rvalueStringLength; + } else { + sit = std::copy_n(view.first.data(), view.first.size(), sit); + } } return result; } |
