26 int compare_no_case(string const & s, string const & s2)
29 string::const_iterator p = s.begin();
30 string::const_iterator p2 = s2.begin();
32 while (p != s.end() && p2 != s2.end()) {
33 int const lc1 = tolower(*p);
34 int const lc2 = tolower(*p2);
36 return (lc1 < lc2) ? -1 : 1;
41 if (s.size() == s2.size())
43 if (s.size() < s2.size())
49 int compare_no_case(string const & s, string const & s2, unsigned int len)
51 //#warning verify this func please
52 string::const_iterator p = s.begin();
53 string::const_iterator p2 = s2.begin();
55 while (i < len && p != s.end() && p2 != s2.end()) {
56 int const lc1 = tolower(*p);
57 int const lc2 = tolower(*p2);
59 return (lc1 < lc2) ? -1 : 1;
64 if (s.size() == s2.size())
66 if (s.size() < s2.size())
72 bool isStrInt(string const & str)
74 if (str.empty()) return false;
76 // Remove leading and trailing white space chars.
77 string tmpstr = frontStrip(strip(str, ' '), ' ');
78 if (tmpstr.empty()) return false;
80 string::const_iterator cit = tmpstr.begin();
81 if ( (*cit) == '-') ++cit;
82 for (; cit != tmpstr.end(); ++cit) {
83 if (!isdigit((*cit))) return false;
89 int strToInt(string const & str)
94 // Remove leading and trailing white space chars.
95 tmpstr = frontStrip(strip(str, ' '), ' ');
96 // Do the conversion proper.
97 return atoi(tmpstr.c_str());
103 string lowercase(string const & a)
107 string::iterator result = tmp.begin();
108 for (string::iterator first = tmp.begin();
109 first != tmp.end(); ++first, ++result) {
110 *result = tolower(*first);
113 transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
119 string uppercase(string const & a)
123 string::iterator result = tmp.begin();
124 for (string::iterator first = tmp.begin();
125 first != tmp.end(); ++first, ++result) {
126 *result = toupper(*first);
129 transform(tmp.begin(), tmp.end(), tmp.begin(), toupper);
138 // "Hey!", you say. "Why do we need the char str[30]?".
139 // Since strstream does not handle memory for us we have to do
140 // that ourselves, if we don't pass str in we have to capture
141 // oss.str() in a tmp variable and delete that manually.
142 // Thus we then require more temporary variables and the code
143 // gets more obfuscated.
145 ostrstream oss(str, 30);
155 string tostr(unsigned long i)
159 ostrstream oss(str, 30);
169 string tostr(void * v)
171 return tostr(long(v));
177 return tostr(long(i));
181 string tostr(unsigned int ui)
183 return tostr(long(ui));
195 return b ? "true" : "false";
199 string tostr(double d)
203 ostrstream oss(tmp, 40);
213 bool prefixIs(string const & a, char const * pre)
215 unsigned int l = strlen(pre);
216 if (l > a.length() || a.empty())
219 return a.compare(0, l, pre, l) == 0;
223 bool suffixIs(string const & a, char c)
225 if (a.empty()) return false;
226 return a[a.length() - 1] == c;
230 bool suffixIs(string const & a, char const * suf)
232 unsigned int suflen = strlen(suf);
233 if (suflen > a.length())
236 return a.compare(a.length() - suflen, suflen, suf) == 0;
241 bool contains(char const * a, string const & b)
243 if (!a || !*a || b.empty()) return false;
244 return strstr(a, b.c_str()) != 0;
248 bool contains(string const & a, char const * b)
252 return a.find(b) != string::npos;
256 bool contains(string const & a, string const & b)
260 return a.find(b) != string::npos;
264 bool contains(char const * a, char const * b)
266 if (!a || !b || !*a || !*b) return false;
267 return strstr(a, b) != 0;
271 unsigned int countChar(string const & a, char const c)
273 #ifdef HAVE_STD_COUNT
274 return count(a.begin(), a.end(), c);
277 count(a.begin(), a.end(), c, n);
283 // ale970405+lasgoutt-970425
284 // rewritten to use new string (Lgb)
285 string token(string const & a, char delim, int n)
287 if (a.empty()) return string();
289 string::size_type k = 0;
290 string::size_type i = 0;
292 // Find delimiter or end of string
294 if ((i = a.find(delim, i)) == string::npos)
298 // i is now the n'th delim (or string::npos)
299 if (i == string::npos) return string();
300 k = a.find(delim, i);
301 // k is now the n'th + 1 delim (or string::npos)
303 return a.substr(i, k - i);
307 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
308 // rewritten to use new string (Lgb)
309 int tokenPos(string const & a, char delim, string const & tok)
315 while (!str.empty()) {
316 str = split(str, tmptok, delim);
325 bool regexMatch(string const & a, string const & pattern)
327 // We massage the pattern a bit so that the usual
328 // shell pattern we all are used to will work.
329 // One nice thing about using a real regex is that
330 // things like "*.*[^~]" will work also.
331 // build the regex string.
332 string regex(pattern);
333 regex = subst(regex, ".", "\\.");
334 regex = subst(regex, "*", ".*");
336 return reg.exact_match(a);
340 string subst(string const & a, char oldchar, char newchar)
343 string::iterator lit = tmp.begin();
344 for(; lit != tmp.end(); ++lit)
345 if ((*lit) == oldchar)
351 string subst(string const & a,
352 char const * oldstr, string const & newstr)
355 string::size_type i = 0;
356 int olen = strlen(oldstr);
357 while((i = lstr.find(oldstr, i)) != string::npos) {
358 lstr.replace(i, olen, newstr);
359 i += newstr.length(); // We need to be sure that we dont
360 // use the same i over and over again.
366 string strip(string const & a, char const c)
368 if (a.empty()) return a;
370 string::size_type i = tmp.find_last_not_of(c);
371 if (i == a.length() - 1) return tmp; // no c's at end of a
372 if (i != string::npos)
373 tmp.erase(i + 1, string::npos);
375 tmp.clear(); // only c in the whole string
380 string frontStrip(string const & a, char const * p)
382 if (a.empty() || !p || !*p) return a;
384 string::size_type i = tmp.find_first_not_of(p);
391 string frontStrip(string const & a, char const c)
393 if (a.empty()) return a;
395 string::size_type i = tmp.find_first_not_of(c);
402 string split(string const & a, string & piece, char delim)
405 string::size_type i = a.find(delim);
406 if (i == a.length() - 1) {
407 piece = a.substr(0, i);
408 } else if (i != string::npos) {
409 piece = a.substr(0, i);
410 tmp = a.substr(i + 1);
413 tmp = a.substr(i + 1);
421 string split(string const & a, char delim)
424 string::size_type i = a.find(delim);
425 if (i != string::npos) // found delim
426 tmp = a.substr(i + 1);
432 string rsplit(string const & a, string & piece, char delim)
435 string::size_type i = a.rfind(delim);
436 if (i != string::npos) { // delimiter was found
437 piece = a.substr(0, i);
438 tmp = a.substr(i + 1);
439 } else { // delimter was not found