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);
64 string tostr(unsigned long i)
67 sprintf(str, "%lu", i);
72 string tostr(void * v)
74 return tostr(long(v));
80 return tostr(long(i));
84 string tostr(unsigned int ui)
86 return tostr(long(ui));
98 return b ? "true" : "false";
103 string tostr(float f)
105 return tostr(double(f));
110 string tostr(double d)
113 sprintf(tmp, "%f", d);
118 bool prefixIs(string const & a, char const * pre)
120 unsigned int l = strlen(pre);
121 if (l > a.length() || a.empty())
124 return a.compare(0, l, pre, l) == 0;
128 bool suffixIs(string const & a, char c)
130 if (a.empty()) return false;
131 return a[a.length() - 1] == c;
135 bool suffixIs(string const & a, char const * suf)
137 unsigned int suflen = strlen(suf);
138 if (suflen > a.length())
141 return a.compare(a.length() - suflen, suflen, suf) == 0;
146 bool contains(char const * a, string const & b)
148 if (!a || !*a || b.empty()) return false;
149 return strstr(a, b.c_str()) != 0;
153 bool contains(string const & a, char const * b)
157 return a.find(b) != string::npos;
161 bool contains(string const & a, string const & b)
165 return a.find(b) != string::npos;
169 bool contains(char const * a, char const * b)
171 if (!a || !b || !*a || !*b) return false;
172 return strstr(a, b) != 0;
176 int countChar(string const & a, char const c)
178 return count(a.begin(), a.end(), c);
182 // ale970405+lasgoutt-970425
183 // rewritten to use new string (Lgb)
184 string token(string const & a, char delim, int n)
186 if (a.empty()) return string();
188 string::size_type k = 0;
189 string::size_type i = 0;
191 // Find delimiter or end of string
193 if ((i = a.find(delim, i)) == string::npos)
197 // i is now the n'th delim (or string::npos)
198 if (i == string::npos) return string();
199 k = a.find(delim, i);
200 // k is now the n'th + 1 delim (or string::npos)
202 return a.substr(i, k - i);
206 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
207 // rewritten to use new string (Lgb)
208 int tokenPos(string const & a, char delim, string const & tok)
214 while (!str.empty()) {
215 str = split(str, tmptok, delim);
224 bool regexMatch(string const & a, string const & pattern)
226 // We massage the pattern a bit so that the usual
227 // shell pattern we all are used to will work.
228 // One nice thing about using a real regex is that
229 // things like "*.*[^~]" will work also.
230 // build the regex string.
231 string regex(pattern);
232 regex = subst(regex, ".", "\\.");
233 regex = subst(regex, "*", ".*");
235 return reg.exact_match(a);
239 string subst(string const & a, char oldchar, char newchar)
242 string::iterator lit = tmp.begin();
243 for(; lit != tmp.end(); ++lit)
244 if ((*lit) == oldchar)
250 string subst(string const & a,
251 char const * oldstr, string const & newstr)
254 string::size_type i = 0;
255 int olen = strlen(oldstr);
256 while((i = lstr.find(oldstr, i)) != string::npos) {
257 lstr.replace(i, olen, newstr);
258 i += newstr.length(); // We need to be sure that we dont
259 // use the same i over and over again.
265 string strip(string const & a, char const c)
267 if (a.empty()) return a;
269 string::size_type i = tmp.find_last_not_of(c);
270 if (i == a.length() - 1) return tmp; // no c's at end of a
271 if (i != string::npos)
272 tmp.erase(i + 1, string::npos);
274 tmp.clear(); // only c in the whole string
279 string frontStrip(string const & a, char const * p)
281 if (a.empty() || !p || !*p) return a;
283 string::size_type i = tmp.find_first_not_of(p);
290 string frontStrip(string const & a, char const c)
292 if (a.empty()) return a;
294 string::size_type i = tmp.find_first_not_of(c);
301 string split(string const & a, string & piece, char delim)
304 string::size_type i = a.find(delim);
305 if (i == a.length() - 1) {
306 piece = a.substr(0, i);
307 } else if (i != string::npos) {
308 piece = a.substr(0, i);
309 tmp = a.substr(i + 1);
312 tmp = a.substr(i + 1);
320 string split(string const & a, char delim)
323 string::size_type i = a.find(delim);
324 if (i != string::npos) // found delim
325 tmp = a.substr(i + 1);
331 string rsplit(string const & a, string & piece, char delim)
334 string::size_type i = a.rfind(delim);
335 if (i != string::npos) { // delimiter was found
336 piece = a.substr(0, i);
337 tmp = a.substr(i + 1);
338 } else { // delimter was not found