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>
24 #include <boost/tokenizer.hpp>
35 #ifndef CXX_GLOBAL_CSTD
44 int compare_no_case(string const & s, string const & s2)
46 string::const_iterator p = s.begin();
47 string::const_iterator p2 = s2.begin();
49 while (p != s.end() && p2 != s2.end()) {
50 int const lc1 = tolower(*p);
51 int const lc2 = tolower(*p2);
53 return (lc1 < lc2) ? -1 : 1;
58 if (s.size() == s2.size())
60 if (s.size() < s2.size())
67 int ascii_tolower(int c) {
68 if (c >= 'A' && c <= 'Z')
75 int compare_ascii_no_case(string const & s, string const & s2)
77 string::const_iterator p = s.begin();
78 string::const_iterator p2 = s2.begin();
80 while (p != s.end() && p2 != s2.end()) {
81 int const lc1 = ascii_tolower(*p);
82 int const lc2 = ascii_tolower(*p2);
84 return (lc1 < lc2) ? -1 : 1;
89 if (s.size() == s2.size())
91 if (s.size() < s2.size())
97 int compare_no_case(string const & s, string const & s2, unsigned int len)
99 string::const_iterator p = s.begin();
100 string::const_iterator p2 = s2.begin();
102 while (i < len && p != s.end() && p2 != s2.end()) {
103 int const lc1 = tolower(*p);
104 int const lc2 = tolower(*p2);
106 return (lc1 < lc2) ? -1 : 1;
112 if (s.size() >= len && s2.size() >= len)
114 if (s.size() < s2.size())
120 bool isStrInt(string const & str)
122 if (str.empty()) return false;
124 // Remove leading and trailing white space chars.
125 string const tmpstr = trim(str);
126 if (tmpstr.empty()) return false;
128 string::const_iterator cit = tmpstr.begin();
129 if ((*cit) == '-') ++cit;
130 string::const_iterator end = tmpstr.end();
131 for (; cit != end; ++cit) {
132 if (!isdigit((*cit))) return false;
138 bool isStrUnsignedInt(string const & str)
140 if (str.empty()) return false;
142 // Remove leading and trailing white space chars.
143 string const tmpstr = trim(str);
144 if (tmpstr.empty()) return false;
146 string::const_iterator cit = tmpstr.begin();
147 string::const_iterator end = tmpstr.end();
148 for (; cit != end; ++cit) {
149 if (!isdigit((*cit))) return false;
155 int strToInt(string const & str)
158 // Remove leading and trailing white space chars.
159 string const tmpstr = trim(str);
160 // Do the conversion proper.
161 return lyx::atoi(tmpstr);
168 unsigned int strToUnsignedInt(string const & str)
170 if (isStrUnsignedInt(str)) {
171 // Remove leading and trailing white space chars.
172 string const tmpstr = trim(str);
173 // Do the conversion proper.
174 return lyx::atoi(tmpstr);
181 bool isStrDbl(string const & str)
183 if (str.empty()) return false;
185 // Remove leading and trailing white space chars.
186 string const tmpstr = trim(str);
187 if (tmpstr.empty()) return false;
188 // if (1 < tmpstr.count('.')) return false;
190 string::const_iterator cit = tmpstr.begin();
191 bool found_dot(false);
192 if ((*cit) == '-') ++cit;
193 string::const_iterator end = tmpstr.end();
194 for (; cit != end; ++cit) {
211 double strToDbl(string const & str)
214 // Remove leading and trailing white space chars.
215 string const tmpstr = trim(str);
216 // Do the conversion proper.
217 return ::atof(tmpstr.c_str());
224 char lowercase(char c)
226 return char(tolower(c));
230 char uppercase(char c)
232 return char(toupper(c));
238 // since we cannot use std::tolower and std::toupper directly in the
239 // calls to std::transform yet, we use these helper clases. (Lgb)
241 struct local_lowercase {
242 char operator()(char c) const {
247 struct local_uppercase {
248 char operator()(char c) const {
253 struct local_ascii_lowercase {
254 char operator()(char c) const {
255 return ascii_tolower(c);
259 } // end of anon namespace
261 string const lowercase(string const & a)
264 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
268 string const uppercase(string const & a)
271 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
276 string const ascii_lowercase(string const & a)
279 transform(tmp.begin(), tmp.end(), tmp.begin(),
280 local_ascii_lowercase());
285 bool prefixIs(string const & a, char const * pre)
289 size_t const l = strlen(pre);
290 string::size_type const alen = a.length();
292 if (l > alen || a.empty())
295 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
296 // Delete this code when the compilers get a bit better.
297 return ::strncmp(a.c_str(), pre, l) == 0;
299 // This is the code that we really want to use
300 // but until gcc ships with a basic_string that
301 // implements std::string correctly we have to
302 // use the code above.
303 return a.compare(0, l, pre, l) == 0;
309 bool prefixIs(string const & a, string const & pre)
311 string::size_type const prelen = pre.length();
312 string::size_type const alen = a.length();
314 if (prelen > alen || a.empty())
317 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
318 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
320 return a.compare(0, prelen, pre) == 0;
326 bool suffixIs(string const & a, char c)
328 if (a.empty()) return false;
329 return a[a.length() - 1] == c;
333 bool suffixIs(string const & a, char const * suf)
337 size_t const suflen = strlen(suf);
338 string::size_type const alen = a.length();
343 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
344 // Delete this code when the compilers get a bit better.
345 string tmp(a, alen - suflen);
346 return ::strncmp(tmp.c_str(), suf, suflen) == 0;
348 // This is the code that we really want to use
349 // but until gcc ships with a basic_string that
350 // implements std::string correctly we have to
351 // use the code above.
352 return a.compare(alen - suflen, suflen, suf) == 0;
358 bool suffixIs(string const & a, string const & suf)
360 string::size_type const suflen = suf.length();
361 string::size_type const alen = a.length();
366 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
367 string tmp(a, alen - suflen);
368 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
370 return a.compare(alen - suflen, suflen, suf) == 0;
376 bool contains(string const & a, string const & b)
380 return a.find(b) != string::npos;
384 bool contains(string const & a, char b)
388 return a.find(b) != string::npos;
392 bool containsOnly(string const & s, string const & cset)
394 return s.find_first_not_of(cset) == string::npos;
398 // ale970405+lasgoutt-970425
399 // rewritten to use new string (Lgb)
400 string const token(string const & a, char delim, int n)
402 if (a.empty()) return string();
404 string::size_type k = 0;
405 string::size_type i = 0;
407 // Find delimiter or end of string
409 if ((i = a.find(delim, i)) == string::npos)
413 // i is now the n'th delim (or string::npos)
414 if (i == string::npos) return string();
415 k = a.find(delim, i);
416 // k is now the n'th + 1 delim (or string::npos)
418 return a.substr(i, k - i);
422 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
423 // rewritten to use new string (Lgb)
424 int tokenPos(string const & a, char delim, string const & tok)
430 while (!str.empty()) {
431 str = split(str, tmptok, delim);
440 bool regexMatch(string const & a, string const & pattern)
442 // We massage the pattern a bit so that the usual
443 // shell pattern we all are used to will work.
444 // One nice thing about using a real regex is that
445 // things like "*.*[^~]" will work also.
446 // build the regex string.
447 string regex(pattern);
448 regex = subst(regex, ".", "\\.");
449 regex = subst(regex, "*", ".*");
450 boost::regex reg(regex);
451 return boost::regex_match(a, reg);
455 string const subst(string const & a, char oldchar, char newchar)
458 string::iterator lit = tmp.begin();
459 string::iterator end = tmp.end();
460 for (; lit != end; ++lit)
461 if ((*lit) == oldchar)
467 string const subst(string const & a,
468 char const * oldstr, string const & newstr)
473 string::size_type i = 0;
474 string::size_type olen = strlen(oldstr);
475 while ((i = lstr.find(oldstr, i)) != string::npos) {
476 lstr.replace(i, olen, newstr);
477 i += newstr.length(); // We need to be sure that we dont
478 // use the same i over and over again.
484 string const subst(string const & a,
485 string const & oldstr, string const & newstr)
488 string::size_type i = 0;
489 string::size_type const olen = oldstr.length();
490 while ((i = lstr.find(oldstr, i)) != string::npos) {
491 lstr.replace(i, olen, newstr);
492 i += newstr.length(); // We need to be sure that we dont
493 // use the same i over and over again.
499 string const trim(string const & a, char const * p)
503 if (a.empty() || !*p)
506 string::size_type r = a.find_last_not_of(p);
507 string::size_type l = a.find_first_not_of(p);
509 // Is this the minimal test? (lgb)
510 if (r == string::npos && l == string::npos)
513 return a.substr(l, r - l + 1);
517 string const rtrim(string const & a, char const * p)
521 if (a.empty() || !*p)
524 string::size_type r = a.find_last_not_of(p);
526 // Is this test really needed? (Lgb)
527 if (r == string::npos)
530 return a.substr(0, r + 1);
534 string const ltrim(string const & a, char const * p)
538 if (a.empty() || !*p)
541 string::size_type l = a.find_first_not_of(p);
543 if (l == string::npos)
546 return a.substr(l, string::npos);
550 string const split(string const & a, string & piece, char delim)
553 string::size_type i = a.find(delim);
554 if (i == a.length() - 1) {
555 piece = a.substr(0, i);
556 } else if (i != string::npos) {
557 piece = a.substr(0, i);
558 tmp = a.substr(i + 1);
561 tmp = a.substr(i + 1);
569 string const split(string const & a, char delim)
572 string::size_type i = a.find(delim);
573 if (i != string::npos) // found delim
574 tmp = a.substr(i + 1);
580 string const rsplit(string const & a, string & piece, char delim)
583 string::size_type i = a.rfind(delim);
584 if (i != string::npos) { // delimiter was found
585 piece = a.substr(0, i);
586 tmp = a.substr(i + 1);
587 } else { // delimter was not found
594 // This function escapes 8-bit characters and other problematic
595 // characters that cause problems in latex labels.
596 string const escape(string const & lab)
598 char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
599 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
601 for (string::size_type i = 0; i < lab.length(); ++i) {
602 unsigned char c= lab[i];
603 if (c >= 128 || c == '=' || c == '%') {
605 enc += hexdigit[c>>4];
606 enc += hexdigit[c & 15];
615 /// gives a vector of stringparts which have the delimiter delim
616 vector<string> const getVectorFromString(string const & str,
617 string const & delim)
623 string keys(rtrim(str));
625 string::size_type const idx = keys.find(delim);
626 if (idx == string::npos) {
627 vec.push_back(ltrim(keys));
630 string const key = trim(keys.substr(0, idx));
633 string::size_type const start = idx + delim.size();
634 keys = keys.substr(start);
638 boost::char_separator<char> sep(delim.c_str());
639 boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
640 return vector<string>(tokens.begin(), tokens.end());
645 // the same vice versa
646 string const getStringFromVector(vector<string> const & vec,
647 string const & delim)
651 for (vector<string>::const_iterator it = vec.begin();
652 it != vec.end(); ++it) {
653 string item = trim(*it);
654 if (item.empty()) continue;
656 if (i++ > 0) str += delim;