From 0aca366cc7d43d675ddbbdbaa8d89160d111fce3 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Wed, 23 Aug 2017 09:43:36 +0200
Subject: [PATCH] explode string by delimiter

---
 alib2std/src/extensions/string.cpp          | 16 +++++++
 alib2std/src/extensions/string.hpp          |  3 ++
 alib2std/test-src/extensions/StringTest.cpp | 48 +++++++++++++++++++++
 alib2std/test-src/extensions/StringTest.h   | 31 ++++++-------
 4 files changed, 83 insertions(+), 15 deletions(-)

diff --git a/alib2std/src/extensions/string.cpp b/alib2std/src/extensions/string.cpp
index 42937a3800..5fc301800f 100644
--- a/alib2std/src/extensions/string.cpp
+++ b/alib2std/src/extensions/string.cpp
@@ -101,4 +101,20 @@ std::string cstringToString ( char * param ) {
 	return res;
 }
 
+ext::vector < std::string > explode ( std::string source, std::string delimiter ) {
+	std::string part;
+	ext::vector < std::string > res;
+	size_t start_pos = 0;
+	size_t end_pos;
+
+	while ( ( end_pos = source.find ( delimiter, start_pos ) ) != std::string::npos ) {
+		res.push_back ( source.substr ( start_pos, end_pos - start_pos ) );
+		start_pos = end_pos + delimiter.size ( );
+	}
+
+	res.push_back ( source.substr ( start_pos, source.size ( ) ) );
+
+	return res;
+}
+
 } /* namespace ext */
diff --git a/alib2std/src/extensions/string.hpp b/alib2std/src/extensions/string.hpp
index 0b836db787..51c2f5f972 100644
--- a/alib2std/src/extensions/string.hpp
+++ b/alib2std/src/extensions/string.hpp
@@ -11,6 +11,7 @@
 #include <string>
 #include <stdexcept>
 #include "compare.hpp"
+#include "vector.hpp"
 
 namespace ext {
 
@@ -112,6 +113,8 @@ struct compare < std::string > {
 
 };
 
+ext::vector < std::string > explode ( std::string source, std::string delimiter );
+
 } /* namespace ext */
 
 #endif /* __STRING_HPP_ */
diff --git a/alib2std/test-src/extensions/StringTest.cpp b/alib2std/test-src/extensions/StringTest.cpp
index ab517dae21..0b7889f321 100644
--- a/alib2std/test-src/extensions/StringTest.cpp
+++ b/alib2std/test-src/extensions/StringTest.cpp
@@ -18,3 +18,51 @@ void StringTest::testToString() {
 	CPPUNIT_ASSERT ( ext::to_string ( a ) == "" );
 }
 
+void StringTest::testExplode ( ) {
+	{
+		std::string one ( "a::bb::ccc" );
+		ext::vector < std::string > oneExploded = ext::explode ( one, "::" );
+
+		std::cout << oneExploded << std::endl;
+
+		CPPUNIT_ASSERT ( oneExploded.size ( ) == 3 );
+		CPPUNIT_ASSERT ( oneExploded [ 0 ] == "a" );
+		CPPUNIT_ASSERT ( oneExploded [ 1 ] == "bb" );
+		CPPUNIT_ASSERT ( oneExploded [ 2 ] == "ccc" );
+	}
+	{
+		std::string one ( "a::bb::ccc::" );
+		ext::vector < std::string > oneExploded = ext::explode ( one, "::" );
+
+		CPPUNIT_ASSERT ( oneExploded.size ( ) == 4 );
+		CPPUNIT_ASSERT ( oneExploded [ 0 ] == "a" );
+		CPPUNIT_ASSERT ( oneExploded [ 1 ] == "bb" );
+		CPPUNIT_ASSERT ( oneExploded [ 2 ] == "ccc" );
+		CPPUNIT_ASSERT ( oneExploded [ 3 ] == "" );
+	}
+	{
+		std::string one ( "" );
+		ext::vector < std::string > oneExploded = ext::explode ( one, "::" );
+
+		CPPUNIT_ASSERT ( oneExploded.size ( ) == 1 );
+		CPPUNIT_ASSERT ( oneExploded [ 0 ] == "" );
+	}
+	{
+		std::string one ( "::aa" );
+		ext::vector < std::string > oneExploded = ext::explode ( one, "::" );
+
+		std::cout << oneExploded << std::endl;
+
+		CPPUNIT_ASSERT ( oneExploded.size ( ) == 2 );
+		CPPUNIT_ASSERT ( oneExploded [ 0 ] == "" );
+		CPPUNIT_ASSERT ( oneExploded [ 1 ] == "aa" );
+	}
+	{
+		std::string one ( "::" );
+		ext::vector < std::string > oneExploded = ext::explode ( one, "::" );
+
+		CPPUNIT_ASSERT ( oneExploded.size ( ) == 2 );
+		CPPUNIT_ASSERT ( oneExploded [ 0 ] == "" );
+		CPPUNIT_ASSERT ( oneExploded [ 1 ] == "" );
+	}
+}
diff --git a/alib2std/test-src/extensions/StringTest.h b/alib2std/test-src/extensions/StringTest.h
index 75be89adb1..28c4c94ed3 100644
--- a/alib2std/test-src/extensions/StringTest.h
+++ b/alib2std/test-src/extensions/StringTest.h
@@ -3,24 +3,25 @@
 
 #include <cppunit/extensions/HelperMacros.h>
 
-class StringTest : public CppUnit::TestFixture
-{
-  CPPUNIT_TEST_SUITE( StringTest );
-  CPPUNIT_TEST( testToString );
-  CPPUNIT_TEST_SUITE_END();
+class StringTest : public CppUnit::TestFixture {
+	CPPUNIT_TEST_SUITE( StringTest );
+	CPPUNIT_TEST( testToString );
+	CPPUNIT_TEST( testExplode );
+	CPPUNIT_TEST_SUITE_END();
 
-  class A {
-  public:
-    explicit operator std::string ( ) const {
-      return "";
-    }
-  };
+	class A {
+	public:
+		explicit operator std::string ( ) const {
+			return "";
+		}
+	};
 
 public:
-  void setUp();
-  void tearDown();
+	void setUp ( );
+	void tearDown ( );
 
-  void testToString();
+	void testToString ( );
+	void testExplode ( );
 };
 
-#endif  // STRING_TEST_H_
+#endif /* STRING_TEST_H_ */
-- 
GitLab