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)
93 transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
98 string uppercase(string const & a)
101 transform(tmp.begin(), tmp.end(), tmp.begin(), toupper);
108 // should use string stream
110 sprintf(str, "%ld", i);
115 string tostr(unsigned long i)
117 // should use string stream
119 sprintf(str, "%lu", i);
124 string tostr(void * v)
126 return tostr(long(v));
132 return tostr(long(i));
136 string tostr(unsigned int ui)
138 return tostr(long(ui));
150 return b ? "true" : "false";
154 string tostr(double d)
156 // should use string stream
158 sprintf(tmp, "%f", d);
163 bool prefixIs(string const & a, char const * pre)
165 unsigned int l = strlen(pre);
166 if (l > a.length() || a.empty())
169 return a.compare(0, l, pre, l) == 0;
173 bool suffixIs(string const & a, char c)
175 if (a.empty()) return false;
176 return a[a.length() - 1] == c;
180 bool suffixIs(string const & a, char const * suf)
182 unsigned int suflen = strlen(suf);
183 if (suflen > a.length())
186 return a.compare(a.length() - suflen, suflen, suf) == 0;
191 bool contains(char const * a, string const & b)
193 if (!a || !*a || b.empty()) return false;
194 return strstr(a, b.c_str()) != 0;
198 bool contains(string const & a, char const * b)
202 return a.find(b) != string::npos;
206 bool contains(string const & a, string const & b)
210 return a.find(b) != string::npos;
214 bool contains(char const * a, char const * b)
216 if (!a || !b || !*a || !*b) return false;
217 return strstr(a, b) != 0;
221 unsigned int countChar(string const & a, char const c)
223 #ifdef HAVE_STD_COUNT
224 return count(a.begin(), a.end(), c);
227 count(a.begin(), a.end(), c, n);
233 // ale970405+lasgoutt-970425
234 // rewritten to use new string (Lgb)
235 string token(string const & a, char delim, int n)
237 if (a.empty()) return string();
239 string::size_type k = 0;
240 string::size_type i = 0;
242 // Find delimiter or end of string
244 if ((i = a.find(delim, i)) == string::npos)
248 // i is now the n'th delim (or string::npos)
249 if (i == string::npos) return string();
250 k = a.find(delim, i);
251 // k is now the n'th + 1 delim (or string::npos)
253 return a.substr(i, k - i);
257 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
258 // rewritten to use new string (Lgb)
259 int tokenPos(string const & a, char delim, string const & tok)
265 while (!str.empty()) {
266 str = split(str, tmptok, delim);
275 bool regexMatch(string const & a, string const & pattern)
277 // We massage the pattern a bit so that the usual
278 // shell pattern we all are used to will work.
279 // One nice thing about using a real regex is that
280 // things like "*.*[^~]" will work also.
281 // build the regex string.
282 string regex(pattern);
283 regex = subst(regex, ".", "\\.");
284 regex = subst(regex, "*", ".*");
286 return reg.exact_match(a);
290 string subst(string const & a, char oldchar, char newchar)
293 string::iterator lit = tmp.begin();
294 for(; lit != tmp.end(); ++lit)
295 if ((*lit) == oldchar)
301 string subst(string const & a,
302 char const * oldstr, string const & newstr)
305 string::size_type i = 0;
306 int olen = strlen(oldstr);
307 while((i = lstr.find(oldstr, i)) != string::npos) {
308 lstr.replace(i, olen, newstr);
309 i += newstr.length(); // We need to be sure that we dont
310 // use the same i over and over again.
316 string strip(string const & a, char const c)
318 if (a.empty()) return a;
320 string::size_type i = tmp.find_last_not_of(c);
321 if (i == a.length() - 1) return tmp; // no c's at end of a
322 if (i != string::npos)
323 tmp.erase(i + 1, string::npos);
325 tmp.clear(); // only c in the whole string
330 string frontStrip(string const & a, char const * p)
332 if (a.empty() || !p || !*p) return a;
334 string::size_type i = tmp.find_first_not_of(p);
341 string frontStrip(string const & a, char const c)
343 if (a.empty()) return a;
345 string::size_type i = tmp.find_first_not_of(c);
352 string split(string const & a, string & piece, char delim)
355 string::size_type i = a.find(delim);
356 if (i == a.length() - 1) {
357 piece = a.substr(0, i);
358 } else if (i != string::npos) {
359 piece = a.substr(0, i);
360 tmp = a.substr(i + 1);
363 tmp = a.substr(i + 1);
371 string split(string const & a, char delim)
374 string::size_type i = a.find(delim);
375 if (i != string::npos) // found delim
376 tmp = a.substr(i + 1);
382 string rsplit(string const & a, string & piece, char delim)
385 string::size_type i = a.rfind(delim);
386 if (i != string::npos) { // delimiter was found
387 piece = a.substr(0, i);
388 tmp = a.substr(i + 1);
389 } else { // delimter was not found