Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions fpsdk_common/include/fpsdk_common/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,17 @@ void StrTrim(std::string& str);
/**
* @brief Split string
*
* Splits a string using a separator string. All parts, including empty ones, are returned. An empty separator
* leads to only one part, that is equal to the input string.
* Splits a string using a separator string. All parts, including empty ones, are returned. An empty separator leads to
* only one part, that is equal to the input string, unless that is empty, in which case an empty vector is returned.
*
* Examples:
*
* "foo,bar,baz" separated by "," --> [ "foo", "bar", "baz" ]
* "foo,bar,baz" separated by "," (max=2) --> [ "foo", "bar,baz" ]
* "foo,,baz,,," separated by "," --> [ "foo", "", "baz", "", "", "" ]
* "foo,bar,baz" separated by "" --> [ "foo,bar,baz" ]
* "" separated by "something" --> [ ]
* "" separated by "" --> [ ]
*
* @param[in] str The string
* @param[in] sep The separator, empty string is allowed
Expand Down
22 changes: 16 additions & 6 deletions fpsdk_common/test/string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,20 @@ TEST(StringTest, StrTrim)
TEST(StringTest, StrSplit)
{
{
const std::string str = "foo,bar,baz";
const auto split = StrSplit(str, ",");
const auto split = StrSplit("foo,bar,baz", ",");
EXPECT_EQ(split.size(), (std::size_t)3);
EXPECT_EQ(split.at(0), "foo");
EXPECT_EQ(split.at(1), "bar");
EXPECT_EQ(split.at(2), "baz");
}
{
const std::string str = "foo,bar,baz";
const auto split = StrSplit(str, ",", 2);
const auto split = StrSplit("foo,bar,baz", ",", 2);
EXPECT_EQ(split.size(), (std::size_t)2);
EXPECT_EQ(split.at(0), "foo");
EXPECT_EQ(split.at(1), "bar,baz");
}
{
const std::string str = "foo,,baz,,,";
const auto split = StrSplit(str, ",");
const auto split = StrSplit("foo,,baz,,,", ",");
EXPECT_EQ(split.size(), (std::size_t)6);
EXPECT_EQ(split.at(0), "foo");
EXPECT_EQ(split.at(1), "");
Expand All @@ -120,6 +117,19 @@ TEST(StringTest, StrSplit)
EXPECT_EQ(split.at(4), "");
EXPECT_EQ(split.at(5), "");
}
{
const auto split = StrSplit("foo,bar,baz", "");
EXPECT_EQ(split.size(), (std::size_t)1);
EXPECT_EQ(split.at(0), "foo,bar,baz");
}
{
const auto split = StrSplit("", "something");
EXPECT_EQ(split.size(), (std::size_t)0);
}
{
const auto split = StrSplit("", "");
EXPECT_EQ(split.size(), (std::size_t)0);
}
}

// ---------------------------------------------------------------------------------------------------------------------
Expand Down
Loading