Added performers. fixes #278

This commit is contained in:
emeric
2022-12-14 20:07:54 +01:00
parent 495005357c
commit 998def846d
27 changed files with 451 additions and 96 deletions
+25 -6
View File
@@ -115,17 +115,21 @@ joinStrings(const std::vector<std::string>& strings, const std::string& delimite
return boost::algorithm::join(strings, delimiter);
}
std::string
std::string_view
stringTrim(std::string_view str, std::string_view whitespaces)
{
std::string_view res;
const auto strBegin = str.find_first_not_of(whitespaces);
if (strBegin == std::string_view::npos)
return ""; // no content
if (strBegin != std::string_view::npos)
{
const auto strEnd {str.find_last_not_of(whitespaces)};
const auto strRange {strEnd - strBegin + 1};
const auto strEnd = str.find_last_not_of(whitespaces);
const auto strRange = strEnd - strBegin + 1;
res = str.substr(strBegin, strRange);
}
return std::string {str.substr(strBegin, strRange)};
return res;
}
std::string
@@ -175,6 +179,21 @@ bufferToString(const std::vector<unsigned char>& data)
return oss.str();
}
void
capitalize(std::string& str)
{
for (auto it {std::begin(str)}; it != std::end(str); ++it)
{
if (std::isspace(*it))
continue;
if (std::isalpha(*it))
*it = std::toupper(*it);
break;
}
}
std::string
replaceInString(std::string_view str, const std::string& from, const std::string& to)
{
+4 -1
View File
@@ -44,7 +44,7 @@ std::string
joinStrings(const std::vector<std::string>& strings, const std::string& delimiter);
[[nodiscard]]
std::string
std::string_view
stringTrim(std::string_view str, std::string_view whitespaces = " \t");
[[nodiscard]]
@@ -66,6 +66,9 @@ stringToUpper(const std::string& str);
std::string
bufferToString(const std::vector<unsigned char>& data);
void
capitalize(std::string& str);
template<typename T>
[[nodiscard]]
std::optional<T> readAs(std::string_view str)
+29
View File
@@ -118,3 +118,32 @@ TEST(StringUtils, readAs)
EXPECT_EQ(StringUtils::readAs<bool>("foo"), std::nullopt);
EXPECT_EQ(StringUtils::readAs<bool>(""), std::nullopt);
}
TEST(StrinUtils, capitalize)
{
struct TestCase
{
std::string input;
std::string expectedOutput;
};
TestCase tests[]
{
{"", ""},
{"C", "C"},
{"c", "C"},
{" c", " C"},
{" cc", " Cc"},
{"(c", "(c"},
{"1c", "1c"},
{"&c", "&c"},
{"c c", "C c"}
};
for (const TestCase& test : tests)
{
std::string str {test.input};
StringUtils::capitalize(str);
EXPECT_EQ(str, test.expectedOutput) << " str was '" << test.input << "'";
}
}