Base: add helper function to quote a string and join a list of strings

This commit is contained in:
wmayer
2023-05-15 23:40:07 +02:00
parent c33588be3e
commit 41e6758bc0
3 changed files with 57 additions and 0 deletions

View File

@@ -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;

View File

@@ -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 = ", ");
};

View File

@@ -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, ");
}