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 bool isAscii(string const & str)
259 int const len = str.length();
260 for (int i = 0; i < len; ++i)
261 if (static_cast<unsigned char>(str[i]) >= 0x80)
267 char lowercase(char c)
269 BOOST_ASSERT(static_cast<unsigned char>(c) < 0x80);
270 return char(tolower(c));
274 char uppercase(char c)
276 BOOST_ASSERT(static_cast<unsigned char>(c) < 0x80);
277 return char(toupper(c));
281 char_type lowercase(char_type c)
284 // We don't know how to lowercase a non-utf16 char
286 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
290 char_type uppercase(char_type c)
293 // We don't know how to uppercase a non-utf16 char
295 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
301 // since we cannot use std::tolower and std::toupper directly in the
302 // calls to std::transform yet, we use these helper clases. (Lgb)
304 struct local_lowercase {
305 char_type operator()(char_type c) const {
307 // We don't know how to lowercase a non-utf16 char
309 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
313 struct local_uppercase {
314 char_type operator()(char_type c) const {
316 // We don't know how to uppercase a non-utf16 char
318 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
322 template<typename Char> struct local_ascii_lowercase {
323 Char operator()(Char c) const {
324 return ascii_tolower(c);
328 } // end of anon namespace
330 docstring const lowercase(docstring const & a)
333 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
338 docstring const uppercase(docstring const & a)
341 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
346 string const ascii_lowercase(string const & a)
349 transform(tmp.begin(), tmp.end(), tmp.begin(),
350 local_ascii_lowercase<char>());
355 docstring const ascii_lowercase(docstring const & a)
358 transform(tmp.begin(), tmp.end(), tmp.begin(),
359 local_ascii_lowercase<char_type>());
364 bool prefixIs(docstring const & a, char_type c)
372 bool prefixIs(string const & a, string const & pre)
374 string::size_type const prelen = pre.length();
375 string::size_type const alen = a.length();
377 if (prelen > alen || a.empty())
380 #if defined(STD_STRING_IS_GOOD)
381 return a.compare(0, prelen, pre) == 0;
383 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
389 bool prefixIs(docstring const & a, docstring const & pre)
391 docstring::size_type const prelen = pre.length();
392 docstring::size_type const alen = a.length();
394 if (prelen > alen || a.empty())
397 return a.compare(0, prelen, pre) == 0;
401 bool suffixIs(string const & a, char c)
403 if (a.empty()) return false;
404 return a[a.length() - 1] == c;
408 bool suffixIs(docstring const & a, char_type c)
412 return a[a.length() - 1] == c;
416 bool suffixIs(string const & a, string const & suf)
418 string::size_type const suflen = suf.length();
419 string::size_type const alen = a.length();
424 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
425 string tmp(a, alen - suflen);
426 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
428 return a.compare(alen - suflen, suflen, suf) == 0;
434 bool containsOnly(string const & s, string const & cset)
436 return s.find_first_not_of(cset) == string::npos;
440 // ale970405+lasgoutt-970425
441 // rewritten to use new string (Lgb)
442 string const token(string const & a, char delim, int n)
444 if (a.empty()) return string();
446 string::size_type k = 0;
447 string::size_type i = 0;
449 // Find delimiter or end of string
451 if ((i = a.find(delim, i)) == string::npos)
455 // i is now the n'th delim (or string::npos)
456 if (i == string::npos) return string();
457 k = a.find(delim, i);
458 // k is now the n'th + 1 delim (or string::npos)
460 return a.substr(i, k - i);
464 docstring const token(docstring const & a, char_type delim, int n)
466 if (a.empty()) return docstring();
468 string::size_type k = 0;
469 string::size_type i = 0;
471 // Find delimiter or end of string
473 if ((i = a.find(delim, i)) == docstring::npos)
477 // i is now the n'th delim (or string::npos)
478 if (i == docstring::npos) return docstring();
479 k = a.find(delim, i);
480 // k is now the n'th + 1 delim (or string::npos)
482 return a.substr(i, k - i);
486 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
487 // rewritten to use new string (Lgb)
488 int tokenPos(string const & a, char delim, string const & tok)
494 while (!str.empty()) {
495 str = split(str, tmptok, delim);
506 /// Substitute all \a oldchar with \a newchar
507 template<typename Ch> inline
508 std::basic_string<Ch> const subst_char(std::basic_string<Ch> const & a,
509 Ch oldchar, Ch newchar)
511 typedef std::basic_string<Ch> String;
513 typename String::iterator lit = tmp.begin();
514 typename String::iterator end = tmp.end();
515 for (; lit != end; ++lit)
516 if ((*lit) == oldchar)
522 /// substitutes all instances of \a oldstr with \a newstr
523 template<typename String> inline
524 String const subst_string(String const & a,
525 String const & oldstr, String const & newstr)
527 BOOST_ASSERT(!oldstr.empty());
529 typename String::size_type i = 0;
530 typename String::size_type const olen = oldstr.length();
531 while ((i = lstr.find(oldstr, i)) != string::npos) {
532 lstr.replace(i, olen, newstr);
533 i += newstr.length(); // We need to be sure that we dont
534 // use the same i over and over again.
542 string const subst(string const & a, char oldchar, char newchar)
544 return subst_char(a, oldchar, newchar);
548 docstring const subst(docstring const & a,
549 char_type oldchar, char_type newchar)
551 return subst_char(a, oldchar, newchar);
555 string const subst(string const & a,
556 string const & oldstr, string const & newstr)
558 return subst_string(a, oldstr, newstr);
562 docstring const subst(docstring const & a,
563 docstring const & oldstr, docstring const & newstr)
565 return subst_string(a, oldstr, newstr);
569 docstring const trim(docstring const & a, char const * p)
573 if (a.empty() || !*p)
576 docstring s = lyx::from_ascii(p);
577 docstring::size_type r = a.find_last_not_of(s);
578 docstring::size_type l = a.find_first_not_of(s);
580 // Is this the minimal test? (lgb)
581 if (r == docstring::npos && l == docstring::npos)
584 return a.substr(l, r - l + 1);
588 string const trim(string const & a, char const * p)
592 if (a.empty() || !*p)
595 string::size_type r = a.find_last_not_of(p);
596 string::size_type l = a.find_first_not_of(p);
598 // Is this the minimal test? (lgb)
599 if (r == string::npos && l == string::npos)
602 return a.substr(l, r - l + 1);
606 string const rtrim(string const & a, char const * p)
610 if (a.empty() || !*p)
613 string::size_type r = a.find_last_not_of(p);
615 // Is this test really needed? (Lgb)
616 if (r == string::npos)
619 return a.substr(0, r + 1);
623 docstring const rtrim(docstring const & a, char const * p)
627 if (a.empty() || !*p)
630 docstring::size_type r = a.find_last_not_of(from_ascii(p));
632 // Is this test really needed? (Lgb)
633 if (r == docstring::npos)
636 return a.substr(0, r + 1);
640 string const ltrim(string const & a, char const * p)
643 if (a.empty() || !*p)
645 string::size_type l = a.find_first_not_of(p);
646 if (l == string::npos)
648 return a.substr(l, string::npos);
652 docstring const ltrim(docstring const & a, char const * p)
655 if (a.empty() || !*p)
657 size_t l = a.find_first_not_of(from_ascii(p));
658 if (l == docstring::npos)
660 return a.substr(l, docstring::npos);
665 template<typename String, typename Char> inline
666 String const doSplit(String const & a, String & piece, Char delim)
669 typename String::size_type i = a.find(delim);
670 if (i == a.length() - 1) {
671 piece = a.substr(0, i);
672 } else if (i != String::npos) {
673 piece = a.substr(0, i);
674 tmp = a.substr(i + 1);
677 tmp = a.substr(i + 1);
687 string const split(string const & a, string & piece, char delim)
689 return doSplit(a, piece, delim);
693 docstring const split(docstring const & a, docstring & piece, char_type delim)
695 return doSplit(a, piece, delim);
699 string const split(string const & a, char delim)
702 string::size_type i = a.find(delim);
703 if (i != string::npos) // found delim
704 tmp = a.substr(i + 1);
710 string const rsplit(string const & a, string & piece, char delim)
713 string::size_type i = a.rfind(delim);
714 if (i != string::npos) { // delimiter was found
715 piece = a.substr(0, i);
716 tmp = a.substr(i + 1);
717 } else { // delimiter was not found
724 docstring const escape(docstring const & lab)
726 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
727 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
729 for (docstring::size_type i = 0; i < lab.length(); ++i) {
730 char_type c = lab[i];
731 if (c >= 128 || c == '=' || c == '%') {
732 // Although char_type is a 32 bit type we know that
733 // UCS4 occupies only 21 bits, so we don't need to
734 // encode bigger values. Test for 2^24 because we
735 // can encode that with the 6 hex digits that are
736 // needed for 21 bits anyway.
737 BOOST_ASSERT(c < (1 << 24));
739 enc += hexdigit[(c>>20) & 15];
740 enc += hexdigit[(c>>16) & 15];
741 enc += hexdigit[(c>>12) & 15];
742 enc += hexdigit[(c>> 8) & 15];
743 enc += hexdigit[(c>> 4) & 15];
744 enc += hexdigit[ c & 15];
755 template<typename String> vector<String> const
756 getVectorFromStringT(String const & str, String const & delim)
758 // Lars would like this code to go, but for now his replacement (below)
759 // doesn't fullfil the same function. I have, therefore, reactivated the
760 // old code for now. Angus 11 Nov 2002.
765 String keys = rtrim(str);
767 typename String::size_type const idx = keys.find(delim);
768 if (idx == String::npos) {
769 vec.push_back(ltrim(keys));
772 String const key = trim(keys.substr(0, idx));
775 typename String::size_type const start = idx + delim.size();
776 keys = keys.substr(start);
780 typedef boost::char_separator<typename String::value_type> Separator;
781 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
782 Separator sep(delim.c_str());
783 Tokenizer tokens(str, sep);
784 return vector<String>(tokens.begin(), tokens.end());
791 vector<string> const getVectorFromString(string const & str,
792 string const & delim)
794 return getVectorFromStringT<string>(str, delim);
798 vector<docstring> const getVectorFromString(docstring const & str,
799 docstring const & delim)
801 return getVectorFromStringT<docstring>(str, delim);
805 // the same vice versa
806 string const getStringFromVector(vector<string> const & vec,
807 string const & delim)
811 for (vector<string>::const_iterator it = vec.begin();
812 it != vec.end(); ++it) {
813 string item = trim(*it);
824 int findToken(char const * const str[], string const & search_token)
828 while (str[i][0] && str[i] != search_token)
836 docstring const externalLineEnding(docstring const & str)
838 #if defined(__APPLE__)
839 // The MAC clipboard uses \r for lineendings, and we use \n
840 return subst(str, '\n', '\r');
841 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
842 // Windows clipboard uses \r\n for lineendings, and we use \n
843 return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
850 docstring const internalLineEnding(docstring const & str)
852 docstring const s = subst(str,
853 lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
854 return subst(s, '\r', '\n');
858 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
862 docstring bformat(docstring const & fmt, int arg1)
864 return (boost::basic_format<char_type>(fmt) % arg1).str();
869 docstring bformat(docstring const & fmt, long arg1)
871 return (boost::basic_format<char_type>(fmt) % arg1).str();
876 docstring bformat(docstring const & fmt, unsigned int arg1)
878 return (boost::basic_format<char_type>(fmt) % arg1).str();
883 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
885 return (boost::basic_format<char_type>(fmt) % arg1).str();
890 docstring bformat(docstring const & fmt, char * arg1)
892 return (boost::basic_format<char_type>(fmt) % arg1).str();
897 docstring bformat(docstring const & fmt, int arg1, int arg2)
899 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
904 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
906 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
911 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
913 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
918 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
920 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
925 docstring bformat(docstring const & fmt,
926 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
928 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
934 docstring bformat(docstring const & fmt, int arg1)
936 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
937 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
938 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
943 docstring bformat(docstring const & fmt, long arg1)
945 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
946 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
947 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
952 docstring bformat(docstring const & fmt, unsigned int arg1)
954 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
955 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
956 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
961 docstring bformat(docstring const & fmt, docstring arg1)
963 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
964 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
965 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
970 docstring bformat(docstring const & fmt, char * arg1)
972 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
973 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
974 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
979 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
981 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
982 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
983 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
984 str = subst(str, lyx::from_ascii("%2$s"), arg2);
985 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
990 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
992 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
993 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
994 docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
995 str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
996 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1001 docstring bformat(docstring const & fmt, int arg1, int arg2)
1003 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
1004 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
1005 docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
1006 str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
1007 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1012 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1014 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1015 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
1016 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
1017 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
1018 str = subst(str, lyx::from_ascii("%2$s"), arg2);
1019 str = subst(str, lyx::from_ascii("%3$s"), arg3);
1020 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1025 docstring bformat(docstring const & fmt,
1026 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1028 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1029 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
1030 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
1031 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
1032 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
1033 str = subst(str, lyx::from_ascii("%2$s"), arg2);
1034 str = subst(str, lyx::from_ascii("%3$s"), arg3);
1035 str = subst(str, lyx::from_ascii("%4$s"), arg4);
1036 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1042 } // namespace support