1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
29 // The new glibstdc++-v3 has not worked out all the quirks regarding cctype
30 // yet. So currently it failes if the to using lines below are stated.
35 int compare_no_case(string const & s, string const & s2)
38 string::const_iterator p = s.begin();
39 string::const_iterator p2 = s2.begin();
41 while (p != s.end() && p2 != s2.end()) {
42 int const lc1 = tolower(*p);
43 int const lc2 = tolower(*p2);
45 return (lc1 < lc2) ? -1 : 1;
50 if (s.size() == s2.size())
52 if (s.size() < s2.size())
58 int compare_no_case(string const & s, string const & s2, unsigned int len)
60 //#warning verify this func please
61 string::const_iterator p = s.begin();
62 string::const_iterator p2 = s2.begin();
64 while (i < len && p != s.end() && p2 != s2.end()) {
65 int const lc1 = tolower(*p);
66 int const lc2 = tolower(*p2);
68 return (lc1 < lc2) ? -1 : 1;
73 if (s.size() == s2.size())
75 if (s.size() < s2.size())
81 bool isStrInt(string const & str)
83 if (str.empty()) return false;
85 // Remove leading and trailing white space chars.
86 string tmpstr = frontStrip(strip(str, ' '), ' ');
87 if (tmpstr.empty()) return false;
89 string::const_iterator cit = tmpstr.begin();
90 if ( (*cit) == '-') ++cit;
91 for (; cit != tmpstr.end(); ++cit) {
92 if (!isdigit((*cit))) return false;
98 int strToInt(string const & str)
103 // Remove leading and trailing white space chars.
104 tmpstr = frontStrip(strip(str, ' '), ' ');
105 // Do the conversion proper.
106 return atoi(tmpstr.c_str());
115 bool isStrDbl(string const & str)
117 if (str.empty()) return false;
119 // Remove leading and trailing white space chars.
120 string tmpstr = frontStrip(strip(str, ' '), ' ');
121 if (tmpstr.empty()) return false;
122 // if (1 < tmpstr.count('.')) return false;
124 string::const_iterator cit = tmpstr.begin();
125 bool found_dot(false);
126 if ( (*cit) == '-') ++cit;
127 for (; cit != tmpstr.end(); ++cit) {
144 double strToDbl(string const & str)
149 // Remove leading and trailing white space chars.
150 tmpstr = frontStrip(strip(str, ' '), ' ');
151 // Do the conversion proper.
152 return atof(tmpstr.c_str());
159 char lowercase(char c)
165 char uppercase(char c)
170 string lowercase(string const & a)
174 string::iterator result = tmp.begin();
175 for (string::iterator first = tmp.begin();
176 first != tmp.end(); ++first, ++result) {
177 *result = lowercase(*first);
180 // transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
186 string uppercase(string const & a)
190 string::iterator result = tmp.begin();
191 for (string::iterator first = tmp.begin();
192 first != tmp.end(); ++first, ++result) {
193 *result = uppercase(*first);
196 // transform(tmp.begin(), tmp.end(), tmp.begin(), toupper);
202 bool prefixIs(string const & a, char const * pre)
204 unsigned int l = strlen(pre);
205 if (l > a.length() || a.empty())
208 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
209 // Delete this code when the compilers get a bit better.
210 return ::strncmp(a.c_str(), pre, l) == 0;
212 // This is the code that we really want to use
213 // but until gcc ships with a basic_string that
214 // implements std::string correctly we have to
215 // use the code above.
216 return a.compare(0, l, pre, l) == 0;
222 bool suffixIs(string const & a, char c)
224 if (a.empty()) return false;
225 return a[a.length() - 1] == c;
229 bool suffixIs(string const & a, char const * suf)
231 unsigned int suflen = strlen(suf);
232 if (suflen > a.length())
235 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
236 // Delete this code when the compilers get a bit better.
237 string tmp(a, a.length() - suflen);
238 return ::strncmp(tmp.c_str(), suf, suflen) == 0;
240 // This is the code that we really want to use
241 // but until gcc ships with a basic_string that
242 // implements std::string correctly we have to
243 // use the code above.
244 return a.compare(a.length() - suflen, suflen, suf) == 0;
250 bool contains(char const * a, string const & b)
252 if (!a || !*a || b.empty()) return false;
253 return strstr(a, b.c_str()) != 0;
257 bool contains(string const & a, char const * b)
261 return a.find(b) != string::npos;
265 bool contains(string const & a, string const & b)
269 return a.find(b) != string::npos;
273 bool contains(char const * a, char const * b)
275 if (!a || !b || !*a || !*b) return false;
276 return strstr(a, b) != 0;
280 bool containsOnly(string const & s, char const * cset)
282 return s.find_first_not_of(cset) == string::npos;
286 bool containsOnly(string const & s, string const & cset)
288 return s.find_first_not_of(cset) == string::npos;
292 bool containsOnly(char const * s, char const * cset)
294 return string(s).find_first_not_of(cset) == string::npos;
298 bool containsOnly(char const * s, string const & cset)
300 return string(s).find_first_not_of(cset) == string::npos;
304 unsigned int countChar(string const & a, char const c)
306 #ifdef HAVE_STD_COUNT
307 return count(a.begin(), a.end(), c);
310 count(a.begin(), a.end(), c, n);
316 // ale970405+lasgoutt-970425
317 // rewritten to use new string (Lgb)
318 string token(string const & a, char delim, int n)
320 if (a.empty()) return string();
322 string::size_type k = 0;
323 string::size_type i = 0;
325 // Find delimiter or end of string
327 if ((i = a.find(delim, i)) == string::npos)
331 // i is now the n'th delim (or string::npos)
332 if (i == string::npos) return string();
333 k = a.find(delim, i);
334 // k is now the n'th + 1 delim (or string::npos)
336 return a.substr(i, k - i);
340 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
341 // rewritten to use new string (Lgb)
342 int tokenPos(string const & a, char delim, string const & tok)
348 while (!str.empty()) {
349 str = split(str, tmptok, delim);
358 bool regexMatch(string const & a, string const & pattern)
360 // We massage the pattern a bit so that the usual
361 // shell pattern we all are used to will work.
362 // One nice thing about using a real regex is that
363 // things like "*.*[^~]" will work also.
364 // build the regex string.
365 string regex(pattern);
366 regex = subst(regex, ".", "\\.");
367 regex = subst(regex, "*", ".*");
369 return reg.exact_match(a);
373 string subst(string const & a, char oldchar, char newchar)
376 string::iterator lit = tmp.begin();
377 for(; lit != tmp.end(); ++lit)
378 if ((*lit) == oldchar)
384 string subst(string const & a,
385 char const * oldstr, string const & newstr)
388 string::size_type i = 0;
389 int olen = strlen(oldstr);
390 while((i = lstr.find(oldstr, i)) != string::npos) {
391 lstr.replace(i, olen, newstr);
392 i += newstr.length(); // We need to be sure that we dont
393 // use the same i over and over again.
399 string strip(string const & a, char const c)
401 if (a.empty()) return a;
403 string::size_type i = tmp.find_last_not_of(c);
404 if (i == a.length() - 1) return tmp; // no c's at end of a
405 if (i != string::npos)
406 tmp.erase(i + 1, string::npos);
408 tmp.erase(); // only c in the whole string
413 string frontStrip(string const & a, char const * p)
415 if (a.empty() || !p || !*p) return a;
417 string::size_type i = tmp.find_first_not_of(p);
424 string frontStrip(string const & a, char const c)
426 if (a.empty()) return a;
428 string::size_type i = tmp.find_first_not_of(c);
435 string split(string const & a, string & piece, char delim)
438 string::size_type i = a.find(delim);
439 if (i == a.length() - 1) {
440 piece = a.substr(0, i);
441 } else if (i != string::npos) {
442 piece = a.substr(0, i);
443 tmp = a.substr(i + 1);
446 tmp = a.substr(i + 1);
454 string split(string const & a, char delim)
457 string::size_type i = a.find(delim);
458 if (i != string::npos) // found delim
459 tmp = a.substr(i + 1);
465 string rsplit(string const & a, string & piece, char delim)
468 string::size_type i = a.rfind(delim);
469 if (i != string::npos) { // delimiter was found
470 piece = a.substr(0, i);
471 tmp = a.substr(i + 1);
472 } else { // delimter was not found