Commit f61e8b8f authored by Purves, Murray's avatar Purves, Murray
Browse files

Adding remove empty entries option to string split

parent 2119faee
Pipeline #16103 passed with stages
in 8 minutes and 41 seconds
......@@ -60,7 +60,8 @@ std::string strip_quotes(const std::string &src)
}
std::vector<std::string> split_string(const std::string &delim,
const std::string &word)
const std::string &word,
bool removeEmptyParts)
{
std::vector<std::string> tokens;
size_t pos = 0;
......@@ -78,6 +79,12 @@ std::vector<std::string> split_string(const std::string &delim,
tokens.resize(tokens.size() - 1);
}
// Remove empty parts if requested
if (removeEmptyParts)
{
tokens.erase(std::remove(tokens.begin(), tokens.end(), ""), tokens.end());
}
return tokens;
} // splitString
......
......@@ -86,8 +86,9 @@ std::string RADIX_PUBLIC join(const std::string &delim, const T &x);
/**
* @brief Split a string into pieces using a delimiter
*/
std::vector<std::string> RADIX_PUBLIC split_string(const std::string &delim,
const std::string &word);
std::vector<std::string> RADIX_PUBLIC
split_string(const std::string &delim, const std::string &word,
bool removeEmptyParts = false);
/**
* @brief Find all instances and replace them
......
......@@ -104,6 +104,20 @@ TEST(Split, NoDelim)
ASSERT_EQ(1, x.size());
EXPECT_EQ("nope", x[0]);
}
TEST(Split, RemoveEmptyEntries)
{
Vec_Str split = split_string(";", "good;;;morning");
ASSERT_EQ(4, split.size());
EXPECT_EQ("good", split[0]);
EXPECT_EQ("morning", split[3]);
split = split_string(";", "good;;;morning", true);
ASSERT_EQ(2, split.size());
EXPECT_EQ("good", split[0]);
EXPECT_EQ("morning", split[1]);
}
TEST(Radix, OrdinalItoa)
{
{
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment