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

more features in std extension

parent 68b3ad91
No related branches found
No related tags found
No related merge requests found
...@@ -77,6 +77,49 @@ inline bool orAll ( T value, Ts ... other ) { ...@@ -77,6 +77,49 @@ inline bool orAll ( T value, Ts ... other ) {
return value || orAll ( 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 */ } /* namespace ext */
   
#endif /* __ALGORITHM_HPP_ */ #endif /* __ALGORITHM_HPP_ */
...@@ -101,8 +101,7 @@ std::string cstringToString ( char * param ) { ...@@ -101,8 +101,7 @@ std::string cstringToString ( char * param ) {
return res; return res;
} }
   
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 part;
ext::vector < std::string > res; ext::vector < std::string > res;
size_t start_pos = 0; size_t start_pos = 0;
size_t end_pos; size_t end_pos;
...@@ -117,4 +116,19 @@ ext::vector < std::string > explode ( std::string source, std::string delimiter ...@@ -117,4 +116,19 @@ ext::vector < std::string > explode ( std::string source, std::string delimiter
return res; 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 */ } /* namespace ext */
...@@ -121,7 +121,9 @@ struct compare < std::string > { ...@@ -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 */ } /* namespace ext */
   
......
/*
* 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 */
...@@ -38,6 +38,8 @@ bool is_same_type(const char* name) { ...@@ -38,6 +38,8 @@ bool is_same_type(const char* name) {
} }
} }
   
std::string erase_template_info ( std::string str );
} /* namespace ext */ } /* namespace ext */
   
#endif /* __TYPEINFO_HPP_ */ #endif /* __TYPEINFO_HPP_ */
...@@ -60,3 +60,20 @@ void AlgorithmTest::testTransform() { ...@@ -60,3 +60,20 @@ void AlgorithmTest::testTransform() {
   
CPPUNIT_ASSERT(ref == out); 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" );
}
...@@ -8,6 +8,7 @@ class AlgorithmTest : public CppUnit::TestFixture ...@@ -8,6 +8,7 @@ class AlgorithmTest : public CppUnit::TestFixture
CPPUNIT_TEST_SUITE( AlgorithmTest ); CPPUNIT_TEST_SUITE( AlgorithmTest );
CPPUNIT_TEST( testExclude ); CPPUNIT_TEST( testExclude );
CPPUNIT_TEST( testTransform ); CPPUNIT_TEST( testTransform );
CPPUNIT_TEST( testFindRange );
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
   
public: public:
...@@ -16,6 +17,7 @@ public: ...@@ -16,6 +17,7 @@ public:
   
void testExclude(); void testExclude();
void testTransform(); void testTransform();
void testFindRange();
}; };
   
#endif // ALGORITHM_TEST_H_ #endif // ALGORITHM_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