15 #ifndef SPROCKIT_COMMON_BASICSTRINGTOKENIZER_H_INCLUDED 16 #define SPROCKIT_COMMON_BASICSTRINGTOKENIZER_H_INCLUDED 22 inline std::string
trim_str(
const std::string& Src,
const std::string& c =
25 size_t p2 = Src.find_last_not_of(c);
26 if (p2 == std::string::npos) {
29 size_t p1 = Src.find_first_not_of(c);
30 if (p1 == std::string::npos) {
33 return Src.substr(p1, (p2 - p1) + 1);
41 namespace BasicStringTokenizer {
58 template <
class CharType,
class Traits,
class StrAlloc,
class DqAlloc>
60 (
const typename std::basic_string<CharType, Traits, StrAlloc>& str,
62 typename std::basic_string<CharType, Traits, StrAlloc>, DqAlloc
64 const typename std::basic_string<CharType, Traits, StrAlloc>& space
67 typedef typename std::basic_string<CharType, Traits, StrAlloc> StringType;
69 typename StringType::size_type walk_pos = 0;
71 while(walk_pos != StringType::npos && walk_pos < str.length()) {
72 typename StringType::size_type token_pos = 0, token_len = 0;
73 typename StringType::size_type delim_pos
74 = str.find_first_of( space, walk_pos );
76 if( delim_pos == StringType::npos ) {
79 token_len = str.length() - token_pos;
81 walk_pos += token_len;
83 else if( delim_pos > walk_pos ) {
86 token_len = delim_pos - token_pos;
90 else if( delim_pos == walk_pos ) {
92 walk_pos = str.find_first_not_of( space, walk_pos );
94 if( walk_pos == StringType::npos ) {
104 tok.push_back( str.substr( token_pos, token_len ) );
117 template <
class CharType,
class Traits,
class Alloc>
118 typename std::basic_string<CharType, Traits, Alloc>
119 trim(
const typename std::basic_string<CharType, Traits, Alloc>& s,
120 const typename std::basic_string<CharType, Traits, Alloc>& spaces
123 typedef typename std::basic_string<CharType, Traits, Alloc> StringType;
125 if(s.length() == 0) {
128 int b = s.find_first_not_of(spaces);
129 int e = s.find_last_not_of(spaces);
134 return StringType(s, b, e - b + 1);
void tokenize(const typename std::basic_string< CharType, Traits, StrAlloc > &str, typename std::deque< typename std::basic_string< CharType, Traits, StrAlloc >, DqAlloc > &tok, const typename std::basic_string< CharType, Traits, StrAlloc > &space=" \t\n")
A utility function to tokenize a std::string.
std::string trim_str(const std::string &Src, const std::string &c=" \r\n")
std::basic_string< CharType, Traits, Alloc > trim(const typename std::basic_string< CharType, Traits, Alloc > &s, const typename std::basic_string< CharType, Traits, Alloc > &spaces=" \t")
A utility function to trim spaces off a std::string.