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
19 #include "BoostFormat.h"
21 #include <boost/regex.hpp>
22 #include <boost/tokenizer.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 = trim(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 = trim(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 = trim(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 = trim(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 = trim(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 = trim(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 struct local_ascii_lowercase {
252 char operator()(char c) const {
253 return ascii_tolower(c);
257 } // end of anon namespace
259 string const lowercase(string const & a)
262 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
266 string const uppercase(string const & a)
269 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
274 string const ascii_lowercase(string const & a)
277 transform(tmp.begin(), tmp.end(), tmp.begin(),
278 local_ascii_lowercase());
283 bool prefixIs(string const & a, string const & pre)
285 string::size_type const prelen = pre.length();
286 string::size_type const alen = a.length();
288 if (prelen > alen || a.empty())
291 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
292 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
294 return a.compare(0, prelen, pre) == 0;
300 bool suffixIs(string const & a, char c)
302 if (a.empty()) return false;
303 return a[a.length() - 1] == c;
307 bool suffixIs(string const & a, string const & suf)
309 string::size_type const suflen = suf.length();
310 string::size_type const alen = a.length();
315 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
316 string tmp(a, alen - suflen);
317 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
319 return a.compare(alen - suflen, suflen, suf) == 0;
325 bool contains(string const & a, string const & b)
329 return a.find(b) != string::npos;
333 bool contains(string const & a, char b)
337 return a.find(b) != string::npos;
341 bool containsOnly(string const & s, string const & cset)
343 return s.find_first_not_of(cset) == string::npos;
347 // ale970405+lasgoutt-970425
348 // rewritten to use new string (Lgb)
349 string const token(string const & a, char delim, int n)
351 if (a.empty()) return string();
353 string::size_type k = 0;
354 string::size_type i = 0;
356 // Find delimiter or end of string
358 if ((i = a.find(delim, i)) == string::npos)
362 // i is now the n'th delim (or string::npos)
363 if (i == string::npos) return string();
364 k = a.find(delim, i);
365 // k is now the n'th + 1 delim (or string::npos)
367 return a.substr(i, k - i);
371 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
372 // rewritten to use new string (Lgb)
373 int tokenPos(string const & a, char delim, string const & tok)
379 while (!str.empty()) {
380 str = split(str, tmptok, delim);
389 bool regexMatch(string const & a, string const & pattern)
391 // We massage the pattern a bit so that the usual
392 // shell pattern we all are used to will work.
393 // One nice thing about using a real regex is that
394 // things like "*.*[^~]" will work also.
395 // build the regex string.
396 string regex(pattern);
397 regex = subst(regex, ".", "\\.");
398 regex = subst(regex, "*", ".*");
399 boost::regex reg(STRCONV(regex));
400 return boost::regex_match(STRCONV(a), reg);
404 string const subst(string const & a, char oldchar, char newchar)
407 string::iterator lit = tmp.begin();
408 string::iterator end = tmp.end();
409 for (; lit != end; ++lit)
410 if ((*lit) == oldchar)
416 string const subst(string const & a,
417 string const & oldstr, string const & newstr)
420 string::size_type i = 0;
421 string::size_type const olen = oldstr.length();
422 while ((i = lstr.find(oldstr, i)) != string::npos) {
423 lstr.replace(i, olen, newstr);
424 i += newstr.length(); // We need to be sure that we dont
425 // use the same i over and over again.
431 string const trim(string const & a, char const * p)
435 if (a.empty() || !*p)
438 string::size_type r = a.find_last_not_of(p);
439 string::size_type l = a.find_first_not_of(p);
441 // Is this the minimal test? (lgb)
442 if (r == string::npos && l == string::npos)
445 return a.substr(l, r - l + 1);
449 string const rtrim(string const & a, char const * p)
453 if (a.empty() || !*p)
456 string::size_type r = a.find_last_not_of(p);
458 // Is this test really needed? (Lgb)
459 if (r == string::npos)
462 return a.substr(0, r + 1);
466 string const ltrim(string const & a, char const * p)
470 if (a.empty() || !*p)
473 string::size_type l = a.find_first_not_of(p);
475 if (l == string::npos)
478 return a.substr(l, string::npos);
482 string const split(string const & a, string & piece, char delim)
485 string::size_type i = a.find(delim);
486 if (i == a.length() - 1) {
487 piece = a.substr(0, i);
488 } else if (i != string::npos) {
489 piece = a.substr(0, i);
490 tmp = a.substr(i + 1);
493 tmp = a.substr(i + 1);
501 string const split(string const & a, char delim)
504 string::size_type i = a.find(delim);
505 if (i != string::npos) // found delim
506 tmp = a.substr(i + 1);
512 string const rsplit(string const & a, string & piece, char delim)
515 string::size_type i = a.rfind(delim);
516 if (i != string::npos) { // delimiter was found
517 piece = a.substr(0, i);
518 tmp = a.substr(i + 1);
519 } else { // delimiter was not found
526 // This function escapes 8-bit characters and other problematic
527 // characters that cause problems in latex labels.
528 string const escape(string const & lab)
530 char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
531 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
533 for (string::size_type i = 0; i < lab.length(); ++i) {
534 unsigned char c= lab[i];
535 if (c >= 128 || c == '=' || c == '%') {
537 enc += hexdigit[c>>4];
538 enc += hexdigit[c & 15];
547 /// gives a vector of stringparts which have the delimiter delim
548 vector<string> const getVectorFromString(string const & str,
549 string const & delim)
551 // Lars would like this code to go, but for now his replacement (below)
552 // doesn't fullfil the same function. I have, therefore, reactivated the
553 // old code for now. Angus 11 Nov 2002.
558 string keys(rtrim(str));
560 string::size_type const idx = keys.find(delim);
561 if (idx == string::npos) {
562 vec.push_back(ltrim(keys));
565 string const key = trim(keys.substr(0, idx));
568 string::size_type const start = idx + delim.size();
569 keys = keys.substr(start);
573 boost::char_separator<char> sep(delim.c_str());
574 boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
575 #ifndef USE_INCLUDED_STRING
576 return vector<string>(tokens.begin(), tokens.end());
579 using boost::tokenizer;
580 using boost::char_separator;
582 tokenizer<char_separator<char> >::iterator it = tokens.begin();
583 tokenizer<char_separator<char> >::iterator end = tokens.end();
584 for (; it != end; ++it) {
585 vec.push_back(STRCONV((*it)));
593 // the same vice versa
594 string const getStringFromVector(vector<string> const & vec,
595 string const & delim)
599 for (vector<string>::const_iterator it = vec.begin();
600 it != vec.end(); ++it) {
601 string item = trim(*it);
614 string bformat(string const & fmt, string const & arg1)
616 return STRCONV((boost::format(fmt) % STRCONV(arg1)).str());
620 string bformat(string const & fmt, string const & arg1, string const & arg2)
622 return STRCONV((boost::format(fmt) % STRCONV(arg1) % STRCONV(arg2)).str());
626 string bformat(string const & fmt, string const & arg1, string const & arg2,
629 return STRCONV((boost::format(fmt) % STRCONV(arg1) % STRCONV(arg2)
630 % STRCONV(arg3)).str());
634 string bformat(string const & fmt, string const & arg1, string const & arg2,
635 string const & arg3, string const & arg4)
637 return STRCONV((boost::format(fmt) % STRCONV(arg1) % STRCONV(arg2)
638 % STRCONV(arg3) % STRCONV(arg4)).str());
643 string bformat(string const & fmt, string const & arg1)
645 lyx::Assert(contains(fmt, "%1$s"));
646 string const str = subst(fmt, "%1$s", arg1);
647 return subst(str, "%%", "%");
651 string bformat(string const & fmt, string const & arg1, string const & arg2)
653 lyx::Assert(contains(fmt, "%1$s"));
654 lyx::Assert(contains(fmt, "%2$s"));
655 string str = subst(fmt, "%1$s", arg1);
656 str = subst(str, "%2$s", arg2);
657 return subst(str, "%%", "%");
661 string bformat(string const & fmt, string const & arg1, string const & arg2,
664 lyx::Assert(contains(fmt, "%1$s"));
665 lyx::Assert(contains(fmt, "%2$s"));
666 lyx::Assert(contains(fmt, "%3$s"));
667 string str = subst(fmt, "%1$s", arg1);
668 str = subst(str, "%2$s", arg2);
669 str = subst(str, "%3$s", arg3);
670 return subst(str, "%%", "%");
674 string bformat(string const & fmt, string const & arg1, string const & arg2,
675 string const & arg3, string const & arg4)
677 lyx::Assert(contains(fmt, "%1$s"));
678 lyx::Assert(contains(fmt, "%2$s"));
679 lyx::Assert(contains(fmt, "%3$s"));
680 lyx::Assert(contains(fmt, "%4$s"));
681 string str = subst(fmt, "%1$s", arg1);
682 str = subst(str, "%2$s", arg2);
683 str = subst(str, "%3$s", arg3);
684 str = subst(str, "%4$s", arg4);
685 return subst(str, "%%", "%");