Skip to content

Commit

Permalink
Add string Join functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <[email protected]>
  • Loading branch information
mjcarroll committed Oct 19, 2020
1 parent 0b2bd23 commit faff823
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
24 changes: 24 additions & 0 deletions include/ignition/common/StringUtils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ namespace ignition
std::vector<std::string> IGNITION_COMMON_VISIBLE Split(
const std::string &_orig, char _delim);

/// \brief Join a sequence of strings with a delimiter
///
/// Note that this will skip any empty entries in the vector,
/// and will also not prepend or append the delimiter to the
/// final output string
///
/// \param[in] _orig The input sequence of strings
/// \param[in] _delim a string delimiter to join the string with
/// \returns a single string composed of strings joined with the delimiter
std::string IGNITION_COMMON_VISIBLE Join(
const std::vector<std::string> &_orig, const std::string &_delim);

/// \brief Join a sequence of strings with a delimiter
///
/// Note that this will skip any empty entries in the vector,
/// and will also not prepend or append the delimiter to the
/// final output string
///
/// \param[in] _orig The input sequence of strings
/// \param[in] _delim a character to join the string with
/// \returns a single string composed of strings joined with the delimiter
std::string IGNITION_COMMON_VISIBLE Join(
const std::vector<std::string> &_orig, char _delim);

/// \brief return true if string starts with another string
/// \param[in] _s1 the string to check
/// \param[in] _s2 the possible prefix
Expand Down
23 changes: 23 additions & 0 deletions src/StringUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ namespace ignition
return pieces;
}

//////////////////////////////////////////////////
std::string Join(const std::vector<std::string> &_orig, const std::string &_delim)
{
std::string ret = "";
for (size_t ii = 0; ii < _orig.size(); ++ii)
{
if (_orig[ii].empty())
continue;

ret += _orig[ii];
if (ii < _orig.size() - 1)
{
ret += _delim;
}
}
return ret;
}
//////////////////////////////////////////////////
std::string Join(const std::vector<std::string> &_orig, char _delim)
{
return Join(_orig, std::string(1, _delim));
}

//////////////////////////////////////////////////
bool StartsWith(const std::string &_s1, const std::string &_s2)
{
Expand Down
53 changes: 53 additions & 0 deletions src/StringUtils_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,59 @@ TEST(StringUtils, SplitOneDelimiterAtEnd)
EXPECT_EQ("", pieces[1]);
}

/////////////////////////////////////////////////
TEST(Join, Simple)
{
{
std::string delim = " ";
auto pieces = std::vector<std::string>{"Hello", "World"};
auto joined = common::Join(pieces, delim);
EXPECT_EQ("Hello World", joined);
}

{
std::string delim = "x";
auto pieces = std::vector<std::string>{"Hello", "World"};
auto joined = common::Join(pieces, delim);
EXPECT_EQ("HelloxWorld", joined);
}

{
std::string delim = "x";
auto pieces = std::vector<std::string>{
"foo", "bar", "baz", "bing"};
auto joined = common::Join(pieces, delim);
EXPECT_EQ("fooxbarxbazxbing", joined);
}
}

/////////////////////////////////////////////////
TEST(Join, EmptyDelim)
{
std::string delim = "";
auto pieces = std::vector<std::string>{"Hello", "World"};
auto joined = common::Join(pieces, delim);
EXPECT_EQ("HelloWorld", joined);
}

/////////////////////////////////////////////////
TEST(Join, EmptyPart)
{
std::string delim = " ";
auto pieces = std::vector<std::string>{"Hello", "", "World"};
auto joined = common::Join(pieces, delim);
EXPECT_EQ("Hello World", joined);
}

/////////////////////////////////////////////////
TEST(Join, EmptyParts)
{
std::string delim = "x";
auto pieces = std::vector<std::string>{};
auto joined = common::Join(pieces, delim);
EXPECT_EQ("", joined);
}

/////////////////////////////////////////////////
TEST(StartsWith, NotInString)
{
Expand Down

0 comments on commit faff823

Please sign in to comment.