From 8586077baae74c90056704c022aad8357788fa38 Mon Sep 17 00:00:00 2001 From: Sebastian Holtermann Date: Mon, 26 Aug 2019 14:47:45 +0200 Subject: Autogen: Modernize cmQtAutoGen methods using cm::string_view --- Source/cmQtAutoGen.cxx | 104 +++++++++++++++++++------------------------------ Source/cmQtAutoGen.h | 10 +++-- 2 files changed, 45 insertions(+), 69 deletions(-) diff --git a/Source/cmQtAutoGen.cxx b/Source/cmQtAutoGen.cxx index 9d29e5c..dc7424e 100644 --- a/Source/cmQtAutoGen.cxx +++ b/Source/cmQtAutoGen.cxx @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -21,7 +22,7 @@ /// @arg valueOpts list of options that accept a value void MergeOptions(std::vector& baseOpts, std::vector const& newOpts, - std::vector const& valueOpts, bool isQt5) + std::initializer_list valueOpts, bool isQt5) { typedef std::vector::iterator Iter; typedef std::vector::const_iterator CIter; @@ -117,60 +118,42 @@ std::string const& cmQtAutoGen::GeneratorNameUpper(GenT genType) std::string cmQtAutoGen::Tools(bool moc, bool uic, bool rcc) { - std::string res; - std::vector lst; + std::array lst; + decltype(lst)::size_type num = 0; if (moc) { - lst.emplace_back("AUTOMOC"); + lst.at(num++) = "AUTOMOC"; } if (uic) { - lst.emplace_back("AUTOUIC"); + lst.at(num++) = "AUTOUIC"; } if (rcc) { - lst.emplace_back("AUTORCC"); + lst.at(num++) = "AUTORCC"; } - switch (lst.size()) { + switch (num) { case 1: - res += lst.at(0); - break; + return std::string(lst[0]); case 2: - res += lst.at(0); - res += " and "; - res += lst.at(1); - break; + return cmStrCat(lst[0], " and ", lst[1]); case 3: - res += lst.at(0); - res += ", "; - res += lst.at(1); - res += " and "; - res += lst.at(2); - break; + return cmStrCat(lst[0], ", ", lst[1], " and ", lst[2]); default: break; } - return res; + return std::string(); } -std::string cmQtAutoGen::Quoted(std::string const& text) +std::string cmQtAutoGen::Quoted(cm::string_view text) { - const std::array, 9> replaces = { - { { "\\", "\\\\" }, - { "\"", "\\\"" }, - { "\a", "\\a" }, - { "\b", "\\b" }, - { "\f", "\\f" }, - { "\n", "\\n" }, - { "\r", "\\r" }, - { "\t", "\\t" }, - { "\v", "\\v" } } - }; + static std::initializer_list> const + replacements = { { "\\", "\\\\" }, { "\"", "\\\"" }, { "\a", "\\a" }, + { "\b", "\\b" }, { "\f", "\\f" }, { "\n", "\\n" }, + { "\r", "\\r" }, { "\t", "\\t" }, { "\v", "\\v" } }; - std::string res = text; - for (auto const& pair : replaces) { + std::string res(text); + for (auto const& pair : replacements) { cmSystemTools::ReplaceString(res, pair.first, pair.second); } - res = '"' + res; - res += '"'; - return res; + return cmStrCat('"', res, '"'); } std::string cmQtAutoGen::QuotedCommand(std::vector const& command) @@ -191,37 +174,31 @@ std::string cmQtAutoGen::QuotedCommand(std::vector const& command) return res; } -std::string cmQtAutoGen::SubDirPrefix(std::string const& filename) +std::string cmQtAutoGen::SubDirPrefix(cm::string_view filename) { - std::string::size_type slash_pos = filename.rfind('/'); - if (slash_pos == std::string::npos) { + auto slashPos = filename.rfind('/'); + if (slashPos == cm::string_view::npos) { return std::string(); } - return filename.substr(0, slash_pos + 1); + return std::string(filename.substr(0, slashPos + 1)); } -std::string cmQtAutoGen::AppendFilenameSuffix(std::string const& filename, - std::string const& suffix) +std::string cmQtAutoGen::AppendFilenameSuffix(cm::string_view filename, + cm::string_view suffix) { - std::string res; - auto pos = filename.rfind('.'); - if (pos != std::string::npos) { - const auto it_dot = filename.begin() + pos; - res.assign(filename.begin(), it_dot); - res.append(suffix); - res.append(it_dot, filename.end()); - } else { - res = filename; - res.append(suffix); + auto dotPos = filename.rfind('.'); + if (dotPos == cm::string_view::npos) { + return cmStrCat(filename, suffix); } - return res; + return cmStrCat(filename.substr(0, dotPos), suffix, + filename.substr(dotPos, filename.size() - dotPos)); } void cmQtAutoGen::UicMergeOptions(std::vector& baseOpts, std::vector const& newOpts, bool isQt5) { - static std::vector const valueOpts = { + static std::initializer_list const valueOpts = { "tr", "translate", "postfix", "generator", "include", // Since Qt 5.3 "g" @@ -233,9 +210,9 @@ void cmQtAutoGen::RccMergeOptions(std::vector& baseOpts, std::vector const& newOpts, bool isQt5) { - static std::vector const valueOpts = { "name", "root", - "compress", - "threshold" }; + static std::initializer_list const valueOpts = { + "name", "root", "compress", "threshold" + }; MergeOptions(baseOpts, newOpts, valueOpts, isQt5); } @@ -349,9 +326,8 @@ bool cmQtAutoGen::RccLister::list(std::string const& qrcFile, // Log command if (verbose) { - std::string msg = - cmStrCat("Running command:\n", QuotedCommand(cmd), '\n'); - cmSystemTools::Stdout(msg); + cmSystemTools::Stdout( + cmStrCat("Running command:\n", QuotedCommand(cmd), '\n')); } result = cmSystemTools::RunSingleCommand( @@ -362,12 +338,10 @@ bool cmQtAutoGen::RccLister::list(std::string const& qrcFile, error = cmStrCat("The rcc list process failed for ", Quoted(qrcFile), '\n'); if (!rccStdOut.empty()) { - error += rccStdOut; - error += "\n"; + error += cmStrCat(rccStdOut, '\n'); } if (!rccStdErr.empty()) { - error += rccStdErr; - error += "\n"; + error += cmStrCat(rccStdErr, '\n'); } return false; } diff --git a/Source/cmQtAutoGen.h b/Source/cmQtAutoGen.h index 619fff1..03468ec 100644 --- a/Source/cmQtAutoGen.h +++ b/Source/cmQtAutoGen.h @@ -5,6 +5,8 @@ #include "cmConfigure.h" // IWYU pragma: keep +#include "cm_string_view.hxx" + #include #include #include @@ -74,16 +76,16 @@ public: static std::string Tools(bool moc, bool uic, bool rcc); /// @brief Returns the string escaped and enclosed in quotes - static std::string Quoted(std::string const& text); + static std::string Quoted(cm::string_view text); static std::string QuotedCommand(std::vector const& command); /// @brief Returns the parent directory of the file with a "/" suffix - static std::string SubDirPrefix(std::string const& filename); + static std::string SubDirPrefix(cm::string_view filename); /// @brief Appends the suffix to the filename before the last dot - static std::string AppendFilenameSuffix(std::string const& filename, - std::string const& suffix); + static std::string AppendFilenameSuffix(cm::string_view filename, + cm::string_view suffix); /// @brief Merges newOpts into baseOpts static void UicMergeOptions(std::vector& baseOpts, -- cgit v0.12