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 prefixIs(docstring const & a, docstring const & pre)
334 docstring::size_type const prelen = pre.length();
335 docstring::size_type const alen = a.length();
337 if (prelen > alen || a.empty())
340 return a.compare(0, prelen, pre) == 0;
344 bool suffixIs(string const & a, char c)
346 if (a.empty()) return false;
347 return a[a.length() - 1] == c;
351 bool suffixIs(string const & a, string const & suf)
353 string::size_type const suflen = suf.length();
354 string::size_type const alen = a.length();
359 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
360 string tmp(a, alen - suflen);
361 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
363 return a.compare(alen - suflen, suflen, suf) == 0;
369 bool containsOnly(string const & s, string const & cset)
371 return s.find_first_not_of(cset) == string::npos;
375 // ale970405+lasgoutt-970425
376 // rewritten to use new string (Lgb)
377 string const token(string const & a, char delim, int n)
379 if (a.empty()) return string();
381 string::size_type k = 0;
382 string::size_type i = 0;
384 // Find delimiter or end of string
386 if ((i = a.find(delim, i)) == string::npos)
390 // i is now the n'th delim (or string::npos)
391 if (i == string::npos) return string();
392 k = a.find(delim, i);
393 // k is now the n'th + 1 delim (or string::npos)
395 return a.substr(i, k - i);
399 docstring const token(docstring const & a, char_type delim, int n)
401 if (a.empty()) return docstring();
403 string::size_type k = 0;
404 string::size_type i = 0;
406 // Find delimiter or end of string
408 if ((i = a.find(delim, i)) == docstring::npos)
412 // i is now the n'th delim (or string::npos)
413 if (i == docstring::npos) return docstring();
414 k = a.find(delim, i);
415 // k is now the n'th + 1 delim (or string::npos)
417 return a.substr(i, k - i);
421 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
422 // rewritten to use new string (Lgb)
423 int tokenPos(string const & a, char delim, string const & tok)
429 while (!str.empty()) {
430 str = split(str, tmptok, delim);
441 /// Substitute all \a oldchar with \a newchar
442 template<typename Ch> inline
443 std::basic_string<Ch> const subst_char(std::basic_string<Ch> const & a,
444 Ch oldchar, Ch newchar)
446 typedef std::basic_string<Ch> String;
448 typename String::iterator lit = tmp.begin();
449 typename String::iterator end = tmp.end();
450 for (; lit != end; ++lit)
451 if ((*lit) == oldchar)
457 /// substitutes all instances of \a oldstr with \a newstr
458 template<typename String> inline
459 String const subst_string(String const & a,
460 String const & oldstr, String const & newstr)
462 BOOST_ASSERT(!oldstr.empty());
464 typename String::size_type i = 0;
465 typename String::size_type const olen = oldstr.length();
466 while ((i = lstr.find(oldstr, i)) != string::npos) {
467 lstr.replace(i, olen, newstr);
468 i += newstr.length(); // We need to be sure that we dont
469 // use the same i over and over again.
477 string const subst(string const & a, char oldchar, char newchar)
479 return subst_char(a, oldchar, newchar);
483 docstring const subst(docstring const & a,
484 char_type oldchar, char_type newchar)
486 return subst_char(a, oldchar, newchar);
490 string const subst(string const & a,
491 string const & oldstr, string const & newstr)
493 return subst_string(a, oldstr, newstr);
497 docstring const subst(docstring const & a,
498 docstring const & oldstr, docstring const & newstr)
500 return subst_string(a, oldstr, newstr);
504 docstring const trim(docstring const & a, char const * p)
508 if (a.empty() || !*p)
511 docstring s = lyx::from_ascii(p);
512 docstring::size_type r = a.find_last_not_of(s);
513 docstring::size_type l = a.find_first_not_of(s);
515 // Is this the minimal test? (lgb)
516 if (r == docstring::npos && l == docstring::npos)
519 return a.substr(l, r - l + 1);
523 string const trim(string const & a, char const * p)
527 if (a.empty() || !*p)
530 string::size_type r = a.find_last_not_of(p);
531 string::size_type l = a.find_first_not_of(p);
533 // Is this the minimal test? (lgb)
534 if (r == string::npos && l == string::npos)
537 return a.substr(l, r - l + 1);
541 string const rtrim(string const & a, char const * p)
545 if (a.empty() || !*p)
548 string::size_type r = a.find_last_not_of(p);
550 // Is this test really needed? (Lgb)
551 if (r == string::npos)
554 return a.substr(0, r + 1);
558 string const ltrim(string const & a, char const * p)
562 if (a.empty() || !*p)
565 string::size_type l = a.find_first_not_of(p);
567 if (l == string::npos)
570 return a.substr(l, string::npos);
576 template<typename String, typename Char> inline
577 String const doSplit(String const & a, String & piece, Char delim)
580 typename String::size_type i = a.find(delim);
581 if (i == a.length() - 1) {
582 piece = a.substr(0, i);
583 } else if (i != String::npos) {
584 piece = a.substr(0, i);
585 tmp = a.substr(i + 1);
588 tmp = a.substr(i + 1);
598 string const split(string const & a, string & piece, char delim)
600 return doSplit(a, piece, delim);
604 docstring const split(docstring const & a, docstring & piece, char_type delim)
606 return doSplit(a, piece, delim);
610 string const split(string const & a, char delim)
613 string::size_type i = a.find(delim);
614 if (i != string::npos) // found delim
615 tmp = a.substr(i + 1);
621 string const rsplit(string const & a, string & piece, char delim)
624 string::size_type i = a.rfind(delim);
625 if (i != string::npos) { // delimiter was found
626 piece = a.substr(0, i);
627 tmp = a.substr(i + 1);
628 } else { // delimiter was not found
635 // This function escapes 8-bit characters and other problematic
636 // characters that cause problems in latex labels.
637 docstring const escape(docstring const & lab)
639 lyx::char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
640 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
642 for (docstring::size_type i = 0; i < lab.length(); ++i) {
643 lyx::char_type c= lab[i];
644 // FIXME We must change the following algorithm for UCS4
645 // chars, but that will be a file format change.
646 if (c >= 128 || c == '=' || c == '%') {
648 enc += hexdigit[c>>4];
649 enc += hexdigit[c & 15];
658 /// gives a vector of stringparts which have the delimiter delim
659 vector<string> const getVectorFromString(string const & str,
660 string const & delim)
662 // Lars would like this code to go, but for now his replacement (below)
663 // doesn't fullfil the same function. I have, therefore, reactivated the
664 // old code for now. Angus 11 Nov 2002.
669 string keys = rtrim(str);
671 string::size_type const idx = keys.find(delim);
672 if (idx == string::npos) {
673 vec.push_back(ltrim(keys));
676 string const key = trim(keys.substr(0, idx));
679 string::size_type const start = idx + delim.size();
680 keys = keys.substr(start);
684 boost::char_separator<char> sep(delim.c_str());
685 boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
686 return vector<string>(tokens.begin(), tokens.end());
691 // the same vice versa
692 string const getStringFromVector(vector<string> const & vec,
693 string const & delim)
697 for (vector<string>::const_iterator it = vec.begin();
698 it != vec.end(); ++it) {
699 string item = trim(*it);
710 int findToken(char const * const str[], string const & search_token)
714 while (str[i][0] && str[i] != search_token)
722 docstring const externalLineEnding(docstring const & str)
724 #if defined(__APPLE__)
725 // The MAC clipboard uses \r for lineendings, and we use \n
726 return subst(str, '\n', '\r');
727 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
728 // Windows clipboard uses \r\n for lineendings, and we use \n
729 return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
736 docstring const internalLineEnding(docstring const & str)
738 docstring const s = subst(str,
739 lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
740 return subst(s, '\r', '\n');
744 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
748 docstring bformat(docstring const & fmt, int arg1)
750 return (boost::basic_format<char_type>(fmt) % arg1).str();
755 docstring bformat(docstring const & fmt, long arg1)
757 return (boost::basic_format<char_type>(fmt) % arg1).str();
762 docstring bformat(docstring const & fmt, unsigned int arg1)
764 return (boost::basic_format<char_type>(fmt) % arg1).str();
769 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
771 return (boost::basic_format<char_type>(fmt) % arg1).str();
776 docstring bformat(docstring const & fmt, char * arg1)
778 return (boost::basic_format<char_type>(fmt) % arg1).str();
783 docstring bformat(docstring const & fmt, int arg1, int arg2)
785 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
790 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
792 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
797 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
799 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
804 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
806 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
811 docstring bformat(docstring const & fmt,
812 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
814 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
820 docstring bformat(docstring const & fmt, int arg1)
822 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
823 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
824 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
829 docstring bformat(docstring const & fmt, long arg1)
831 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
832 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
833 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
838 docstring bformat(docstring const & fmt, unsigned int arg1)
840 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
841 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
842 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
847 docstring bformat(docstring const & fmt, docstring arg1)
849 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
850 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
851 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
856 docstring bformat(docstring const & fmt, char * arg1)
858 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
859 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
860 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
865 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
867 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
868 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
869 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
870 str = subst(str, lyx::from_ascii("%2$s"), arg2);
871 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
876 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
878 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
879 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
880 docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
881 str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
882 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
887 docstring bformat(docstring const & fmt, int arg1, int arg2)
889 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
890 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
891 docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
892 str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
893 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
898 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
900 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
901 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
902 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
903 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
904 str = subst(str, lyx::from_ascii("%2$s"), arg2);
905 str = subst(str, lyx::from_ascii("%3$s"), arg3);
906 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
911 docstring bformat(docstring const & fmt,
912 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
914 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
915 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
916 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
917 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
918 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
919 str = subst(str, lyx::from_ascii("%2$s"), arg2);
920 str = subst(str, lyx::from_ascii("%3$s"), arg3);
921 str = subst(str, lyx::from_ascii("%4$s"), arg4);
922 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
928 } // namespace support