Skip to content
Snippets Groups Projects
Commit 0aca366c authored by Jan Trávníček's avatar Jan Trávníček
Browse files

explode string by delimiter

parent 0fc707bc
No related branches found
No related tags found
No related merge requests found
......@@ -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 */
......@@ -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_ */
......@@ -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 ] == "" );
}
}
......@@ -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_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment