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 LStr2Int(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)
49 string::const_iterator cit = a.begin();
50 for(; cit != a.end(); ++cit) {
51 tmp += char(tolower(*cit));
60 sprintf(str, "%ld", i);
64 string tostr(unsigned long i)
67 sprintf(str, "%lu", i);
71 string tostr(void * v)
73 return tostr(long(v));
79 return tostr(long(i));
82 string tostr(unsigned int ui)
84 return tostr(long(ui));
89 return tostr(long(c));
94 return b ? "true" : "false";
100 return tostr(double(f));
104 string tostr(double d)
107 sprintf(tmp, "%f", d);
112 bool prefixIs(string const & a, char const * pre)
114 unsigned int l = strlen(pre);
115 if (l > a.length() || a.empty())
118 return a.compare(0, l, pre, l) == 0;
122 bool suffixIs(string const & a, char c)
124 if (a.empty()) return false;
125 return a[a.length()-1] == c;
129 bool suffixIs(string const & a, char const * suf)
131 unsigned int suflen = strlen(suf);
132 if (suflen > a.length())
135 return a.compare(a.length() - suflen, suflen, suf) == 0;
140 bool contains(char const * a, string const & b)
142 if (!a || !*a || b.empty()) return false;
143 return strstr(a, b.c_str()) != 0;
147 bool contains(string const & a, char const * b)
151 return a.find(b) != string::npos;
155 bool contains(string const & a, string const & b)
159 return a.find(b) != string::npos;
163 bool contains(char const * a, char const * b)
165 if (!a || !b || !*a || !*b) return false;
166 return strstr(a, b) != 0;
170 int countChar(string const & a, char const c)
173 count(a.begin(), a.end(), c, n);
178 // ale970405+lasgoutt-970425
179 // rewritten to use new string (Lgb)
180 string token(string const & a, char delim, int n)
182 if (a.empty()) return string();
184 string::size_type k = 0;
185 string::size_type i = 0;
187 // Find delimiter or end of string
189 if ((i = a.find(delim, i)) == string::npos)
193 // i is now the n'th delim (or string::npos)
194 if (i == string::npos) return string();
195 k = a.find(delim, i);
196 // k is now the n'th + 1 delim (or string::npos)
198 return a.substr(i, k - i);
202 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
203 // rewritten to use new string (Lgb)
204 int tokenPos(string const & a, char delim, string const & tok)
210 while (!str.empty()) {
211 str = split(str, tmptok, delim);
220 bool regexMatch(string const & a, string const & pattern)
227 string::size_type si=0, pi=0;
228 string::size_type const sl = a.length();
229 string::size_type const pl = pattern.length();
231 while (si < sl && pi < pl) {
232 if (pattern[pi]=='*') {
233 // Skip all consequtive *s
234 while (pattern[pi] == '*') {
240 // Get next chunk of pattern to match
243 split(pattern.substr(pi, pl-1), chunk, '*');
245 if (!chunk.empty() && pattern[pl-1] == '*' &&
250 // Last chunk, see if tail matches
251 if (sl < chunk.length()) {
254 temp = a.substr(sl - chunk.length(), sl - 1);
255 return temp == chunk;
257 // Middle chunk, see if we can find a match
259 while (!match && si<sl) {
260 temp = a.substr(si, sl - 1);
261 match = prefixIs(temp, chunk.c_str());
266 si += chunk.length()-1;
267 pi += chunk.length();
268 if (si==sl && pi==pl-1)
271 } else if (a[si++] != pattern[pi++]) {
275 if (pi < pl || si < sl)
281 string subst(string const & a, char oldchar, char newchar)
284 string::iterator lit = tmp.begin();
285 for(; lit != tmp.end(); ++lit)
286 if ((*lit) == oldchar)
292 string subst(string const & a,
293 char const * oldstr, string const & newstr)
296 string::size_type i = 0;
297 int olen = strlen(oldstr);
298 while((i = lstr.find(oldstr, i)) != string::npos) {
299 lstr.replace(i, olen, newstr);
300 i += newstr.length(); // We need to be sure that we dont
301 // use the same i over and over again.
307 string strip(string const & a, char const c)
309 if (a.empty()) return a;
311 string::size_type i = tmp.find_last_not_of(c);
312 if (i == a.length() - 1) return tmp; // no c's at end of a
313 if (i != string::npos)
314 tmp.erase(i + 1, string::npos);
316 tmp.erase(); // only c in the whole string
321 string frontStrip(string const & a, char const * p)
323 if (a.empty() || !p || !*p) return a;
325 string::size_type i = tmp.find_first_not_of(p);
332 string frontStrip(string const & a, char const c)
334 if (a.empty()) return a;
336 string::size_type i = tmp.find_first_not_of(c);
343 string split(string const & a, string & piece, char delim)
346 string::size_type i = a.find(delim);
347 if (i == a.length() - 1) {
348 piece = a.substr(0, i);
349 } else if (i != string::npos) {
350 piece = a.substr(0, i);
351 tmp = a.substr(i + 1);
354 tmp = a.substr(i + 1);
362 string split(string const & a, char delim)
365 string::size_type i = a.find(delim);
366 if (i != string::npos) // found delim
367 tmp = a.substr(i + 1);
373 string rsplit(string const & a, string & piece, char delim)
376 string::size_type i = a.rfind(delim);
377 if (i != string::npos) { // delimiter was found
378 piece = a.substr(0, i);
379 tmp = a.substr(i + 1);
380 } else { // delimter was not found