3 This file is part of libmspstrings
4 Copyright © 2006-2007 Mikko Rasa
5 Distributed under the LGPL
15 template<bool long_sep, bool allow_empty>
16 vector<string> do_split(const string &str, const string &sep, int max_split)
18 vector<string> result;
21 while(start<str.size())
23 unsigned end=long_sep ? str.find(sep, start) : str.find_first_of(sep, start);
24 if(end!=start || allow_empty)
26 if(max_split>=0 && result.size()==static_cast<unsigned>(max_split))
28 result.push_back(str.substr(start));
32 result.push_back(str.substr(start, end-start));
38 start=end+(long_sep ? sep.size() : 1);
40 if(allow_empty && start==str.size())
41 result.push_back(string());
52 Compares two strings, ignoring case.
54 @param s1 First string
55 @param s2 Second string
57 @return -1 if s1<s2, 0 if s1==s2, 1 if s1>s2
59 int strcasecmp(const string &s1, const string &s2)
61 string::const_iterator i1=s1.begin();
62 string::const_iterator i2=s2.begin();
63 for(; (i1!=s1.end() && i2!=s2.end()); ++i1, ++i2)
65 const char c1=::tolower(*i1);
66 const char c2=::tolower(*i2);
67 if(c1!=c2) return c1-c2;
69 if(i1!=s1.end()) return *i1;
70 if(i2!=s2.end()) return -*i2;
75 Returns a lowercase copy of the given string.
77 string tolower(const string &str)
80 transform(result.begin(), result.end(), result.begin(), ::tolower);
85 Returns an uppercase copy of the given string.
87 string toupper(const string &str)
90 transform(result.begin(), result.end(), result.begin(), ::toupper);
94 vector<string> split_fields(const string &str, const string &sep, int max_split)
96 return do_split<true, true>(str, sep, max_split);
99 vector<string> split_fields(const string &str, char sep, int max_split)
101 return split_fields(str, string(1, sep), max_split);
104 vector<string> split_long(const string &str, const string &sep, int max_split)
106 return do_split<true, false>(str, sep, max_split);
109 vector<string> split(const string &str, const string &sep, int max_split)
111 return do_split<false, false>(str, sep, max_split);
114 vector<string> split(const string &str, char sep, int max_split)
116 return split(str, string(1, sep), max_split);
120 Splits a string to parts.
122 @param str String to be split
123 @param sep A set of separator characters
124 @param allow_empty Whether or not to produce empty parts for sequences of
125 more than one separator character
127 vector<string> split(const string &str, const string &sep, bool allow_empty)
129 vector<string> result;
133 start=str.find_first_not_of(sep);
135 while(start<str.size())
137 unsigned end=str.find_first_of(sep, start);
138 result.push_back(str.substr(start, end-start));
140 if(end==string::npos)
146 if(start==str.size())
147 result.push_back(string());
150 start=str.find_first_not_of(sep, end);
156 vector<string> split(const string &str, char sep, bool allow_empty)
158 return split(str, string(1, sep), allow_empty);
162 Builds a single string from the strings in the given sequence by concatenating
165 @param seq A sequence of strings
166 @param sep Separator to be inserted between strings
169 string join(const T &seq, const string &sep)
172 for(typename T::const_iterator i=seq.begin(); i!=seq.end(); ++i)
181 template string join<list<string> >(const list<string> &, const string &);
182 template string join<vector<string> >(const vector<string> &, const string &);
185 Returns a copy of the given string with leading and trailing whitespace
188 string strip(const string &s)
191 if(!result.erase(0, result.find_first_not_of(" \t\r\n")).empty())
192 result.erase(result.find_last_not_of(" \t\r\n")+1);