Skip to content

Commit

Permalink
lib: added another constructor to BStringList
Browse files Browse the repository at this point in the history
- splits a string at substrings
- added a unit test
  • Loading branch information
franku committed Jul 4, 2019
1 parent 10b014d commit 307b773
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
16 changes: 16 additions & 0 deletions core/src/lib/bstringlist.cc
Expand Up @@ -27,6 +27,22 @@

BStringList::BStringList() : std::vector<std::string>() { return; }

BStringList::BStringList(const std::string& string_to_split,
std::string string_separator)
: std::vector<std::string>()
{
std::size_t find_pos = 0;
std::size_t start_pos = 0;

do {
find_pos = string_to_split.find(string_separator, start_pos);
std::string temp;
temp.assign(string_to_split, start_pos, find_pos - start_pos);
push_back(temp);
start_pos = find_pos + string_separator.size();
} while (find_pos != std::string::npos);
}

BStringList::BStringList(const std::string& string_to_split, char separator)
: std::vector<std::string>()
{
Expand Down
3 changes: 2 additions & 1 deletion core/src/lib/bstringlist.h
Expand Up @@ -27,7 +27,8 @@
class BStringList : public std::vector<std::string> {
public:
BStringList();
BStringList(const std::string& string_to_convert, char separator);
BStringList(const std::string& string_to_split, char separator);
BStringList(const std::string& string_to_split, std::string string_separator);
BStringList& operator=(const BStringList& rhs);
BStringList(const BStringList& other);
BStringList& operator<<(const std::string& rhs);
Expand Down
4 changes: 0 additions & 4 deletions core/src/tests/CMakeLists.txt
Expand Up @@ -287,10 +287,6 @@ IF(HAVE_EXECINFO_H AND HAVE_BACKTRACE AND HAVE_BACKTRACE_SYMBOLS)
${GTEST_MAIN_LIBRARIES}
)

add_test(NAME test_backtrace
COMMAND test_backtrace
)

gtest_discover_tests(test_backtrace TEST_PREFIX gtest:)
ENDIF()

13 changes: 13 additions & 0 deletions core/src/tests/lib_tests.cc
Expand Up @@ -96,6 +96,19 @@ TEST(BStringList, SplitStringTest)
EXPECT_STREQ("String", list1.front().c_str());
}

TEST(BStringList, SplitStringTestStringSeparator)
{
std::string test{"Test::123::String::::"};
BStringList list1(test, "::");
EXPECT_EQ(5, list1.size());

EXPECT_STREQ("Test", list1.front().c_str());
list1.erase(list1.begin());
EXPECT_STREQ("123", list1.front().c_str());
list1.erase(list1.begin());
EXPECT_STREQ("String", list1.front().c_str());
}

TEST(BNet, ReadoutCommandIdFromStringTest)
{
bool ok;
Expand Down

0 comments on commit 307b773

Please sign in to comment.