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 docstring const escape(docstring const & lab)
707 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
708 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
710 for (docstring::size_type i = 0; i < lab.length(); ++i) {
711 char_type c = lab[i];
712 if (c >= 128 || c == '=' || c == '%') {
713 // Although char_type is a 32 bit type we know that
714 // UCS4 occupies only 21 bits, so we don't need to
715 // encode bigger values. Test for 2^24 because we
716 // can encode that with the 6 hex digits that are
717 // needed for 21 bits anyway.
718 BOOST_ASSERT(c < (1 << 24));
720 enc += hexdigit[(c>>20) & 15];
721 enc += hexdigit[(c>>16) & 15];
722 enc += hexdigit[(c>>12) & 15];
723 enc += hexdigit[(c>> 8) & 15];
724 enc += hexdigit[(c>> 4) & 15];
725 enc += hexdigit[ c & 15];
734 /// gives a vector of stringparts which have the delimiter delim
735 vector<string> const getVectorFromString(string const & str,
736 string const & delim)
738 // Lars would like this code to go, but for now his replacement (below)
739 // doesn't fullfil the same function. I have, therefore, reactivated the
740 // old code for now. Angus 11 Nov 2002.
745 string keys = rtrim(str);
747 string::size_type const idx = keys.find(delim);
748 if (idx == string::npos) {
749 vec.push_back(ltrim(keys));
752 string const key = trim(keys.substr(0, idx));
755 string::size_type const start = idx + delim.size();
756 keys = keys.substr(start);
760 boost::char_separator<char> sep(delim.c_str());
761 boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
762 return vector<string>(tokens.begin(), tokens.end());
767 // the same vice versa
768 string const getStringFromVector(vector<string> const & vec,
769 string const & delim)
773 for (vector<string>::const_iterator it = vec.begin();
774 it != vec.end(); ++it) {
775 string item = trim(*it);
786 int findToken(char const * const str[], string const & search_token)
790 while (str[i][0] && str[i] != search_token)
798 docstring const externalLineEnding(docstring const & str)
800 #if defined(__APPLE__)
801 // The MAC clipboard uses \r for lineendings, and we use \n
802 return subst(str, '\n', '\r');
803 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
804 // Windows clipboard uses \r\n for lineendings, and we use \n
805 return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
812 docstring const internalLineEnding(docstring const & str)
814 docstring const s = subst(str,
815 lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
816 return subst(s, '\r', '\n');
820 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
824 docstring bformat(docstring const & fmt, int arg1)
826 return (boost::basic_format<char_type>(fmt) % arg1).str();
831 docstring bformat(docstring const & fmt, long arg1)
833 return (boost::basic_format<char_type>(fmt) % arg1).str();
838 docstring bformat(docstring const & fmt, unsigned int arg1)
840 return (boost::basic_format<char_type>(fmt) % arg1).str();
845 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
847 return (boost::basic_format<char_type>(fmt) % arg1).str();
852 docstring bformat(docstring const & fmt, char * arg1)
854 return (boost::basic_format<char_type>(fmt) % arg1).str();
859 docstring bformat(docstring const & fmt, int arg1, int arg2)
861 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
866 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
868 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
873 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
875 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
880 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
882 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
887 docstring bformat(docstring const & fmt,
888 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
890 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
896 docstring bformat(docstring const & fmt, int arg1)
898 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
899 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
900 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
905 docstring bformat(docstring const & fmt, long arg1)
907 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
908 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
909 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
914 docstring bformat(docstring const & fmt, unsigned int arg1)
916 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
917 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
918 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
923 docstring bformat(docstring const & fmt, docstring arg1)
925 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
926 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
927 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
932 docstring bformat(docstring const & fmt, char * arg1)
934 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
935 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
936 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
941 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
943 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
944 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
945 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
946 str = subst(str, lyx::from_ascii("%2$s"), arg2);
947 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
952 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
954 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
955 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
956 docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
957 str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
958 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
963 docstring bformat(docstring const & fmt, int arg1, int arg2)
965 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
966 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
967 docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
968 str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
969 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
974 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
976 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
977 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
978 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
979 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
980 str = subst(str, lyx::from_ascii("%2$s"), arg2);
981 str = subst(str, lyx::from_ascii("%3$s"), arg3);
982 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
987 docstring bformat(docstring const & fmt,
988 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
990 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
991 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
992 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
993 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
994 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
995 str = subst(str, lyx::from_ascii("%2$s"), arg2);
996 str = subst(str, lyx::from_ascii("%3$s"), arg3);
997 str = subst(str, lyx::from_ascii("%4$s"), arg4);
998 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1004 } // namespace support