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) {
232 bool isHexChar(char_type c)
244 c == 'a' || c == 'A' ||
245 c == 'b' || c == 'B' ||
246 c == 'c' || c == 'C' ||
247 c == 'd' || c == 'D' ||
248 c == 'e' || c == 'E' ||
249 c == 'f' || c == 'F';
255 bool isHex(docstring const & str)
259 if (str.length() > 2 && str[0] == '0' &&
260 (str[1] == 'x' || str[1] == 'X'))
263 int const len = str.length();
265 for (; index < len; ++index) {
266 if (!isHexChar(str[index]))
273 int hexToInt(docstring const & str)
275 string s = to_ascii(str);
277 sscanf(s.c_str(), "%x", &h);
282 bool isAscii(docstring const & str)
284 int const len = str.length();
285 for (int i = 0; i < len; ++i)
292 char lowercase(char c)
294 return char(tolower(c));
298 char uppercase(char c)
300 return char(toupper(c));
303 // FIXME for lowercase() and uppercase() function below:
304 // 1) std::tolower() and std::toupper() are templates that
305 // compile fine with char_type. With the test (c >= 256) we
306 // do not trust these function to do the right thing with
308 // 2) these functions use the current locale, which is wrong
309 // if it is not latin1 based (latin1 is a subset of UCS4).
311 char_type lowercase(char_type c)
320 char_type uppercase(char_type c)
331 // since we cannot use std::tolower and std::toupper directly in the
332 // calls to std::transform yet, we use these helper clases. (Lgb)
334 struct local_lowercase {
335 char operator()(char c) const {
340 struct local_uppercase {
341 char operator()(char c) const {
346 struct local_ascii_lowercase {
347 char operator()(char c) const {
348 return ascii_tolower(c);
352 } // end of anon namespace
354 string const lowercase(string const & a)
357 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
361 string const uppercase(string const & a)
364 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
369 string const ascii_lowercase(string const & a)
372 transform(tmp.begin(), tmp.end(), tmp.begin(),
373 local_ascii_lowercase());
378 bool prefixIs(string const & a, string const & pre)
380 string::size_type const prelen = pre.length();
381 string::size_type const alen = a.length();
383 if (prelen > alen || a.empty())
386 #if defined(STD_STRING_IS_GOOD)
387 return a.compare(0, prelen, pre) == 0;
389 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
395 bool prefixIs(docstring const & a, docstring const & pre)
397 docstring::size_type const prelen = pre.length();
398 docstring::size_type const alen = a.length();
400 if (prelen > alen || a.empty())
403 return a.compare(0, prelen, pre) == 0;
407 bool suffixIs(string const & a, char c)
409 if (a.empty()) return false;
410 return a[a.length() - 1] == c;
414 bool suffixIs(string const & a, string const & suf)
416 string::size_type const suflen = suf.length();
417 string::size_type const alen = a.length();
422 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
423 string tmp(a, alen - suflen);
424 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
426 return a.compare(alen - suflen, suflen, suf) == 0;
432 bool containsOnly(string const & s, string const & cset)
434 return s.find_first_not_of(cset) == string::npos;
438 // ale970405+lasgoutt-970425
439 // rewritten to use new string (Lgb)
440 string const token(string const & a, char delim, int n)
442 if (a.empty()) return string();
444 string::size_type k = 0;
445 string::size_type i = 0;
447 // Find delimiter or end of string
449 if ((i = a.find(delim, i)) == string::npos)
453 // i is now the n'th delim (or string::npos)
454 if (i == string::npos) return string();
455 k = a.find(delim, i);
456 // k is now the n'th + 1 delim (or string::npos)
458 return a.substr(i, k - i);
462 docstring const token(docstring const & a, char_type delim, int n)
464 if (a.empty()) return docstring();
466 string::size_type k = 0;
467 string::size_type i = 0;
469 // Find delimiter or end of string
471 if ((i = a.find(delim, i)) == docstring::npos)
475 // i is now the n'th delim (or string::npos)
476 if (i == docstring::npos) return docstring();
477 k = a.find(delim, i);
478 // k is now the n'th + 1 delim (or string::npos)
480 return a.substr(i, k - i);
484 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
485 // rewritten to use new string (Lgb)
486 int tokenPos(string const & a, char delim, string const & tok)
492 while (!str.empty()) {
493 str = split(str, tmptok, delim);
504 /// Substitute all \a oldchar with \a newchar
505 template<typename Ch> inline
506 std::basic_string<Ch> const subst_char(std::basic_string<Ch> const & a,
507 Ch oldchar, Ch newchar)
509 typedef std::basic_string<Ch> String;
511 typename String::iterator lit = tmp.begin();
512 typename String::iterator end = tmp.end();
513 for (; lit != end; ++lit)
514 if ((*lit) == oldchar)
520 /// substitutes all instances of \a oldstr with \a newstr
521 template<typename String> inline
522 String const subst_string(String const & a,
523 String const & oldstr, String const & newstr)
525 BOOST_ASSERT(!oldstr.empty());
527 typename String::size_type i = 0;
528 typename String::size_type const olen = oldstr.length();
529 while ((i = lstr.find(oldstr, i)) != string::npos) {
530 lstr.replace(i, olen, newstr);
531 i += newstr.length(); // We need to be sure that we dont
532 // use the same i over and over again.
540 string const subst(string const & a, char oldchar, char newchar)
542 return subst_char(a, oldchar, newchar);
546 docstring const subst(docstring const & a,
547 char_type oldchar, char_type newchar)
549 return subst_char(a, oldchar, newchar);
553 string const subst(string const & a,
554 string const & oldstr, string const & newstr)
556 return subst_string(a, oldstr, newstr);
560 docstring const subst(docstring const & a,
561 docstring const & oldstr, docstring const & newstr)
563 return subst_string(a, oldstr, newstr);
567 docstring const trim(docstring const & a, char const * p)
571 if (a.empty() || !*p)
574 docstring s = lyx::from_ascii(p);
575 docstring::size_type r = a.find_last_not_of(s);
576 docstring::size_type l = a.find_first_not_of(s);
578 // Is this the minimal test? (lgb)
579 if (r == docstring::npos && l == docstring::npos)
582 return a.substr(l, r - l + 1);
586 string const trim(string const & a, char const * p)
590 if (a.empty() || !*p)
593 string::size_type r = a.find_last_not_of(p);
594 string::size_type l = a.find_first_not_of(p);
596 // Is this the minimal test? (lgb)
597 if (r == string::npos && l == string::npos)
600 return a.substr(l, r - l + 1);
604 string const rtrim(string const & a, char const * p)
608 if (a.empty() || !*p)
611 string::size_type r = a.find_last_not_of(p);
613 // Is this test really needed? (Lgb)
614 if (r == string::npos)
617 return a.substr(0, r + 1);
621 string const ltrim(string const & a, char const * p)
624 if (a.empty() || !*p)
626 string::size_type l = a.find_first_not_of(p);
627 if (l == string::npos)
629 return a.substr(l, string::npos);
633 docstring const ltrim(docstring const & a, char const * p)
636 if (a.empty() || !*p)
638 size_t l = a.find_first_not_of(from_ascii(p));
639 if (l == docstring::npos)
641 return a.substr(l, docstring::npos);
646 template<typename String, typename Char> inline
647 String const doSplit(String const & a, String & piece, Char delim)
650 typename String::size_type i = a.find(delim);
651 if (i == a.length() - 1) {
652 piece = a.substr(0, i);
653 } else if (i != String::npos) {
654 piece = a.substr(0, i);
655 tmp = a.substr(i + 1);
658 tmp = a.substr(i + 1);
668 string const split(string const & a, string & piece, char delim)
670 return doSplit(a, piece, delim);
674 docstring const split(docstring const & a, docstring & piece, char_type delim)
676 return doSplit(a, piece, delim);
680 string const split(string const & a, char delim)
683 string::size_type i = a.find(delim);
684 if (i != string::npos) // found delim
685 tmp = a.substr(i + 1);
691 string const rsplit(string const & a, string & piece, char delim)
694 string::size_type i = a.rfind(delim);
695 if (i != string::npos) { // delimiter was found
696 piece = a.substr(0, i);
697 tmp = a.substr(i + 1);
698 } else { // delimiter was not found
705 // This function escapes 8-bit characters and other problematic
706 // characters that cause problems in latex labels.
707 docstring const escape(docstring const & lab)
709 lyx::char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
710 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
712 for (docstring::size_type i = 0; i < lab.length(); ++i) {
713 lyx::char_type c = lab[i];
714 // FIXME We must change the following algorithm for UCS4
715 // chars, but that will be a file format change.
716 if (c >= 128 || c == '=' || c == '%') {
718 enc += hexdigit[c>>4];
719 enc += hexdigit[c & 15];
728 /// gives a vector of stringparts which have the delimiter delim
729 vector<string> const getVectorFromString(string const & str,
730 string const & delim)
732 // Lars would like this code to go, but for now his replacement (below)
733 // doesn't fullfil the same function. I have, therefore, reactivated the
734 // old code for now. Angus 11 Nov 2002.
739 string keys = rtrim(str);
741 string::size_type const idx = keys.find(delim);
742 if (idx == string::npos) {
743 vec.push_back(ltrim(keys));
746 string const key = trim(keys.substr(0, idx));
749 string::size_type const start = idx + delim.size();
750 keys = keys.substr(start);
754 boost::char_separator<char> sep(delim.c_str());
755 boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
756 return vector<string>(tokens.begin(), tokens.end());
761 // the same vice versa
762 string const getStringFromVector(vector<string> const & vec,
763 string const & delim)
767 for (vector<string>::const_iterator it = vec.begin();
768 it != vec.end(); ++it) {
769 string item = trim(*it);
780 int findToken(char const * const str[], string const & search_token)
784 while (str[i][0] && str[i] != search_token)
792 docstring const externalLineEnding(docstring const & str)
794 #if defined(__APPLE__)
795 // The MAC clipboard uses \r for lineendings, and we use \n
796 return subst(str, '\n', '\r');
797 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
798 // Windows clipboard uses \r\n for lineendings, and we use \n
799 return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
806 docstring const internalLineEnding(docstring const & str)
808 docstring const s = subst(str,
809 lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
810 return subst(s, '\r', '\n');
814 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
818 docstring bformat(docstring const & fmt, int arg1)
820 return (boost::basic_format<char_type>(fmt) % arg1).str();
825 docstring bformat(docstring const & fmt, long arg1)
827 return (boost::basic_format<char_type>(fmt) % arg1).str();
832 docstring bformat(docstring const & fmt, unsigned int arg1)
834 return (boost::basic_format<char_type>(fmt) % arg1).str();
839 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
841 return (boost::basic_format<char_type>(fmt) % arg1).str();
846 docstring bformat(docstring const & fmt, char * arg1)
848 return (boost::basic_format<char_type>(fmt) % arg1).str();
853 docstring bformat(docstring const & fmt, int arg1, int arg2)
855 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
860 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
862 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
867 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
869 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
874 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
876 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
881 docstring bformat(docstring const & fmt,
882 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
884 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
890 docstring bformat(docstring const & fmt, int arg1)
892 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
893 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
894 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
899 docstring bformat(docstring const & fmt, long arg1)
901 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
902 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
903 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
908 docstring bformat(docstring const & fmt, unsigned int arg1)
910 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
911 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
912 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
917 docstring bformat(docstring const & fmt, docstring arg1)
919 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
920 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
921 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
926 docstring bformat(docstring const & fmt, char * arg1)
928 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
929 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
930 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
935 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
937 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
938 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
939 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
940 str = subst(str, lyx::from_ascii("%2$s"), arg2);
941 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
946 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
948 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
949 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
950 docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
951 str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
952 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
957 docstring bformat(docstring const & fmt, int arg1, int arg2)
959 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
960 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
961 docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
962 str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
963 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
968 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
970 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
971 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
972 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
973 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
974 str = subst(str, lyx::from_ascii("%2$s"), arg2);
975 str = subst(str, lyx::from_ascii("%3$s"), arg3);
976 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
981 docstring bformat(docstring const & fmt,
982 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
984 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
985 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
986 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
987 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
988 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
989 str = subst(str, lyx::from_ascii("%2$s"), arg2);
990 str = subst(str, lyx::from_ascii("%3$s"), arg3);
991 str = subst(str, lyx::from_ascii("%4$s"), arg4);
992 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
998 } // namespace support