Add overload of join_string for vector argument + unittest
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 8 Nov 2016 09:02:56 +0000 (10:02 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 8 Nov 2016 09:02:56 +0000 (10:02 +0100)
src/stringfunc.cpp
src/stringfunc.hxx
test/stringfunc.cpp

index fa5729c..c50eb24 100644 (file)
@@ -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
index 7f108a4..13ef703 100644 (file)
@@ -35,6 +35,7 @@ on this file might be covered by the GNU General Public License.
 #define __STRINGFUNC_HXX
 
 #include <list>
+#include <vector>
 #include <string>
 #include <sstream>
 #include <stdexcept>
@@ -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:
index fe50d4c..ee18355 100644 (file)
@@ -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)
 {