Autogen: Modernize cmQtAutoGen methods using cm::string_view

This commit is contained in:
Sebastian Holtermann
2019-08-26 14:47:45 +02:00
parent 52a8fb2d53
commit 8586077baa
2 changed files with 45 additions and 69 deletions
+39 -65
View File
@@ -12,6 +12,7 @@
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <initializer_list>
#include <sstream> #include <sstream>
#include <utility> #include <utility>
@@ -21,7 +22,7 @@
/// @arg valueOpts list of options that accept a value /// @arg valueOpts list of options that accept a value
void MergeOptions(std::vector<std::string>& baseOpts, void MergeOptions(std::vector<std::string>& baseOpts,
std::vector<std::string> const& newOpts, std::vector<std::string> const& newOpts,
std::vector<std::string> const& valueOpts, bool isQt5) std::initializer_list<cm::string_view> valueOpts, bool isQt5)
{ {
typedef std::vector<std::string>::iterator Iter; typedef std::vector<std::string>::iterator Iter;
typedef std::vector<std::string>::const_iterator CIter; typedef std::vector<std::string>::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 cmQtAutoGen::Tools(bool moc, bool uic, bool rcc)
{ {
std::string res; std::array<cm::string_view, 3> lst;
std::vector<std::string> lst; decltype(lst)::size_type num = 0;
if (moc) { if (moc) {
lst.emplace_back("AUTOMOC"); lst.at(num++) = "AUTOMOC";
} }
if (uic) { if (uic) {
lst.emplace_back("AUTOUIC"); lst.at(num++) = "AUTOUIC";
} }
if (rcc) { if (rcc) {
lst.emplace_back("AUTORCC"); lst.at(num++) = "AUTORCC";
} }
switch (lst.size()) { switch (num) {
case 1: case 1:
res += lst.at(0); return std::string(lst[0]);
break;
case 2: case 2:
res += lst.at(0); return cmStrCat(lst[0], " and ", lst[1]);
res += " and ";
res += lst.at(1);
break;
case 3: case 3:
res += lst.at(0); return cmStrCat(lst[0], ", ", lst[1], " and ", lst[2]);
res += ", ";
res += lst.at(1);
res += " and ";
res += lst.at(2);
break;
default: default:
break; 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<std::pair<const char*, const char*>, 9> replaces = { static std::initializer_list<std::pair<const char*, const char*>> const
{ { "\\", "\\\\" }, replacements = { { "\\", "\\\\" }, { "\"", "\\\"" }, { "\a", "\\a" },
{ "\"", "\\\"" }, { "\b", "\\b" }, { "\f", "\\f" }, { "\n", "\\n" },
{ "\a", "\\a" }, { "\r", "\\r" }, { "\t", "\\t" }, { "\v", "\\v" } };
{ "\b", "\\b" },
{ "\f", "\\f" },
{ "\n", "\\n" },
{ "\r", "\\r" },
{ "\t", "\\t" },
{ "\v", "\\v" } }
};
std::string res = text; std::string res(text);
for (auto const& pair : replaces) { for (auto const& pair : replacements) {
cmSystemTools::ReplaceString(res, pair.first, pair.second); cmSystemTools::ReplaceString(res, pair.first, pair.second);
} }
res = '"' + res; return cmStrCat('"', res, '"');
res += '"';
return res;
} }
std::string cmQtAutoGen::QuotedCommand(std::vector<std::string> const& command) std::string cmQtAutoGen::QuotedCommand(std::vector<std::string> const& command)
@@ -191,37 +174,31 @@ std::string cmQtAutoGen::QuotedCommand(std::vector<std::string> const& command)
return res; 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('/'); auto slashPos = filename.rfind('/');
if (slash_pos == std::string::npos) { if (slashPos == cm::string_view::npos) {
return std::string(); 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 cmQtAutoGen::AppendFilenameSuffix(cm::string_view filename,
std::string const& suffix) cm::string_view suffix)
{ {
std::string res; auto dotPos = filename.rfind('.');
auto pos = filename.rfind('.'); if (dotPos == cm::string_view::npos) {
if (pos != std::string::npos) { return cmStrCat(filename, suffix);
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);
} }
return res; return cmStrCat(filename.substr(0, dotPos), suffix,
filename.substr(dotPos, filename.size() - dotPos));
} }
void cmQtAutoGen::UicMergeOptions(std::vector<std::string>& baseOpts, void cmQtAutoGen::UicMergeOptions(std::vector<std::string>& baseOpts,
std::vector<std::string> const& newOpts, std::vector<std::string> const& newOpts,
bool isQt5) bool isQt5)
{ {
static std::vector<std::string> const valueOpts = { static std::initializer_list<cm::string_view> const valueOpts = {
"tr", "translate", "postfix", "generator", "tr", "translate", "postfix", "generator",
"include", // Since Qt 5.3 "include", // Since Qt 5.3
"g" "g"
@@ -233,9 +210,9 @@ void cmQtAutoGen::RccMergeOptions(std::vector<std::string>& baseOpts,
std::vector<std::string> const& newOpts, std::vector<std::string> const& newOpts,
bool isQt5) bool isQt5)
{ {
static std::vector<std::string> const valueOpts = { "name", "root", static std::initializer_list<cm::string_view> const valueOpts = {
"compress", "name", "root", "compress", "threshold"
"threshold" }; };
MergeOptions(baseOpts, newOpts, valueOpts, isQt5); MergeOptions(baseOpts, newOpts, valueOpts, isQt5);
} }
@@ -349,9 +326,8 @@ bool cmQtAutoGen::RccLister::list(std::string const& qrcFile,
// Log command // Log command
if (verbose) { if (verbose) {
std::string msg = cmSystemTools::Stdout(
cmStrCat("Running command:\n", QuotedCommand(cmd), '\n'); cmStrCat("Running command:\n", QuotedCommand(cmd), '\n'));
cmSystemTools::Stdout(msg);
} }
result = cmSystemTools::RunSingleCommand( result = cmSystemTools::RunSingleCommand(
@@ -362,12 +338,10 @@ bool cmQtAutoGen::RccLister::list(std::string const& qrcFile,
error = error =
cmStrCat("The rcc list process failed for ", Quoted(qrcFile), '\n'); cmStrCat("The rcc list process failed for ", Quoted(qrcFile), '\n');
if (!rccStdOut.empty()) { if (!rccStdOut.empty()) {
error += rccStdOut; error += cmStrCat(rccStdOut, '\n');
error += "\n";
} }
if (!rccStdErr.empty()) { if (!rccStdErr.empty()) {
error += rccStdErr; error += cmStrCat(rccStdErr, '\n');
error += "\n";
} }
return false; return false;
} }
+6 -4
View File
@@ -5,6 +5,8 @@
#include "cmConfigure.h" // IWYU pragma: keep #include "cmConfigure.h" // IWYU pragma: keep
#include "cm_string_view.hxx"
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -74,16 +76,16 @@ public:
static std::string Tools(bool moc, bool uic, bool rcc); static std::string Tools(bool moc, bool uic, bool rcc);
/// @brief Returns the string escaped and enclosed in quotes /// @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<std::string> const& command); static std::string QuotedCommand(std::vector<std::string> const& command);
/// @brief Returns the parent directory of the file with a "/" suffix /// @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 /// @brief Appends the suffix to the filename before the last dot
static std::string AppendFilenameSuffix(std::string const& filename, static std::string AppendFilenameSuffix(cm::string_view filename,
std::string const& suffix); cm::string_view suffix);
/// @brief Merges newOpts into baseOpts /// @brief Merges newOpts into baseOpts
static void UicMergeOptions(std::vector<std::string>& baseOpts, static void UicMergeOptions(std::vector<std::string>& baseOpts,