3 This file is part of libmspstrings
4 Copyright © 2006-2007 Mikko Rasa
5 Distributed under the LGPL
16 Compares two strings, ignoring case.
18 @param s1 First string
19 @param s2 Second string
21 @return -1 if s1<s2, 0 if s1==s2, 1 if s1>s2
23 int strcasecmp(const string &s1, const string &s2)
25 string::const_iterator i1=s1.begin();
26 string::const_iterator i2=s2.begin();
27 for(; (i1!=s1.end() && i2!=s2.end()); ++i1, ++i2)
29 const char c1=::tolower(*i1);
30 const char c2=::tolower(*i2);
31 if(c1!=c2) return c1-c2;
33 if(i1!=s1.end()) return *i1;
34 if(i2!=s2.end()) return -*i2;
39 Returns a lowercase copy of the given string.
41 string tolower(const string &str)
44 transform(result.begin(), result.end(), result.begin(), ::tolower);
49 Returns an uppercase copy of the given string.
51 string toupper(const string &str)
54 transform(result.begin(), result.end(), result.begin(), ::toupper);
59 Splits a string to parts.
61 @param str String to be split
62 @param sep A set of separator characters
63 @param allow_empty Whether or not to produce empty parts for sequences of
64 more than one separator character
66 vector<string> split(const string &str, const string &sep, bool allow_empty)
68 vector<string> result;
72 start=str.find_first_not_of(sep);
74 while(start<str.size())
76 unsigned end=str.find_first_of(sep, start);
77 result.push_back(str.substr(start, end-start));
86 result.push_back(string());
89 start=str.find_first_not_of(sep, end);
95 vector<string> split(const string &str, char sep, bool allow_empty)
97 return split(str, string(1, sep), allow_empty);
101 Builds a single string from the strings in the given sequence by concatenating
104 @param seq A sequence of strings
105 @param sep Separator to be inserted between strings
108 string join(const T &seq, const string &sep)
111 for(typename T::const_iterator i=seq.begin(); i!=seq.end(); ++i)
120 template string join<list<string> >(const list<string> &, const string &);
121 template string join<vector<string> >(const vector<string> &, const string &);
124 Returns a copy of the given string with leading and trailing whitespace
127 string strip(const string &s)
130 if(!result.erase(0, result.find_first_not_of(" \t\n")).empty())
131 result.erase(result.find_last_not_of(" \t\n")+1);