22 int compare_no_case(string const & s, string const & s2)
25 string::const_iterator p = s.begin();
26 string::const_iterator p2 = s2.begin();
28 while (p != s.end() && p2 != s2.end()) {
29 int const lc1 = tolower(*p);
30 int const lc2 = tolower(*p2);
32 return (lc1 < lc2) ? -1 : 1;
37 if (s.size() == s2.size())
39 if (s.size() < s2.size())
45 int compare_no_case(string const & s, string const & s2, unsigned int len)
47 //#warning verify this func please
48 string::const_iterator p = s.begin();
49 string::const_iterator p2 = s2.begin();
51 while (i < len && p != s.end() && p2 != s2.end()) {
52 int const lc1 = tolower(*p);
53 int const lc2 = tolower(*p2);
55 return (lc1 < lc2) ? -1 : 1;
60 if (s.size() == s2.size())
62 if (s.size() < s2.size())
68 bool isStrInt(string const & str)
70 if (str.empty()) return false;
72 // Remove leading and trailing white space chars.
73 string tmpstr = frontStrip(strip(str, ' '), ' ');
74 if (tmpstr.empty()) return false;
76 string::const_iterator cit = tmpstr.begin();
77 if ( (*cit) == '-') ++cit;
78 for (; cit != tmpstr.end(); ++cit) {
79 if (!isdigit((*cit))) return false;
85 int strToInt(string const & str)
90 // Remove leading and trailing white space chars.
91 tmpstr = frontStrip(strip(str, ' '), ' ');
92 // Do the conversion proper.
93 return atoi(tmpstr.c_str());
99 string lowercase(string const & a)
103 string::iterator result = tmp.begin();
104 for (string::iterator first = tmp.begin();
105 first != tmp.end(); ++first, ++result) {
106 *result = tolower(*first);
109 // transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
115 string uppercase(string const & a)
119 string::iterator result = tmp.begin();
120 for (string::iterator first = tmp.begin();
121 first != tmp.end(); ++first, ++result) {
122 *result = toupper(*first);
125 // transform(tmp.begin(), tmp.end(), tmp.begin(), toupper);
131 bool prefixIs(string const & a, char const * pre)
133 unsigned int l = strlen(pre);
134 if (l > a.length() || a.empty())
137 return a.compare(0, l, pre, l) == 0;
141 bool suffixIs(string const & a, char c)
143 if (a.empty()) return false;
144 return a[a.length() - 1] == c;
148 bool suffixIs(string const & a, char const * suf)
150 unsigned int suflen = strlen(suf);
151 if (suflen > a.length())
154 return a.compare(a.length() - suflen, suflen, suf) == 0;
159 bool contains(char const * a, string const & b)
161 if (!a || !*a || b.empty()) return false;
162 return strstr(a, b.c_str()) != 0;
166 bool contains(string const & a, char const * b)
170 return a.find(b) != string::npos;
174 bool contains(string const & a, string const & b)
178 return a.find(b) != string::npos;
182 bool contains(char const * a, char const * b)
184 if (!a || !b || !*a || !*b) return false;
185 return strstr(a, b) != 0;
189 unsigned int countChar(string const & a, char const c)
191 #ifdef HAVE_STD_COUNT
192 return count(a.begin(), a.end(), c);
195 count(a.begin(), a.end(), c, n);
201 // ale970405+lasgoutt-970425
202 // rewritten to use new string (Lgb)
203 string token(string const & a, char delim, int n)
205 if (a.empty()) return string();
207 string::size_type k = 0;
208 string::size_type i = 0;
210 // Find delimiter or end of string
212 if ((i = a.find(delim, i)) == string::npos)
216 // i is now the n'th delim (or string::npos)
217 if (i == string::npos) return string();
218 k = a.find(delim, i);
219 // k is now the n'th + 1 delim (or string::npos)
221 return a.substr(i, k - i);
225 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
226 // rewritten to use new string (Lgb)
227 int tokenPos(string const & a, char delim, string const & tok)
233 while (!str.empty()) {
234 str = split(str, tmptok, delim);
243 bool regexMatch(string const & a, string const & pattern)
245 // We massage the pattern a bit so that the usual
246 // shell pattern we all are used to will work.
247 // One nice thing about using a real regex is that
248 // things like "*.*[^~]" will work also.
249 // build the regex string.
250 string regex(pattern);
251 regex = subst(regex, ".", "\\.");
252 regex = subst(regex, "*", ".*");
254 return reg.exact_match(a);
258 string subst(string const & a, char oldchar, char newchar)
261 string::iterator lit = tmp.begin();
262 for(; lit != tmp.end(); ++lit)
263 if ((*lit) == oldchar)
269 string subst(string const & a,
270 char const * oldstr, string const & newstr)
273 string::size_type i = 0;
274 int olen = strlen(oldstr);
275 while((i = lstr.find(oldstr, i)) != string::npos) {
276 lstr.replace(i, olen, newstr);
277 i += newstr.length(); // We need to be sure that we dont
278 // use the same i over and over again.
284 string strip(string const & a, char const c)
286 if (a.empty()) return a;
288 string::size_type i = tmp.find_last_not_of(c);
289 if (i == a.length() - 1) return tmp; // no c's at end of a
290 if (i != string::npos)
291 tmp.erase(i + 1, string::npos);
293 tmp.clear(); // only c in the whole string
298 string frontStrip(string const & a, char const * p)
300 if (a.empty() || !p || !*p) return a;
302 string::size_type i = tmp.find_first_not_of(p);
309 string frontStrip(string const & a, char const c)
311 if (a.empty()) return a;
313 string::size_type i = tmp.find_first_not_of(c);
320 string split(string const & a, string & piece, char delim)
323 string::size_type i = a.find(delim);
324 if (i == a.length() - 1) {
325 piece = a.substr(0, i);
326 } else if (i != string::npos) {
327 piece = a.substr(0, i);
328 tmp = a.substr(i + 1);
331 tmp = a.substr(i + 1);
339 string split(string const & a, char delim)
342 string::size_type i = a.find(delim);
343 if (i != string::npos) // found delim
344 tmp = a.substr(i + 1);
350 string rsplit(string const & a, string & piece, char delim)
353 string::size_type i = a.rfind(delim);
354 if (i != string::npos) { // delimiter was found
355 piece = a.substr(0, i);
356 tmp = a.substr(i + 1);
357 } else { // delimter was not found