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

use string view in string trim

parent fd93cba8
No related branches found
No related tags found
1 merge request!102Merge jt
......@@ -395,7 +395,7 @@ qType:
res.m_value += m_source->getCharacter ( );
m_source->advance ( readNextLine );
}
ext::rtrim ( res.m_value );
res.m_value = ext::rtrim ( res.m_value );
 
res.m_type = TokenType::TYPE;
return res;
......
......@@ -433,9 +433,10 @@ inline bool not_isspace ( int ch ) {
*
* \return string without spaces at front of it
*/
inline void ltrim ( std::string & s ) {
inline std::string_view ltrim ( std::string_view s ) {
auto endPos = std::find_if ( s.begin ( ), s.end ( ), not_isspace );
s.erase ( s.begin ( ), endPos );
s.remove_prefix ( std::distance ( s.begin ( ), endPos ) );
return s;
}
 
/**
......@@ -446,9 +447,10 @@ inline void ltrim ( std::string & s ) {
*
* \return string without spaces at back of it
*/
inline void rtrim ( std::string & s ) {
inline std::string_view rtrim ( std::string_view s ) {
auto beginPos = std::find_if ( s.rbegin ( ), s.rend ( ), not_isspace ).base ( );
s.erase ( beginPos, s.end ( ) );
s.remove_suffix ( std::distance ( beginPos, s.end ( ) ) );
return s;
}
 
/**
......@@ -459,11 +461,16 @@ inline void rtrim ( std::string & s ) {
*
* \return string without spaces at both sides of it
*/
inline void trim ( std::string & s ) {
ltrim(s);
rtrim(s);
inline std::string_view trim ( std::string_view s ) {
return rtrim(ltrim(s));
}
 
// Creates a string view from a pair of iterators
// http://stackoverflow.com/q/33750600/882436
inline std::string_view make_string_view ( std::string::const_iterator begin, std::string::const_iterator end ) {
return std::string_view { ( begin != end ) ? & * begin : nullptr, ( typename std::string_view::size_type ) std::max ( std::distance ( begin, end ), ( typename std::string_view::difference_type ) 0 ) };
} // make_string_view
} /* namespace ext */
 
#endif /* __STRING_HPP_ */
......@@ -12,29 +12,29 @@
namespace ext {
 
std::string erase_template_info ( std::string str ) {
std::string::iterator begin = str.begin ( );
for ( ; ; ) {
std::pair < std::string::iterator, std::string::iterator > rangeRes = ext::find_range ( str.begin ( ), str.end ( ), '<', '>' );
std::pair < std::string::iterator, std::string::iterator > rangeRes = ext::find_range ( begin, str.end ( ), '<', '>' );
 
if ( rangeRes.first == rangeRes.second )
break;
 
str.erase ( rangeRes.first, rangeRes.second );
begin = str.erase ( rangeRes.first, rangeRes.second );
}
return str;
}
 
ext::vector < std::string > get_template_info ( std::string str ) {
ext::vector < std::string > get_template_info ( const std::string & str ) {
ext::vector < std::string > res;
std::string::const_iterator begin = str.begin ( );
for ( ; ; ) {
std::pair < std::string::iterator, std::string::iterator > rangeRes = ext::find_range ( str.begin ( ), str.end ( ), '<', '>' );
std::pair < std::string::const_iterator, std::string::const_iterator > rangeRes = ext::find_range ( begin, str.end ( ), '<', '>' );
 
if ( rangeRes.first == rangeRes.second )
break;
std::string tmp ( rangeRes.first + 1, rangeRes.second - 1 );
ext::trim ( tmp );
res.push_back ( tmp );
res.push_back ( std::string ( ext::trim ( ext::make_string_view ( rangeRes.first + 1, rangeRes.second - 1 ) ) ) );
 
str.erase ( rangeRes.first, rangeRes.second );
begin = rangeRes.second;
}
 
return res;
......
......@@ -70,7 +70,7 @@ bool is_same_type ( const std::string & name ) {
}
 
std::string erase_template_info ( std::string str );
ext::vector < std::string > get_template_info ( std::string str );
ext::vector < std::string > get_template_info ( const std::string & str );
 
} /* namespace ext */
 
......
......@@ -73,6 +73,7 @@ TEST_CASE ( "TypeInfo", "[unit][std][bits]" ) {
 
SECTION ( "Get Template Info" ) {
ext::vector < std::string > templateInfo = ext::get_template_info ( "bar::Foo < int >::Baz < foo::Foo < bar > >" );
CHECK ( templateInfo == std::vector < std::string > { "int", "foo::Foo < bar >" } );
CHECK ( templateInfo [ 0 ] == "int" );
CHECK ( templateInfo [ 1 ] == "foo::Foo < bar >" );
}
}
......@@ -23,8 +23,7 @@ public:
~HistoryRegister ( ) {
if ( m_allowHistory ) {
std::string line = m_lexer.getLine ( );
ext::trim ( line );
ReadlinePromptHistory::addHistory ( line );
ReadlinePromptHistory::addHistory ( ( std::string ) ext::trim ( line ) );
}
}
};
......
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