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());
98 string lowercase(string const & a)
102 string::iterator result = tmp.begin();
103 for (string::iterator first = tmp.begin();
104 first != tmp.end(); ++first, ++result) {
105 *result = tolower(*first);
108 // transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
114 string uppercase(string const & a)
118 string::iterator result = tmp.begin();
119 for (string::iterator first = tmp.begin();
120 first != tmp.end(); ++first, ++result) {
121 *result = toupper(*first);
124 // transform(tmp.begin(), tmp.end(), tmp.begin(), toupper);
130 bool prefixIs(string const & a, char const * pre)
132 unsigned int l = strlen(pre);
133 if (l > a.length() || a.empty())
136 return a.compare(0, l, pre, l) == 0;
140 bool suffixIs(string const & a, char c)
142 if (a.empty()) return false;
143 return a[a.length() - 1] == c;
147 bool suffixIs(string const & a, char const * suf)
149 unsigned int suflen = strlen(suf);
150 if (suflen > a.length())
153 return a.compare(a.length() - suflen, suflen, suf) == 0;
158 bool contains(char const * a, string const & b)
160 if (!a || !*a || b.empty()) return false;
161 return strstr(a, b.c_str()) != 0;
165 bool contains(string const & a, char const * b)
169 return a.find(b) != string::npos;
173 bool contains(string const & a, string const & b)
177 return a.find(b) != string::npos;
181 bool contains(char const * a, char const * b)
183 if (!a || !b || !*a || !*b) return false;
184 return strstr(a, b) != 0;
188 unsigned int countChar(string const & a, char const c)
190 #ifdef HAVE_STD_COUNT
191 return count(a.begin(), a.end(), c);
194 count(a.begin(), a.end(), c, n);
200 // ale970405+lasgoutt-970425
201 // rewritten to use new string (Lgb)
202 string token(string const & a, char delim, int n)
204 if (a.empty()) return string();
206 string::size_type k = 0;
207 string::size_type i = 0;
209 // Find delimiter or end of string
211 if ((i = a.find(delim, i)) == string::npos)
215 // i is now the n'th delim (or string::npos)
216 if (i == string::npos) return string();
217 k = a.find(delim, i);
218 // k is now the n'th + 1 delim (or string::npos)
220 return a.substr(i, k - i);
224 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
225 // rewritten to use new string (Lgb)
226 int tokenPos(string const & a, char delim, string const & tok)
232 while (!str.empty()) {
233 str = split(str, tmptok, delim);
242 bool regexMatch(string const & a, string const & pattern)
244 // We massage the pattern a bit so that the usual
245 // shell pattern we all are used to will work.
246 // One nice thing about using a real regex is that
247 // things like "*.*[^~]" will work also.
248 // build the regex string.
249 string regex(pattern);
250 regex = subst(regex, ".", "\\.");
251 regex = subst(regex, "*", ".*");
253 return reg.exact_match(a);
257 string subst(string const & a, char oldchar, char newchar)
260 string::iterator lit = tmp.begin();
261 for(; lit != tmp.end(); ++lit)
262 if ((*lit) == oldchar)
268 string subst(string const & a,
269 char const * oldstr, string const & newstr)
272 string::size_type i = 0;
273 int olen = strlen(oldstr);
274 while((i = lstr.find(oldstr, i)) != string::npos) {
275 lstr.replace(i, olen, newstr);
276 i += newstr.length(); // We need to be sure that we dont
277 // use the same i over and over again.
283 string strip(string const & a, char const c)
285 if (a.empty()) return a;
287 string::size_type i = tmp.find_last_not_of(c);
288 if (i == a.length() - 1) return tmp; // no c's at end of a
289 if (i != string::npos)
290 tmp.erase(i + 1, string::npos);
292 tmp.clear(); // only c in the whole string
297 string frontStrip(string const & a, char const * p)
299 if (a.empty() || !p || !*p) return a;
301 string::size_type i = tmp.find_first_not_of(p);
308 string frontStrip(string const & a, char const c)
310 if (a.empty()) return a;
312 string::size_type i = tmp.find_first_not_of(c);
319 string split(string const & a, string & piece, char delim)
322 string::size_type i = a.find(delim);
323 if (i == a.length() - 1) {
324 piece = a.substr(0, i);
325 } else if (i != string::npos) {
326 piece = a.substr(0, i);
327 tmp = a.substr(i + 1);
330 tmp = a.substr(i + 1);
338 string split(string const & a, char delim)
341 string::size_type i = a.find(delim);
342 if (i != string::npos) // found delim
343 tmp = a.substr(i + 1);
349 string rsplit(string const & a, string & piece, char delim)
352 string::size_type i = a.rfind(delim);
353 if (i != string::npos) { // delimiter was found
354 piece = a.substr(0, i);
355 tmp = a.substr(i + 1);
356 } else { // delimter was not found