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"
17 #include "support/qstring_helpers.h"
21 #include <boost/tokenizer.hpp>
22 #include <boost/assert.hpp>
24 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
26 #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 char_type const lc1 = lowercase(*p);
80 char_type const lc2 = lowercase(*p2);
82 return (lc1 < lc2) ? -1 : 1;
87 if (s.size() == s2.size())
89 if (s.size() < s2.size())
97 template<typename Char>
98 Char ascii_tolower(Char c) {
99 if (c >= 'A' && c <= 'Z')
100 return c - 'A' + 'a';
107 int compare_ascii_no_case(string const & s, string const & s2)
109 string::const_iterator p = s.begin();
110 string::const_iterator p2 = s2.begin();
112 while (p != s.end() && p2 != s2.end()) {
113 int const lc1 = ascii_tolower(*p);
114 int const lc2 = ascii_tolower(*p2);
116 return (lc1 < lc2) ? -1 : 1;
121 if (s.size() == s2.size())
123 if (s.size() < s2.size())
129 int compare_ascii_no_case(docstring const & s, docstring const & s2)
131 docstring::const_iterator p = s.begin();
132 docstring::const_iterator p2 = s2.begin();
134 while (p != s.end() && p2 != s2.end()) {
135 char_type const lc1 = ascii_tolower(*p);
136 char_type const lc2 = ascii_tolower(*p2);
138 return (lc1 < lc2) ? -1 : 1;
143 if (s.size() == s2.size())
145 if (s.size() < s2.size())
151 int compare_no_case(string const & s, string const & s2, unsigned int len)
153 string::const_iterator p = s.begin();
154 string::const_iterator p2 = s2.begin();
156 while (i < len && p != s.end() && p2 != s2.end()) {
157 int const lc1 = tolower(*p);
158 int const lc2 = tolower(*p2);
160 return (lc1 < lc2) ? -1 : 1;
166 if (s.size() >= len && s2.size() >= len)
168 if (s.size() < s2.size())
174 bool isStrInt(string const & str)
176 if (str.empty()) return false;
178 // Remove leading and trailing white space chars.
179 string const tmpstr = trim(str);
180 if (tmpstr.empty()) return false;
182 string::const_iterator cit = tmpstr.begin();
183 if ((*cit) == '-') ++cit;
184 string::const_iterator end = tmpstr.end();
185 for (; cit != end; ++cit) {
186 if (!isdigit((*cit))) return false;
192 bool isStrUnsignedInt(string const & str)
194 if (str.empty()) return false;
196 // Remove leading and trailing white space chars.
197 string const tmpstr = trim(str);
198 if (tmpstr.empty()) return false;
200 string::const_iterator cit = tmpstr.begin();
201 string::const_iterator end = tmpstr.end();
202 for (; cit != end; ++cit) {
203 if (!isdigit((*cit))) return false;
209 bool isStrDbl(string const & str)
211 if (str.empty()) return false;
213 // Remove leading and trailing white space chars.
214 string const tmpstr = trim(str);
215 if (tmpstr.empty()) return false;
216 // if (1 < tmpstr.count('.')) return false;
218 string::const_iterator cit = tmpstr.begin();
219 bool found_dot(false);
220 if ((*cit) == '-') ++cit;
221 string::const_iterator end = tmpstr.end();
222 for (; cit != end; ++cit) {
242 bool isHexChar(char_type c)
254 c == 'a' || c == 'A' ||
255 c == 'b' || c == 'B' ||
256 c == 'c' || c == 'C' ||
257 c == 'd' || c == 'D' ||
258 c == 'e' || c == 'E' ||
259 c == 'f' || c == 'F';
265 bool isHex(docstring const & str)
269 if (str.length() > 2 && str[0] == '0' &&
270 (str[1] == 'x' || str[1] == 'X'))
273 int const len = str.length();
275 for (; index < len; ++index) {
276 if (!isHexChar(str[index]))
283 int hexToInt(docstring const & str)
285 string s = to_ascii(str);
287 sscanf(s.c_str(), "%x", &h);
292 bool isAscii(docstring const & str)
294 int const len = str.length();
295 for (int i = 0; i < len; ++i)
302 char lowercase(char c)
304 return char(tolower(c));
308 char uppercase(char c)
310 return char(toupper(c));
314 char_type lowercase(char_type c)
317 // We don't know how to lowercase a non-utf16 char
319 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
323 char_type uppercase(char_type c)
326 // We don't know how to uppercase a non-utf16 char
328 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
334 // since we cannot use std::tolower and std::toupper directly in the
335 // calls to std::transform yet, we use these helper clases. (Lgb)
337 struct local_lowercase {
338 char operator()(char c) const {
341 char_type operator()(char_type c) const {
343 // We don't know how to lowercase a non-utf16 char
345 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
349 struct local_uppercase {
350 char operator()(char c) const {
355 template<typename Char> struct local_ascii_lowercase {
356 Char operator()(Char c) const {
357 return ascii_tolower(c);
361 } // end of anon namespace
363 string const lowercase(string const & a)
366 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
371 docstring const lowercase(docstring const & a)
374 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
379 string const uppercase(string const & a)
382 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
387 string const ascii_lowercase(string const & a)
390 transform(tmp.begin(), tmp.end(), tmp.begin(),
391 local_ascii_lowercase<char>());
396 docstring const ascii_lowercase(docstring const & a)
399 transform(tmp.begin(), tmp.end(), tmp.begin(),
400 local_ascii_lowercase<char_type>());
405 bool prefixIs(docstring const & a, char_type c)
413 bool prefixIs(string const & a, string const & pre)
415 string::size_type const prelen = pre.length();
416 string::size_type const alen = a.length();
418 if (prelen > alen || a.empty())
421 #if defined(STD_STRING_IS_GOOD)
422 return a.compare(0, prelen, pre) == 0;
424 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
430 bool prefixIs(docstring const & a, docstring const & pre)
432 docstring::size_type const prelen = pre.length();
433 docstring::size_type const alen = a.length();
435 if (prelen > alen || a.empty())
438 return a.compare(0, prelen, pre) == 0;
442 bool suffixIs(string const & a, char c)
444 if (a.empty()) return false;
445 return a[a.length() - 1] == c;
449 bool suffixIs(docstring const & a, char_type c)
453 return a[a.length() - 1] == c;
457 bool suffixIs(string const & a, string const & suf)
459 string::size_type const suflen = suf.length();
460 string::size_type const alen = a.length();
465 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
466 string tmp(a, alen - suflen);
467 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
469 return a.compare(alen - suflen, suflen, suf) == 0;
475 bool containsOnly(string const & s, string const & cset)
477 return s.find_first_not_of(cset) == string::npos;
481 // ale970405+lasgoutt-970425
482 // rewritten to use new string (Lgb)
483 string const token(string const & a, char delim, int n)
485 if (a.empty()) return string();
487 string::size_type k = 0;
488 string::size_type i = 0;
490 // Find delimiter or end of string
492 if ((i = a.find(delim, i)) == string::npos)
496 // i is now the n'th delim (or string::npos)
497 if (i == string::npos) return string();
498 k = a.find(delim, i);
499 // k is now the n'th + 1 delim (or string::npos)
501 return a.substr(i, k - i);
505 docstring const token(docstring const & a, char_type delim, int n)
507 if (a.empty()) return docstring();
509 string::size_type k = 0;
510 string::size_type i = 0;
512 // Find delimiter or end of string
514 if ((i = a.find(delim, i)) == docstring::npos)
518 // i is now the n'th delim (or string::npos)
519 if (i == docstring::npos) return docstring();
520 k = a.find(delim, i);
521 // k is now the n'th + 1 delim (or string::npos)
523 return a.substr(i, k - i);
527 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
528 // rewritten to use new string (Lgb)
529 int tokenPos(string const & a, char delim, string const & tok)
535 while (!str.empty()) {
536 str = split(str, tmptok, delim);
547 /// Substitute all \a oldchar with \a newchar
548 template<typename Ch> inline
549 std::basic_string<Ch> const subst_char(std::basic_string<Ch> const & a,
550 Ch oldchar, Ch newchar)
552 typedef std::basic_string<Ch> String;
554 typename String::iterator lit = tmp.begin();
555 typename String::iterator end = tmp.end();
556 for (; lit != end; ++lit)
557 if ((*lit) == oldchar)
563 /// substitutes all instances of \a oldstr with \a newstr
564 template<typename String> inline
565 String const subst_string(String const & a,
566 String const & oldstr, String const & newstr)
568 BOOST_ASSERT(!oldstr.empty());
570 typename String::size_type i = 0;
571 typename String::size_type const olen = oldstr.length();
572 while ((i = lstr.find(oldstr, i)) != string::npos) {
573 lstr.replace(i, olen, newstr);
574 i += newstr.length(); // We need to be sure that we dont
575 // use the same i over and over again.
583 string const subst(string const & a, char oldchar, char newchar)
585 return subst_char(a, oldchar, newchar);
589 docstring const subst(docstring const & a,
590 char_type oldchar, char_type newchar)
592 return subst_char(a, oldchar, newchar);
596 string const subst(string const & a,
597 string const & oldstr, string const & newstr)
599 return subst_string(a, oldstr, newstr);
603 docstring const subst(docstring const & a,
604 docstring const & oldstr, docstring const & newstr)
606 return subst_string(a, oldstr, newstr);
610 docstring const trim(docstring const & a, char const * p)
614 if (a.empty() || !*p)
617 docstring s = lyx::from_ascii(p);
618 docstring::size_type r = a.find_last_not_of(s);
619 docstring::size_type l = a.find_first_not_of(s);
621 // Is this the minimal test? (lgb)
622 if (r == docstring::npos && l == docstring::npos)
625 return a.substr(l, r - l + 1);
629 string const trim(string const & a, char const * p)
633 if (a.empty() || !*p)
636 string::size_type r = a.find_last_not_of(p);
637 string::size_type l = a.find_first_not_of(p);
639 // Is this the minimal test? (lgb)
640 if (r == string::npos && l == string::npos)
643 return a.substr(l, r - l + 1);
647 string const rtrim(string const & a, char const * p)
651 if (a.empty() || !*p)
654 string::size_type r = a.find_last_not_of(p);
656 // Is this test really needed? (Lgb)
657 if (r == string::npos)
660 return a.substr(0, r + 1);
664 docstring const rtrim(docstring const & a, char const * p)
668 if (a.empty() || !*p)
671 docstring::size_type r = a.find_last_not_of(from_ascii(p));
673 // Is this test really needed? (Lgb)
674 if (r == docstring::npos)
677 return a.substr(0, r + 1);
681 string const ltrim(string const & a, char const * p)
684 if (a.empty() || !*p)
686 string::size_type l = a.find_first_not_of(p);
687 if (l == string::npos)
689 return a.substr(l, string::npos);
693 docstring const ltrim(docstring const & a, char const * p)
696 if (a.empty() || !*p)
698 size_t l = a.find_first_not_of(from_ascii(p));
699 if (l == docstring::npos)
701 return a.substr(l, docstring::npos);
706 template<typename String, typename Char> inline
707 String const doSplit(String const & a, String & piece, Char delim)
710 typename String::size_type i = a.find(delim);
711 if (i == a.length() - 1) {
712 piece = a.substr(0, i);
713 } else if (i != String::npos) {
714 piece = a.substr(0, i);
715 tmp = a.substr(i + 1);
718 tmp = a.substr(i + 1);
728 string const split(string const & a, string & piece, char delim)
730 return doSplit(a, piece, delim);
734 docstring const split(docstring const & a, docstring & piece, char_type delim)
736 return doSplit(a, piece, delim);
740 string const split(string const & a, char delim)
743 string::size_type i = a.find(delim);
744 if (i != string::npos) // found delim
745 tmp = a.substr(i + 1);
751 string const rsplit(string const & a, string & piece, char delim)
754 string::size_type i = a.rfind(delim);
755 if (i != string::npos) { // delimiter was found
756 piece = a.substr(0, i);
757 tmp = a.substr(i + 1);
758 } else { // delimiter was not found
765 docstring const escape(docstring const & lab)
767 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
768 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
770 for (docstring::size_type i = 0; i < lab.length(); ++i) {
771 char_type c = lab[i];
772 if (c >= 128 || c == '=' || c == '%') {
773 // Although char_type is a 32 bit type we know that
774 // UCS4 occupies only 21 bits, so we don't need to
775 // encode bigger values. Test for 2^24 because we
776 // can encode that with the 6 hex digits that are
777 // needed for 21 bits anyway.
778 BOOST_ASSERT(c < (1 << 24));
780 enc += hexdigit[(c>>20) & 15];
781 enc += hexdigit[(c>>16) & 15];
782 enc += hexdigit[(c>>12) & 15];
783 enc += hexdigit[(c>> 8) & 15];
784 enc += hexdigit[(c>> 4) & 15];
785 enc += hexdigit[ c & 15];
796 template<typename String> vector<String> const
797 getVectorFromStringT(String const & str, String const & delim)
799 // Lars would like this code to go, but for now his replacement (below)
800 // doesn't fullfil the same function. I have, therefore, reactivated the
801 // old code for now. Angus 11 Nov 2002.
806 String keys = rtrim(str);
808 typename String::size_type const idx = keys.find(delim);
809 if (idx == String::npos) {
810 vec.push_back(ltrim(keys));
813 String const key = trim(keys.substr(0, idx));
816 typename String::size_type const start = idx + delim.size();
817 keys = keys.substr(start);
821 typedef boost::char_separator<typename String::value_type> Separator;
822 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
823 Separator sep(delim.c_str());
824 Tokenizer tokens(str, sep);
825 return vector<String>(tokens.begin(), tokens.end());
832 vector<string> const getVectorFromString(string const & str,
833 string const & delim)
835 return getVectorFromStringT<string>(str, delim);
839 vector<docstring> const getVectorFromString(docstring const & str,
840 docstring const & delim)
842 return getVectorFromStringT<docstring>(str, delim);
846 // the same vice versa
847 string const getStringFromVector(vector<string> const & vec,
848 string const & delim)
852 for (vector<string>::const_iterator it = vec.begin();
853 it != vec.end(); ++it) {
854 string item = trim(*it);
865 int findToken(char const * const str[], string const & search_token)
869 while (str[i][0] && str[i] != search_token)
877 docstring const externalLineEnding(docstring const & str)
879 #if defined(__APPLE__)
880 // The MAC clipboard uses \r for lineendings, and we use \n
881 return subst(str, '\n', '\r');
882 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
883 // Windows clipboard uses \r\n for lineendings, and we use \n
884 return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
891 docstring const internalLineEnding(docstring const & str)
893 docstring const s = subst(str,
894 lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
895 return subst(s, '\r', '\n');
899 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
903 docstring bformat(docstring const & fmt, int arg1)
905 return (boost::basic_format<char_type>(fmt) % arg1).str();
910 docstring bformat(docstring const & fmt, long arg1)
912 return (boost::basic_format<char_type>(fmt) % arg1).str();
917 docstring bformat(docstring const & fmt, unsigned int arg1)
919 return (boost::basic_format<char_type>(fmt) % arg1).str();
924 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
926 return (boost::basic_format<char_type>(fmt) % arg1).str();
931 docstring bformat(docstring const & fmt, char * arg1)
933 return (boost::basic_format<char_type>(fmt) % arg1).str();
938 docstring bformat(docstring const & fmt, int arg1, int arg2)
940 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
945 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
947 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
952 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
954 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
959 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
961 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
966 docstring bformat(docstring const & fmt,
967 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
969 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
975 docstring bformat(docstring const & fmt, int arg1)
977 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
978 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
979 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
984 docstring bformat(docstring const & fmt, long arg1)
986 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
987 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
988 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
993 docstring bformat(docstring const & fmt, unsigned int arg1)
995 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
996 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
997 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1002 docstring bformat(docstring const & fmt, docstring arg1)
1004 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1005 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
1006 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1011 docstring bformat(docstring const & fmt, char * arg1)
1013 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1014 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
1015 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1020 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1022 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1023 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
1024 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
1025 str = subst(str, lyx::from_ascii("%2$s"), arg2);
1026 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1031 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1033 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1034 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
1035 docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
1036 str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
1037 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1042 docstring bformat(docstring const & fmt, int arg1, int arg2)
1044 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
1045 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
1046 docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
1047 str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
1048 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1053 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1055 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1056 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
1057 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
1058 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
1059 str = subst(str, lyx::from_ascii("%2$s"), arg2);
1060 str = subst(str, lyx::from_ascii("%3$s"), arg3);
1061 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1066 docstring bformat(docstring const & fmt,
1067 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1069 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1070 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
1071 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
1072 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
1073 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
1074 str = subst(str, lyx::from_ascii("%2$s"), arg2);
1075 str = subst(str, lyx::from_ascii("%3$s"), arg3);
1076 str = subst(str, lyx::from_ascii("%4$s"), arg4);
1077 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1083 } // namespace support