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
18 #include "BoostFormat.h"
20 #include <boost/regex.hpp>
21 #include <boost/tokenizer.hpp>
32 #ifndef CXX_GLOBAL_CSTD
41 int compare_no_case(string const & s, string const & s2)
43 string::const_iterator p = s.begin();
44 string::const_iterator p2 = s2.begin();
46 while (p != s.end() && p2 != s2.end()) {
47 int const lc1 = tolower(*p);
48 int const lc2 = tolower(*p2);
50 return (lc1 < lc2) ? -1 : 1;
55 if (s.size() == s2.size())
57 if (s.size() < s2.size())
64 int ascii_tolower(int c) {
65 if (c >= 'A' && c <= 'Z')
72 int compare_ascii_no_case(string const & s, string const & s2)
74 string::const_iterator p = s.begin();
75 string::const_iterator p2 = s2.begin();
77 while (p != s.end() && p2 != s2.end()) {
78 int const lc1 = ascii_tolower(*p);
79 int const lc2 = ascii_tolower(*p2);
81 return (lc1 < lc2) ? -1 : 1;
86 if (s.size() == s2.size())
88 if (s.size() < s2.size())
94 int compare_no_case(string const & s, string const & s2, unsigned int len)
96 string::const_iterator p = s.begin();
97 string::const_iterator p2 = s2.begin();
99 while (i < len && p != s.end() && p2 != s2.end()) {
100 int const lc1 = tolower(*p);
101 int const lc2 = tolower(*p2);
103 return (lc1 < lc2) ? -1 : 1;
109 if (s.size() >= len && s2.size() >= len)
111 if (s.size() < s2.size())
117 bool isStrInt(string const & str)
119 if (str.empty()) return false;
121 // Remove leading and trailing white space chars.
122 string const tmpstr = trim(str);
123 if (tmpstr.empty()) return false;
125 string::const_iterator cit = tmpstr.begin();
126 if ((*cit) == '-') ++cit;
127 string::const_iterator end = tmpstr.end();
128 for (; cit != end; ++cit) {
129 if (!isdigit((*cit))) return false;
135 bool isStrUnsignedInt(string const & str)
137 if (str.empty()) return false;
139 // Remove leading and trailing white space chars.
140 string const tmpstr = trim(str);
141 if (tmpstr.empty()) return false;
143 string::const_iterator cit = tmpstr.begin();
144 string::const_iterator end = tmpstr.end();
145 for (; cit != end; ++cit) {
146 if (!isdigit((*cit))) return false;
152 int strToInt(string const & str)
155 // Remove leading and trailing white space chars.
156 string const tmpstr = trim(str);
157 // Do the conversion proper.
158 return lyx::atoi(tmpstr);
165 unsigned int strToUnsignedInt(string const & str)
167 if (isStrUnsignedInt(str)) {
168 // Remove leading and trailing white space chars.
169 string const tmpstr = trim(str);
170 // Do the conversion proper.
171 return lyx::atoi(tmpstr);
178 bool isStrDbl(string const & str)
180 if (str.empty()) return false;
182 // Remove leading and trailing white space chars.
183 string const tmpstr = trim(str);
184 if (tmpstr.empty()) return false;
185 // if (1 < tmpstr.count('.')) return false;
187 string::const_iterator cit = tmpstr.begin();
188 bool found_dot(false);
189 if ((*cit) == '-') ++cit;
190 string::const_iterator end = tmpstr.end();
191 for (; cit != end; ++cit) {
208 double strToDbl(string const & str)
211 // Remove leading and trailing white space chars.
212 string const tmpstr = trim(str);
213 // Do the conversion proper.
214 return ::atof(tmpstr.c_str());
221 char lowercase(char c)
223 return char(tolower(c));
227 char uppercase(char c)
229 return char(toupper(c));
235 // since we cannot use std::tolower and std::toupper directly in the
236 // calls to std::transform yet, we use these helper clases. (Lgb)
238 struct local_lowercase {
239 char operator()(char c) const {
244 struct local_uppercase {
245 char operator()(char c) const {
250 struct local_ascii_lowercase {
251 char operator()(char c) const {
252 return ascii_tolower(c);
256 } // end of anon namespace
258 string const lowercase(string const & a)
261 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
265 string const uppercase(string const & a)
268 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
273 string const ascii_lowercase(string const & a)
276 transform(tmp.begin(), tmp.end(), tmp.begin(),
277 local_ascii_lowercase());
282 bool prefixIs(string const & a, char const * pre)
286 size_t const l = strlen(pre);
287 string::size_type const alen = a.length();
289 if (l > alen || a.empty())
292 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
293 // Delete this code when the compilers get a bit better.
294 return ::strncmp(a.c_str(), pre, l) == 0;
296 // This is the code that we really want to use
297 // but until gcc ships with a basic_string that
298 // implements std::string correctly we have to
299 // use the code above.
300 return a.compare(0, l, pre, l) == 0;
306 bool prefixIs(string const & a, string const & pre)
308 string::size_type const prelen = pre.length();
309 string::size_type const alen = a.length();
311 if (prelen > alen || a.empty())
314 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
315 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
317 return a.compare(0, prelen, pre) == 0;
323 bool suffixIs(string const & a, char c)
325 if (a.empty()) return false;
326 return a[a.length() - 1] == c;
330 bool suffixIs(string const & a, char const * suf)
334 size_t const suflen = strlen(suf);
335 string::size_type const alen = a.length();
340 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
341 // Delete this code when the compilers get a bit better.
342 string tmp(a, alen - suflen);
343 return ::strncmp(tmp.c_str(), suf, suflen) == 0;
345 // This is the code that we really want to use
346 // but until gcc ships with a basic_string that
347 // implements std::string correctly we have to
348 // use the code above.
349 return a.compare(alen - suflen, suflen, suf) == 0;
355 bool suffixIs(string const & a, string const & suf)
357 string::size_type const suflen = suf.length();
358 string::size_type const alen = a.length();
363 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
364 string tmp(a, alen - suflen);
365 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
367 return a.compare(alen - suflen, suflen, suf) == 0;
373 bool contains(string const & a, string const & b)
377 return a.find(b) != string::npos;
381 bool contains(string const & a, char b)
385 return a.find(b) != string::npos;
389 bool containsOnly(string const & s, string const & cset)
391 return s.find_first_not_of(cset) == string::npos;
395 // ale970405+lasgoutt-970425
396 // rewritten to use new string (Lgb)
397 string const token(string const & a, char delim, int n)
399 if (a.empty()) return string();
401 string::size_type k = 0;
402 string::size_type i = 0;
404 // Find delimiter or end of string
406 if ((i = a.find(delim, i)) == string::npos)
410 // i is now the n'th delim (or string::npos)
411 if (i == string::npos) return string();
412 k = a.find(delim, i);
413 // k is now the n'th + 1 delim (or string::npos)
415 return a.substr(i, k - i);
419 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
420 // rewritten to use new string (Lgb)
421 int tokenPos(string const & a, char delim, string const & tok)
427 while (!str.empty()) {
428 str = split(str, tmptok, delim);
437 bool regexMatch(string const & a, string const & pattern)
439 // We massage the pattern a bit so that the usual
440 // shell pattern we all are used to will work.
441 // One nice thing about using a real regex is that
442 // things like "*.*[^~]" will work also.
443 // build the regex string.
444 string regex(pattern);
445 regex = subst(regex, ".", "\\.");
446 regex = subst(regex, "*", ".*");
447 boost::regex reg(STRCONV(regex));
448 return boost::regex_match(STRCONV(a), reg);
452 string const subst(string const & a, char oldchar, char newchar)
455 string::iterator lit = tmp.begin();
456 string::iterator end = tmp.end();
457 for (; lit != end; ++lit)
458 if ((*lit) == oldchar)
464 string const subst(string const & a,
465 char const * oldstr, string const & newstr)
470 string::size_type i = 0;
471 string::size_type olen = strlen(oldstr);
472 while ((i = lstr.find(oldstr, i)) != string::npos) {
473 lstr.replace(i, olen, newstr);
474 i += newstr.length(); // We need to be sure that we dont
475 // use the same i over and over again.
481 string const subst(string const & a,
482 string const & oldstr, string const & newstr)
485 string::size_type i = 0;
486 string::size_type const olen = oldstr.length();
487 while ((i = lstr.find(oldstr, i)) != string::npos) {
488 lstr.replace(i, olen, newstr);
489 i += newstr.length(); // We need to be sure that we dont
490 // use the same i over and over again.
496 string const trim(string const & a, char const * p)
500 if (a.empty() || !*p)
503 string::size_type r = a.find_last_not_of(p);
504 string::size_type l = a.find_first_not_of(p);
506 // Is this the minimal test? (lgb)
507 if (r == string::npos && l == string::npos)
510 return a.substr(l, r - l + 1);
514 string const rtrim(string const & a, char const * p)
518 if (a.empty() || !*p)
521 string::size_type r = a.find_last_not_of(p);
523 // Is this test really needed? (Lgb)
524 if (r == string::npos)
527 return a.substr(0, r + 1);
531 string const ltrim(string const & a, char const * p)
535 if (a.empty() || !*p)
538 string::size_type l = a.find_first_not_of(p);
540 if (l == string::npos)
543 return a.substr(l, string::npos);
547 string const split(string const & a, string & piece, char delim)
550 string::size_type i = a.find(delim);
551 if (i == a.length() - 1) {
552 piece = a.substr(0, i);
553 } else if (i != string::npos) {
554 piece = a.substr(0, i);
555 tmp = a.substr(i + 1);
558 tmp = a.substr(i + 1);
566 string const split(string const & a, char delim)
569 string::size_type i = a.find(delim);
570 if (i != string::npos) // found delim
571 tmp = a.substr(i + 1);
577 string const rsplit(string const & a, string & piece, char delim)
580 string::size_type i = a.rfind(delim);
581 if (i != string::npos) { // delimiter was found
582 piece = a.substr(0, i);
583 tmp = a.substr(i + 1);
584 } else { // delimter was not found
591 // This function escapes 8-bit characters and other problematic
592 // characters that cause problems in latex labels.
593 string const escape(string const & lab)
595 char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
596 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
598 for (string::size_type i = 0; i < lab.length(); ++i) {
599 unsigned char c= lab[i];
600 if (c >= 128 || c == '=' || c == '%') {
602 enc += hexdigit[c>>4];
603 enc += hexdigit[c & 15];
612 /// gives a vector of stringparts which have the delimiter delim
613 vector<string> const getVectorFromString(string const & str,
614 string const & delim)
616 // Lars would like this code to go, but for now his replacement (below)
617 // doesn't fullfil the same function. I have, therefore, reactivated the
618 // old code for now. Angus 11 Nov 2002.
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 #ifndef USE_INCLUDED_STRING
641 return vector<string>(tokens.begin(), tokens.end());
644 using boost::tokenizer;
645 using boost::char_separator;
647 tokenizer<char_separator<char> >::iterator it = tokens.begin();
648 tokenizer<char_separator<char> >::iterator end = tokens.end();
649 for (; it != end; ++it) {
650 vec.push_back(STRCONV((*it)));
658 // the same vice versa
659 string const getStringFromVector(vector<string> const & vec,
660 string const & delim)
664 for (vector<string>::const_iterator it = vec.begin();
665 it != vec.end(); ++it) {
666 string item = trim(*it);
667 if (item.empty()) continue;
669 if (i++ > 0) str += delim;
678 string bformat(char const * fmt, string const & arg1)
680 return STRCONV((boost::format(fmt) % STRCONV(arg1)).str());
684 string bformat(char const * fmt, string const & arg1, string const & arg2)
686 return STRCONV((boost::format(fmt) % STRCONV(arg1) % STRCONV(arg2)).str());
689 string bformat(char const * fmt, string const & arg1, string const & arg2,
690 string const & arg3, string const & arg4)
692 return STRCONV((boost::format(fmt) % STRCONV(arg1) % STRCONV(arg2)
693 % STRCONV(arg3) % STRCONV(arg4)).str());
698 string bformat(char const * fmt, string const & arg1)
700 Assert(contains(fmt, "%1$s"));
701 string const str = subst(fmt, "%1$s", arg1);
702 return subst(str, "%%", "%");
706 string bformat(char const * fmt, string const & arg1, string const & arg2)
708 Assert(contains(fmt, "%1$s"));
709 Assert(contains(fmt, "%2$s"));
710 string str = subst(fmt, "%1$s", arg1);
711 str = subst(str, "%2$s", arg2);
712 return subst(str, "%%", "%");
716 string bformat(char const * fmt, string const & arg1, string const & arg2,
717 string const & arg3, string const & arg4)
719 Assert(contains(fmt, "%1$s"));
720 Assert(contains(fmt, "%2$s"));
721 Assert(contains(fmt, "%3$s"));
722 Assert(contains(fmt, "%4$s"));
723 string str = subst(fmt, "%1$s", arg1);
724 str = subst(str, "%2$s", arg2);
725 str = subst(str, "%3$s", arg3);
726 str = subst(str, "%4$s", arg4);
727 return subst(str, "%%", "%");