15 bool isStrInt(string const & str)
17 if (str.empty()) return false;
19 // Remove leading and trailing white space chars.
20 string tmpstr = frontStrip(strip(str, ' '), ' ');
21 if (tmpstr.empty()) return false;
23 string::const_iterator cit = tmpstr.begin();
24 if ( (*cit) == '-') ++cit;
25 for (; cit != tmpstr.end(); ++cit) {
26 if (!isdigit((*cit))) return false;
32 int strToInt(string const & str)
37 // Remove leading and trailing white space chars.
38 tmpstr = frontStrip(strip(str, ' '), ' ');
39 // Do the conversion proper.
40 return atoi(tmpstr.c_str());
46 string lowercase(string const & a)
50 string::const_iterator cit = a.begin();
51 for(; cit != a.end(); ++cit) {
52 tmp += static_cast<char>(tolower(*cit));
57 transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
62 string uppercase(string const & a)
65 transform(tmp.begin(), tmp.end(), tmp.begin(), toupper);
72 // should use string stream
74 sprintf(str, "%ld", i);
79 string tostr(unsigned long i)
81 // should use string stream
83 sprintf(str, "%lu", i);
88 string tostr(void * v)
90 return tostr(long(v));
96 return tostr(long(i));
100 string tostr(unsigned int ui)
102 return tostr(long(ui));
114 return b ? "true" : "false";
119 string tostr(float f)
121 return tostr(double(f));
126 string tostr(double d)
128 // should use string stream
130 sprintf(tmp, "%f", d);
135 bool prefixIs(string const & a, char const * pre)
137 unsigned int l = strlen(pre);
138 if (l > a.length() || a.empty())
141 return a.compare(0, l, pre, l) == 0;
145 bool suffixIs(string const & a, char c)
147 if (a.empty()) return false;
148 return a[a.length() - 1] == c;
152 bool suffixIs(string const & a, char const * suf)
154 unsigned int suflen = strlen(suf);
155 if (suflen > a.length())
158 return a.compare(a.length() - suflen, suflen, suf) == 0;
163 bool contains(char const * a, string const & b)
165 if (!a || !*a || b.empty()) return false;
166 return strstr(a, b.c_str()) != 0;
170 bool contains(string const & a, char const * b)
174 return a.find(b) != string::npos;
178 bool contains(string const & a, string const & b)
182 return a.find(b) != string::npos;
186 bool contains(char const * a, char const * b)
188 if (!a || !b || !*a || !*b) return false;
189 return strstr(a, b) != 0;
193 int countChar(string const & a, char const c)
195 return count(a.begin(), a.end(), c);
199 // ale970405+lasgoutt-970425
200 // rewritten to use new string (Lgb)
201 string token(string const & a, char delim, int n)
203 if (a.empty()) return string();
205 string::size_type k = 0;
206 string::size_type i = 0;
208 // Find delimiter or end of string
210 if ((i = a.find(delim, i)) == string::npos)
214 // i is now the n'th delim (or string::npos)
215 if (i == string::npos) return string();
216 k = a.find(delim, i);
217 // k is now the n'th + 1 delim (or string::npos)
219 return a.substr(i, k - i);
223 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
224 // rewritten to use new string (Lgb)
225 int tokenPos(string const & a, char delim, string const & tok)
231 while (!str.empty()) {
232 str = split(str, tmptok, delim);
241 bool regexMatch(string const & a, string const & pattern)
243 // We massage the pattern a bit so that the usual
244 // shell pattern we all are used to will work.
245 // One nice thing about using a real regex is that
246 // things like "*.*[^~]" will work also.
247 // build the regex string.
248 string regex(pattern);
249 regex = subst(regex, ".", "\\.");
250 regex = subst(regex, "*", ".*");
252 return reg.exact_match(a);
256 string subst(string const & a, char oldchar, char newchar)
259 string::iterator lit = tmp.begin();
260 for(; lit != tmp.end(); ++lit)
261 if ((*lit) == oldchar)
267 string subst(string const & a,
268 char const * oldstr, string const & newstr)
271 string::size_type i = 0;
272 int olen = strlen(oldstr);
273 while((i = lstr.find(oldstr, i)) != string::npos) {
274 lstr.replace(i, olen, newstr);
275 i += newstr.length(); // We need to be sure that we dont
276 // use the same i over and over again.
282 string strip(string const & a, char const c)
284 if (a.empty()) return a;
286 string::size_type i = tmp.find_last_not_of(c);
287 if (i == a.length() - 1) return tmp; // no c's at end of a
288 if (i != string::npos)
289 tmp.erase(i + 1, string::npos);
291 tmp.clear(); // only c in the whole string
296 string frontStrip(string const & a, char const * p)
298 if (a.empty() || !p || !*p) return a;
300 string::size_type i = tmp.find_first_not_of(p);
307 string frontStrip(string const & a, char const c)
309 if (a.empty()) return a;
311 string::size_type i = tmp.find_first_not_of(c);
318 string split(string const & a, string & piece, char delim)
321 string::size_type i = a.find(delim);
322 if (i == a.length() - 1) {
323 piece = a.substr(0, i);
324 } else if (i != string::npos) {
325 piece = a.substr(0, i);
326 tmp = a.substr(i + 1);
329 tmp = a.substr(i + 1);
337 string split(string const & a, char delim)
340 string::size_type i = a.find(delim);
341 if (i != string::npos) // found delim
342 tmp = a.substr(i + 1);
348 string rsplit(string const & a, string & piece, char delim)
351 string::size_type i = a.rfind(delim);
352 if (i != string::npos) { // delimiter was found
353 piece = a.substr(0, i);
354 tmp = a.substr(i + 1);
355 } else { // delimter was not found