diff --git a/alib2std/src/extensions/algorithm.hpp b/alib2std/src/extensions/algorithm.hpp index 34da27e717cdc7941b4f6b0ceff538b39cc3837c..a0615c6c39894590c2b1fc1bf7cb2e51cd945c9f 100644 --- a/alib2std/src/extensions/algorithm.hpp +++ b/alib2std/src/extensions/algorithm.hpp @@ -77,6 +77,49 @@ inline bool orAll ( T value, Ts ... other ) { return value || orAll ( other ... ); } +template < class Iterator, class Value > +std::pair < Iterator, Iterator > __find_range ( Iterator begin, Iterator end, const Value & open, const Value & close ) { + for ( ; begin != end && * begin != open && * begin != close ; ++ begin ); + + if ( begin == end || * begin == close ) + return std::make_pair ( begin, begin ); + + Iterator openPos = begin; + + for ( ; ; ) { + std::pair < Iterator, Iterator > subRange = __find_range ( begin + 1, end, open, close ); + if ( subRange.first == subRange.second ) + break; + begin = subRange.second; + } + + if ( begin == end ) + return std::make_pair ( begin, begin ); + + ++ begin; + + for ( ; begin != end && * begin != close ; ++ begin ); + + if ( begin == end ) + return std::make_pair ( begin, begin ); + + Iterator closePos = begin; + + return std::make_pair ( openPos, closePos ); +} + +template < class Iterator, class Value > +std::pair < Iterator, Iterator > find_range ( Iterator begin, Iterator end, const Value & open, const Value & close ) { + std::pair < Iterator, Iterator > res = __find_range ( begin, end, open, close ); + + if ( res.first == res.second ) + return res; + + ++ res.second; + + return res; +} + } /* namespace ext */ #endif /* __ALGORITHM_HPP_ */ diff --git a/alib2std/src/extensions/string.cpp b/alib2std/src/extensions/string.cpp index 5fc301800f2a2d2ab99f320efe8e99cbdb95a06e..31c9f3bff4c1a44dc6657b8f5f3f170a446d0576 100644 --- a/alib2std/src/extensions/string.cpp +++ b/alib2std/src/extensions/string.cpp @@ -101,8 +101,7 @@ 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 > explode ( const std::string & source, const std::string & delimiter ) { ext::vector < std::string > res; size_t start_pos = 0; size_t end_pos; @@ -117,4 +116,19 @@ ext::vector < std::string > explode ( std::string source, std::string delimiter return res; } +std::string implode ( const std::vector < std::string > & source, const std::string & delimiter ) { + std::stringstream ss; + bool first = true; + for ( const std::string & str : source ) { + if ( first ) + first = false; + else + ss << delimiter; + + ss << str; + } + + return ss.str ( ); +} + } /* namespace ext */ diff --git a/alib2std/src/extensions/string.hpp b/alib2std/src/extensions/string.hpp index bff158c0919cfbbd4be730ee55d7feea7c9e24a4..445acbe0518f7ba34707f50a1d7dcd769ad3b89d 100644 --- a/alib2std/src/extensions/string.hpp +++ b/alib2std/src/extensions/string.hpp @@ -121,7 +121,9 @@ struct compare < std::string > { }; -ext::vector < std::string > explode ( std::string source, std::string delimiter ); +ext::vector < std::string > explode ( const std::string & source, const std::string & delimiter ); + +std::string implode ( const std::vector < std::string > & source, const std::string & delimiter ); } /* namespace ext */ diff --git a/alib2std/src/extensions/typeinfo.cpp b/alib2std/src/extensions/typeinfo.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5e6077efc46c9b72039106c43c27ebb38210f46 --- /dev/null +++ b/alib2std/src/extensions/typeinfo.cpp @@ -0,0 +1,25 @@ +/* + * typeinfo.cpp + * + * Created on: Oct 19, 2017 + * Author: Jan Travnicek + */ + +#include <typeinfo> +#include <algorithm> + +namespace ext { + +std::string erase_template_info ( std::string str ) { + for ( ; ; ) { + std::pair < std::string::iterator, std::string::iterator > range = ext::find_range ( str.begin ( ), str.end ( ), '<', '>' ); + + if ( range.first == range.second ) + break; + + str.erase ( range.first, range.second ); + } + return str; +} + +} /* namespace ext */ diff --git a/alib2std/src/extensions/typeinfo.hpp b/alib2std/src/extensions/typeinfo.hpp index de1497ccafbd9d32c2831b26d554231bf1dc86d9..1b4ff8c1e64941300ea836d9691fb0c1b1e8f90a 100644 --- a/alib2std/src/extensions/typeinfo.hpp +++ b/alib2std/src/extensions/typeinfo.hpp @@ -38,6 +38,8 @@ bool is_same_type(const char* name) { } } +std::string erase_template_info ( std::string str ); + } /* namespace ext */ #endif /* __TYPEINFO_HPP_ */ diff --git a/alib2std/test-src/extensions/AlgorithmTest.cpp b/alib2std/test-src/extensions/AlgorithmTest.cpp index ecf45cdd4c470a01327941892e48358b5da3786f..2a0952edc6605dd89d33656963e226636bf6f2d1 100644 --- a/alib2std/test-src/extensions/AlgorithmTest.cpp +++ b/alib2std/test-src/extensions/AlgorithmTest.cpp @@ -60,3 +60,20 @@ void AlgorithmTest::testTransform() { CPPUNIT_ASSERT(ref == out); } + +void AlgorithmTest::testFindRange ( ) { + std::string str = "abc<dee<fd<<>>>th>::rrr"; + + std::pair < std::string::iterator, std::string::iterator > range = ext::find_range ( str.begin ( ), str.end ( ), '<', '>' ); + + std::cerr << range.first - str.begin ( ) << std::endl; + std::cerr << range.second - str.begin ( ) << std::endl; + + CPPUNIT_ASSERT ( range.first - str.begin ( ) == 3 ); + CPPUNIT_ASSERT ( range.second - str.begin ( ) == 18 ); + + str.erase ( range.first, range.second ); + std::cerr << str << std::endl; + + CPPUNIT_ASSERT ( str == "abc::rrr" ); +} diff --git a/alib2std/test-src/extensions/AlgorithmTest.h b/alib2std/test-src/extensions/AlgorithmTest.h index 0483ba815718109e632092ed92a11387daa62c81..688e981e3b9c0de7a5ee40315d50779df5e48d7d 100644 --- a/alib2std/test-src/extensions/AlgorithmTest.h +++ b/alib2std/test-src/extensions/AlgorithmTest.h @@ -8,6 +8,7 @@ class AlgorithmTest : public CppUnit::TestFixture CPPUNIT_TEST_SUITE( AlgorithmTest ); CPPUNIT_TEST( testExclude ); CPPUNIT_TEST( testTransform ); + CPPUNIT_TEST( testFindRange ); CPPUNIT_TEST_SUITE_END(); public: @@ -16,6 +17,7 @@ public: void testExclude(); void testTransform(); + void testFindRange(); }; #endif // ALGORITHM_TEST_H_