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) {
229 char lowercase(char c)
231 return char(tolower(c));
235 char uppercase(char c)
237 return char(toupper(c));
240 // FIXME for lowercase() and uppercase() function below:
241 // 1) std::tolower() and std::toupper() are templates that
242 // compile fine with char_type. With the test (c >= 256) we
243 // do not trust these function to do the right thing with
245 // 2) these functions use the current locale, which is wrong
246 // if it is not latin1 based (latin1 is a subset of UCS4).
248 char_type lowercase(char_type c)
257 char_type uppercase(char_type c)
268 // since we cannot use std::tolower and std::toupper directly in the
269 // calls to std::transform yet, we use these helper clases. (Lgb)
271 struct local_lowercase {
272 char operator()(char c) const {
277 struct local_uppercase {
278 char operator()(char c) const {
283 struct local_ascii_lowercase {
284 char operator()(char c) const {
285 return ascii_tolower(c);
289 } // end of anon namespace
291 string const lowercase(string const & a)
294 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
298 string const uppercase(string const & a)
301 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
306 string const ascii_lowercase(string const & a)
309 transform(tmp.begin(), tmp.end(), tmp.begin(),
310 local_ascii_lowercase());
315 bool prefixIs(string const & a, string const & pre)
317 string::size_type const prelen = pre.length();
318 string::size_type const alen = a.length();
320 if (prelen > alen || a.empty())
323 #if defined(STD_STRING_IS_GOOD)
324 return a.compare(0, prelen, pre) == 0;
326 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
332 bool suffixIs(string const & a, char c)
334 if (a.empty()) return false;
335 return a[a.length() - 1] == c;
339 bool suffixIs(string const & a, string const & suf)
341 string::size_type const suflen = suf.length();
342 string::size_type const alen = a.length();
347 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
348 string tmp(a, alen - suflen);
349 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
351 return a.compare(alen - suflen, suflen, suf) == 0;
357 bool containsOnly(string const & s, string const & cset)
359 return s.find_first_not_of(cset) == string::npos;
363 // ale970405+lasgoutt-970425
364 // rewritten to use new string (Lgb)
365 string const token(string const & a, char delim, int n)
367 if (a.empty()) return string();
369 string::size_type k = 0;
370 string::size_type i = 0;
372 // Find delimiter or end of string
374 if ((i = a.find(delim, i)) == string::npos)
378 // i is now the n'th delim (or string::npos)
379 if (i == string::npos) return string();
380 k = a.find(delim, i);
381 // k is now the n'th + 1 delim (or string::npos)
383 return a.substr(i, k - i);
387 docstring const token(docstring const & a, char_type delim, int n)
389 if (a.empty()) return docstring();
391 string::size_type k = 0;
392 string::size_type i = 0;
394 // Find delimiter or end of string
396 if ((i = a.find(delim, i)) == docstring::npos)
400 // i is now the n'th delim (or string::npos)
401 if (i == docstring::npos) return docstring();
402 k = a.find(delim, i);
403 // k is now the n'th + 1 delim (or string::npos)
405 return a.substr(i, k - i);
409 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
410 // rewritten to use new string (Lgb)
411 int tokenPos(string const & a, char delim, string const & tok)
417 while (!str.empty()) {
418 str = split(str, tmptok, delim);
429 /// Substitute all \a oldchar with \a newchar
430 template<typename Ch> inline
431 std::basic_string<Ch> const subst_char(std::basic_string<Ch> const & a,
432 Ch oldchar, Ch newchar)
434 typedef std::basic_string<Ch> String;
436 typename String::iterator lit = tmp.begin();
437 typename String::iterator end = tmp.end();
438 for (; lit != end; ++lit)
439 if ((*lit) == oldchar)
445 /// substitutes all instances of \a oldstr with \a newstr
446 template<typename String> inline
447 String const subst_string(String const & a,
448 String const & oldstr, String const & newstr)
450 BOOST_ASSERT(!oldstr.empty());
452 typename String::size_type i = 0;
453 typename String::size_type const olen = oldstr.length();
454 while ((i = lstr.find(oldstr, i)) != string::npos) {
455 lstr.replace(i, olen, newstr);
456 i += newstr.length(); // We need to be sure that we dont
457 // use the same i over and over again.
465 string const subst(string const & a, char oldchar, char newchar)
467 return subst_char(a, oldchar, newchar);
471 docstring const subst(docstring const & a,
472 char_type oldchar, char_type newchar)
474 return subst_char(a, oldchar, newchar);
478 string const subst(string const & a,
479 string const & oldstr, string const & newstr)
481 return subst_string(a, oldstr, newstr);
485 docstring const subst(docstring const & a,
486 docstring const & oldstr, docstring const & newstr)
488 return subst_string(a, oldstr, newstr);
492 docstring const trim(docstring const & a, char const * p)
496 if (a.empty() || !*p)
499 docstring s = lyx::from_ascii(p);
500 docstring::size_type r = a.find_last_not_of(s);
501 docstring::size_type l = a.find_first_not_of(s);
503 // Is this the minimal test? (lgb)
504 if (r == docstring::npos && l == docstring::npos)
507 return a.substr(l, r - l + 1);
511 string const trim(string const & a, char const * p)
515 if (a.empty() || !*p)
518 string::size_type r = a.find_last_not_of(p);
519 string::size_type l = a.find_first_not_of(p);
521 // Is this the minimal test? (lgb)
522 if (r == string::npos && l == string::npos)
525 return a.substr(l, r - l + 1);
529 string const rtrim(string const & a, char const * p)
533 if (a.empty() || !*p)
536 string::size_type r = a.find_last_not_of(p);
538 // Is this test really needed? (Lgb)
539 if (r == string::npos)
542 return a.substr(0, r + 1);
546 string const ltrim(string const & a, char const * p)
550 if (a.empty() || !*p)
553 string::size_type l = a.find_first_not_of(p);
555 if (l == string::npos)
558 return a.substr(l, string::npos);
564 template<typename String, typename Char> inline
565 String const doSplit(String const & a, String & piece, Char delim)
568 typename String::size_type i = a.find(delim);
569 if (i == a.length() - 1) {
570 piece = a.substr(0, i);
571 } else if (i != String::npos) {
572 piece = a.substr(0, i);
573 tmp = a.substr(i + 1);
576 tmp = a.substr(i + 1);
586 string const split(string const & a, string & piece, char delim)
588 return doSplit(a, piece, delim);
592 docstring const split(docstring const & a, docstring & piece, char_type delim)
594 return doSplit(a, piece, delim);
598 string const split(string const & a, char delim)
601 string::size_type i = a.find(delim);
602 if (i != string::npos) // found delim
603 tmp = a.substr(i + 1);
609 string const rsplit(string const & a, string & piece, char delim)
612 string::size_type i = a.rfind(delim);
613 if (i != string::npos) { // delimiter was found
614 piece = a.substr(0, i);
615 tmp = a.substr(i + 1);
616 } else { // delimiter was not found
623 // This function escapes 8-bit characters and other problematic
624 // characters that cause problems in latex labels.
625 docstring const escape(docstring const & lab)
627 lyx::char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
628 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
630 for (docstring::size_type i = 0; i < lab.length(); ++i) {
631 lyx::char_type c= lab[i];
632 // FIXME We must change the following algorithm for UCS4
633 // chars, but that will be a file format change.
634 if (c >= 128 || c == '=' || c == '%') {
636 enc += hexdigit[c>>4];
637 enc += hexdigit[c & 15];
646 /// gives a vector of stringparts which have the delimiter delim
647 vector<string> const getVectorFromString(string const & str,
648 string const & delim)
650 // Lars would like this code to go, but for now his replacement (below)
651 // doesn't fullfil the same function. I have, therefore, reactivated the
652 // old code for now. Angus 11 Nov 2002.
657 string keys = rtrim(str);
659 string::size_type const idx = keys.find(delim);
660 if (idx == string::npos) {
661 vec.push_back(ltrim(keys));
664 string const key = trim(keys.substr(0, idx));
667 string::size_type const start = idx + delim.size();
668 keys = keys.substr(start);
672 boost::char_separator<char> sep(delim.c_str());
673 boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
674 return vector<string>(tokens.begin(), tokens.end());
679 // the same vice versa
680 string const getStringFromVector(vector<string> const & vec,
681 string const & delim)
685 for (vector<string>::const_iterator it = vec.begin();
686 it != vec.end(); ++it) {
687 string item = trim(*it);
698 int findToken(char const * const str[], string const & search_token)
702 while (str[i][0] && str[i] != search_token)
710 docstring const externalLineEnding(docstring const & str)
712 #if defined(__APPLE__)
713 // The MAC clipboard uses \r for lineendings, and we use \n
714 return subst(str, '\n', '\r');
715 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
716 // Windows clipboard uses \r\n for lineendings, and we use \n
717 return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
724 docstring const internalLineEnding(docstring const & str)
726 docstring const s = subst(str,
727 lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
728 return subst(s, '\r', '\n');
732 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
736 docstring bformat(docstring const & fmt, int arg1)
738 return (boost::basic_format<char_type>(fmt) % arg1).str();
743 docstring bformat(docstring const & fmt, long arg1)
745 return (boost::basic_format<char_type>(fmt) % arg1).str();
750 docstring bformat(docstring const & fmt, unsigned int arg1)
752 return (boost::basic_format<char_type>(fmt) % arg1).str();
757 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
759 return (boost::basic_format<char_type>(fmt) % arg1).str();
764 docstring bformat(docstring const & fmt, char * arg1)
766 return (boost::basic_format<char_type>(fmt) % arg1).str();
771 docstring bformat(docstring const & fmt, int arg1, int arg2)
773 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
778 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
780 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
785 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
787 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
792 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
794 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
799 docstring bformat(docstring const & fmt,
800 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
802 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
808 docstring bformat(docstring const & fmt, int arg1)
810 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
811 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
812 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
817 docstring bformat(docstring const & fmt, long arg1)
819 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
820 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
821 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
826 docstring bformat(docstring const & fmt, unsigned int arg1)
828 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
829 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
830 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
835 docstring bformat(docstring const & fmt, docstring arg1)
837 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
838 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
839 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
844 docstring bformat(docstring const & fmt, char * arg1)
846 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
847 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
848 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
853 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
855 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
856 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
857 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
858 str = subst(str, lyx::from_ascii("%2$s"), arg2);
859 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
864 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
866 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
867 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
868 docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
869 str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
870 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
875 docstring bformat(docstring const & fmt, int arg1, int arg2)
877 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
878 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
879 docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
880 str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
881 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
886 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
888 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
889 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
890 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
891 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
892 str = subst(str, lyx::from_ascii("%2$s"), arg2);
893 str = subst(str, lyx::from_ascii("%3$s"), arg3);
894 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
899 docstring bformat(docstring const & fmt,
900 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
902 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
903 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
904 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
905 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
906 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
907 str = subst(str, lyx::from_ascii("%2$s"), arg2);
908 str = subst(str, lyx::from_ascii("%3$s"), arg3);
909 str = subst(str, lyx::from_ascii("%4$s"), arg4);
910 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
916 } // namespace support