17 using std::ostringstream;
28 int compare_no_case(string const & s, string const & s2)
31 string::const_iterator p = s.begin();
32 string::const_iterator p2 = s2.begin();
34 while (p != s.end() && p2 != s2.end()) {
35 int const lc1 = tolower(*p);
36 int const lc2 = tolower(*p2);
38 return (lc1 < lc2) ? -1 : 1;
43 if (s.size() == s2.size())
45 if (s.size() < s2.size())
51 int compare_no_case(string const & s, string const & s2, unsigned int len)
53 //#warning verify this func please
54 string::const_iterator p = s.begin();
55 string::const_iterator p2 = s2.begin();
57 while (i < len && p != s.end() && p2 != s2.end()) {
58 int const lc1 = tolower(*p);
59 int const lc2 = tolower(*p2);
61 return (lc1 < lc2) ? -1 : 1;
66 if (s.size() == s2.size())
68 if (s.size() < s2.size())
74 bool isStrInt(string const & str)
76 if (str.empty()) return false;
78 // Remove leading and trailing white space chars.
79 string tmpstr = frontStrip(strip(str, ' '), ' ');
80 if (tmpstr.empty()) return false;
82 string::const_iterator cit = tmpstr.begin();
83 if ( (*cit) == '-') ++cit;
84 for (; cit != tmpstr.end(); ++cit) {
85 if (!isdigit((*cit))) return false;
91 int strToInt(string const & str)
96 // Remove leading and trailing white space chars.
97 tmpstr = frontStrip(strip(str, ' '), ' ');
98 // Do the conversion proper.
99 return atoi(tmpstr.c_str());
105 string lowercase(string const & a)
109 string::iterator result = tmp.begin();
110 for (string::iterator first = tmp.begin();
111 first != tmp.end(); ++first, ++result) {
112 *result = tolower(*first);
115 transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
121 string uppercase(string const & a)
125 string::iterator result = tmp.begin();
126 for (string::iterator first = tmp.begin();
127 first != tmp.end(); ++first, ++result) {
128 *result = toupper(*first);
131 transform(tmp.begin(), tmp.end(), tmp.begin(), toupper);
140 // "Hey!", you say. "Why do we need the char str[30]?".
141 // Since strstream does not handle memory for us we have to do
142 // that ourselves, if we don't pass str in we have to capture
143 // oss.str() in a tmp variable and delete that manually.
144 // Thus we then require more temporary variables and the code
145 // gets more obfuscated.
147 ostrstream oss(str, 30);
153 return oss.str().c_str();
158 string tostr(unsigned long i)
162 ostrstream oss(str, 30);
168 return oss.str().c_str();
173 string tostr(void * v)
175 return tostr(long(v));
181 return tostr(long(i));
185 string tostr(unsigned int ui)
187 return tostr(long(ui));
199 return b ? "true" : "false";
203 string tostr(double d)
207 ostrstream oss(tmp, 40);
213 return oss.str().c_str();
218 bool prefixIs(string const & a, char const * pre)
220 unsigned int l = strlen(pre);
221 if (l > a.length() || a.empty())
224 return a.compare(0, l, pre, l) == 0;
228 bool suffixIs(string const & a, char c)
230 if (a.empty()) return false;
231 return a[a.length() - 1] == c;
235 bool suffixIs(string const & a, char const * suf)
237 unsigned int suflen = strlen(suf);
238 if (suflen > a.length())
241 return a.compare(a.length() - suflen, suflen, suf) == 0;
246 bool contains(char const * a, string const & b)
248 if (!a || !*a || b.empty()) return false;
249 return strstr(a, b.c_str()) != 0;
253 bool contains(string const & a, char const * b)
257 return a.find(b) != string::npos;
261 bool contains(string const & a, string const & b)
265 return a.find(b) != string::npos;
269 bool contains(char const * a, char const * b)
271 if (!a || !b || !*a || !*b) return false;
272 return strstr(a, b) != 0;
276 unsigned int countChar(string const & a, char const c)
278 #ifdef HAVE_STD_COUNT
279 return count(a.begin(), a.end(), c);
282 count(a.begin(), a.end(), c, n);
288 // ale970405+lasgoutt-970425
289 // rewritten to use new string (Lgb)
290 string token(string const & a, char delim, int n)
292 if (a.empty()) return string();
294 string::size_type k = 0;
295 string::size_type i = 0;
297 // Find delimiter or end of string
299 if ((i = a.find(delim, i)) == string::npos)
303 // i is now the n'th delim (or string::npos)
304 if (i == string::npos) return string();
305 k = a.find(delim, i);
306 // k is now the n'th + 1 delim (or string::npos)
308 return a.substr(i, k - i);
312 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
313 // rewritten to use new string (Lgb)
314 int tokenPos(string const & a, char delim, string const & tok)
320 while (!str.empty()) {
321 str = split(str, tmptok, delim);
330 bool regexMatch(string const & a, string const & pattern)
332 // We massage the pattern a bit so that the usual
333 // shell pattern we all are used to will work.
334 // One nice thing about using a real regex is that
335 // things like "*.*[^~]" will work also.
336 // build the regex string.
337 string regex(pattern);
338 regex = subst(regex, ".", "\\.");
339 regex = subst(regex, "*", ".*");
341 return reg.exact_match(a);
345 string subst(string const & a, char oldchar, char newchar)
348 string::iterator lit = tmp.begin();
349 for(; lit != tmp.end(); ++lit)
350 if ((*lit) == oldchar)
356 string subst(string const & a,
357 char const * oldstr, string const & newstr)
360 string::size_type i = 0;
361 int olen = strlen(oldstr);
362 while((i = lstr.find(oldstr, i)) != string::npos) {
363 lstr.replace(i, olen, newstr);
364 i += newstr.length(); // We need to be sure that we dont
365 // use the same i over and over again.
371 string strip(string const & a, char const c)
373 if (a.empty()) return a;
375 string::size_type i = tmp.find_last_not_of(c);
376 if (i == a.length() - 1) return tmp; // no c's at end of a
377 if (i != string::npos)
378 tmp.erase(i + 1, string::npos);
380 tmp.clear(); // only c in the whole string
385 string frontStrip(string const & a, char const * p)
387 if (a.empty() || !p || !*p) return a;
389 string::size_type i = tmp.find_first_not_of(p);
396 string frontStrip(string const & a, char const c)
398 if (a.empty()) return a;
400 string::size_type i = tmp.find_first_not_of(c);
407 string split(string const & a, string & piece, char delim)
410 string::size_type i = a.find(delim);
411 if (i == a.length() - 1) {
412 piece = a.substr(0, i);
413 } else if (i != string::npos) {
414 piece = a.substr(0, i);
415 tmp = a.substr(i + 1);
418 tmp = a.substr(i + 1);
426 string split(string const & a, char delim)
429 string::size_type i = a.find(delim);
430 if (i != string::npos) // found delim
431 tmp = a.substr(i + 1);
437 string rsplit(string const & a, string & piece, char delim)
440 string::size_type i = a.rfind(delim);
441 if (i != string::npos) { // delimiter was found
442 piece = a.substr(0, i);
443 tmp = a.substr(i + 1);
444 } else { // delimter was not found