From 376ec4fab8e0054085a62a20bbbd00a072c3056d Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Tue, 8 Nov 2016 10:02:56 +0100 Subject: [PATCH] Add overload of join_string for vector argument + unittest --- src/stringfunc.cpp | 21 +++++++++++++++++++++ src/stringfunc.hxx | 6 ++++++ test/stringfunc.cpp | 21 +++++++++++++++++++++ 3 files changed, 48 insertions(+), 0 deletions(-) diff --git a/src/stringfunc.cpp b/src/stringfunc.cpp index fa5729c..c50eb24 100644 --- a/src/stringfunc.cpp +++ b/src/stringfunc.cpp @@ -431,6 +431,27 @@ std::string join_string( } // eo join_string(const std::list< std::string >&,const std::string&) +/** @brief same as join_string for list, except uses a vector */ +std::string join_string( + const std::vector< std::string >& parts, + const std::string& delimiter +) +{ + std::string result; + if (! parts.empty() ) + { + std::vector< std::string >::const_iterator it= parts.begin(); + result = *it; + while ( ++it != parts.end() ) + { + result+= delimiter; + result+= *it; + } + } + return result; +} // eo join_string(const std::vector< std::string >&,const std::string&) + + /* ** conversions diff --git a/src/stringfunc.hxx b/src/stringfunc.hxx index 7f108a4..13ef703 100644 --- a/src/stringfunc.hxx +++ b/src/stringfunc.hxx @@ -35,6 +35,7 @@ on this file might be covered by the GNU General Public License. #define __STRINGFUNC_HXX #include +#include #include #include #include @@ -126,6 +127,11 @@ std::string join_string( const std::string& delimiter = "\n" ); +std::string join_string( + const std::vector< std::string >& parts, + const std::string& delimiter = "\n" +); + /* ** conversions: diff --git a/test/stringfunc.cpp b/test/stringfunc.cpp index fe50d4c..ee18355 100644 --- a/test/stringfunc.cpp +++ b/test/stringfunc.cpp @@ -621,6 +621,27 @@ BOOST_AUTO_TEST_CASE(JoinString1) } // eo JoinString1 +BOOST_AUTO_TEST_CASE(JoinStringVector) +{ + std::vector< std::string > parts; + get_push_back_filler(parts)("1")("2")("drei"); + + std::string joined_string= join_string(parts,"/"); + // we should have slashes between the strings: + BOOST_CHECK_EQUAL( std::string("1/2/drei") , joined_string ); + + parts.push_back( std::string() ); + joined_string= join_string(parts,"/"); + // now we should have an additional trailing slash: + BOOST_CHECK_EQUAL( std::string("1/2/drei/") , joined_string ); + + parts.insert(parts.begin(), ""); + joined_string= join_string(parts,"/"); + // now we should have an additional leading slash: + BOOST_CHECK_EQUAL( std::string("/1/2/drei/") , joined_string ); + +} // eo JoinStringVector + BOOST_AUTO_TEST_CASE(ConversionStringInt) { -- 1.7.1