3 This file is part of libmspstrings
4 Copyright © 2006-2007 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());
53 int strcasecmp(const string &s1, const string &s2)
55 string::const_iterator i1=s1.begin();
56 string::const_iterator i2=s2.begin();
57 for(; (i1!=s1.end() && i2!=s2.end()); ++i1, ++i2)
59 const char c1=::tolower(*i1);
60 const char c2=::tolower(*i2);
61 if(c1!=c2) return c1-c2;
63 if(i1!=s1.end()) return *i1;
64 if(i2!=s2.end()) return -*i2;
68 string tolower(const string &str)
71 transform(result.begin(), result.end(), result.begin(), ::tolower);
75 string toupper(const string &str)
78 transform(result.begin(), result.end(), result.begin(), ::toupper);
82 vector<string> split(const string &str, const string &sep, int max_split)
84 return do_split<false, false>(str, sep, max_split);
87 vector<string> split(const string &str, char sep, int max_split)
89 return split(str, string(1, sep), max_split);
92 vector<string> split_long(const string &str, const string &sep, int max_split)
94 return do_split<true, false>(str, sep, max_split);
97 vector<string> split_fields(const string &str, const string &sep, int max_split)
99 return do_split<true, true>(str, sep, max_split);
102 vector<string> split_fields(const string &str, char sep, int max_split)
104 return split_fields(str, string(1, sep), max_split);
108 Splits a string to parts.
110 @param str String to be split
111 @param sep A set of separator characters
112 @param allow_empty Whether or not to produce empty parts for sequences of
113 more than one separator character
115 vector<string> split(const string &str, const string &sep, bool allow_empty)
117 vector<string> result;
121 start=str.find_first_not_of(sep);
123 while(start<str.size())
125 unsigned end=str.find_first_of(sep, start);
126 result.push_back(str.substr(start, end-start));
128 if(end==string::npos)
134 if(start==str.size())
135 result.push_back(string());
138 start=str.find_first_not_of(sep, end);
144 vector<string> split(const string &str, char sep, bool allow_empty)
146 return split(str, string(1, sep), allow_empty);
149 string strip(const string &s)
152 if(!result.erase(0, result.find_first_not_of(" \t\r\n")).empty())
153 result.erase(result.find_last_not_of(" \t\r\n")+1);
157 string c_unescape(const std::string &str)
160 unsigned numeric_type=0;
161 unsigned numeric_pos=0;
162 unsigned numeric_value=0;
164 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
169 if(*i>='0' && *i<='9')
171 else if(*i>='a' && *i<='f')
173 else if(*i>='A' && *i<='F')
176 throw InvalidParameterValue("Invalid hexadecimal digit");
178 numeric_value=(numeric_value<<4 | digit);
182 result+=numeric_value;
186 else if(numeric_type==8)
189 if(*i>='0' && *i<='7')
192 throw InvalidParameterValue("Invalid octal digit");
194 numeric_value=(numeric_value<<3 | digit);
198 result+=numeric_value;
210 else if(*i>='0' && *i<='3')
214 numeric_value=*i-'0';
237 throw InvalidParameterValue("Invalid escape sequence");
248 throw InvalidParameterValue("Stray backslash at end of string");
253 string c_escape(const string &str, bool escape_8bit)
257 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
279 else if(static_cast<unsigned char>(*i)<' ' || (escape_8bit && (*i&0x80)))
281 char buf[4]={'\\', '0'+((*i>>6)&7), '0'+((*i>>3)&7), '0'+(*i&7)};
282 result.append(buf, 4);