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
30 // The new glibstdc++-v3 has not worked out all the quirks regarding cctype
31 // yet. So currently it failes if the to using lines below are stated.
36 int compare_no_case(string const & s, string const & s2)
39 string::const_iterator p = s.begin();
40 string::const_iterator p2 = s2.begin();
42 while (p != s.end() && p2 != s2.end()) {
43 int const lc1 = tolower(*p);
44 int const lc2 = tolower(*p2);
46 return (lc1 < lc2) ? -1 : 1;
51 if (s.size() == s2.size())
53 if (s.size() < s2.size())
59 int compare_no_case(string const & s, string const & s2, unsigned int len)
61 //#warning verify this func please
62 string::const_iterator p = s.begin();
63 string::const_iterator p2 = s2.begin();
65 while (i < len && p != s.end() && p2 != s2.end()) {
66 int const lc1 = tolower(*p);
67 int const lc2 = tolower(*p2);
69 return (lc1 < lc2) ? -1 : 1;
74 if (s.size() == s2.size())
76 if (s.size() < s2.size())
82 bool isStrInt(string const & str)
84 if (str.empty()) return false;
86 // Remove leading and trailing white space chars.
87 string tmpstr = frontStrip(strip(str, ' '), ' ');
88 if (tmpstr.empty()) return false;
90 string::const_iterator cit = tmpstr.begin();
91 if ( (*cit) == '-') ++cit;
92 string::const_iterator end = tmpstr.end();
93 for (; cit != end; ++cit) {
94 if (!isdigit((*cit))) return false;
100 int strToInt(string const & str)
103 // Remove leading and trailing white space chars.
104 string tmpstr = frontStrip(strip(str, ' '), ' ');
105 // Do the conversion proper.
106 return atoi(tmpstr.c_str());
113 bool isStrDbl(string const & str)
115 if (str.empty()) return false;
117 // Remove leading and trailing white space chars.
118 string tmpstr = frontStrip(strip(str, ' '), ' ');
119 if (tmpstr.empty()) return false;
120 // if (1 < tmpstr.count('.')) return false;
122 string::const_iterator cit = tmpstr.begin();
123 bool found_dot(false);
124 if ( (*cit) == '-') ++cit;
125 string::const_iterator end = tmpstr.end();
126 for (; cit != end; ++cit) {
143 double strToDbl(string const & str)
146 // Remove leading and trailing white space chars.
147 string tmpstr = frontStrip(strip(str, ' '), ' ');
148 // Do the conversion proper.
149 return atof(tmpstr.c_str());
156 char lowercase(char c)
162 char uppercase(char c)
168 string const lowercase(string const & a)
172 string::iterator result = tmp.begin();
173 string::iterator end = tmp.end();
174 for (string::iterator first = tmp.begin();
175 first != end; ++first, ++result) {
176 *result = lowercase(*first);
179 // transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
185 string const uppercase(string const & a)
189 string::iterator result = tmp.begin();
190 string::iterator end = tmp.end();
191 for (string::iterator first = tmp.begin();
192 first != 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)
206 unsigned int l = strlen(pre);
207 if (l > a.length() || a.empty())
210 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
211 // Delete this code when the compilers get a bit better.
212 return ::strncmp(a.c_str(), pre, l) == 0;
214 // This is the code that we really want to use
215 // but until gcc ships with a basic_string that
216 // implements std::string correctly we have to
217 // use the code above.
218 return a.compare(0, l, pre, l) == 0;
224 bool suffixIs(string const & a, char c)
226 if (a.empty()) return false;
227 return a[a.length() - 1] == c;
231 bool suffixIs(string const & a, char const * suf)
235 unsigned int const suflen = strlen(suf);
236 if (suflen > a.length())
239 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
240 // Delete this code when the compilers get a bit better.
241 string tmp(a, a.length() - suflen);
242 return ::strncmp(tmp.c_str(), suf, suflen) == 0;
244 // This is the code that we really want to use
245 // but until gcc ships with a basic_string that
246 // implements std::string correctly we have to
247 // use the code above.
248 return a.compare(a.length() - suflen, suflen, suf) == 0;
254 bool contains(char const * a, string const & b)
258 if (!*a || b.empty()) return false;
259 return strstr(a, b.c_str()) != 0;
263 bool contains(string const & a, char const * b)
269 return a.find(b) != string::npos;
273 bool contains(string const & a, string const & b)
277 return a.find(b) != string::npos;
281 bool contains(char const * a, char const * b)
285 if (!*a || !*b) return false;
286 return strstr(a, b) != 0;
290 bool containsOnly(string const & s, char const * cset)
294 return s.find_first_not_of(cset) == string::npos;
298 bool containsOnly(string const & s, string const & cset)
300 return s.find_first_not_of(cset) == string::npos;
304 bool containsOnly(char const * s, char const * cset)
308 return string(s).find_first_not_of(cset) == string::npos;
312 bool containsOnly(char const * s, string const & cset)
316 return string(s).find_first_not_of(cset) == string::npos;
320 unsigned int countChar(string const & a, char c)
322 #ifdef HAVE_STD_COUNT
323 return count(a.begin(), a.end(), c);
326 count(a.begin(), a.end(), c, n);
332 // ale970405+lasgoutt-970425
333 // rewritten to use new string (Lgb)
334 string const token(string const & a, char delim, int n)
336 if (a.empty()) return string();
338 string::size_type k = 0;
339 string::size_type i = 0;
341 // Find delimiter or end of string
343 if ((i = a.find(delim, i)) == string::npos)
347 // i is now the n'th delim (or string::npos)
348 if (i == string::npos) return string();
349 k = a.find(delim, i);
350 // k is now the n'th + 1 delim (or string::npos)
352 return a.substr(i, k - i);
356 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
357 // rewritten to use new string (Lgb)
358 int tokenPos(string const & a, char delim, string const & tok)
364 while (!str.empty()) {
365 str = split(str, tmptok, delim);
374 bool regexMatch(string const & a, string const & pattern)
376 // We massage the pattern a bit so that the usual
377 // shell pattern we all are used to will work.
378 // One nice thing about using a real regex is that
379 // things like "*.*[^~]" will work also.
380 // build the regex string.
381 string regex(pattern);
382 regex = subst(regex, ".", "\\.");
383 regex = subst(regex, "*", ".*");
385 return reg.exact_match(a);
389 string const subst(string const & a, char oldchar, char newchar)
392 string::iterator lit = tmp.begin();
393 string::iterator end = tmp.end();
394 for(; lit != end; ++lit)
395 if ((*lit) == oldchar)
401 string const subst(string const & a,
402 char const * oldstr, string const & newstr)
407 string::size_type i = 0;
408 int olen = strlen(oldstr);
409 while((i = lstr.find(oldstr, i)) != string::npos) {
410 lstr.replace(i, olen, newstr);
411 i += newstr.length(); // We need to be sure that we dont
412 // use the same i over and over again.
418 string const strip(string const & a, char c)
420 if (a.empty()) return a;
422 string::size_type i = tmp.find_last_not_of(c);
423 if (i == a.length() - 1) return tmp; // no c's at end of a
424 if (i != string::npos)
425 tmp.erase(i + 1, string::npos);
427 tmp.erase(); // only c in the whole string
432 string const frontStrip(string const & a, char const * p)
436 if (a.empty() || !*p) return a;
438 string::size_type i = tmp.find_first_not_of(p);
445 string const frontStrip(string const & a, char c)
447 if (a.empty()) return a;
449 string::size_type i = tmp.find_first_not_of(c);
456 string const split(string const & a, string & piece, char delim)
459 string::size_type i = a.find(delim);
460 if (i == a.length() - 1) {
461 piece = a.substr(0, i);
462 } else if (i != string::npos) {
463 piece = a.substr(0, i);
464 tmp = a.substr(i + 1);
467 tmp = a.substr(i + 1);
475 string const split(string const & a, char delim)
478 string::size_type i = a.find(delim);
479 if (i != string::npos) // found delim
480 tmp = a.substr(i + 1);
486 string const rsplit(string const & a, string & piece, char delim)
489 string::size_type i = a.rfind(delim);
490 if (i != string::npos) { // delimiter was found
491 piece = a.substr(0, i);
492 tmp = a.substr(i + 1);
493 } else { // delimter was not found