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"
16 #include "support/convert.h"
17 #include "support/debug.h"
18 #include "support/lyxlib.h"
19 #include "support/qstring_helpers.h"
20 #include "support/textutils.h"
22 #include <boost/tokenizer.hpp>
23 #include <boost/assert.hpp>
36 #ifndef CXX_GLOBAL_CSTD
45 // Using this allows us to have docstring default arguments in headers
46 // without #include "support/docstring" there.
47 docstring const & empty_docstring()
53 // Using this allows us to have std::string default arguments in headers
54 // without #include <string>
55 std::string const & empty_string()
62 * Convert a QChar into a UCS4 character.
63 * This is a hack (it does only make sense for the common part of the UCS4
64 * and UTF16 encodings) and should not be used.
65 * This does only exist because of performance reasons (a real conversion
66 * using iconv is too slow on windows).
68 static inline char_type qchar_to_ucs4(QChar const & qchar)
70 BOOST_ASSERT(is_utf16(static_cast<char_type>(qchar.unicode())));
71 return static_cast<char_type>(qchar.unicode());
76 * Convert a UCS4 character into a QChar.
77 * This is a hack (it does only make sense for the common part of the UCS4
78 * and UTF16 encodings) and should not be used.
79 * This does only exist because of performance reasons (a real conversion
80 * using iconv is too slow on windows).
82 static inline QChar const ucs4_to_qchar(char_type const ucs4)
84 BOOST_ASSERT(is_utf16(ucs4));
85 return QChar(static_cast<unsigned short>(ucs4));
90 /// Maximum valid UCS4 code point
91 char_type const ucs4_max = 0x10ffff;
95 bool isLetterChar(char_type c)
99 // outside the UCS4 range
101 // assume that all non-utf16 characters are letters
104 return ucs4_to_qchar(c).isLetter();
108 bool isAlphaASCII(char_type c)
110 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
114 bool isPrintable(char_type c)
118 // outside the UCS4 range
120 // assume that all non-utf16 characters are printable
123 return ucs4_to_qchar(c).isPrint();
127 bool isPrintableNonspace(char_type c)
131 // outside the UCS4 range
133 // assume that all non-utf16 characters are printable and
137 QChar const qc = ucs4_to_qchar(c);
138 return qc.isPrint() && !qc.isSpace();
142 bool isSpace(char_type c)
145 // assume that no non-utf16 character is a space
146 // c outside the UCS4 range is catched as well
149 QChar const qc = ucs4_to_qchar(c);
154 bool isDigit(char_type c)
157 // assume that no non-utf16 character is a digit
158 // c outside the UCS4 range is catched as well
160 return ucs4_to_qchar(c).isDigit();
164 bool isDigitASCII(char_type c)
166 return '0' <= c && c <= '9';
171 int compare_no_case(docstring const & s, docstring const & s2)
173 docstring::const_iterator p = s.begin();
174 docstring::const_iterator p2 = s2.begin();
176 while (p != s.end() && p2 != s2.end()) {
177 char_type const lc1 = lowercase(*p);
178 char_type const lc2 = lowercase(*p2);
180 return (lc1 < lc2) ? -1 : 1;
185 if (s.size() == s2.size())
187 if (s.size() < s2.size())
195 template<typename Char>
196 Char ascii_tolower(Char c) {
197 if (c >= 'A' && c <= 'Z')
198 return c - 'A' + 'a';
205 int compare_ascii_no_case(string const & s, string const & s2)
207 string::const_iterator p = s.begin();
208 string::const_iterator p2 = s2.begin();
210 while (p != s.end() && p2 != s2.end()) {
211 int const lc1 = ascii_tolower(*p);
212 int const lc2 = ascii_tolower(*p2);
214 return (lc1 < lc2) ? -1 : 1;
219 if (s.size() == s2.size())
221 if (s.size() < s2.size())
227 int compare_ascii_no_case(docstring const & s, docstring const & s2)
229 docstring::const_iterator p = s.begin();
230 docstring::const_iterator p2 = s2.begin();
232 while (p != s.end() && p2 != s2.end()) {
233 char_type const lc1 = ascii_tolower(*p);
234 char_type const lc2 = ascii_tolower(*p2);
236 return (lc1 < lc2) ? -1 : 1;
241 if (s.size() == s2.size())
243 if (s.size() < s2.size())
249 bool isStrInt(string const & str)
254 // Remove leading and trailing white space chars.
255 string const tmpstr = trim(str);
259 string::const_iterator cit = tmpstr.begin();
263 string::const_iterator end = tmpstr.end();
264 for (; cit != end; ++cit)
265 if (!isdigit((*cit)))
272 bool isStrUnsignedInt(string const & str)
277 // Remove leading and trailing white space chars.
278 string const tmpstr = trim(str);
282 string::const_iterator cit = tmpstr.begin();
283 string::const_iterator end = tmpstr.end();
284 for (; cit != end; ++cit)
285 if (!isdigit((*cit)))
292 bool isStrDbl(string const & str)
297 // Remove leading and trailing white space chars.
298 string const tmpstr = trim(str);
301 // if (tmpstr.count('.') > 1) return false;
303 string::const_iterator cit = tmpstr.begin();
304 bool found_dot = false;
307 string::const_iterator end = tmpstr.end();
308 for (; cit != end; ++cit) {
309 if (!isdigit(*cit) && *cit != '.')
321 static bool isHexChar(char_type c)
333 c == 'a' || c == 'A' ||
334 c == 'b' || c == 'B' ||
335 c == 'c' || c == 'C' ||
336 c == 'd' || c == 'D' ||
337 c == 'e' || c == 'E' ||
338 c == 'f' || c == 'F';
342 bool isHex(docstring const & str)
346 if (str.length() > 2 && str[0] == '0' &&
347 (str[1] == 'x' || str[1] == 'X'))
350 int const len = str.length();
352 for (; index < len; ++index) {
353 if (!isHexChar(str[index]))
360 int hexToInt(docstring const & str)
362 string s = to_ascii(str);
364 sscanf(s.c_str(), "%x", &h);
369 bool isAscii(docstring const & str)
371 int const len = str.length();
372 for (int i = 0; i < len; ++i)
379 bool isAscii(string const & str)
381 int const len = str.length();
382 for (int i = 0; i < len; ++i)
383 if (static_cast<unsigned char>(str[i]) >= 0x80)
389 char lowercase(char c)
391 BOOST_ASSERT(static_cast<unsigned char>(c) < 0x80);
392 return char(tolower(c));
396 char uppercase(char c)
398 BOOST_ASSERT(static_cast<unsigned char>(c) < 0x80);
399 return char(toupper(c));
403 char_type lowercase(char_type c)
406 // We don't know how to lowercase a non-utf16 char
408 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
412 char_type uppercase(char_type c)
415 // We don't know how to uppercase a non-utf16 char
417 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
423 // since we cannot use std::tolower and std::toupper directly in the
424 // calls to std::transform yet, we use these helper clases. (Lgb)
426 struct local_lowercase {
427 char_type operator()(char_type c) const {
429 // We don't know how to lowercase a non-utf16 char
431 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
435 struct local_uppercase {
436 char_type operator()(char_type c) const {
438 // We don't know how to uppercase a non-utf16 char
440 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
444 template<typename Char> struct local_ascii_lowercase {
445 Char operator()(Char c) const { return ascii_tolower(c); }
448 } // end of anon namespace
450 docstring const lowercase(docstring const & a)
453 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
458 docstring const uppercase(docstring const & a)
461 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
466 string const ascii_lowercase(string const & a)
469 transform(tmp.begin(), tmp.end(), tmp.begin(),
470 local_ascii_lowercase<char>());
475 docstring const ascii_lowercase(docstring const & a)
478 transform(tmp.begin(), tmp.end(), tmp.begin(),
479 local_ascii_lowercase<char_type>());
484 bool prefixIs(docstring const & a, char_type c)
492 bool prefixIs(string const & a, string const & pre)
494 size_t const prelen = pre.length();
495 size_t const alen = a.length();
497 if (prelen > alen || a.empty())
499 #if defined(STD_STRING_IS_GOOD)
500 return a.compare(0, prelen, pre) == 0;
502 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
507 bool prefixIs(docstring const & a, docstring const & pre)
509 size_t const prelen = pre.length();
510 size_t const alen = a.length();
512 if (prelen > alen || a.empty())
515 return a.compare(0, prelen, pre) == 0;
519 bool suffixIs(string const & a, char c)
521 if (a.empty()) return false;
522 return a[a.length() - 1] == c;
526 bool suffixIs(docstring const & a, char_type c)
530 return a[a.length() - 1] == c;
534 bool suffixIs(string const & a, string const & suf)
536 size_t const suflen = suf.length();
537 size_t const alen = a.length();
542 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
543 string tmp(a, alen - suflen);
544 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
546 return a.compare(alen - suflen, suflen, suf) == 0;
551 bool containsOnly(string const & s, string const & cset)
553 return s.find_first_not_of(cset) == string::npos;
557 // ale970405+lasgoutt-970425
558 // rewritten to use new string (Lgb)
559 string const token(string const & a, char delim, int n)
567 // Find delimiter or end of string
569 if ((i = a.find(delim, i)) == string::npos)
575 // i is now the n'th delim (or string::npos)
576 if (i == string::npos)
579 k = a.find(delim, i);
580 // k is now the n'th + 1 delim (or string::npos)
582 return a.substr(i, k - i);
586 docstring const token(docstring const & a, char_type delim, int n)
594 // Find delimiter or end of string
596 if ((i = a.find(delim, i)) == docstring::npos)
602 // i is now the n'th delim (or string::npos)
603 if (i == docstring::npos)
606 k = a.find(delim, i);
607 // k is now the n'th + 1 delim (or string::npos)
609 return a.substr(i, k - i);
613 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
614 // rewritten to use new string (Lgb)
615 int tokenPos(string const & a, char delim, string const & tok)
621 while (!str.empty()) {
622 str = split(str, tmptok, delim);
633 /// Substitute all \a oldchar with \a newchar
634 template<typename Ch> inline
635 std::basic_string<Ch> const subst_char(std::basic_string<Ch> const & a,
636 Ch oldchar, Ch newchar)
638 typedef std::basic_string<Ch> String;
640 typename String::iterator lit = tmp.begin();
641 typename String::iterator end = tmp.end();
642 for (; lit != end; ++lit)
643 if ((*lit) == oldchar)
648 /// Substitute all \a oldchar with \a newchar
649 docstring const subst_char(docstring const & a,
650 docstring::value_type oldchar, docstring::value_type newchar)
653 docstring::iterator lit = tmp.begin();
654 docstring::iterator end = tmp.end();
655 for (; lit != end; ++lit)
656 if ((*lit) == oldchar)
662 /// substitutes all instances of \a oldstr with \a newstr
663 template<typename String> inline
664 String const subst_string(String const & a,
665 String const & oldstr, String const & newstr)
667 BOOST_ASSERT(!oldstr.empty());
670 size_t const olen = oldstr.length();
671 while ((i = lstr.find(oldstr, i)) != string::npos) {
672 lstr.replace(i, olen, newstr);
673 i += newstr.length(); // We need to be sure that we dont
674 // use the same i over and over again.
679 docstring const subst_string(docstring const & a,
680 docstring const & oldstr, docstring const & newstr)
682 BOOST_ASSERT(!oldstr.empty());
685 size_t const olen = oldstr.length();
686 while ((i = lstr.find(oldstr, i)) != string::npos) {
687 lstr.replace(i, olen, newstr);
688 i += newstr.length(); // We need to be sure that we dont
689 // use the same i over and over again.
697 string const subst(string const & a, char oldchar, char newchar)
699 return subst_char(a, oldchar, newchar);
703 docstring const subst(docstring const & a,
704 char_type oldchar, char_type newchar)
706 return subst_char(a, oldchar, newchar);
710 string const subst(string const & a,
711 string const & oldstr, string const & newstr)
713 return subst_string(a, oldstr, newstr);
717 docstring const subst(docstring const & a,
718 docstring const & oldstr, docstring const & newstr)
720 return subst_string(a, oldstr, newstr);
724 docstring const trim(docstring const & a, char const * p)
728 if (a.empty() || !*p)
731 docstring s = from_ascii(p);
732 size_t r = a.find_last_not_of(s);
733 size_t l = a.find_first_not_of(s);
735 // Is this the minimal test? (lgb)
736 if (r == docstring::npos && l == docstring::npos)
739 return a.substr(l, r - l + 1);
743 string const trim(string const & a, char const * p)
747 if (a.empty() || !*p)
750 size_t r = a.find_last_not_of(p);
751 size_t l = a.find_first_not_of(p);
753 // Is this the minimal test? (lgb)
754 if (r == string::npos && l == string::npos)
757 return a.substr(l, r - l + 1);
761 string const rtrim(string const & a, char const * p)
765 if (a.empty() || !*p)
768 size_t r = a.find_last_not_of(p);
770 // Is this test really needed? (Lgb)
771 if (r == string::npos)
774 return a.substr(0, r + 1);
778 docstring const rtrim(docstring const & a, char const * p)
782 if (a.empty() || !*p)
785 size_t r = a.find_last_not_of(from_ascii(p));
787 // Is this test really needed? (Lgb)
788 if (r == docstring::npos)
791 return a.substr(0, r + 1);
795 string const ltrim(string const & a, char const * p)
798 if (a.empty() || !*p)
800 size_t l = a.find_first_not_of(p);
801 if (l == string::npos)
803 return a.substr(l, string::npos);
807 docstring const ltrim(docstring const & a, char const * p)
810 if (a.empty() || !*p)
812 size_t l = a.find_first_not_of(from_ascii(p));
813 if (l == docstring::npos)
815 return a.substr(l, docstring::npos);
820 template<typename String, typename Char> inline
821 String const doSplit(String const & a, String & piece, Char delim)
824 size_t i = a.find(delim);
825 if (i == a.length() - 1) {
826 piece = a.substr(0, i);
827 } else if (i != String::npos) {
828 piece = a.substr(0, i);
829 tmp = a.substr(i + 1);
832 tmp = a.substr(i + 1);
839 template<typename Char> inline
840 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
843 size_t i = a.find(delim);
844 if (i == a.length() - 1) {
845 piece = a.substr(0, i);
846 } else if (i != docstring::npos) {
847 piece = a.substr(0, i);
848 tmp = a.substr(i + 1);
851 tmp = a.substr(i + 1);
861 string const split(string const & a, string & piece, char delim)
863 return doSplit(a, piece, delim);
867 docstring const split(docstring const & a, docstring & piece, char_type delim)
869 return doSplit(a, piece, delim);
873 string const split(string const & a, char delim)
876 size_t i = a.find(delim);
877 if (i != string::npos) // found delim
878 tmp = a.substr(i + 1);
884 string const rsplit(string const & a, string & piece, char delim)
887 size_t i = a.rfind(delim);
888 if (i != string::npos) { // delimiter was found
889 piece = a.substr(0, i);
890 tmp = a.substr(i + 1);
891 } else { // delimiter was not found
898 docstring const escape(docstring const & lab)
900 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
901 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
903 for (size_t i = 0; i < lab.length(); ++i) {
904 char_type c = lab[i];
905 if (c >= 128 || c == '=' || c == '%') {
906 // Although char_type is a 32 bit type we know that
907 // UCS4 occupies only 21 bits, so we don't need to
908 // encode bigger values. Test for 2^24 because we
909 // can encode that with the 6 hex digits that are
910 // needed for 21 bits anyway.
911 BOOST_ASSERT(c < (1 << 24));
913 enc += hexdigit[(c>>20) & 15];
914 enc += hexdigit[(c>>16) & 15];
915 enc += hexdigit[(c>>12) & 15];
916 enc += hexdigit[(c>> 8) & 15];
917 enc += hexdigit[(c>> 4) & 15];
918 enc += hexdigit[ c & 15];
929 template<typename String> vector<String> const
930 getVectorFromStringT(String const & str, String const & delim)
932 // Lars would like this code to go, but for now his replacement (below)
933 // doesn't fullfil the same function. I have, therefore, reactivated the
934 // old code for now. Angus 11 Nov 2002.
939 String keys = rtrim(str);
941 size_t const idx = keys.find(delim);
942 if (idx == String::npos) {
943 vec.push_back(ltrim(keys));
946 String const key = trim(keys.substr(0, idx));
949 size_t const start = idx + delim.size();
950 keys = keys.substr(start);
954 typedef boost::char_separator<typename String::value_type> Separator;
955 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
956 Separator sep(delim.c_str());
957 Tokenizer tokens(str, sep);
958 return vector<String>(tokens.begin(), tokens.end());
965 vector<string> const getVectorFromString(string const & str,
966 string const & delim)
968 return getVectorFromStringT<string>(str, delim);
972 vector<docstring> const getVectorFromString(docstring const & str,
973 docstring const & delim)
975 return getVectorFromStringT<docstring>(str, delim);
979 // the same vice versa
980 string const getStringFromVector(vector<string> const & vec,
981 string const & delim)
985 for (vector<string>::const_iterator it = vec.begin();
986 it != vec.end(); ++it) {
987 string item = trim(*it);
998 int findToken(char const * const str[], string const & search_token)
1002 while (str[i][0] && str[i] != search_token)
1010 docstring const externalLineEnding(docstring const & str)
1012 #if defined(__APPLE__)
1013 // The MAC clipboard uses \r for lineendings, and we use \n
1014 return subst(str, '\n', '\r');
1015 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
1016 // Windows clipboard uses \r\n for lineendings, and we use \n
1017 return subst(str, from_ascii("\n"), from_ascii("\r\n"));
1024 docstring const internalLineEnding(docstring const & str)
1026 docstring const s = subst(str, from_ascii("\r\n"), from_ascii("\n"));
1027 return subst(s, '\r', '\n');
1032 docstring bformat(docstring const & fmt, int arg1)
1034 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1035 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1036 return subst(str, from_ascii("%%"), from_ascii("%"));
1041 docstring bformat(docstring const & fmt, long arg1)
1043 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1044 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1045 return subst(str, from_ascii("%%"), from_ascii("%"));
1050 docstring bformat(docstring const & fmt, unsigned int arg1)
1052 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1053 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1054 return subst(str, from_ascii("%%"), from_ascii("%"));
1059 docstring bformat(docstring const & fmt, docstring arg1)
1061 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1062 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1063 return subst(str, from_ascii("%%"), from_ascii("%"));
1068 docstring bformat(docstring const & fmt, char * arg1)
1070 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1071 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1072 return subst(str, from_ascii("%%"), from_ascii("%"));
1077 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1079 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1080 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1081 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1082 str = subst(str, from_ascii("%2$s"), arg2);
1083 return subst(str, from_ascii("%%"), from_ascii("%"));
1088 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1090 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1091 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1092 docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1093 str = subst(fmt, from_ascii("%2$s"), arg2);
1094 return subst(str, from_ascii("%%"), from_ascii("%"));
1099 docstring bformat(docstring const & fmt, int arg1, int arg2)
1101 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1102 BOOST_ASSERT(contains(fmt, from_ascii("%2$d")));
1103 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1104 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1105 return subst(str, from_ascii("%%"), from_ascii("%"));
1110 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1112 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1113 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1114 BOOST_ASSERT(contains(fmt, from_ascii("%3$s")));
1115 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1116 str = subst(str, from_ascii("%2$s"), arg2);
1117 str = subst(str, from_ascii("%3$s"), arg3);
1118 return subst(str, from_ascii("%%"), from_ascii("%"));
1123 docstring bformat(docstring const & fmt,
1124 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1126 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1127 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1128 BOOST_ASSERT(contains(fmt, from_ascii("%3$s")));
1129 BOOST_ASSERT(contains(fmt, from_ascii("%4$s")));
1130 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1131 str = subst(str, from_ascii("%2$s"), arg2);
1132 str = subst(str, from_ascii("%3$s"), arg3);
1133 str = subst(str, from_ascii("%4$s"), arg4);
1134 return subst(str, from_ascii("%%"), from_ascii("%"));
1137 } // namespace support