10 template<bool long_sep, bool allow_empty>
11 vector<string> do_split(const string &str, const string &sep, int max_split)
13 vector<string> result;
16 while(start<str.size())
18 unsigned end = long_sep ? str.find(sep, start) : str.find_first_of(sep, start);
19 if(end!=start || allow_empty)
21 if(max_split>=0 && result.size()==static_cast<unsigned>(max_split))
23 result.push_back(str.substr(start));
27 result.push_back(str.substr(start, end-start));
33 start = end+(long_sep ? sep.size() : 1);
35 if(allow_empty && start==str.size())
36 result.push_back(string());
42 bool check_str(const std::string &str, int (*pred)(int))
44 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
55 int strcasecmp(const string &s1, const string &s2)
57 string::const_iterator i1 = s1.begin();
58 string::const_iterator i2 = s2.begin();
59 for(; (i1!=s1.end() && i2!=s2.end()); ++i1, ++i2)
61 const char c1 = ::tolower(*i1);
62 const char c2 = ::tolower(*i2);
63 if(c1!=c2) return c1-c2;
65 if(i1!=s1.end()) return *i1;
66 if(i2!=s2.end()) return -*i2;
70 string tolower(const string &str)
73 transform(result.begin(), result.end(), result.begin(), ::tolower);
77 string toupper(const string &str)
80 transform(result.begin(), result.end(), result.begin(), ::toupper);
84 bool isnumrc(const string &str)
86 return check_str(str, isdigit);
89 bool isalpha(const string &str)
91 return check_str(str, isalpha);
94 bool isalnum(const string &str)
96 return check_str(str, isalnum);
99 vector<string> split(const string &str, const string &sep, int max_split)
101 return do_split<false, false>(str, sep, max_split);
104 vector<string> split(const string &str, char sep, int max_split)
106 return split(str, string(1, sep), max_split);
109 vector<string> split_long(const string &str, const string &sep, int max_split)
111 return do_split<true, false>(str, sep, max_split);
114 vector<string> split_fields(const string &str, const string &sep, int max_split)
116 return do_split<true, true>(str, sep, max_split);
119 vector<string> split_fields(const string &str, char sep, int max_split)
121 return split_fields(str, string(1, sep), max_split);
124 string strip(const string &s)
127 if(!result.erase(0, result.find_first_not_of(" \t\r\n")).empty())
128 result.erase(result.find_last_not_of(" \t\r\n")+1);
132 string c_unescape(const std::string &str)
135 unsigned numeric_type = 0;
136 unsigned numeric_pos = 0;
137 unsigned numeric_value = 0;
139 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
144 if(*i>='0' && *i<='9')
146 else if(*i>='a' && *i<='f')
148 else if(*i>='A' && *i<='F')
151 throw invalid_argument("c_unescape");
153 numeric_value = (numeric_value<<4 | digit);
157 result += numeric_value;
161 else if(numeric_type==8)
164 if(*i>='0' && *i<='7')
167 throw invalid_argument("c_unescape");
169 numeric_value = (numeric_value<<3 | digit);
173 result += numeric_value;
185 else if(*i>='0' && *i<='3')
189 numeric_value = *i-'0';
212 throw invalid_argument("c_unescape");
223 throw invalid_argument("c_unescape");
228 string c_escape(const string &str, bool escape_8bit)
232 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
254 else if(static_cast<unsigned char>(*i)<' ' || (escape_8bit && (*i&0x80)))
256 char buf[4] = {'\\', '0'+((*i>>6)&3), '0'+((*i>>3)&7), '0'+(*i&7)};
257 result.append(buf, 4);