From d02a99d9d3aa8d8f33efbd3255c78d6b87a99a22 Mon Sep 17 00:00:00 2001 From: Sebastian Holtermann Date: Tue, 6 Aug 2019 10:32:07 +0200 Subject: Autogen: Modernize code to use cm::string_view for the info writer --- Source/cmQtAutoGenInitializer.cxx | 33 ++++++++++++++------------------- Source/cmQtAutoGenInitializer.h | 17 +++++++++-------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/Source/cmQtAutoGenInitializer.cxx b/Source/cmQtAutoGenInitializer.cxx index da6094d..7e72fd6 100644 --- a/Source/cmQtAutoGenInitializer.cxx +++ b/Source/cmQtAutoGenInitializer.cxx @@ -156,30 +156,27 @@ std::string cmQtAutoGenInitializer::InfoWriter::ListJoin(IT it_begin, return res; } -std::string cmQtAutoGenInitializer::InfoWriter::ConfigKey( - const char* key, std::string const& config) +inline std::string cmQtAutoGenInitializer::InfoWriter::ConfigKey( + cm::string_view key, std::string const& config) { - std::string ckey = key; - ckey += '_'; - ckey += config; - return ckey; + return cmStrCat(key, "_", config); } -void cmQtAutoGenInitializer::InfoWriter::Write(const char* key, +void cmQtAutoGenInitializer::InfoWriter::Write(cm::string_view key, std::string const& value) { Ofs_ << "set(" << key << " " << cmOutputConverter::EscapeForCMake(value) << ")\n"; }; -void cmQtAutoGenInitializer::InfoWriter::WriteUInt(const char* key, +void cmQtAutoGenInitializer::InfoWriter::WriteUInt(cm::string_view key, unsigned int value) { Ofs_ << "set(" << key << " " << value << ")\n"; }; template -void cmQtAutoGenInitializer::InfoWriter::WriteStrings(const char* key, +void cmQtAutoGenInitializer::InfoWriter::WriteStrings(cm::string_view key, C const& container) { Ofs_ << "set(" << key << " \"" @@ -187,31 +184,29 @@ void cmQtAutoGenInitializer::InfoWriter::WriteStrings(const char* key, } void cmQtAutoGenInitializer::InfoWriter::WriteConfig( - const char* key, std::map const& map) + cm::string_view key, std::map const& map) { for (auto const& item : map) { - Write(ConfigKey(key, item.first).c_str(), item.second); + Write(ConfigKey(key, item.first), item.second); } }; template void cmQtAutoGenInitializer::InfoWriter::WriteConfigStrings( - const char* key, std::map const& map) + cm::string_view key, std::map const& map) { for (auto const& item : map) { - WriteStrings(ConfigKey(key, item.first).c_str(), item.second); + WriteStrings(ConfigKey(key, item.first), item.second); } } void cmQtAutoGenInitializer::InfoWriter::WriteNestedLists( - const char* key, std::vector> const& lists) + cm::string_view key, std::vector> const& lists) { std::vector seplist; - for (const std::vector& list : lists) { - std::string blist = "{"; - blist += ListJoin(list.begin(), list.end()); - blist += "}"; - seplist.push_back(std::move(blist)); + seplist.reserve(lists.size()); + for (std::vector const& list : lists) { + seplist.push_back(cmStrCat("{", ListJoin(list.begin(), list.end()), "}")); } Write(key, cmJoin(seplist, cmQtAutoGen::ListSep)); }; diff --git a/Source/cmQtAutoGenInitializer.h b/Source/cmQtAutoGenInitializer.h index eb0d35e..7d72cad 100644 --- a/Source/cmQtAutoGenInitializer.h +++ b/Source/cmQtAutoGenInitializer.h @@ -6,6 +6,7 @@ #include "cmConfigure.h" // IWYU pragma: keep #include "cmGeneratedFileStream.h" #include "cmQtAutoGen.h" +#include "cm_string_view.hxx" #include #include @@ -85,24 +86,24 @@ public: /// @return True if the file is open explicit operator bool() const { return static_cast(Ofs_); } - void Write(const char* text) { Ofs_ << text; } - void Write(const char* key, std::string const& value); - void WriteUInt(const char* key, unsigned int value); + void Write(cm::string_view text) { Ofs_ << text; } + void Write(cm::string_view, std::string const& value); + void WriteUInt(cm::string_view, unsigned int value); template - void WriteStrings(const char* key, C const& container); - void WriteConfig(const char* key, + void WriteStrings(cm::string_view, C const& container); + void WriteConfig(cm::string_view, std::map const& map); template - void WriteConfigStrings(const char* key, + void WriteConfigStrings(cm::string_view, std::map const& map); - void WriteNestedLists(const char* key, + void WriteNestedLists(cm::string_view, std::vector> const& lists); private: template static std::string ListJoin(IT it_begin, IT it_end); - static std::string ConfigKey(const char* key, std::string const& config); + static std::string ConfigKey(cm::string_view, std::string const& config); private: cmGeneratedFileStream Ofs_; -- cgit v0.12