M
ma740988
I'm interested in displaying the variable names for methods with
single arguments: The code below does just that and produce the right
output: i.e
arg_a
arg_b
arg_c
Something tells me the parse function could be alot simpler.
Critiques welcomed. Thanks
# include <sstream>
# include <string>
# include <iostream>
# include <iomanip>
# include <algorithm>
void parse ( std::string& to_parse ) {
std::string::size_type const posb = to_parse.find_first_of
( '(' ) ;
std::string::size_type const pose = to_parse.find_last_of ( ')' ) ;
if ( posb == std::string::npos || pose == std::string::npos ) {
return ;
}
to_parse = to_parse.substr( posb , pose );
std::replace( to_parse.begin(), to_parse.end(), ' ', '+' );
std::string::iterator it ;
int end = 0; int beg = 0 ;
int sz = to_parse.size();
for( it = to_parse.end() - 1, sz; it != to_parse.begin() ; --it, --
sz ) {
if ( *it == ')' ) {
continue ;
} else if ( *it == '+' ) {
if ( end ) { break ; }
else { continue; }
} else {
if ( !end ) {
end = sz ;
} else {
beg = sz - 1;
}
}
}
to_parse = to_parse.substr( beg, end - beg );
}
int main() {
const std::string strr( "void Set_Whatever( int arg_a )\n"
"void Set_This( unsigned int arg_b )\n"
"void Set_That( double arg_c )\n" );
std::istringstream isss( strr );
std::string mline ;
while ( std::getline ( isss, mline ) ) {
parse ( mline ) ;
std::cout << mline << std::endl;
}
std::cin.get() ;
}
single arguments: The code below does just that and produce the right
output: i.e
arg_a
arg_b
arg_c
Something tells me the parse function could be alot simpler.
Critiques welcomed. Thanks
# include <sstream>
# include <string>
# include <iostream>
# include <iomanip>
# include <algorithm>
void parse ( std::string& to_parse ) {
std::string::size_type const posb = to_parse.find_first_of
( '(' ) ;
std::string::size_type const pose = to_parse.find_last_of ( ')' ) ;
if ( posb == std::string::npos || pose == std::string::npos ) {
return ;
}
to_parse = to_parse.substr( posb , pose );
std::replace( to_parse.begin(), to_parse.end(), ' ', '+' );
std::string::iterator it ;
int end = 0; int beg = 0 ;
int sz = to_parse.size();
for( it = to_parse.end() - 1, sz; it != to_parse.begin() ; --it, --
sz ) {
if ( *it == ')' ) {
continue ;
} else if ( *it == '+' ) {
if ( end ) { break ; }
else { continue; }
} else {
if ( !end ) {
end = sz ;
} else {
beg = sz - 1;
}
}
}
to_parse = to_parse.substr( beg, end - beg );
}
int main() {
const std::string strr( "void Set_Whatever( int arg_a )\n"
"void Set_This( unsigned int arg_b )\n"
"void Set_That( double arg_c )\n" );
std::istringstream isss( strr );
std::string mline ;
while ( std::getline ( isss, mline ) ) {
parse ( mline ) ;
std::cout << mline << std::endl;
}
std::cin.get() ;
}