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/lstrings.h"
15 #include "support/lyxlib.h"
16 #include "support/convert.h"
20 #include <boost/tokenizer.hpp>
21 #include <boost/assert.hpp>
23 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
25 #include <boost/format.hpp>
41 #ifndef CXX_GLOBAL_CSTD
51 int compare_no_case(string const & s, string const & s2)
53 string::const_iterator p = s.begin();
54 string::const_iterator p2 = s2.begin();
56 while (p != s.end() && p2 != s2.end()) {
57 int const lc1 = tolower(*p);
58 int const lc2 = tolower(*p2);
60 return (lc1 < lc2) ? -1 : 1;
65 if (s.size() == s2.size())
67 if (s.size() < s2.size())
73 int compare_no_case(docstring const & s, docstring const & s2)
75 docstring::const_iterator p = s.begin();
76 docstring::const_iterator p2 = s2.begin();
78 while (p != s.end() && p2 != s2.end()) {
79 int const lc1 = tolower(*p);
80 int const lc2 = tolower(*p2);
82 return (lc1 < lc2) ? -1 : 1;
87 if (s.size() == s2.size())
89 if (s.size() < s2.size())
97 int ascii_tolower(int c) {
98 if (c >= 'A' && c <= 'Z')
104 template<typename String> inline
105 int do_compare_ascii_no_case(String const & s, String const & s2)
107 typename String::const_iterator p = s.begin();
108 typename String::const_iterator p2 = s2.begin();
110 while (p != s.end() && p2 != s2.end()) {
111 int const lc1 = ascii_tolower(*p);
112 int const lc2 = ascii_tolower(*p2);
114 return (lc1 < lc2) ? -1 : 1;
119 if (s.size() == s2.size())
121 if (s.size() < s2.size())
129 int compare_ascii_no_case(string const & s, string const & s2)
131 return do_compare_ascii_no_case(s, s2);
135 int compare_ascii_no_case(docstring const & s, docstring const & s2)
137 return do_compare_ascii_no_case(s, s2);
141 int compare_no_case(string const & s, string const & s2, unsigned int len)
143 string::const_iterator p = s.begin();
144 string::const_iterator p2 = s2.begin();
146 while (i < len && p != s.end() && p2 != s2.end()) {
147 int const lc1 = tolower(*p);
148 int const lc2 = tolower(*p2);
150 return (lc1 < lc2) ? -1 : 1;
156 if (s.size() >= len && s2.size() >= len)
158 if (s.size() < s2.size())
164 bool isStrInt(string const & str)
166 if (str.empty()) return false;
168 // Remove leading and trailing white space chars.
169 string const tmpstr = trim(str);
170 if (tmpstr.empty()) return false;
172 string::const_iterator cit = tmpstr.begin();
173 if ((*cit) == '-') ++cit;
174 string::const_iterator end = tmpstr.end();
175 for (; cit != end; ++cit) {
176 if (!isdigit((*cit))) return false;
182 bool isStrUnsignedInt(string const & str)
184 if (str.empty()) return false;
186 // Remove leading and trailing white space chars.
187 string const tmpstr = trim(str);
188 if (tmpstr.empty()) return false;
190 string::const_iterator cit = tmpstr.begin();
191 string::const_iterator end = tmpstr.end();
192 for (; cit != end; ++cit) {
193 if (!isdigit((*cit))) return false;
199 bool isStrDbl(string const & str)
201 if (str.empty()) return false;
203 // Remove leading and trailing white space chars.
204 string const tmpstr = trim(str);
205 if (tmpstr.empty()) return false;
206 // if (1 < tmpstr.count('.')) return false;
208 string::const_iterator cit = tmpstr.begin();
209 bool found_dot(false);
210 if ((*cit) == '-') ++cit;
211 string::const_iterator end = tmpstr.end();
212 for (; cit != end; ++cit) {
232 bool isHexChar(char_type c)
244 c == 'a' || c == 'A' ||
245 c == 'b' || c == 'B' ||
246 c == 'c' || c == 'C' ||
247 c == 'd' || c == 'D' ||
248 c == 'e' || c == 'E' ||
249 c == 'f' || c == 'F';
255 bool isHex(docstring const & str)
259 if (str.length() > 2 && str[0] == '0' &&
260 (str[1] == 'x' || str[1] == 'X'))
263 int const len = str.length();
265 for (; index < len; ++index) {
266 if (!isHexChar(str[index]))
273 int hexToInt(docstring const & str)
275 string s = to_ascii(str);
277 sscanf(s.c_str(), "%x", &h);
282 char lowercase(char c)
284 return char(tolower(c));
288 char uppercase(char c)
290 return char(toupper(c));
293 // FIXME for lowercase() and uppercase() function below:
294 // 1) std::tolower() and std::toupper() are templates that
295 // compile fine with char_type. With the test (c >= 256) we
296 // do not trust these function to do the right thing with
298 // 2) these functions use the current locale, which is wrong
299 // if it is not latin1 based (latin1 is a subset of UCS4).
301 char_type lowercase(char_type c)
310 char_type uppercase(char_type c)
321 // since we cannot use std::tolower and std::toupper directly in the
322 // calls to std::transform yet, we use these helper clases. (Lgb)
324 struct local_lowercase {
325 char operator()(char c) const {
330 struct local_uppercase {
331 char operator()(char c) const {
336 struct local_ascii_lowercase {
337 char operator()(char c) const {
338 return ascii_tolower(c);
342 } // end of anon namespace
344 string const lowercase(string const & a)
347 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
351 string const uppercase(string const & a)
354 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
359 string const ascii_lowercase(string const & a)
362 transform(tmp.begin(), tmp.end(), tmp.begin(),
363 local_ascii_lowercase());
368 bool prefixIs(string const & a, string const & pre)
370 string::size_type const prelen = pre.length();
371 string::size_type const alen = a.length();
373 if (prelen > alen || a.empty())
376 #if defined(STD_STRING_IS_GOOD)
377 return a.compare(0, prelen, pre) == 0;
379 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
385 bool prefixIs(docstring const & a, docstring const & pre)
387 docstring::size_type const prelen = pre.length();
388 docstring::size_type const alen = a.length();
390 if (prelen > alen || a.empty())
393 return a.compare(0, prelen, pre) == 0;
397 bool suffixIs(string const & a, char c)
399 if (a.empty()) return false;
400 return a[a.length() - 1] == c;
404 bool suffixIs(string const & a, string const & suf)
406 string::size_type const suflen = suf.length();
407 string::size_type const alen = a.length();
412 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
413 string tmp(a, alen - suflen);
414 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
416 return a.compare(alen - suflen, suflen, suf) == 0;
422 bool containsOnly(string const & s, string const & cset)
424 return s.find_first_not_of(cset) == string::npos;
428 // ale970405+lasgoutt-970425
429 // rewritten to use new string (Lgb)
430 string const token(string const & a, char delim, int n)
432 if (a.empty()) return string();
434 string::size_type k = 0;
435 string::size_type i = 0;
437 // Find delimiter or end of string
439 if ((i = a.find(delim, i)) == string::npos)
443 // i is now the n'th delim (or string::npos)
444 if (i == string::npos) return string();
445 k = a.find(delim, i);
446 // k is now the n'th + 1 delim (or string::npos)
448 return a.substr(i, k - i);
452 docstring const token(docstring const & a, char_type delim, int n)
454 if (a.empty()) return docstring();
456 string::size_type k = 0;
457 string::size_type i = 0;
459 // Find delimiter or end of string
461 if ((i = a.find(delim, i)) == docstring::npos)
465 // i is now the n'th delim (or string::npos)
466 if (i == docstring::npos) return docstring();
467 k = a.find(delim, i);
468 // k is now the n'th + 1 delim (or string::npos)
470 return a.substr(i, k - i);
474 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
475 // rewritten to use new string (Lgb)
476 int tokenPos(string const & a, char delim, string const & tok)
482 while (!str.empty()) {
483 str = split(str, tmptok, delim);
494 /// Substitute all \a oldchar with \a newchar
495 template<typename Ch> inline
496 std::basic_string<Ch> const subst_char(std::basic_string<Ch> const & a,
497 Ch oldchar, Ch newchar)
499 typedef std::basic_string<Ch> String;
501 typename String::iterator lit = tmp.begin();
502 typename String::iterator end = tmp.end();
503 for (; lit != end; ++lit)
504 if ((*lit) == oldchar)
510 /// substitutes all instances of \a oldstr with \a newstr
511 template<typename String> inline
512 String const subst_string(String const & a,
513 String const & oldstr, String const & newstr)
515 BOOST_ASSERT(!oldstr.empty());
517 typename String::size_type i = 0;
518 typename String::size_type const olen = oldstr.length();
519 while ((i = lstr.find(oldstr, i)) != string::npos) {
520 lstr.replace(i, olen, newstr);
521 i += newstr.length(); // We need to be sure that we dont
522 // use the same i over and over again.
530 string const subst(string const & a, char oldchar, char newchar)
532 return subst_char(a, oldchar, newchar);
536 docstring const subst(docstring const & a,
537 char_type oldchar, char_type newchar)
539 return subst_char(a, oldchar, newchar);
543 string const subst(string const & a,
544 string const & oldstr, string const & newstr)
546 return subst_string(a, oldstr, newstr);
550 docstring const subst(docstring const & a,
551 docstring const & oldstr, docstring const & newstr)
553 return subst_string(a, oldstr, newstr);
557 docstring const trim(docstring const & a, char const * p)
561 if (a.empty() || !*p)
564 docstring s = lyx::from_ascii(p);
565 docstring::size_type r = a.find_last_not_of(s);
566 docstring::size_type l = a.find_first_not_of(s);
568 // Is this the minimal test? (lgb)
569 if (r == docstring::npos && l == docstring::npos)
572 return a.substr(l, r - l + 1);
576 string const trim(string const & a, char const * p)
580 if (a.empty() || !*p)
583 string::size_type r = a.find_last_not_of(p);
584 string::size_type l = a.find_first_not_of(p);
586 // Is this the minimal test? (lgb)
587 if (r == string::npos && l == string::npos)
590 return a.substr(l, r - l + 1);
594 string const rtrim(string const & a, char const * p)
598 if (a.empty() || !*p)
601 string::size_type r = a.find_last_not_of(p);
603 // Is this test really needed? (Lgb)
604 if (r == string::npos)
607 return a.substr(0, r + 1);
611 string const ltrim(string const & a, char const * p)
614 if (a.empty() || !*p)
616 string::size_type l = a.find_first_not_of(p);
617 if (l == string::npos)
619 return a.substr(l, string::npos);
623 docstring const ltrim(docstring const & a, char const * p)
626 if (a.empty() || !*p)
628 size_t l = a.find_first_not_of(from_ascii(p));
629 if (l == docstring::npos)
631 return a.substr(l, docstring::npos);
636 template<typename String, typename Char> inline
637 String const doSplit(String const & a, String & piece, Char delim)
640 typename String::size_type i = a.find(delim);
641 if (i == a.length() - 1) {
642 piece = a.substr(0, i);
643 } else if (i != String::npos) {
644 piece = a.substr(0, i);
645 tmp = a.substr(i + 1);
648 tmp = a.substr(i + 1);
658 string const split(string const & a, string & piece, char delim)
660 return doSplit(a, piece, delim);
664 docstring const split(docstring const & a, docstring & piece, char_type delim)
666 return doSplit(a, piece, delim);
670 string const split(string const & a, char delim)
673 string::size_type i = a.find(delim);
674 if (i != string::npos) // found delim
675 tmp = a.substr(i + 1);
681 string const rsplit(string const & a, string & piece, char delim)
684 string::size_type i = a.rfind(delim);
685 if (i != string::npos) { // delimiter was found
686 piece = a.substr(0, i);
687 tmp = a.substr(i + 1);
688 } else { // delimiter was not found
695 // This function escapes 8-bit characters and other problematic
696 // characters that cause problems in latex labels.
697 docstring const escape(docstring const & lab)
699 lyx::char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
700 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
702 for (docstring::size_type i = 0; i < lab.length(); ++i) {
703 lyx::char_type c = lab[i];
704 // FIXME We must change the following algorithm for UCS4
705 // chars, but that will be a file format change.
706 if (c >= 128 || c == '=' || c == '%') {
708 enc += hexdigit[c>>4];
709 enc += hexdigit[c & 15];
718 /// gives a vector of stringparts which have the delimiter delim
719 vector<string> const getVectorFromString(string const & str,
720 string const & delim)
722 // Lars would like this code to go, but for now his replacement (below)
723 // doesn't fullfil the same function. I have, therefore, reactivated the
724 // old code for now. Angus 11 Nov 2002.
729 string keys = rtrim(str);
731 string::size_type const idx = keys.find(delim);
732 if (idx == string::npos) {
733 vec.push_back(ltrim(keys));
736 string const key = trim(keys.substr(0, idx));
739 string::size_type const start = idx + delim.size();
740 keys = keys.substr(start);
744 boost::char_separator<char> sep(delim.c_str());
745 boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
746 return vector<string>(tokens.begin(), tokens.end());
751 // the same vice versa
752 string const getStringFromVector(vector<string> const & vec,
753 string const & delim)
757 for (vector<string>::const_iterator it = vec.begin();
758 it != vec.end(); ++it) {
759 string item = trim(*it);
770 int findToken(char const * const str[], string const & search_token)
774 while (str[i][0] && str[i] != search_token)
782 docstring const externalLineEnding(docstring const & str)
784 #if defined(__APPLE__)
785 // The MAC clipboard uses \r for lineendings, and we use \n
786 return subst(str, '\n', '\r');
787 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
788 // Windows clipboard uses \r\n for lineendings, and we use \n
789 return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
796 docstring const internalLineEnding(docstring const & str)
798 docstring const s = subst(str,
799 lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
800 return subst(s, '\r', '\n');
804 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
808 docstring bformat(docstring const & fmt, int arg1)
810 return (boost::basic_format<char_type>(fmt) % arg1).str();
815 docstring bformat(docstring const & fmt, long arg1)
817 return (boost::basic_format<char_type>(fmt) % arg1).str();
822 docstring bformat(docstring const & fmt, unsigned int arg1)
824 return (boost::basic_format<char_type>(fmt) % arg1).str();
829 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
831 return (boost::basic_format<char_type>(fmt) % arg1).str();
836 docstring bformat(docstring const & fmt, char * arg1)
838 return (boost::basic_format<char_type>(fmt) % arg1).str();
843 docstring bformat(docstring const & fmt, int arg1, int arg2)
845 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
850 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
852 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
857 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
859 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
864 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
866 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
871 docstring bformat(docstring const & fmt,
872 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
874 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
880 docstring bformat(docstring const & fmt, int arg1)
882 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
883 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
884 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
889 docstring bformat(docstring const & fmt, long arg1)
891 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
892 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
893 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
898 docstring bformat(docstring const & fmt, unsigned int arg1)
900 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
901 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
902 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
907 docstring bformat(docstring const & fmt, docstring arg1)
909 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
910 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
911 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
916 docstring bformat(docstring const & fmt, char * arg1)
918 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
919 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
920 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
925 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
927 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
928 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
929 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
930 str = subst(str, lyx::from_ascii("%2$s"), arg2);
931 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
936 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
938 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
939 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
940 docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
941 str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
942 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
947 docstring bformat(docstring const & fmt, int arg1, int arg2)
949 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
950 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
951 docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
952 str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
953 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
958 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
960 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
961 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
962 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
963 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
964 str = subst(str, lyx::from_ascii("%2$s"), arg2);
965 str = subst(str, lyx::from_ascii("%3$s"), arg3);
966 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
971 docstring bformat(docstring const & fmt,
972 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
974 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
975 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
976 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
977 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
978 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
979 str = subst(str, lyx::from_ascii("%2$s"), arg2);
980 str = subst(str, lyx::from_ascii("%3$s"), arg3);
981 str = subst(str, lyx::from_ascii("%4$s"), arg4);
982 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
988 } // namespace support