#include <iostream>
#include <algorithm>
inline bool not_space(char c){
return c != ' ' && ( 9 > c || c > 13 );
}
inline bool my_isspace(char c){
return std::isspace(static_cast<unsigned char>(c));
}
template<typename FN>
auto find_if(std::string & s, FN fn){
return std::find_if(s.begin(), s.end(), fn);
}
template<typename FN>
auto rfind_if(std::string & s, FN fn){
return std::find_if(s.rbegin(), s.rend(), fn).base();
}
inline void ltrim(std::string &s) {
s.erase(s.begin(), find_if(s, not_space));
}
inline void rtrim(std::string &s) {
s.erase(rfind_if(s, not_space), s.end());
}
// modify a copy
std::string trim(std::string const& s){
std::string t{s};
ltrim(t);
rtrim(t);
return t;
}
// modify a move
std::string trim(std::string&& s){
ltrim(s);
rtrim(s);
return s;
}
int main(){
std::string s{" abc "};
std::cout << trim(s) << '\n'; // copy
std::string ss{" def "};
std::cout << trim(std::move(ss)) << '\n'; // move
std::string const t{" xyz "};
std::cout << trim(t) << '\n'; // copy
std::cout << '"' << trim(" sdf ") << '"'; // move
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKaW5saW5lIGJvb2wgbm90X3NwYWNlKGNoYXIgYyl7CiAgICByZXR1cm4gYyAhPSAnICcgJiYgKCA5ID4gYyB8fCBjID4gMTMgKTsKfQoKaW5saW5lIGJvb2wgbXlfaXNzcGFjZShjaGFyIGMpewogICAgcmV0dXJuIHN0ZDo6aXNzcGFjZShzdGF0aWNfY2FzdDx1bnNpZ25lZCBjaGFyPihjKSk7Cn0KCnRlbXBsYXRlPHR5cGVuYW1lIEZOPgphdXRvIGZpbmRfaWYoc3RkOjpzdHJpbmcgJiBzLCBGTiBmbil7CiAgICByZXR1cm4gc3RkOjpmaW5kX2lmKHMuYmVnaW4oKSwgcy5lbmQoKSwgZm4pOwp9CnRlbXBsYXRlPHR5cGVuYW1lIEZOPgphdXRvIHJmaW5kX2lmKHN0ZDo6c3RyaW5nICYgcywgRk4gZm4pewogICAgcmV0dXJuIHN0ZDo6ZmluZF9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