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.
14 #include "support/std_string.h"
17 #include "support/std_sstream.h"
19 #include "BoostFormat.h"
23 #include <boost/regex.hpp>
24 #include <boost/tokenizer.hpp>
35 #ifndef CXX_GLOBAL_CSTD
46 int compare_no_case(string const & s, string const & s2)
48 string::const_iterator p = s.begin();
49 string::const_iterator p2 = s2.begin();
51 while (p != s.end() && p2 != s2.end()) {
52 int const lc1 = tolower(*p);
53 int const lc2 = tolower(*p2);
55 return (lc1 < lc2) ? -1 : 1;
60 if (s.size() == s2.size())
62 if (s.size() < s2.size())
69 int ascii_tolower(int c) {
70 if (c >= 'A' && c <= 'Z')
77 int compare_ascii_no_case(string const & s, string const & s2)
79 string::const_iterator p = s.begin();
80 string::const_iterator p2 = s2.begin();
82 while (p != s.end() && p2 != s2.end()) {
83 int const lc1 = ascii_tolower(*p);
84 int const lc2 = ascii_tolower(*p2);
86 return (lc1 < lc2) ? -1 : 1;
91 if (s.size() == s2.size())
93 if (s.size() < s2.size())
99 int compare_no_case(string const & s, string const & s2, unsigned int len)
101 string::const_iterator p = s.begin();
102 string::const_iterator p2 = s2.begin();
104 while (i < len && p != s.end() && p2 != s2.end()) {
105 int const lc1 = tolower(*p);
106 int const lc2 = tolower(*p2);
108 return (lc1 < lc2) ? -1 : 1;
114 if (s.size() >= len && s2.size() >= len)
116 if (s.size() < s2.size())
122 bool isStrInt(string const & str)
124 if (str.empty()) return false;
126 // Remove leading and trailing white space chars.
127 string const tmpstr = trim(str);
128 if (tmpstr.empty()) return false;
130 string::const_iterator cit = tmpstr.begin();
131 if ((*cit) == '-') ++cit;
132 string::const_iterator end = tmpstr.end();
133 for (; cit != end; ++cit) {
134 if (!isdigit((*cit))) return false;
140 bool isStrUnsignedInt(string const & str)
142 if (str.empty()) return false;
144 // Remove leading and trailing white space chars.
145 string const tmpstr = trim(str);
146 if (tmpstr.empty()) return false;
148 string::const_iterator cit = tmpstr.begin();
149 string::const_iterator end = tmpstr.end();
150 for (; cit != end; ++cit) {
151 if (!isdigit((*cit))) return false;
157 int strToInt(string const & str)
160 // Remove leading and trailing white space chars.
161 string const tmpstr = trim(str);
162 // Do the conversion proper.
170 unsigned int strToUnsignedInt(string const & str)
172 if (isStrUnsignedInt(str)) {
173 // Remove leading and trailing white space chars.
174 string const tmpstr = trim(str);
175 // Do the conversion proper.
183 bool isStrDbl(string const & str)
185 if (str.empty()) return false;
187 // Remove leading and trailing white space chars.
188 string const tmpstr = trim(str);
189 if (tmpstr.empty()) return false;
190 // if (1 < tmpstr.count('.')) return false;
192 string::const_iterator cit = tmpstr.begin();
193 bool found_dot(false);
194 if ((*cit) == '-') ++cit;
195 string::const_iterator end = tmpstr.end();
196 for (; cit != end; ++cit) {
213 double strToDbl(string const & str)
216 // Remove leading and trailing white space chars.
217 string const tmpstr = trim(str);
218 // Do the conversion proper.
219 return ::atof(tmpstr.c_str());
226 char lowercase(char c)
228 return char(tolower(c));
232 char uppercase(char c)
234 return char(toupper(c));
240 // since we cannot use std::tolower and std::toupper directly in the
241 // calls to std::transform yet, we use these helper clases. (Lgb)
243 struct local_lowercase {
244 char operator()(char c) const {
249 struct local_uppercase {
250 char operator()(char c) const {
255 struct local_ascii_lowercase {
256 char operator()(char c) const {
257 return ascii_tolower(c);
261 } // end of anon namespace
263 string const lowercase(string const & a)
266 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
270 string const uppercase(string const & a)
273 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
278 string const ascii_lowercase(string const & a)
281 transform(tmp.begin(), tmp.end(), tmp.begin(),
282 local_ascii_lowercase());
287 bool prefixIs(string const & a, string const & pre)
289 string::size_type const prelen = pre.length();
290 string::size_type const alen = a.length();
292 if (prelen > alen || a.empty())
295 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
296 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
298 return a.compare(0, prelen, pre) == 0;
304 bool suffixIs(string const & a, char c)
306 if (a.empty()) return false;
307 return a[a.length() - 1] == c;
311 bool suffixIs(string const & a, string const & suf)
313 string::size_type const suflen = suf.length();
314 string::size_type const alen = a.length();
319 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
320 string tmp(a, alen - suflen);
321 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
323 return a.compare(alen - suflen, suflen, suf) == 0;
329 bool contains(string const & a, string const & b)
333 return a.find(b) != string::npos;
337 bool contains(string const & a, char b)
341 return a.find(b) != string::npos;
345 bool containsOnly(string const & s, string const & cset)
347 return s.find_first_not_of(cset) == string::npos;
351 // ale970405+lasgoutt-970425
352 // rewritten to use new string (Lgb)
353 string const token(string const & a, char delim, int n)
355 if (a.empty()) return string();
357 string::size_type k = 0;
358 string::size_type i = 0;
360 // Find delimiter or end of string
362 if ((i = a.find(delim, i)) == string::npos)
366 // i is now the n'th delim (or string::npos)
367 if (i == string::npos) return string();
368 k = a.find(delim, i);
369 // k is now the n'th + 1 delim (or string::npos)
371 return a.substr(i, k - i);
375 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
376 // rewritten to use new string (Lgb)
377 int tokenPos(string const & a, char delim, string const & tok)
383 while (!str.empty()) {
384 str = split(str, tmptok, delim);
393 bool regexMatch(string const & a, string const & pattern)
395 // We massage the pattern a bit so that the usual
396 // shell pattern we all are used to will work.
397 // One nice thing about using a real regex is that
398 // things like "*.*[^~]" will work also.
399 // build the regex string.
400 string regex(pattern);
401 regex = subst(regex, ".", "\\.");
402 regex = subst(regex, "*", ".*");
403 boost::regex reg(STRCONV(regex));
404 return boost::regex_match(STRCONV(a), reg);
408 string const subst(string const & a, char oldchar, char newchar)
411 string::iterator lit = tmp.begin();
412 string::iterator end = tmp.end();
413 for (; lit != end; ++lit)
414 if ((*lit) == oldchar)
420 string const subst(string const & a,
421 string const & oldstr, string const & newstr)
424 string::size_type i = 0;
425 string::size_type const olen = oldstr.length();
426 while ((i = lstr.find(oldstr, i)) != string::npos) {
427 lstr.replace(i, olen, newstr);
428 i += newstr.length(); // We need to be sure that we dont
429 // use the same i over and over again.
435 string const trim(string const & a, char const * p)
439 if (a.empty() || !*p)
442 string::size_type r = a.find_last_not_of(p);
443 string::size_type l = a.find_first_not_of(p);
445 // Is this the minimal test? (lgb)
446 if (r == string::npos && l == string::npos)
449 return a.substr(l, r - l + 1);
453 string const rtrim(string const & a, char const * p)
457 if (a.empty() || !*p)
460 string::size_type r = a.find_last_not_of(p);
462 // Is this test really needed? (Lgb)
463 if (r == string::npos)
466 return a.substr(0, r + 1);
470 string const ltrim(string const & a, char const * p)
474 if (a.empty() || !*p)
477 string::size_type l = a.find_first_not_of(p);
479 if (l == string::npos)
482 return a.substr(l, string::npos);
486 string const split(string const & a, string & piece, char delim)
489 string::size_type i = a.find(delim);
490 if (i == a.length() - 1) {
491 piece = a.substr(0, i);
492 } else if (i != string::npos) {
493 piece = a.substr(0, i);
494 tmp = a.substr(i + 1);
497 tmp = a.substr(i + 1);
505 string const split(string const & a, char delim)
508 string::size_type i = a.find(delim);
509 if (i != string::npos) // found delim
510 tmp = a.substr(i + 1);
516 string const rsplit(string const & a, string & piece, char delim)
519 string::size_type i = a.rfind(delim);
520 if (i != string::npos) { // delimiter was found
521 piece = a.substr(0, i);
522 tmp = a.substr(i + 1);
523 } else { // delimiter was not found
530 // This function escapes 8-bit characters and other problematic
531 // characters that cause problems in latex labels.
532 string const escape(string const & lab)
534 char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
535 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
537 for (string::size_type i = 0; i < lab.length(); ++i) {
538 unsigned char c= lab[i];
539 if (c >= 128 || c == '=' || c == '%') {
541 enc += hexdigit[c>>4];
542 enc += hexdigit[c & 15];
551 /// gives a vector of stringparts which have the delimiter delim
552 vector<string> const getVectorFromString(string const & str,
553 string const & delim)
555 // Lars would like this code to go, but for now his replacement (below)
556 // doesn't fullfil the same function. I have, therefore, reactivated the
557 // old code for now. Angus 11 Nov 2002.
562 string keys(rtrim(str));
564 string::size_type const idx = keys.find(delim);
565 if (idx == string::npos) {
566 vec.push_back(ltrim(keys));
569 string const key = trim(keys.substr(0, idx));
572 string::size_type const start = idx + delim.size();
573 keys = keys.substr(start);
577 boost::char_separator<char> sep(delim.c_str());
578 boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
579 #ifndef USE_INCLUDED_STRING
580 return vector<string>(tokens.begin(), tokens.end());
583 using boost::tokenizer;
584 using boost::char_separator;
586 tokenizer<char_separator<char> >::iterator it = tokens.begin();
587 tokenizer<char_separator<char> >::iterator end = tokens.end();
588 for (; it != end; ++it) {
589 vec.push_back(STRCONV((*it)));
597 // the same vice versa
598 string const getStringFromVector(vector<string> const & vec,
599 string const & delim)
603 for (vector<string>::const_iterator it = vec.begin();
604 it != vec.end(); ++it) {
605 string item = trim(*it);
618 string bformat(string const & fmt, string const & arg1)
620 return STRCONV((boost::format(STRCONV(fmt)) % STRCONV(arg1)).str());
624 string bformat(string const & fmt, string const & arg1, string const & arg2)
626 return STRCONV((boost::format(STRCONV(fmt)) % STRCONV(arg1)
627 % STRCONV(arg2)).str());
631 string bformat(string const & fmt, int arg1, int arg2)
633 return STRCONV((boost::format(STRCONV(fmt)) % arg1 % arg2).str());
637 string bformat(string const & fmt, string const & arg1, string const & arg2,
640 return STRCONV((boost::format(STRCONV(fmt)) % STRCONV(arg1)
641 % STRCONV(arg2) % STRCONV(arg3)).str());
645 string bformat(string const & fmt, string const & arg1, string const & arg2,
646 string const & arg3, string const & arg4)
648 return STRCONV((boost::format(STRCONV(fmt)) % STRCONV(arg1)
649 % STRCONV(arg2) % STRCONV(arg3) % STRCONV(arg4)).str());
653 string bformat(string const & fmt, string const & arg1, string const & arg2,
654 string const & arg3, string const & arg4, string const & arg5)
656 return STRCONV((boost::format(STRCONV(fmt)) % STRCONV(arg1)
657 % STRCONV(arg2) % STRCONV(arg3) % STRCONV(arg4)
658 % STRCONV(arg5)).str());
663 string bformat(string const & fmt, string const & arg1)
665 Assert(contains(fmt, "%1$s"));
666 string const str = subst(fmt, "%1$s", arg1);
667 return subst(str, "%%", "%");
671 string bformat(string const & fmt, string const & arg1, string const & arg2)
673 Assert(contains(fmt, "%1$s"));
674 Assert(contains(fmt, "%2$s"));
675 string str = subst(fmt, "%1$s", arg1);
676 str = subst(str, "%2$s", arg2);
677 return subst(str, "%%", "%");
681 string bformat(string const & fmt, int arg1, int arg2)
683 Assert(contains(fmt, "%1$d"));
684 Assert(contains(fmt, "%2$d"));
685 string str = subst(fmt, "%1$d", tostr(arg1));
686 str = subst(str, "%2$d", tostr(arg2));
687 return subst(str, "%%", "%");
691 string bformat(string const & fmt, string const & arg1, string const & arg2,
694 Assert(contains(fmt, "%1$s"));
695 Assert(contains(fmt, "%2$s"));
696 Assert(contains(fmt, "%3$s"));
697 string str = subst(fmt, "%1$s", arg1);
698 str = subst(str, "%2$s", arg2);
699 str = subst(str, "%3$s", arg3);
700 return subst(str, "%%", "%");
704 string bformat(string const & fmt, string const & arg1, string const & arg2,
705 string const & arg3, string const & arg4)
707 Assert(contains(fmt, "%1$s"));
708 Assert(contains(fmt, "%2$s"));
709 Assert(contains(fmt, "%3$s"));
710 Assert(contains(fmt, "%4$s"));
711 string str = subst(fmt, "%1$s", arg1);
712 str = subst(str, "%2$s", arg2);
713 str = subst(str, "%3$s", arg3);
714 str = subst(str, "%4$s", arg4);
715 return subst(str, "%%", "%");
719 string bformat(string const & fmt, string const & arg1, string const & arg2,
720 string const & arg3, string const & arg4, string const & arg5)
722 Assert(contains(fmt, "%1$s"));
723 Assert(contains(fmt, "%2$s"));
724 Assert(contains(fmt, "%3$s"));
725 Assert(contains(fmt, "%4$s"));
726 Assert(contains(fmt, "%5$s"));
727 string str = subst(fmt, "%1$s", arg1);
728 str = subst(str, "%2$s", arg2);
729 str = subst(str, "%3$s", arg3);
730 str = subst(str, "%4$s", arg4);
731 str = subst(str, "%5$s", arg5);
732 return subst(str, "%%", "%");
737 } // namespace support