11 template<bool long_sep, bool allow_empty>
12 vector<string> do_split(const string &str, const string &sep, int max_split)
14 vector<string> result;
17 while(start<str.size())
19 unsigned end = long_sep ? str.find(sep, start) : str.find_first_of(sep, start);
20 if(end!=start || allow_empty)
22 if(max_split>=0 && result.size()==static_cast<unsigned>(max_split))
24 result.push_back(str.substr(start));
28 result.push_back(str.substr(start, end-start));
34 start = end+(long_sep ? sep.size() : 1);
36 if(allow_empty && start==str.size())
37 result.push_back(string());
43 bool check_str(const std::string &str, int (*pred)(int))
45 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
56 int strcasecmp(const string &s1, const string &s2)
58 string::const_iterator i1 = s1.begin();
59 string::const_iterator i2 = s2.begin();
60 for(; (i1!=s1.end() && i2!=s2.end()); ++i1, ++i2)
62 const char c1 = ::tolower(*i1);
63 const char c2 = ::tolower(*i2);
64 if(c1!=c2) return c1-c2;
66 if(i1!=s1.end()) return *i1;
67 if(i2!=s2.end()) return -*i2;
71 string tolower(const string &str)
74 transform(result.begin(), result.end(), result.begin(), ::tolower);
78 string toupper(const string &str)
81 transform(result.begin(), result.end(), result.begin(), ::toupper);
85 bool isnumrc(const string &str)
87 return check_str(str, isdigit);
90 bool isalpha(const string &str)
92 return check_str(str, isalpha);
95 bool isalnum(const string &str)
97 return check_str(str, isalnum);
100 vector<string> split(const string &str, const string &sep, int max_split)
102 return do_split<false, false>(str, sep, max_split);
105 vector<string> split(const string &str, char sep, int max_split)
107 return split(str, string(1, sep), max_split);
110 vector<string> split_long(const string &str, const string &sep, int max_split)
112 return do_split<true, false>(str, sep, max_split);
115 vector<string> split_fields(const string &str, const string &sep, int max_split)
117 return do_split<true, true>(str, sep, max_split);
120 vector<string> split_fields(const string &str, char sep, int max_split)
122 return split_fields(str, string(1, sep), max_split);
125 string strip(const string &s)
128 if(!result.erase(0, result.find_first_not_of(" \t\r\n")).empty())
129 result.erase(result.find_last_not_of(" \t\r\n")+1);
133 string c_unescape(const std::string &str)
136 unsigned numeric_type = 0;
137 unsigned numeric_pos = 0;
138 unsigned numeric_value = 0;
140 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
145 if(*i>='0' && *i<='9')
147 else if(*i>='a' && *i<='f')
149 else if(*i>='A' && *i<='F')
152 throw invalid_argument("c_unescape");
154 numeric_value = (numeric_value<<4 | digit);
158 result += numeric_value;
162 else if(numeric_type==8)
165 if(*i>='0' && *i<='7')
168 throw invalid_argument("c_unescape");
170 numeric_value = (numeric_value<<3 | digit);
174 result += numeric_value;
186 else if(*i>='0' && *i<='3')
190 numeric_value = *i-'0';
213 throw invalid_argument("c_unescape");
224 throw invalid_argument("c_unescape");
229 string c_escape(const string &str, bool escape_8bit)
233 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
255 else if(static_cast<unsigned char>(*i)<' ' || (escape_8bit && (*i&0x80)))
257 char buf[4] = { '\\', 0 };
258 for(unsigned j=0; j<3; ++j)
259 buf[1+j] = '0'+((static_cast<unsigned char>(*i)>>(6-j*3))&7);
260 result.append(buf, 4);