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(docstring const & s, docstring const & s2)
53 docstring::const_iterator p = s.begin();
54 docstring::const_iterator p2 = s2.begin();
56 while (p != s.end() && p2 != s2.end()) {
57 char_type const lc1 = lowercase(*p);
58 char_type const lc2 = lowercase(*p2);
60 return (lc1 < lc2) ? -1 : 1;
65 if (s.size() == s2.size())
67 if (s.size() < s2.size())
75 template<typename Char>
76 Char ascii_tolower(Char c) {
77 if (c >= 'A' && c <= 'Z')
85 int compare_ascii_no_case(string const & s, string const & s2)
87 string::const_iterator p = s.begin();
88 string::const_iterator p2 = s2.begin();
90 while (p != s.end() && p2 != s2.end()) {
91 int const lc1 = ascii_tolower(*p);
92 int const lc2 = ascii_tolower(*p2);
94 return (lc1 < lc2) ? -1 : 1;
99 if (s.size() == s2.size())
101 if (s.size() < s2.size())
107 int compare_ascii_no_case(docstring const & s, docstring const & s2)
109 docstring::const_iterator p = s.begin();
110 docstring::const_iterator p2 = s2.begin();
112 while (p != s.end() && p2 != s2.end()) {
113 char_type const lc1 = ascii_tolower(*p);
114 char_type const lc2 = ascii_tolower(*p2);
116 return (lc1 < lc2) ? -1 : 1;
121 if (s.size() == s2.size())
123 if (s.size() < s2.size())
129 bool isStrInt(string const & str)
131 if (str.empty()) return false;
133 // Remove leading and trailing white space chars.
134 string const tmpstr = trim(str);
135 if (tmpstr.empty()) return false;
137 string::const_iterator cit = tmpstr.begin();
138 if ((*cit) == '-') ++cit;
139 string::const_iterator end = tmpstr.end();
140 for (; cit != end; ++cit) {
141 if (!isdigit((*cit))) return false;
147 bool isStrUnsignedInt(string const & str)
149 if (str.empty()) return false;
151 // Remove leading and trailing white space chars.
152 string const tmpstr = trim(str);
153 if (tmpstr.empty()) return false;
155 string::const_iterator cit = tmpstr.begin();
156 string::const_iterator end = tmpstr.end();
157 for (; cit != end; ++cit) {
158 if (!isdigit((*cit))) return false;
164 bool isStrDbl(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;
171 // if (1 < tmpstr.count('.')) return false;
173 string::const_iterator cit = tmpstr.begin();
174 bool found_dot(false);
175 if ((*cit) == '-') ++cit;
176 string::const_iterator end = tmpstr.end();
177 for (; cit != end; ++cit) {
197 bool isHexChar(char_type c)
209 c == 'a' || c == 'A' ||
210 c == 'b' || c == 'B' ||
211 c == 'c' || c == 'C' ||
212 c == 'd' || c == 'D' ||
213 c == 'e' || c == 'E' ||
214 c == 'f' || c == 'F';
220 bool isHex(docstring const & str)
224 if (str.length() > 2 && str[0] == '0' &&
225 (str[1] == 'x' || str[1] == 'X'))
228 int const len = str.length();
230 for (; index < len; ++index) {
231 if (!isHexChar(str[index]))
238 int hexToInt(docstring const & str)
240 string s = to_ascii(str);
242 sscanf(s.c_str(), "%x", &h);
247 bool isAscii(docstring const & str)
249 int const len = str.length();
250 for (int i = 0; i < len; ++i)
257 char lowercase(char c)
259 return char(tolower(c));
263 char uppercase(char c)
265 return char(toupper(c));
269 char_type lowercase(char_type c)
272 // We don't know how to lowercase a non-utf16 char
274 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
278 char_type uppercase(char_type c)
281 // We don't know how to uppercase a non-utf16 char
283 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
289 // since we cannot use std::tolower and std::toupper directly in the
290 // calls to std::transform yet, we use these helper clases. (Lgb)
292 struct local_lowercase {
293 char_type operator()(char_type c) const {
295 // We don't know how to lowercase a non-utf16 char
297 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
301 struct local_uppercase {
302 char_type operator()(char_type c) const {
304 // We don't know how to uppercase a non-utf16 char
306 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
310 template<typename Char> struct local_ascii_lowercase {
311 Char operator()(Char c) const {
312 return ascii_tolower(c);
316 } // end of anon namespace
318 docstring const lowercase(docstring const & a)
321 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
326 docstring const uppercase(docstring const & a)
329 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
334 string const ascii_lowercase(string const & a)
337 transform(tmp.begin(), tmp.end(), tmp.begin(),
338 local_ascii_lowercase<char>());
343 docstring const ascii_lowercase(docstring const & a)
346 transform(tmp.begin(), tmp.end(), tmp.begin(),
347 local_ascii_lowercase<char_type>());
352 bool prefixIs(docstring const & a, char_type c)
360 bool prefixIs(string const & a, string const & pre)
362 string::size_type const prelen = pre.length();
363 string::size_type const alen = a.length();
365 if (prelen > alen || a.empty())
368 #if defined(STD_STRING_IS_GOOD)
369 return a.compare(0, prelen, pre) == 0;
371 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
377 bool prefixIs(docstring const & a, docstring const & pre)
379 docstring::size_type const prelen = pre.length();
380 docstring::size_type const alen = a.length();
382 if (prelen > alen || a.empty())
385 return a.compare(0, prelen, pre) == 0;
389 bool suffixIs(string const & a, char c)
391 if (a.empty()) return false;
392 return a[a.length() - 1] == c;
396 bool suffixIs(docstring const & a, char_type c)
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 docstring const rtrim(docstring const & a, char const * p)
615 if (a.empty() || !*p)
618 docstring::size_type r = a.find_last_not_of(from_ascii(p));
620 // Is this test really needed? (Lgb)
621 if (r == docstring::npos)
624 return a.substr(0, r + 1);
628 string const ltrim(string const & a, char const * p)
631 if (a.empty() || !*p)
633 string::size_type l = a.find_first_not_of(p);
634 if (l == string::npos)
636 return a.substr(l, string::npos);
640 docstring const ltrim(docstring const & a, char const * p)
643 if (a.empty() || !*p)
645 size_t l = a.find_first_not_of(from_ascii(p));
646 if (l == docstring::npos)
648 return a.substr(l, docstring::npos);
653 template<typename String, typename Char> inline
654 String const doSplit(String const & a, String & piece, Char delim)
657 typename String::size_type i = a.find(delim);
658 if (i == a.length() - 1) {
659 piece = a.substr(0, i);
660 } else if (i != String::npos) {
661 piece = a.substr(0, i);
662 tmp = a.substr(i + 1);
665 tmp = a.substr(i + 1);
675 string const split(string const & a, string & piece, char delim)
677 return doSplit(a, piece, delim);
681 docstring const split(docstring const & a, docstring & piece, char_type delim)
683 return doSplit(a, piece, delim);
687 string const split(string const & a, char delim)
690 string::size_type i = a.find(delim);
691 if (i != string::npos) // found delim
692 tmp = a.substr(i + 1);
698 string const rsplit(string const & a, string & piece, char delim)
701 string::size_type i = a.rfind(delim);
702 if (i != string::npos) { // delimiter was found
703 piece = a.substr(0, i);
704 tmp = a.substr(i + 1);
705 } else { // delimiter was not found
712 docstring const escape(docstring const & lab)
714 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
715 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
717 for (docstring::size_type i = 0; i < lab.length(); ++i) {
718 char_type c = lab[i];
719 if (c >= 128 || c == '=' || c == '%') {
720 // Although char_type is a 32 bit type we know that
721 // UCS4 occupies only 21 bits, so we don't need to
722 // encode bigger values. Test for 2^24 because we
723 // can encode that with the 6 hex digits that are
724 // needed for 21 bits anyway.
725 BOOST_ASSERT(c < (1 << 24));
727 enc += hexdigit[(c>>20) & 15];
728 enc += hexdigit[(c>>16) & 15];
729 enc += hexdigit[(c>>12) & 15];
730 enc += hexdigit[(c>> 8) & 15];
731 enc += hexdigit[(c>> 4) & 15];
732 enc += hexdigit[ c & 15];
743 template<typename String> vector<String> const
744 getVectorFromStringT(String const & str, String const & delim)
746 // Lars would like this code to go, but for now his replacement (below)
747 // doesn't fullfil the same function. I have, therefore, reactivated the
748 // old code for now. Angus 11 Nov 2002.
753 String keys = rtrim(str);
755 typename String::size_type const idx = keys.find(delim);
756 if (idx == String::npos) {
757 vec.push_back(ltrim(keys));
760 String const key = trim(keys.substr(0, idx));
763 typename String::size_type const start = idx + delim.size();
764 keys = keys.substr(start);
768 typedef boost::char_separator<typename String::value_type> Separator;
769 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
770 Separator sep(delim.c_str());
771 Tokenizer tokens(str, sep);
772 return vector<String>(tokens.begin(), tokens.end());
779 vector<string> const getVectorFromString(string const & str,
780 string const & delim)
782 return getVectorFromStringT<string>(str, delim);
786 vector<docstring> const getVectorFromString(docstring const & str,
787 docstring const & delim)
789 return getVectorFromStringT<docstring>(str, delim);
793 // the same vice versa
794 string const getStringFromVector(vector<string> const & vec,
795 string const & delim)
799 for (vector<string>::const_iterator it = vec.begin();
800 it != vec.end(); ++it) {
801 string item = trim(*it);
812 int findToken(char const * const str[], string const & search_token)
816 while (str[i][0] && str[i] != search_token)
824 docstring const externalLineEnding(docstring const & str)
826 #if defined(__APPLE__)
827 // The MAC clipboard uses \r for lineendings, and we use \n
828 return subst(str, '\n', '\r');
829 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
830 // Windows clipboard uses \r\n for lineendings, and we use \n
831 return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
838 docstring const internalLineEnding(docstring const & str)
840 docstring const s = subst(str,
841 lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
842 return subst(s, '\r', '\n');
846 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
850 docstring bformat(docstring const & fmt, int arg1)
852 return (boost::basic_format<char_type>(fmt) % arg1).str();
857 docstring bformat(docstring const & fmt, long arg1)
859 return (boost::basic_format<char_type>(fmt) % arg1).str();
864 docstring bformat(docstring const & fmt, unsigned int arg1)
866 return (boost::basic_format<char_type>(fmt) % arg1).str();
871 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
873 return (boost::basic_format<char_type>(fmt) % arg1).str();
878 docstring bformat(docstring const & fmt, char * arg1)
880 return (boost::basic_format<char_type>(fmt) % arg1).str();
885 docstring bformat(docstring const & fmt, int arg1, int arg2)
887 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
892 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
894 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
899 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
901 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
906 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
908 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
913 docstring bformat(docstring const & fmt,
914 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
916 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
922 docstring bformat(docstring const & fmt, int arg1)
924 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
925 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
926 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
931 docstring bformat(docstring const & fmt, long arg1)
933 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
934 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
935 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
940 docstring bformat(docstring const & fmt, unsigned int arg1)
942 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
943 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
944 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
949 docstring bformat(docstring const & fmt, docstring arg1)
951 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
952 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
953 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
958 docstring bformat(docstring const & fmt, char * arg1)
960 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
961 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
962 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
967 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
969 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
970 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
971 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
972 str = subst(str, lyx::from_ascii("%2$s"), arg2);
973 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
978 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
980 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
981 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
982 docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
983 str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
984 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
989 docstring bformat(docstring const & fmt, int arg1, int arg2)
991 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
992 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
993 docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
994 str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
995 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1000 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1002 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1003 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
1004 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
1005 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
1006 str = subst(str, lyx::from_ascii("%2$s"), arg2);
1007 str = subst(str, lyx::from_ascii("%3$s"), arg3);
1008 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1013 docstring bformat(docstring const & fmt,
1014 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1016 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1017 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
1018 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
1019 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
1020 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
1021 str = subst(str, lyx::from_ascii("%2$s"), arg2);
1022 str = subst(str, lyx::from_ascii("%3$s"), arg3);
1023 str = subst(str, lyx::from_ascii("%4$s"), arg4);
1024 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1030 } // namespace support