15 int compare_no_case(string const & s, string const & s2)
18 string::const_iterator p = s.begin();
19 string::const_iterator p2 = s2.begin();
21 while (p != s.end() && p2 != s2.end()) {
22 int const lc1 = tolower(*p);
23 int const lc2 = tolower(*p2);
25 return (lc1 < lc2) ? -1 : 1;
30 if (s.size() == s2.size())
32 if (s.size() < s2.size())
37 int compare_no_case(string const & s, string const & s2, unsigned int len)
39 //#warning verify this func please
40 string::const_iterator p = s.begin();
41 string::const_iterator p2 = s2.begin();
43 while (i < len && p != s.end() && p2 != s2.end()) {
44 int const lc1 = tolower(*p);
45 int const lc2 = tolower(*p2);
47 return (lc1 < lc2) ? -1 : 1;
52 if (s.size() == s2.size())
54 if (s.size() < s2.size())
59 bool isStrInt(string const & str)
61 if (str.empty()) return false;
63 // Remove leading and trailing white space chars.
64 string tmpstr = frontStrip(strip(str, ' '), ' ');
65 if (tmpstr.empty()) return false;
67 string::const_iterator cit = tmpstr.begin();
68 if ( (*cit) == '-') ++cit;
69 for (; cit != tmpstr.end(); ++cit) {
70 if (!isdigit((*cit))) return false;
76 int strToInt(string const & str)
81 // Remove leading and trailing white space chars.
82 tmpstr = frontStrip(strip(str, ' '), ' ');
83 // Do the conversion proper.
84 return atoi(tmpstr.c_str());
90 string lowercase(string const & a)
94 string::const_iterator cit = a.begin();
95 for(; cit != a.end(); ++cit) {
96 tmp += static_cast<char>(tolower(*cit));
101 transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
106 string uppercase(string const & a)
109 transform(tmp.begin(), tmp.end(), tmp.begin(), toupper);
116 // should use string stream
118 sprintf(str, "%ld", i);
123 string tostr(unsigned long i)
125 // should use string stream
127 sprintf(str, "%lu", i);
132 string tostr(void * v)
134 return tostr(long(v));
140 return tostr(long(i));
144 string tostr(unsigned int ui)
146 return tostr(long(ui));
158 return b ? "true" : "false";
163 string tostr(float f)
165 return tostr(double(f));
170 string tostr(double d)
172 // should use string stream
174 sprintf(tmp, "%f", d);
179 bool prefixIs(string const & a, char const * pre)
181 unsigned int l = strlen(pre);
182 if (l > a.length() || a.empty())
185 return a.compare(0, l, pre, l) == 0;
189 bool suffixIs(string const & a, char c)
191 if (a.empty()) return false;
192 return a[a.length() - 1] == c;
196 bool suffixIs(string const & a, char const * suf)
198 unsigned int suflen = strlen(suf);
199 if (suflen > a.length())
202 return a.compare(a.length() - suflen, suflen, suf) == 0;
207 bool contains(char const * a, string const & b)
209 if (!a || !*a || b.empty()) return false;
210 return strstr(a, b.c_str()) != 0;
214 bool contains(string const & a, char const * b)
218 return a.find(b) != string::npos;
222 bool contains(string const & a, string const & b)
226 return a.find(b) != string::npos;
230 bool contains(char const * a, char const * b)
232 if (!a || !b || !*a || !*b) return false;
233 return strstr(a, b) != 0;
237 int countChar(string const & a, char const c)
239 return count(a.begin(), a.end(), c);
243 // ale970405+lasgoutt-970425
244 // rewritten to use new string (Lgb)
245 string token(string const & a, char delim, int n)
247 if (a.empty()) return string();
249 string::size_type k = 0;
250 string::size_type i = 0;
252 // Find delimiter or end of string
254 if ((i = a.find(delim, i)) == string::npos)
258 // i is now the n'th delim (or string::npos)
259 if (i == string::npos) return string();
260 k = a.find(delim, i);
261 // k is now the n'th + 1 delim (or string::npos)
263 return a.substr(i, k - i);
267 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
268 // rewritten to use new string (Lgb)
269 int tokenPos(string const & a, char delim, string const & tok)
275 while (!str.empty()) {
276 str = split(str, tmptok, delim);
285 bool regexMatch(string const & a, string const & pattern)
287 // We massage the pattern a bit so that the usual
288 // shell pattern we all are used to will work.
289 // One nice thing about using a real regex is that
290 // things like "*.*[^~]" will work also.
291 // build the regex string.
292 string regex(pattern);
293 regex = subst(regex, ".", "\\.");
294 regex = subst(regex, "*", ".*");
296 return reg.exact_match(a);
300 string subst(string const & a, char oldchar, char newchar)
303 string::iterator lit = tmp.begin();
304 for(; lit != tmp.end(); ++lit)
305 if ((*lit) == oldchar)
311 string subst(string const & a,
312 char const * oldstr, string const & newstr)
315 string::size_type i = 0;
316 int olen = strlen(oldstr);
317 while((i = lstr.find(oldstr, i)) != string::npos) {
318 lstr.replace(i, olen, newstr);
319 i += newstr.length(); // We need to be sure that we dont
320 // use the same i over and over again.
326 string strip(string const & a, char const c)
328 if (a.empty()) return a;
330 string::size_type i = tmp.find_last_not_of(c);
331 if (i == a.length() - 1) return tmp; // no c's at end of a
332 if (i != string::npos)
333 tmp.erase(i + 1, string::npos);
335 tmp.clear(); // only c in the whole string
340 string frontStrip(string const & a, char const * p)
342 if (a.empty() || !p || !*p) return a;
344 string::size_type i = tmp.find_first_not_of(p);
351 string frontStrip(string const & a, char const c)
353 if (a.empty()) return a;
355 string::size_type i = tmp.find_first_not_of(c);
362 string split(string const & a, string & piece, char delim)
365 string::size_type i = a.find(delim);
366 if (i == a.length() - 1) {
367 piece = a.substr(0, i);
368 } else if (i != string::npos) {
369 piece = a.substr(0, i);
370 tmp = a.substr(i + 1);
373 tmp = a.substr(i + 1);
381 string split(string const & a, char delim)
384 string::size_type i = a.find(delim);
385 if (i != string::npos) // found delim
386 tmp = a.substr(i + 1);
392 string rsplit(string const & a, string & piece, char delim)
395 string::size_type i = a.rfind(delim);
396 if (i != string::npos) { // delimiter was found
397 piece = a.substr(0, i);
398 tmp = a.substr(i + 1);
399 } else { // delimter was not found