15 // The new glibstdc++-v3 has not worked out all the quirks regarding cctype
16 // yet. So currently it failes if the to using lines below are stated.
21 int compare_no_case(string const & s, string const & s2)
24 string::const_iterator p = s.begin();
25 string::const_iterator p2 = s2.begin();
27 while (p != s.end() && p2 != s2.end()) {
28 int const lc1 = tolower(*p);
29 int const lc2 = tolower(*p2);
31 return (lc1 < lc2) ? -1 : 1;
36 if (s.size() == s2.size())
38 if (s.size() < s2.size())
44 int compare_no_case(string const & s, string const & s2, unsigned int len)
46 //#warning verify this func please
47 string::const_iterator p = s.begin();
48 string::const_iterator p2 = s2.begin();
50 while (i < len && p != s.end() && p2 != s2.end()) {
51 int const lc1 = tolower(*p);
52 int const lc2 = tolower(*p2);
54 return (lc1 < lc2) ? -1 : 1;
59 if (s.size() == s2.size())
61 if (s.size() < s2.size())
67 bool isStrInt(string const & str)
69 if (str.empty()) return false;
71 // Remove leading and trailing white space chars.
72 string tmpstr = frontStrip(strip(str, ' '), ' ');
73 if (tmpstr.empty()) return false;
75 string::const_iterator cit = tmpstr.begin();
76 if ( (*cit) == '-') ++cit;
77 for (; cit != tmpstr.end(); ++cit) {
78 if (!isdigit((*cit))) return false;
84 int strToInt(string const & str)
89 // Remove leading and trailing white space chars.
90 tmpstr = frontStrip(strip(str, ' '), ' ');
91 // Do the conversion proper.
92 return atoi(tmpstr.c_str());
101 bool isStrDbl(string const & str)
103 if (str.empty()) return false;
105 // Remove leading and trailing white space chars.
106 string tmpstr = frontStrip(strip(str, ' '), ' ');
107 if (tmpstr.empty()) return false;
108 // if (1 < tmpstr.count('.')) return false;
110 string::const_iterator cit = tmpstr.begin();
111 bool found_dot(false);
112 if ( (*cit) == '-') ++cit;
113 for (; cit != tmpstr.end(); ++cit) {
130 double strToDbl(string const & str)
135 // Remove leading and trailing white space chars.
136 tmpstr = frontStrip(strip(str, ' '), ' ');
137 // Do the conversion proper.
138 return atof(tmpstr.c_str());
146 string lowercase(string const & a)
150 string::iterator result = tmp.begin();
151 for (string::iterator first = tmp.begin();
152 first != tmp.end(); ++first, ++result) {
153 *result = tolower(*first);
156 // transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
162 string uppercase(string const & a)
166 string::iterator result = tmp.begin();
167 for (string::iterator first = tmp.begin();
168 first != tmp.end(); ++first, ++result) {
169 *result = toupper(*first);
172 // transform(tmp.begin(), tmp.end(), tmp.begin(), toupper);
178 bool prefixIs(string const & a, char const * pre)
180 unsigned int l = strlen(pre);
181 if (l > a.length() || a.empty())
184 return a.compare(0, l, pre, l) == 0;
188 bool suffixIs(string const & a, char c)
190 if (a.empty()) return false;
191 return a[a.length() - 1] == c;
195 bool suffixIs(string const & a, char const * suf)
197 unsigned int suflen = strlen(suf);
198 if (suflen > a.length())
201 return a.compare(a.length() - suflen, suflen, suf) == 0;
206 bool contains(char const * a, string const & b)
208 if (!a || !*a || b.empty()) return false;
209 return strstr(a, b.c_str()) != 0;
213 bool contains(string const & a, char const * b)
217 return a.find(b) != string::npos;
221 bool contains(string const & a, string const & b)
225 return a.find(b) != string::npos;
229 bool contains(char const * a, char const * b)
231 if (!a || !b || !*a || !*b) return false;
232 return strstr(a, b) != 0;
236 bool containsOnly(string const & s, char const * cset)
238 return s.find_first_not_of(cset) == string::npos;
242 bool containsOnly(string const & s, string const & cset)
244 return s.find_first_not_of(cset) == string::npos;
248 bool containsOnly(char const * s, char const * cset)
250 return string(s).find_first_not_of(cset) == string::npos;
254 bool containsOnly(char const * s, string const & cset)
256 return string(s).find_first_not_of(cset) == string::npos;
260 unsigned int countChar(string const & a, char const c)
262 #ifdef HAVE_STD_COUNT
263 return count(a.begin(), a.end(), c);
266 count(a.begin(), a.end(), c, n);
272 // ale970405+lasgoutt-970425
273 // rewritten to use new string (Lgb)
274 string token(string const & a, char delim, int n)
276 if (a.empty()) return string();
278 string::size_type k = 0;
279 string::size_type i = 0;
281 // Find delimiter or end of string
283 if ((i = a.find(delim, i)) == string::npos)
287 // i is now the n'th delim (or string::npos)
288 if (i == string::npos) return string();
289 k = a.find(delim, i);
290 // k is now the n'th + 1 delim (or string::npos)
292 return a.substr(i, k - i);
296 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
297 // rewritten to use new string (Lgb)
298 int tokenPos(string const & a, char delim, string const & tok)
304 while (!str.empty()) {
305 str = split(str, tmptok, delim);
314 bool regexMatch(string const & a, string const & pattern)
316 // We massage the pattern a bit so that the usual
317 // shell pattern we all are used to will work.
318 // One nice thing about using a real regex is that
319 // things like "*.*[^~]" will work also.
320 // build the regex string.
321 string regex(pattern);
322 regex = subst(regex, ".", "\\.");
323 regex = subst(regex, "*", ".*");
325 return reg.exact_match(a);
329 string subst(string const & a, char oldchar, char newchar)
332 string::iterator lit = tmp.begin();
333 for(; lit != tmp.end(); ++lit)
334 if ((*lit) == oldchar)
340 string subst(string const & a,
341 char const * oldstr, string const & newstr)
344 string::size_type i = 0;
345 int olen = strlen(oldstr);
346 while((i = lstr.find(oldstr, i)) != string::npos) {
347 lstr.replace(i, olen, newstr);
348 i += newstr.length(); // We need to be sure that we dont
349 // use the same i over and over again.
355 string strip(string const & a, char const c)
357 if (a.empty()) return a;
359 string::size_type i = tmp.find_last_not_of(c);
360 if (i == a.length() - 1) return tmp; // no c's at end of a
361 if (i != string::npos)
362 tmp.erase(i + 1, string::npos);
364 tmp.erase(); // only c in the whole string
369 string frontStrip(string const & a, char const * p)
371 if (a.empty() || !p || !*p) return a;
373 string::size_type i = tmp.find_first_not_of(p);
380 string frontStrip(string const & a, char const c)
382 if (a.empty()) return a;
384 string::size_type i = tmp.find_first_not_of(c);
391 string split(string const & a, string & piece, char delim)
394 string::size_type i = a.find(delim);
395 if (i == a.length() - 1) {
396 piece = a.substr(0, i);
397 } else if (i != string::npos) {
398 piece = a.substr(0, i);
399 tmp = a.substr(i + 1);
402 tmp = a.substr(i + 1);
410 string split(string const & a, char delim)
413 string::size_type i = a.find(delim);
414 if (i != string::npos) // found delim
415 tmp = a.substr(i + 1);
421 string rsplit(string const & a, string & piece, char delim)
424 string::size_type i = a.rfind(delim);
425 if (i != string::npos) { // delimiter was found
426 piece = a.substr(0, i);
427 tmp = a.substr(i + 1);
428 } else { // delimter was not found