13 bool isStrInt(string const & str)
15 if (str.empty()) return false;
17 // Remove leading and trailing white space chars.
18 string tmpstr = frontStrip(strip(str, ' '), ' ');
19 if (tmpstr.empty()) return false;
21 string::const_iterator cit = tmpstr.begin();
22 if ( (*cit) == '-') ++cit;
23 for (; cit != tmpstr.end(); ++cit) {
24 if (!isdigit((*cit))) return false;
30 int LStr2Int(string const & str)
35 // Remove leading and trailing white space chars.
36 tmpstr = frontStrip(strip(str, ' '), ' ');
37 // Do the conversion proper.
38 return atoi(tmpstr.c_str());
44 string lowercase(string const & a)
47 string::const_iterator cit = a.begin();
48 for(; cit != a.end(); ++cit) {
49 tmp += char(tolower(*cit));
58 sprintf(str, "%ld", i);
62 string tostr(unsigned long i)
65 sprintf(str, "%lu", i);
69 string tostr(void * v)
71 return tostr(long(v));
77 return tostr(long(i));
80 string tostr(unsigned int ui)
82 return tostr(long(ui));
87 return tostr(long(c));
92 return b ? "true" : "false";
98 return tostr(double(f));
102 string tostr(double d)
105 sprintf(tmp, "%f", d);
110 bool prefixIs(string const & a, char const * pre)
112 unsigned int l = strlen(pre);
113 if (l > a.length() || a.empty())
116 return a.compare(0, l, pre, l) == 0;
120 bool suffixIs(string const & a, char c)
122 if (a.empty()) return false;
123 return a[a.length()-1] == c;
127 bool suffixIs(string const & a, char const * suf)
129 unsigned int suflen = strlen(suf);
130 if (suflen > a.length())
133 return a.compare(a.length() - suflen, suflen, suf) == 0;
138 bool contains(char const * a, string const & b)
140 if (!a || !*a || b.empty()) return false;
141 return strstr(a, b.c_str()) != 0;
145 bool contains(string const & a, char const * b)
149 return a.find(b) != string::npos;
153 bool contains(string const & a, string const & b)
157 return a.find(b) != string::npos;
161 bool contains(char const * a, char const * b)
163 if (!a || !b || !*a || !*b) return false;
164 return strstr(a, b) != 0;
168 int countChar(string const & a, char const c)
171 count(a.begin(), a.end(), c, n);
176 // ale970405+lasgoutt-970425
177 // rewritten to use new string (Lgb)
178 string token(string const & a, char delim, int n)
180 if (a.empty()) return string();
182 string::size_type k = 0;
183 string::size_type i = 0;
185 // Find delimiter or end of string
187 if ((i = a.find(delim, i)) == string::npos)
191 // i is now the n'th delim (or string::npos)
192 if (i == string::npos) return string();
193 k = a.find(delim, i);
194 // k is now the n'th + 1 delim (or string::npos)
196 return a.substr(i, k - i);
200 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
201 // rewritten to use new string (Lgb)
202 int tokenPos(string const & a, char delim, string const & tok)
208 while (!str.empty()) {
209 str = split(str, tmptok, delim);
218 bool regexMatch(string const & a, string const & pattern)
225 string::size_type si=0, pi=0;
226 string::size_type const sl = a.length();
227 string::size_type const pl = pattern.length();
229 while (si < sl && pi < pl) {
230 if (pattern[pi]=='*') {
231 // Skip all consequtive *s
232 while (pattern[pi] == '*') {
238 // Get next chunk of pattern to match
241 split(pattern.substr(pi, pl-1), chunk, '*');
243 if (!chunk.empty() && pattern[pl-1] == '*' &&
248 // Last chunk, see if tail matches
249 if (sl < chunk.length()) {
252 temp = a.substr(sl - chunk.length(), sl - 1);
253 return temp == chunk;
255 // Middle chunk, see if we can find a match
257 while (!match && si<sl) {
258 temp = a.substr(si, sl - 1);
259 match = prefixIs(temp, chunk.c_str());
264 si += chunk.length()-1;
265 pi += chunk.length();
266 if (si==sl && pi==pl-1)
269 } else if (a[si++] != pattern[pi++]) {
273 if (pi < pl || si < sl)
279 string subst(string const & a, char oldchar, char newchar)
282 string::iterator lit = tmp.begin();
283 for(; lit != tmp.end(); ++lit)
284 if ((*lit) == oldchar)
290 string subst(string const & a,
291 char const * oldstr, string const & newstr)
294 string::size_type i = 0;
295 int olen = strlen(oldstr);
296 while((i = lstr.find(oldstr, i)) != string::npos) {
297 lstr.replace(i, olen, newstr);
298 i += newstr.length(); // We need to be sure that we dont
299 // use the same i over and over again.
305 string strip(string const & a, char const c)
307 if (a.empty()) return a;
309 string::size_type i = tmp.find_last_not_of(c);
310 if (i == a.length() - 1) return tmp; // no c's at end of a
311 if (i != string::npos)
312 tmp.erase(i + 1, string::npos);
314 tmp.erase(); // only c in the whole string
319 string frontStrip(string const & a, char const * p)
321 if (a.empty() || !p || !*p) return a;
323 string::size_type i = tmp.find_first_not_of(p);
330 string frontStrip(string const & a, char const c)
332 if (a.empty()) return a;
334 string::size_type i = tmp.find_first_not_of(c);
341 string split(string const & a, string & piece, char delim)
344 string::size_type i = a.find(delim);
345 if (i == a.length() - 1) {
346 piece = a.substr(0, i);
347 } else if (i != string::npos) {
348 piece = a.substr(0, i);
349 tmp = a.substr(i + 1);
352 tmp = a.substr(i + 1);
360 string split(string const & a, char delim)
363 string::size_type i = a.find(delim);
364 if (i != string::npos) // found delim
365 tmp = a.substr(i + 1);
371 string rsplit(string const & a, string & piece, char delim)
374 string::size_type i = a.rfind(delim);
375 if (i != string::npos) { // delimiter was found
376 piece = a.substr(0, i);
377 tmp = a.substr(i + 1);
378 } else { // delimter was not found