14 bool isStrInt(string const & str)
16 if (str.empty()) return false;
18 // Remove leading and trailing white space chars.
19 string tmpstr = frontStrip(strip(str, ' '), ' ');
20 if (tmpstr.empty()) return false;
22 string::const_iterator cit = tmpstr.begin();
23 if ( (*cit) == '-') ++cit;
24 for (; cit != tmpstr.end(); ++cit) {
25 if (!isdigit((*cit))) return false;
31 int strToInt(string const & str)
36 // Remove leading and trailing white space chars.
37 tmpstr = frontStrip(strip(str, ' '), ' ');
38 // Do the conversion proper.
39 return atoi(tmpstr.c_str());
45 string lowercase(string const & a)
48 string::const_iterator cit = a.begin();
49 for(; cit != a.end(); ++cit) {
50 tmp += static_cast<char>(tolower(*cit));
59 sprintf(str, "%ld", i);
63 string tostr(unsigned long i)
66 sprintf(str, "%lu", i);
70 string tostr(void * v)
72 return tostr(long(v));
78 return tostr(long(i));
81 string tostr(unsigned int ui)
83 return tostr(long(ui));
93 return b ? "true" : "false";
99 return tostr(double(f));
103 string tostr(double d)
106 sprintf(tmp, "%f", d);
111 bool prefixIs(string const & a, char const * pre)
113 unsigned int l = strlen(pre);
114 if (l > a.length() || a.empty())
117 return a.compare(0, l, pre, l) == 0;
121 bool suffixIs(string const & a, char c)
123 if (a.empty()) return false;
124 return a[a.length() - 1] == c;
128 bool suffixIs(string const & a, char const * suf)
130 unsigned int suflen = strlen(suf);
131 if (suflen > a.length())
134 return a.compare(a.length() - suflen, suflen, suf) == 0;
139 bool contains(char const * a, string const & b)
141 if (!a || !*a || b.empty()) return false;
142 return strstr(a, b.c_str()) != 0;
146 bool contains(string const & a, char const * b)
150 return a.find(b) != string::npos;
154 bool contains(string const & a, string const & b)
158 return a.find(b) != string::npos;
162 bool contains(char const * a, char const * b)
164 if (!a || !b || !*a || !*b) return false;
165 return strstr(a, b) != 0;
169 int countChar(string const & a, char const c)
171 return count(a.begin(), a.end(), c);
175 // ale970405+lasgoutt-970425
176 // rewritten to use new string (Lgb)
177 string token(string const & a, char delim, int n)
179 if (a.empty()) return string();
181 string::size_type k = 0;
182 string::size_type i = 0;
184 // Find delimiter or end of string
186 if ((i = a.find(delim, i)) == string::npos)
190 // i is now the n'th delim (or string::npos)
191 if (i == string::npos) return string();
192 k = a.find(delim, i);
193 // k is now the n'th + 1 delim (or string::npos)
195 return a.substr(i, k - i);
199 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
200 // rewritten to use new string (Lgb)
201 int tokenPos(string const & a, char delim, string const & tok)
207 while (!str.empty()) {
208 str = split(str, tmptok, delim);
217 bool regexMatch(string const & a, string const & pattern)
219 // We massage the pattern a bit so that the usual
220 // shell pattern we all are used to will work.
221 // One nice thing about using a real regex is that
222 // things like "*.*[^~]" will work also.
223 // build the regex string.
224 string regex(pattern);
225 regex = subst(regex, ".", "\\.");
226 regex = subst(regex, "*", ".*");
228 return reg.exact_match(a);
232 string subst(string const & a, char oldchar, char newchar)
235 string::iterator lit = tmp.begin();
236 for(; lit != tmp.end(); ++lit)
237 if ((*lit) == oldchar)
243 string subst(string const & a,
244 char const * oldstr, string const & newstr)
247 string::size_type i = 0;
248 int olen = strlen(oldstr);
249 while((i = lstr.find(oldstr, i)) != string::npos) {
250 lstr.replace(i, olen, newstr);
251 i += newstr.length(); // We need to be sure that we dont
252 // use the same i over and over again.
258 string strip(string const & a, char const c)
260 if (a.empty()) return a;
262 string::size_type i = tmp.find_last_not_of(c);
263 if (i == a.length() - 1) return tmp; // no c's at end of a
264 if (i != string::npos)
265 tmp.erase(i + 1, string::npos);
267 tmp.clear(); // only c in the whole string
272 string frontStrip(string const & a, char const * p)
274 if (a.empty() || !p || !*p) return a;
276 string::size_type i = tmp.find_first_not_of(p);
283 string frontStrip(string const & a, char const c)
285 if (a.empty()) return a;
287 string::size_type i = tmp.find_first_not_of(c);
294 string split(string const & a, string & piece, char delim)
297 string::size_type i = a.find(delim);
298 if (i == a.length() - 1) {
299 piece = a.substr(0, i);
300 } else if (i != string::npos) {
301 piece = a.substr(0, i);
302 tmp = a.substr(i + 1);
305 tmp = a.substr(i + 1);
313 string split(string const & a, char delim)
316 string::size_type i = a.find(delim);
317 if (i != string::npos) // found delim
318 tmp = a.substr(i + 1);
324 string rsplit(string const & a, string & piece, char delim)
327 string::size_type i = a.rfind(delim);
328 if (i != string::npos) { // delimiter was found
329 piece = a.substr(0, i);
330 tmp = a.substr(i + 1);
331 } else { // delimter was not found