3 This file is part of libmspstrings
4 Copyright © 2006-2008 Mikko Rasa
5 Distributed under the LGPL
10 #include <msp/core/except.h>
17 template<bool long_sep, bool allow_empty>
18 vector<string> do_split(const string &str, const string &sep, int max_split)
20 vector<string> result;
23 while(start<str.size())
25 unsigned end = long_sep ? str.find(sep, start) : str.find_first_of(sep, start);
26 if(end!=start || allow_empty)
28 if(max_split>=0 && result.size()==static_cast<unsigned>(max_split))
30 result.push_back(str.substr(start));
34 result.push_back(str.substr(start, end-start));
40 start = end+(long_sep ? sep.size() : 1);
42 if(allow_empty && start==str.size())
43 result.push_back(string());
49 bool check_str(const std::string &str, int (*pred)(int))
51 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
62 int strcasecmp(const string &s1, const string &s2)
64 string::const_iterator i1 = s1.begin();
65 string::const_iterator i2 = s2.begin();
66 for(; (i1!=s1.end() && i2!=s2.end()); ++i1, ++i2)
68 const char c1 = ::tolower(*i1);
69 const char c2 = ::tolower(*i2);
70 if(c1!=c2) return c1-c2;
72 if(i1!=s1.end()) return *i1;
73 if(i2!=s2.end()) return -*i2;
77 string tolower(const string &str)
80 transform(result.begin(), result.end(), result.begin(), ::tolower);
84 string toupper(const string &str)
87 transform(result.begin(), result.end(), result.begin(), ::toupper);
91 bool isnumrc(const string &str)
93 return check_str(str, isdigit);
96 bool isalpha(const string &str)
98 return check_str(str, isalpha);
101 bool isalnum(const string &str)
103 return check_str(str, isalnum);
106 vector<string> split(const string &str, const string &sep, int max_split)
108 return do_split<false, false>(str, sep, max_split);
111 vector<string> split(const string &str, char sep, int max_split)
113 return split(str, string(1, sep), max_split);
116 vector<string> split_long(const string &str, const string &sep, int max_split)
118 return do_split<true, false>(str, sep, max_split);
121 vector<string> split_fields(const string &str, const string &sep, int max_split)
123 return do_split<true, true>(str, sep, max_split);
126 vector<string> split_fields(const string &str, char sep, int max_split)
128 return split_fields(str, string(1, sep), max_split);
131 string strip(const string &s)
134 if(!result.erase(0, result.find_first_not_of(" \t\r\n")).empty())
135 result.erase(result.find_last_not_of(" \t\r\n")+1);
139 string c_unescape(const std::string &str)
142 unsigned numeric_type = 0;
143 unsigned numeric_pos = 0;
144 unsigned numeric_value = 0;
146 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
151 if(*i>='0' && *i<='9')
153 else if(*i>='a' && *i<='f')
155 else if(*i>='A' && *i<='F')
158 throw InvalidParameterValue("Invalid hexadecimal digit");
160 numeric_value = (numeric_value<<4 | digit);
164 result += numeric_value;
168 else if(numeric_type==8)
171 if(*i>='0' && *i<='7')
174 throw InvalidParameterValue("Invalid octal digit");
176 numeric_value = (numeric_value<<3 | digit);
180 result += numeric_value;
192 else if(*i>='0' && *i<='3')
196 numeric_value = *i-'0';
219 throw InvalidParameterValue("Invalid escape sequence");
230 throw InvalidParameterValue("Stray backslash at end of string");
235 string c_escape(const string &str, bool escape_8bit)
239 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
261 else if(static_cast<unsigned char>(*i)<' ' || (escape_8bit && (*i&0x80)))
263 char buf[4] = {'\\', '0'+((*i>>6)&3), '0'+((*i>>3)&7), '0'+(*i&7)};
264 result.append(buf, 4);