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 return a.compare(0, l, pre, l) == 0;
212 bool suffixIs(string const & a, char c)
214 if (a.empty()) return false;
215 return a[a.length() - 1] == c;
219 bool suffixIs(string const & a, char const * suf)
221 unsigned int suflen = strlen(suf);
222 if (suflen > a.length())
225 return a.compare(a.length() - suflen, suflen, suf) == 0;
230 bool contains(char const * a, string const & b)
232 if (!a || !*a || b.empty()) return false;
233 return strstr(a, b.c_str()) != 0;
237 bool contains(string const & a, char const * b)
241 return a.find(b) != string::npos;
245 bool contains(string const & a, string const & b)
249 return a.find(b) != string::npos;
253 bool contains(char const * a, char const * b)
255 if (!a || !b || !*a || !*b) return false;
256 return strstr(a, b) != 0;
260 bool containsOnly(string const & s, char const * cset)
262 return s.find_first_not_of(cset) == string::npos;
266 bool containsOnly(string const & s, string const & cset)
268 return s.find_first_not_of(cset) == string::npos;
272 bool containsOnly(char const * s, char const * cset)
274 return string(s).find_first_not_of(cset) == string::npos;
278 bool containsOnly(char const * s, string const & cset)
280 return string(s).find_first_not_of(cset) == string::npos;
284 unsigned int countChar(string const & a, char const c)
286 #ifdef HAVE_STD_COUNT
287 return count(a.begin(), a.end(), c);
290 count(a.begin(), a.end(), c, n);
296 // ale970405+lasgoutt-970425
297 // rewritten to use new string (Lgb)
298 string token(string const & a, char delim, int n)
300 if (a.empty()) return string();
302 string::size_type k = 0;
303 string::size_type i = 0;
305 // Find delimiter or end of string
307 if ((i = a.find(delim, i)) == string::npos)
311 // i is now the n'th delim (or string::npos)
312 if (i == string::npos) return string();
313 k = a.find(delim, i);
314 // k is now the n'th + 1 delim (or string::npos)
316 return a.substr(i, k - i);
320 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
321 // rewritten to use new string (Lgb)
322 int tokenPos(string const & a, char delim, string const & tok)
328 while (!str.empty()) {
329 str = split(str, tmptok, delim);
338 bool regexMatch(string const & a, string const & pattern)
340 // We massage the pattern a bit so that the usual
341 // shell pattern we all are used to will work.
342 // One nice thing about using a real regex is that
343 // things like "*.*[^~]" will work also.
344 // build the regex string.
345 string regex(pattern);
346 regex = subst(regex, ".", "\\.");
347 regex = subst(regex, "*", ".*");
349 return reg.exact_match(a);
353 string subst(string const & a, char oldchar, char newchar)
356 string::iterator lit = tmp.begin();
357 for(; lit != tmp.end(); ++lit)
358 if ((*lit) == oldchar)
364 string subst(string const & a,
365 char const * oldstr, string const & newstr)
368 string::size_type i = 0;
369 int olen = strlen(oldstr);
370 while((i = lstr.find(oldstr, i)) != string::npos) {
371 lstr.replace(i, olen, newstr);
372 i += newstr.length(); // We need to be sure that we dont
373 // use the same i over and over again.
379 string strip(string const & a, char const c)
381 if (a.empty()) return a;
383 string::size_type i = tmp.find_last_not_of(c);
384 if (i == a.length() - 1) return tmp; // no c's at end of a
385 if (i != string::npos)
386 tmp.erase(i + 1, string::npos);
388 tmp.erase(); // only c in the whole string
393 string frontStrip(string const & a, char const * p)
395 if (a.empty() || !p || !*p) return a;
397 string::size_type i = tmp.find_first_not_of(p);
404 string frontStrip(string const & a, char const c)
406 if (a.empty()) return a;
408 string::size_type i = tmp.find_first_not_of(c);
415 string split(string const & a, string & piece, char delim)
418 string::size_type i = a.find(delim);
419 if (i == a.length() - 1) {
420 piece = a.substr(0, i);
421 } else if (i != string::npos) {
422 piece = a.substr(0, i);
423 tmp = a.substr(i + 1);
426 tmp = a.substr(i + 1);
434 string split(string const & a, char delim)
437 string::size_type i = a.find(delim);
438 if (i != string::npos) // found delim
439 tmp = a.substr(i + 1);
445 string rsplit(string const & a, string & piece, char delim)
448 string::size_type i = a.rfind(delim);
449 if (i != string::npos) { // delimiter was found
450 piece = a.substr(0, i);
451 tmp = a.substr(i + 1);
452 } else { // delimter was not found