1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
22 #include <boost/regex.hpp>
33 #ifndef CXX_GLOBAL_CSTD
42 int compare_no_case(string const & s, string const & s2)
44 string::const_iterator p = s.begin();
45 string::const_iterator p2 = s2.begin();
47 while (p != s.end() && p2 != s2.end()) {
48 int const lc1 = tolower(*p);
49 int const lc2 = tolower(*p2);
51 return (lc1 < lc2) ? -1 : 1;
56 if (s.size() == s2.size())
58 if (s.size() < s2.size())
65 int ascii_tolower(int c) {
66 if (c >= 'A' && c <= 'Z')
73 int compare_ascii_no_case(string const & s, string const & s2)
75 string::const_iterator p = s.begin();
76 string::const_iterator p2 = s2.begin();
78 while (p != s.end() && p2 != s2.end()) {
79 int const lc1 = ascii_tolower(*p);
80 int const lc2 = ascii_tolower(*p2);
82 return (lc1 < lc2) ? -1 : 1;
87 if (s.size() == s2.size())
89 if (s.size() < s2.size())
95 int compare_no_case(string const & s, string const & s2, unsigned int len)
97 string::const_iterator p = s.begin();
98 string::const_iterator p2 = s2.begin();
100 while (i < len && p != s.end() && p2 != s2.end()) {
101 int const lc1 = tolower(*p);
102 int const lc2 = tolower(*p2);
104 return (lc1 < lc2) ? -1 : 1;
110 if (s.size() >= len && s2.size() >= len)
112 if (s.size() < s2.size())
118 bool isStrInt(string const & str)
120 if (str.empty()) return false;
122 // Remove leading and trailing white space chars.
123 string const tmpstr = frontStrip(strip(str, ' '), ' ');
124 if (tmpstr.empty()) return false;
126 string::const_iterator cit = tmpstr.begin();
127 if ((*cit) == '-') ++cit;
128 string::const_iterator end = tmpstr.end();
129 for (; cit != end; ++cit) {
130 if (!isdigit((*cit))) return false;
136 bool isStrUnsignedInt(string const & str)
138 if (str.empty()) return false;
140 // Remove leading and trailing white space chars.
141 string const tmpstr = frontStrip(strip(str, ' '), ' ');
142 if (tmpstr.empty()) return false;
144 string::const_iterator cit = tmpstr.begin();
145 string::const_iterator end = tmpstr.end();
146 for (; cit != end; ++cit) {
147 if (!isdigit((*cit))) return false;
153 int strToInt(string const & str)
156 // Remove leading and trailing white space chars.
157 string const tmpstr = frontStrip(strip(str, ' '), ' ');
158 // Do the conversion proper.
159 return lyx::atoi(tmpstr);
166 unsigned int strToUnsignedInt(string const & str)
168 if (isStrUnsignedInt(str)) {
169 // Remove leading and trailing white space chars.
170 string const tmpstr = frontStrip(strip(str, ' '), ' ');
171 // Do the conversion proper.
172 return lyx::atoi(tmpstr);
179 bool isStrDbl(string const & str)
181 if (str.empty()) return false;
183 // Remove leading and trailing white space chars.
184 string const tmpstr = frontStrip(strip(str, ' '), ' ');
185 if (tmpstr.empty()) return false;
186 // if (1 < tmpstr.count('.')) return false;
188 string::const_iterator cit = tmpstr.begin();
189 bool found_dot(false);
190 if ((*cit) == '-') ++cit;
191 string::const_iterator end = tmpstr.end();
192 for (; cit != end; ++cit) {
209 double strToDbl(string const & str)
212 // Remove leading and trailing white space chars.
213 string const tmpstr = frontStrip(strip(str, ' '), ' ');
214 // Do the conversion proper.
215 return ::atof(tmpstr.c_str());
222 char lowercase(char c)
224 return char(tolower(c));
228 char uppercase(char c)
230 return char(toupper(c));
236 // since we cannot use std::tolower and std::toupper directly in the
237 // calls to std::transform yet, we use these helper clases. (Lgb)
239 struct local_lowercase {
240 char operator()(char c) const {
245 struct local_uppercase {
246 char operator()(char c) const {
251 } // end of anon namespace
253 string const lowercase(string const & a)
256 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
260 string const uppercase(string const & a)
263 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
268 bool prefixIs(string const & a, char const * pre)
272 size_t const l = strlen(pre);
273 string::size_type const alen = a.length();
275 if (l > alen || a.empty())
278 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
279 // Delete this code when the compilers get a bit better.
280 return ::strncmp(a.c_str(), pre, l) == 0;
282 // This is the code that we really want to use
283 // but until gcc ships with a basic_string that
284 // implements std::string correctly we have to
285 // use the code above.
286 return a.compare(0, l, pre, l) == 0;
292 bool prefixIs(string const & a, string const & pre)
294 string::size_type const prelen = pre.length();
295 string::size_type const alen = a.length();
297 if (prelen > alen || a.empty())
300 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
301 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
303 return a.compare(0, prelen, pre) == 0;
309 bool suffixIs(string const & a, char c)
311 if (a.empty()) return false;
312 return a[a.length() - 1] == c;
316 bool suffixIs(string const & a, char const * suf)
320 size_t const suflen = strlen(suf);
321 string::size_type const alen = a.length();
326 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
327 // Delete this code when the compilers get a bit better.
328 string tmp(a, alen - suflen);
329 return ::strncmp(tmp.c_str(), suf, suflen) == 0;
331 // This is the code that we really want to use
332 // but until gcc ships with a basic_string that
333 // implements std::string correctly we have to
334 // use the code above.
335 return a.compare(alen - suflen, suflen, suf) == 0;
341 bool suffixIs(string const & a, string const & suf)
343 string::size_type const suflen = suf.length();
344 string::size_type const alen = a.length();
349 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
350 string tmp(a, alen - suflen);
351 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
353 return a.compare(alen - suflen, suflen, suf) == 0;
359 bool contains(char const * a, string const & b)
363 return contains(at, b);
367 bool contains(string const & a, char const * b)
371 return contains(a, bt);
375 bool contains(string const & a, string const & b)
379 return a.find(b) != string::npos;
383 bool contains(string const & a, char b)
387 return a.find(b) != string::npos;
391 bool contains(char const * a, char const * b)
396 return contains(at, bt);
400 bool containsOnly(string const & s, char const * cset)
404 return s.find_first_not_of(cset) == string::npos;
408 bool containsOnly(string const & s, string const & cset)
410 return s.find_first_not_of(cset) == string::npos;
414 bool containsOnly(char const * s, char const * cset)
416 lyx::Assert(s && cset);
418 return string(s).find_first_not_of(cset) == string::npos;
422 bool containsOnly(char const * s, string const & cset)
426 return string(s).find_first_not_of(cset) == string::npos;
430 // ale970405+lasgoutt-970425
431 // rewritten to use new string (Lgb)
432 string const token(string const & a, char delim, int n)
434 if (a.empty()) return string();
436 string::size_type k = 0;
437 string::size_type i = 0;
439 // Find delimiter or end of string
441 if ((i = a.find(delim, i)) == string::npos)
445 // i is now the n'th delim (or string::npos)
446 if (i == string::npos) return string();
447 k = a.find(delim, i);
448 // k is now the n'th + 1 delim (or string::npos)
450 return a.substr(i, k - i);
454 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
455 // rewritten to use new string (Lgb)
456 int tokenPos(string const & a, char delim, string const & tok)
462 while (!str.empty()) {
463 str = split(str, tmptok, delim);
472 bool regexMatch(string const & a, string const & pattern)
474 // We massage the pattern a bit so that the usual
475 // shell pattern we all are used to will work.
476 // One nice thing about using a real regex is that
477 // things like "*.*[^~]" will work also.
478 // build the regex string.
479 string regex(pattern);
480 regex = subst(regex, ".", "\\.");
481 regex = subst(regex, "*", ".*");
482 boost::regex reg(regex);
483 return boost::regex_match(a, reg);
487 string const subst(string const & a, char oldchar, char newchar)
490 string::iterator lit = tmp.begin();
491 string::iterator end = tmp.end();
492 for (; lit != end; ++lit)
493 if ((*lit) == oldchar)
499 string const subst(string const & a,
500 char const * oldstr, string const & newstr)
505 string::size_type i = 0;
506 string::size_type olen = strlen(oldstr);
507 while ((i = lstr.find(oldstr, i)) != string::npos) {
508 lstr.replace(i, olen, newstr);
509 i += newstr.length(); // We need to be sure that we dont
510 // use the same i over and over again.
516 string const subst(string const & a,
517 string const & oldstr, string const & newstr)
520 string::size_type i = 0;
521 string::size_type const olen = oldstr.length();
522 while ((i = lstr.find(oldstr, i)) != string::npos) {
523 lstr.replace(i, olen, newstr);
524 i += newstr.length(); // We need to be sure that we dont
525 // use the same i over and over again.
531 string const strip(string const & a, char c)
533 if (a.empty()) return a;
535 string::size_type i = tmp.find_last_not_of(c);
536 if (i == a.length() - 1) return tmp; // no c's at end of a
537 if (i != string::npos)
538 tmp.erase(i + 1, string::npos);
539 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
540 /// Needed for broken string::find_last_not_of
541 else if (tmp[0] != c) {
542 if (a.length() == 1) return tmp;
543 tmp.erase(1, string::npos);
547 tmp.erase(); // only c in the whole string
552 string const frontStrip(string const & a, char const * p)
556 if (a.empty() || !*p) return a;
558 string::size_type i = tmp.find_first_not_of(p);
565 string const frontStrip(string const & a, char c)
567 if (a.empty()) return a;
569 string::size_type i = tmp.find_first_not_of(c);
576 string const split(string const & a, string & piece, char delim)
579 string::size_type i = a.find(delim);
580 if (i == a.length() - 1) {
581 piece = a.substr(0, i);
582 } else if (i != string::npos) {
583 piece = a.substr(0, i);
584 tmp = a.substr(i + 1);
587 tmp = a.substr(i + 1);
595 string const split(string const & a, char delim)
598 string::size_type i = a.find(delim);
599 if (i != string::npos) // found delim
600 tmp = a.substr(i + 1);
606 string const rsplit(string const & a, string & piece, char delim)
609 string::size_type i = a.rfind(delim);
610 if (i != string::npos) { // delimiter was found
611 piece = a.substr(0, i);
612 tmp = a.substr(i + 1);
613 } else { // delimter was not found
620 // This function escapes 8-bit characters and other problematic
621 // characters that cause problems in latex labels.
622 string const escape(string const & lab)
624 char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
625 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
627 for (string::size_type i = 0; i < lab.length(); ++i) {
628 unsigned char c= lab[i];
629 if (c >= 128 || c == '=' || c == '%') {
631 enc += hexdigit[c>>4];
632 enc += hexdigit[c & 15];
641 /// gives a vector of stringparts which have the delimiter delim
642 vector<string> const getVectorFromString(string const & str,
643 string const & delim)
648 string keys(strip(str));
650 string::size_type const idx = keys.find(delim);
651 if (idx == string::npos) {
652 vec.push_back(frontStrip(keys));
655 string const key = strip(frontStrip(keys.substr(0, idx)));
658 string::size_type const start = idx + delim.size();
659 keys = keys.substr(start);
664 // the same vice versa
665 string const getStringFromVector(vector<string> const & vec,
666 string const & delim)
670 for (vector<string>::const_iterator it = vec.begin();
671 it != vec.end(); ++it) {
672 string item = strip(frontStrip(*it));
673 if (item.empty()) continue;
675 if (i++ > 0) str += delim;