3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
9 * Full author contact details are available in file CREDITS
15 #pragma implementation
23 #include <boost/regex.hpp>
34 #ifndef CXX_GLOBAL_CSTD
43 int compare_no_case(string const & s, string const & s2)
45 string::const_iterator p = s.begin();
46 string::const_iterator p2 = s2.begin();
48 while (p != s.end() && p2 != s2.end()) {
49 int const lc1 = tolower(*p);
50 int const lc2 = tolower(*p2);
52 return (lc1 < lc2) ? -1 : 1;
57 if (s.size() == s2.size())
59 if (s.size() < s2.size())
66 int ascii_tolower(int c) {
67 if (c >= 'A' && c <= 'Z')
74 int compare_ascii_no_case(string const & s, string const & s2)
76 string::const_iterator p = s.begin();
77 string::const_iterator p2 = s2.begin();
79 while (p != s.end() && p2 != s2.end()) {
80 int const lc1 = ascii_tolower(*p);
81 int const lc2 = ascii_tolower(*p2);
83 return (lc1 < lc2) ? -1 : 1;
88 if (s.size() == s2.size())
90 if (s.size() < s2.size())
96 int compare_no_case(string const & s, string const & s2, unsigned int len)
98 string::const_iterator p = s.begin();
99 string::const_iterator p2 = s2.begin();
101 while (i < len && p != s.end() && p2 != s2.end()) {
102 int const lc1 = tolower(*p);
103 int const lc2 = tolower(*p2);
105 return (lc1 < lc2) ? -1 : 1;
111 if (s.size() >= len && s2.size() >= len)
113 if (s.size() < s2.size())
119 bool isStrInt(string const & str)
121 if (str.empty()) return false;
123 // Remove leading and trailing white space chars.
124 string const tmpstr = trim(str);
125 if (tmpstr.empty()) return false;
127 string::const_iterator cit = tmpstr.begin();
128 if ((*cit) == '-') ++cit;
129 string::const_iterator end = tmpstr.end();
130 for (; cit != end; ++cit) {
131 if (!isdigit((*cit))) return false;
137 bool isStrUnsignedInt(string const & str)
139 if (str.empty()) return false;
141 // Remove leading and trailing white space chars.
142 string const tmpstr = trim(str);
143 if (tmpstr.empty()) return false;
145 string::const_iterator cit = tmpstr.begin();
146 string::const_iterator end = tmpstr.end();
147 for (; cit != end; ++cit) {
148 if (!isdigit((*cit))) return false;
154 int strToInt(string const & str)
157 // Remove leading and trailing white space chars.
158 string const tmpstr = trim(str);
159 // Do the conversion proper.
160 return lyx::atoi(tmpstr);
167 unsigned int strToUnsignedInt(string const & str)
169 if (isStrUnsignedInt(str)) {
170 // Remove leading and trailing white space chars.
171 string const tmpstr = trim(str);
172 // Do the conversion proper.
173 return lyx::atoi(tmpstr);
180 bool isStrDbl(string const & str)
182 if (str.empty()) return false;
184 // Remove leading and trailing white space chars.
185 string const tmpstr = trim(str);
186 if (tmpstr.empty()) return false;
187 // if (1 < tmpstr.count('.')) return false;
189 string::const_iterator cit = tmpstr.begin();
190 bool found_dot(false);
191 if ((*cit) == '-') ++cit;
192 string::const_iterator end = tmpstr.end();
193 for (; cit != end; ++cit) {
210 double strToDbl(string const & str)
213 // Remove leading and trailing white space chars.
214 string const tmpstr = trim(str);
215 // Do the conversion proper.
216 return ::atof(tmpstr.c_str());
223 char lowercase(char c)
225 return char(tolower(c));
229 char uppercase(char c)
231 return char(toupper(c));
237 // since we cannot use std::tolower and std::toupper directly in the
238 // calls to std::transform yet, we use these helper clases. (Lgb)
240 struct local_lowercase {
241 char operator()(char c) const {
246 struct local_uppercase {
247 char operator()(char c) const {
252 struct local_ascii_lowercase {
253 char operator()(char c) const {
254 return ascii_tolower(c);
258 } // end of anon namespace
260 string const lowercase(string const & a)
263 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
267 string const uppercase(string const & a)
270 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
275 string const ascii_lowercase(string const & a)
278 transform(tmp.begin(), tmp.end(), tmp.begin(),
279 local_ascii_lowercase());
284 bool prefixIs(string const & a, char const * pre)
288 size_t const l = strlen(pre);
289 string::size_type const alen = a.length();
291 if (l > alen || a.empty())
294 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
295 // Delete this code when the compilers get a bit better.
296 return ::strncmp(a.c_str(), pre, l) == 0;
298 // This is the code that we really want to use
299 // but until gcc ships with a basic_string that
300 // implements std::string correctly we have to
301 // use the code above.
302 return a.compare(0, l, pre, l) == 0;
308 bool prefixIs(string const & a, string const & pre)
310 string::size_type const prelen = pre.length();
311 string::size_type const alen = a.length();
313 if (prelen > alen || a.empty())
316 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
317 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
319 return a.compare(0, prelen, pre) == 0;
325 bool suffixIs(string const & a, char c)
327 if (a.empty()) return false;
328 return a[a.length() - 1] == c;
332 bool suffixIs(string const & a, char const * suf)
336 size_t const suflen = strlen(suf);
337 string::size_type const alen = a.length();
342 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
343 // Delete this code when the compilers get a bit better.
344 string tmp(a, alen - suflen);
345 return ::strncmp(tmp.c_str(), suf, suflen) == 0;
347 // This is the code that we really want to use
348 // but until gcc ships with a basic_string that
349 // implements std::string correctly we have to
350 // use the code above.
351 return a.compare(alen - suflen, suflen, suf) == 0;
357 bool suffixIs(string const & a, string const & suf)
359 string::size_type const suflen = suf.length();
360 string::size_type const alen = a.length();
365 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
366 string tmp(a, alen - suflen);
367 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
369 return a.compare(alen - suflen, suflen, suf) == 0;
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 containsOnly(string const & s, string const & cset)
393 return s.find_first_not_of(cset) == string::npos;
397 // ale970405+lasgoutt-970425
398 // rewritten to use new string (Lgb)
399 string const token(string const & a, char delim, int n)
401 if (a.empty()) return string();
403 string::size_type k = 0;
404 string::size_type i = 0;
406 // Find delimiter or end of string
408 if ((i = a.find(delim, i)) == string::npos)
412 // i is now the n'th delim (or string::npos)
413 if (i == string::npos) return string();
414 k = a.find(delim, i);
415 // k is now the n'th + 1 delim (or string::npos)
417 return a.substr(i, k - i);
421 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
422 // rewritten to use new string (Lgb)
423 int tokenPos(string const & a, char delim, string const & tok)
429 while (!str.empty()) {
430 str = split(str, tmptok, delim);
439 bool regexMatch(string const & a, string const & pattern)
441 // We massage the pattern a bit so that the usual
442 // shell pattern we all are used to will work.
443 // One nice thing about using a real regex is that
444 // things like "*.*[^~]" will work also.
445 // build the regex string.
446 string regex(pattern);
447 regex = subst(regex, ".", "\\.");
448 regex = subst(regex, "*", ".*");
449 boost::regex reg(regex);
450 return boost::regex_match(a, reg);
454 string const subst(string const & a, char oldchar, char newchar)
457 string::iterator lit = tmp.begin();
458 string::iterator end = tmp.end();
459 for (; lit != end; ++lit)
460 if ((*lit) == oldchar)
466 string const subst(string const & a,
467 char const * oldstr, string const & newstr)
472 string::size_type i = 0;
473 string::size_type olen = strlen(oldstr);
474 while ((i = lstr.find(oldstr, i)) != string::npos) {
475 lstr.replace(i, olen, newstr);
476 i += newstr.length(); // We need to be sure that we dont
477 // use the same i over and over again.
483 string const subst(string const & a,
484 string const & oldstr, string const & newstr)
487 string::size_type i = 0;
488 string::size_type const olen = oldstr.length();
489 while ((i = lstr.find(oldstr, i)) != string::npos) {
490 lstr.replace(i, olen, newstr);
491 i += newstr.length(); // We need to be sure that we dont
492 // use the same i over and over again.
498 string const trim(string const & a, char const * p)
502 if (a.empty() || !*p)
505 string::size_type r = a.find_last_not_of(p);
506 string::size_type l = a.find_first_not_of(p);
508 // Is this the minimal test? (lgb)
509 if (r == string::npos && l == string::npos)
512 return a.substr(l, r - l + 1);
516 string const rtrim(string const & a, char const * p)
520 if (a.empty() || !*p)
523 string::size_type r = a.find_last_not_of(p);
525 // Is this test really needed? (Lgb)
526 if (r == string::npos)
529 return a.substr(0, r + 1);
533 string const ltrim(string const & a, char const * p)
537 if (a.empty() || !*p)
540 string::size_type l = a.find_first_not_of(p);
542 if (l == string::npos)
545 return a.substr(l, string::npos);
549 string const split(string const & a, string & piece, char delim)
552 string::size_type i = a.find(delim);
553 if (i == a.length() - 1) {
554 piece = a.substr(0, i);
555 } else if (i != string::npos) {
556 piece = a.substr(0, i);
557 tmp = a.substr(i + 1);
560 tmp = a.substr(i + 1);
568 string const split(string const & a, char delim)
571 string::size_type i = a.find(delim);
572 if (i != string::npos) // found delim
573 tmp = a.substr(i + 1);
579 string const rsplit(string const & a, string & piece, char delim)
582 string::size_type i = a.rfind(delim);
583 if (i != string::npos) { // delimiter was found
584 piece = a.substr(0, i);
585 tmp = a.substr(i + 1);
586 } else { // delimter was not found
593 // This function escapes 8-bit characters and other problematic
594 // characters that cause problems in latex labels.
595 string const escape(string const & lab)
597 char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
598 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
600 for (string::size_type i = 0; i < lab.length(); ++i) {
601 unsigned char c= lab[i];
602 if (c >= 128 || c == '=' || c == '%') {
604 enc += hexdigit[c>>4];
605 enc += hexdigit[c & 15];
614 /// gives a vector of stringparts which have the delimiter delim
615 vector<string> const getVectorFromString(string const & str,
616 string const & delim)
621 string keys(rtrim(str));
623 string::size_type const idx = keys.find(delim);
624 if (idx == string::npos) {
625 vec.push_back(ltrim(keys));
628 string const key = trim(keys.substr(0, idx));
631 string::size_type const start = idx + delim.size();
632 keys = keys.substr(start);
637 // the same vice versa
638 string const getStringFromVector(vector<string> const & vec,
639 string const & delim)
643 for (vector<string>::const_iterator it = vec.begin();
644 it != vec.end(); ++it) {
645 string item = trim(*it);
646 if (item.empty()) continue;
648 if (i++ > 0) str += delim;