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>
35 #ifdef LIBC_WCTYPE_USES_UCS4
36 // We can use the libc ctype functions because we unset the LC_CTYPE
37 // category of the current locale in gettext.C
40 // Steal some code from somewhere else, e.g. glib (look at gunicode.h)
41 // The code that we currently use does not really work.
51 #ifndef CXX_GLOBAL_CSTD
61 int compare_no_case(string const & s, string const & s2)
63 string::const_iterator p = s.begin();
64 string::const_iterator p2 = s2.begin();
66 while (p != s.end() && p2 != s2.end()) {
67 int const lc1 = tolower(*p);
68 int const lc2 = tolower(*p2);
70 return (lc1 < lc2) ? -1 : 1;
75 if (s.size() == s2.size())
77 if (s.size() < s2.size())
83 int compare_no_case(docstring const & s, docstring const & s2)
85 docstring::const_iterator p = s.begin();
86 docstring::const_iterator p2 = s2.begin();
88 while (p != s.end() && p2 != s2.end()) {
89 char_type const lc1 = lowercase(*p);
90 char_type const lc2 = lowercase(*p2);
92 return (lc1 < lc2) ? -1 : 1;
97 if (s.size() == s2.size())
99 if (s.size() < s2.size())
107 template<typename Char>
108 Char ascii_tolower(Char c) {
109 if (c >= 'A' && c <= 'Z')
110 return c - 'A' + 'a';
117 int compare_ascii_no_case(string const & s, string const & s2)
119 string::const_iterator p = s.begin();
120 string::const_iterator p2 = s2.begin();
122 while (p != s.end() && p2 != s2.end()) {
123 int const lc1 = ascii_tolower(*p);
124 int const lc2 = ascii_tolower(*p2);
126 return (lc1 < lc2) ? -1 : 1;
131 if (s.size() == s2.size())
133 if (s.size() < s2.size())
139 int compare_ascii_no_case(docstring const & s, docstring const & s2)
141 docstring::const_iterator p = s.begin();
142 docstring::const_iterator p2 = s2.begin();
144 while (p != s.end() && p2 != s2.end()) {
145 char_type const lc1 = ascii_tolower(*p);
146 char_type const lc2 = ascii_tolower(*p2);
148 return (lc1 < lc2) ? -1 : 1;
153 if (s.size() == s2.size())
155 if (s.size() < s2.size())
161 int compare_no_case(string const & s, string const & s2, unsigned int len)
163 string::const_iterator p = s.begin();
164 string::const_iterator p2 = s2.begin();
166 while (i < len && p != s.end() && p2 != s2.end()) {
167 int const lc1 = tolower(*p);
168 int const lc2 = tolower(*p2);
170 return (lc1 < lc2) ? -1 : 1;
176 if (s.size() >= len && s2.size() >= len)
178 if (s.size() < s2.size())
184 bool isStrInt(string const & str)
186 if (str.empty()) return false;
188 // Remove leading and trailing white space chars.
189 string const tmpstr = trim(str);
190 if (tmpstr.empty()) return false;
192 string::const_iterator cit = tmpstr.begin();
193 if ((*cit) == '-') ++cit;
194 string::const_iterator end = tmpstr.end();
195 for (; cit != end; ++cit) {
196 if (!isdigit((*cit))) return false;
202 bool isStrUnsignedInt(string const & str)
204 if (str.empty()) return false;
206 // Remove leading and trailing white space chars.
207 string const tmpstr = trim(str);
208 if (tmpstr.empty()) return false;
210 string::const_iterator cit = tmpstr.begin();
211 string::const_iterator end = tmpstr.end();
212 for (; cit != end; ++cit) {
213 if (!isdigit((*cit))) return false;
219 bool isStrDbl(string const & str)
221 if (str.empty()) return false;
223 // Remove leading and trailing white space chars.
224 string const tmpstr = trim(str);
225 if (tmpstr.empty()) return false;
226 // if (1 < tmpstr.count('.')) return false;
228 string::const_iterator cit = tmpstr.begin();
229 bool found_dot(false);
230 if ((*cit) == '-') ++cit;
231 string::const_iterator end = tmpstr.end();
232 for (; cit != end; ++cit) {
252 bool isHexChar(char_type c)
264 c == 'a' || c == 'A' ||
265 c == 'b' || c == 'B' ||
266 c == 'c' || c == 'C' ||
267 c == 'd' || c == 'D' ||
268 c == 'e' || c == 'E' ||
269 c == 'f' || c == 'F';
275 bool isHex(docstring const & str)
279 if (str.length() > 2 && str[0] == '0' &&
280 (str[1] == 'x' || str[1] == 'X'))
283 int const len = str.length();
285 for (; index < len; ++index) {
286 if (!isHexChar(str[index]))
293 int hexToInt(docstring const & str)
295 string s = to_ascii(str);
297 sscanf(s.c_str(), "%x", &h);
302 bool isAscii(docstring const & str)
304 int const len = str.length();
305 for (int i = 0; i < len; ++i)
312 char lowercase(char c)
314 return char(tolower(c));
318 char uppercase(char c)
320 return char(toupper(c));
325 // for lowercase() and uppercase() function below when wchar_t is not used:
326 // 1) std::tolower() and std::toupper() are templates that
327 // compile fine with char_type. With the test (c >= 256) we
328 // do not trust these function to do the right thing with
330 // 2) these functions use the current locale, which is wrong
331 // if it is not latin1 based (latin1 is a subset of UCS4).
333 char_type lowercase(char_type c)
335 #ifdef LIBC_WCTYPE_USES_UCS4
346 char_type uppercase(char_type c)
348 #ifdef LIBC_WCTYPE_USES_UCS4
361 // since we cannot use std::tolower and std::toupper directly in the
362 // calls to std::transform yet, we use these helper clases. (Lgb)
364 template<typename Char> struct local_lowercase {
365 Char operator()(Char c) const {
370 struct local_uppercase {
371 char operator()(char c) const {
376 struct local_ascii_lowercase {
377 char operator()(char c) const {
378 return ascii_tolower(c);
382 } // end of anon namespace
384 string const lowercase(string const & a)
387 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase<char>());
392 docstring const lowercase(docstring const & a)
395 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase<char_type>());
400 string const uppercase(string const & a)
403 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
408 string const ascii_lowercase(string const & a)
411 transform(tmp.begin(), tmp.end(), tmp.begin(),
412 local_ascii_lowercase());
417 bool prefixIs(string const & a, string const & pre)
419 string::size_type const prelen = pre.length();
420 string::size_type const alen = a.length();
422 if (prelen > alen || a.empty())
425 #if defined(STD_STRING_IS_GOOD)
426 return a.compare(0, prelen, pre) == 0;
428 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
434 bool prefixIs(docstring const & a, docstring const & pre)
436 docstring::size_type const prelen = pre.length();
437 docstring::size_type const alen = a.length();
439 if (prelen > alen || a.empty())
442 return a.compare(0, prelen, pre) == 0;
446 bool suffixIs(string const & a, char c)
448 if (a.empty()) return false;
449 return a[a.length() - 1] == c;
453 bool suffixIs(string const & a, string const & suf)
455 string::size_type const suflen = suf.length();
456 string::size_type const alen = a.length();
461 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
462 string tmp(a, alen - suflen);
463 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
465 return a.compare(alen - suflen, suflen, suf) == 0;
471 bool containsOnly(string const & s, string const & cset)
473 return s.find_first_not_of(cset) == string::npos;
477 // ale970405+lasgoutt-970425
478 // rewritten to use new string (Lgb)
479 string const token(string const & a, char delim, int n)
481 if (a.empty()) return string();
483 string::size_type k = 0;
484 string::size_type i = 0;
486 // Find delimiter or end of string
488 if ((i = a.find(delim, i)) == string::npos)
492 // i is now the n'th delim (or string::npos)
493 if (i == string::npos) return string();
494 k = a.find(delim, i);
495 // k is now the n'th + 1 delim (or string::npos)
497 return a.substr(i, k - i);
501 docstring const token(docstring const & a, char_type delim, int n)
503 if (a.empty()) return docstring();
505 string::size_type k = 0;
506 string::size_type i = 0;
508 // Find delimiter or end of string
510 if ((i = a.find(delim, i)) == docstring::npos)
514 // i is now the n'th delim (or string::npos)
515 if (i == docstring::npos) return docstring();
516 k = a.find(delim, i);
517 // k is now the n'th + 1 delim (or string::npos)
519 return a.substr(i, k - i);
523 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
524 // rewritten to use new string (Lgb)
525 int tokenPos(string const & a, char delim, string const & tok)
531 while (!str.empty()) {
532 str = split(str, tmptok, delim);
543 /// Substitute all \a oldchar with \a newchar
544 template<typename Ch> inline
545 std::basic_string<Ch> const subst_char(std::basic_string<Ch> const & a,
546 Ch oldchar, Ch newchar)
548 typedef std::basic_string<Ch> String;
550 typename String::iterator lit = tmp.begin();
551 typename String::iterator end = tmp.end();
552 for (; lit != end; ++lit)
553 if ((*lit) == oldchar)
559 /// substitutes all instances of \a oldstr with \a newstr
560 template<typename String> inline
561 String const subst_string(String const & a,
562 String const & oldstr, String const & newstr)
564 BOOST_ASSERT(!oldstr.empty());
566 typename String::size_type i = 0;
567 typename String::size_type const olen = oldstr.length();
568 while ((i = lstr.find(oldstr, i)) != string::npos) {
569 lstr.replace(i, olen, newstr);
570 i += newstr.length(); // We need to be sure that we dont
571 // use the same i over and over again.
579 string const subst(string const & a, char oldchar, char newchar)
581 return subst_char(a, oldchar, newchar);
585 docstring const subst(docstring const & a,
586 char_type oldchar, char_type newchar)
588 return subst_char(a, oldchar, newchar);
592 string const subst(string const & a,
593 string const & oldstr, string const & newstr)
595 return subst_string(a, oldstr, newstr);
599 docstring const subst(docstring const & a,
600 docstring const & oldstr, docstring const & newstr)
602 return subst_string(a, oldstr, newstr);
606 docstring const trim(docstring const & a, char const * p)
610 if (a.empty() || !*p)
613 docstring s = lyx::from_ascii(p);
614 docstring::size_type r = a.find_last_not_of(s);
615 docstring::size_type l = a.find_first_not_of(s);
617 // Is this the minimal test? (lgb)
618 if (r == docstring::npos && l == docstring::npos)
621 return a.substr(l, r - l + 1);
625 string const trim(string const & a, char const * p)
629 if (a.empty() || !*p)
632 string::size_type r = a.find_last_not_of(p);
633 string::size_type l = a.find_first_not_of(p);
635 // Is this the minimal test? (lgb)
636 if (r == string::npos && l == string::npos)
639 return a.substr(l, r - l + 1);
643 string const rtrim(string const & a, char const * p)
647 if (a.empty() || !*p)
650 string::size_type r = a.find_last_not_of(p);
652 // Is this test really needed? (Lgb)
653 if (r == string::npos)
656 return a.substr(0, r + 1);
660 string const ltrim(string const & a, char const * p)
663 if (a.empty() || !*p)
665 string::size_type l = a.find_first_not_of(p);
666 if (l == string::npos)
668 return a.substr(l, string::npos);
672 docstring const ltrim(docstring const & a, char const * p)
675 if (a.empty() || !*p)
677 size_t l = a.find_first_not_of(from_ascii(p));
678 if (l == docstring::npos)
680 return a.substr(l, docstring::npos);
685 template<typename String, typename Char> inline
686 String const doSplit(String const & a, String & piece, Char delim)
689 typename String::size_type i = a.find(delim);
690 if (i == a.length() - 1) {
691 piece = a.substr(0, i);
692 } else if (i != String::npos) {
693 piece = a.substr(0, i);
694 tmp = a.substr(i + 1);
697 tmp = a.substr(i + 1);
707 string const split(string const & a, string & piece, char delim)
709 return doSplit(a, piece, delim);
713 docstring const split(docstring const & a, docstring & piece, char_type delim)
715 return doSplit(a, piece, delim);
719 string const split(string const & a, char delim)
722 string::size_type i = a.find(delim);
723 if (i != string::npos) // found delim
724 tmp = a.substr(i + 1);
730 string const rsplit(string const & a, string & piece, char delim)
733 string::size_type i = a.rfind(delim);
734 if (i != string::npos) { // delimiter was found
735 piece = a.substr(0, i);
736 tmp = a.substr(i + 1);
737 } else { // delimiter was not found
744 docstring const escape(docstring const & lab)
746 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
747 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
749 for (docstring::size_type i = 0; i < lab.length(); ++i) {
750 char_type c = lab[i];
751 if (c >= 128 || c == '=' || c == '%') {
752 // Although char_type is a 32 bit type we know that
753 // UCS4 occupies only 21 bits, so we don't need to
754 // encode bigger values. Test for 2^24 because we
755 // can encode that with the 6 hex digits that are
756 // needed for 21 bits anyway.
757 BOOST_ASSERT(c < (1 << 24));
759 enc += hexdigit[(c>>20) & 15];
760 enc += hexdigit[(c>>16) & 15];
761 enc += hexdigit[(c>>12) & 15];
762 enc += hexdigit[(c>> 8) & 15];
763 enc += hexdigit[(c>> 4) & 15];
764 enc += hexdigit[ c & 15];
773 /// gives a vector of stringparts which have the delimiter delim
774 vector<string> const getVectorFromString(string const & str,
775 string const & delim)
777 // Lars would like this code to go, but for now his replacement (below)
778 // doesn't fullfil the same function. I have, therefore, reactivated the
779 // old code for now. Angus 11 Nov 2002.
784 string keys = rtrim(str);
786 string::size_type const idx = keys.find(delim);
787 if (idx == string::npos) {
788 vec.push_back(ltrim(keys));
791 string const key = trim(keys.substr(0, idx));
794 string::size_type const start = idx + delim.size();
795 keys = keys.substr(start);
799 boost::char_separator<char> sep(delim.c_str());
800 boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
801 return vector<string>(tokens.begin(), tokens.end());
806 // the same vice versa
807 string const getStringFromVector(vector<string> const & vec,
808 string const & delim)
812 for (vector<string>::const_iterator it = vec.begin();
813 it != vec.end(); ++it) {
814 string item = trim(*it);
825 int findToken(char const * const str[], string const & search_token)
829 while (str[i][0] && str[i] != search_token)
837 docstring const externalLineEnding(docstring const & str)
839 #if defined(__APPLE__)
840 // The MAC clipboard uses \r for lineendings, and we use \n
841 return subst(str, '\n', '\r');
842 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
843 // Windows clipboard uses \r\n for lineendings, and we use \n
844 return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
851 docstring const internalLineEnding(docstring const & str)
853 docstring const s = subst(str,
854 lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
855 return subst(s, '\r', '\n');
859 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
863 docstring bformat(docstring const & fmt, int arg1)
865 return (boost::basic_format<char_type>(fmt) % arg1).str();
870 docstring bformat(docstring const & fmt, long arg1)
872 return (boost::basic_format<char_type>(fmt) % arg1).str();
877 docstring bformat(docstring const & fmt, unsigned int arg1)
879 return (boost::basic_format<char_type>(fmt) % arg1).str();
884 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
886 return (boost::basic_format<char_type>(fmt) % arg1).str();
891 docstring bformat(docstring const & fmt, char * arg1)
893 return (boost::basic_format<char_type>(fmt) % arg1).str();
898 docstring bformat(docstring const & fmt, int arg1, int arg2)
900 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
905 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
907 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
912 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
914 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
919 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
921 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
926 docstring bformat(docstring const & fmt,
927 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
929 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
935 docstring bformat(docstring const & fmt, int arg1)
937 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
938 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
939 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
944 docstring bformat(docstring const & fmt, long arg1)
946 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
947 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
948 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
953 docstring bformat(docstring const & fmt, unsigned int arg1)
955 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
956 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
957 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
962 docstring bformat(docstring const & fmt, docstring arg1)
964 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
965 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
966 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
971 docstring bformat(docstring const & fmt, char * arg1)
973 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
974 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
975 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
980 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
982 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
983 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
984 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
985 str = subst(str, lyx::from_ascii("%2$s"), arg2);
986 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
991 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
993 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
994 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
995 docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
996 str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
997 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1002 docstring bformat(docstring const & fmt, int arg1, int arg2)
1004 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
1005 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
1006 docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
1007 str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
1008 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1013 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1015 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1016 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
1017 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
1018 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
1019 str = subst(str, lyx::from_ascii("%2$s"), arg2);
1020 str = subst(str, lyx::from_ascii("%3$s"), arg3);
1021 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1026 docstring bformat(docstring const & fmt,
1027 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1029 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1030 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
1031 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
1032 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
1033 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
1034 str = subst(str, lyx::from_ascii("%2$s"), arg2);
1035 str = subst(str, lyx::from_ascii("%3$s"), arg3);
1036 str = subst(str, lyx::from_ascii("%4$s"), arg4);
1037 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1043 } // namespace support