Base: add helper function to quote a string and join a list of strings
This commit is contained in:
@@ -291,6 +291,30 @@ std::string Base::Tools::escapeEncodeFilename(const std::string& s)
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string Base::Tools::quoted(const char* name)
|
||||
{
|
||||
std::stringstream str;
|
||||
str << "\"" << name << "\"";
|
||||
return str.str();
|
||||
}
|
||||
|
||||
std::string Base::Tools::quoted(const std::string& name)
|
||||
{
|
||||
std::stringstream str;
|
||||
str << "\"" << name << "\"";
|
||||
return str.str();
|
||||
}
|
||||
|
||||
std::string Base::Tools::joinList(const std::vector<std::string>& vec,
|
||||
const std::string& sep)
|
||||
{
|
||||
std::stringstream str;
|
||||
for (const auto& it : vec) {
|
||||
str << it << sep;
|
||||
}
|
||||
return str.str();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
using namespace Base;
|
||||
|
||||
@@ -274,6 +274,29 @@ struct BaseExport Tools
|
||||
static inline QString fromStdString(const std::string & s) {
|
||||
return QString::fromUtf8(s.c_str(), static_cast<int>(s.size()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief quoted Creates a quoted string.
|
||||
* @param String to be quoted.
|
||||
* @return A quoted std::string.
|
||||
*/
|
||||
static std::string quoted(const char*);
|
||||
/**
|
||||
* @brief quoted Creates a quoted string.
|
||||
* @param String to be quoted.
|
||||
* @return A quoted std::string.
|
||||
*/
|
||||
static std::string quoted(const std::string&);
|
||||
|
||||
/**
|
||||
* @brief joinList
|
||||
* Join the vector of strings \a vec using the separator \a sep
|
||||
* @param vec
|
||||
* @param sep
|
||||
* @return
|
||||
*/
|
||||
static std::string joinList(const std::vector<std::string>& vec,
|
||||
const std::string& sep = ", ");
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -40,3 +40,13 @@ TEST(BaseToolsSuite, TestUniqueName8)
|
||||
{
|
||||
EXPECT_EQ(Base::Tools::getUniqueName("Body12345", {"Body"}, 3), "Body12346");
|
||||
}
|
||||
|
||||
TEST(BaseToolsSuite, TestQuote)
|
||||
{
|
||||
EXPECT_EQ(Base::Tools::quoted("Test"), "\"Test\"");
|
||||
}
|
||||
|
||||
TEST(BaseToolsSuite, TestJoinList)
|
||||
{
|
||||
EXPECT_EQ(Base::Tools::joinList({"AB", "CD"}), "AB, CD, ");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user