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>
35 // Using this allows us to have docstring default arguments in headers
36 // without #include "support/docstring" there.
37 docstring const & empty_docstring()
43 // Using this allows us to have string default arguments in headers
44 // without #include <string>
45 string const & empty_string()
52 * Convert a QChar into a UCS4 character.
53 * This is a hack (it does only make sense for the common part of the UCS4
54 * and UTF16 encodings) and should not be used.
55 * This does only exist because of performance reasons (a real conversion
56 * using iconv is too slow on windows).
58 static inline char_type qchar_to_ucs4(QChar const & qchar)
60 BOOST_ASSERT(is_utf16(static_cast<char_type>(qchar.unicode())));
61 return static_cast<char_type>(qchar.unicode());
66 * Convert a UCS4 character into a QChar.
67 * This is a hack (it does only make sense for the common part of the UCS4
68 * and UTF16 encodings) and should not be used.
69 * This does only exist because of performance reasons (a real conversion
70 * using iconv is too slow on windows).
72 static inline QChar const ucs4_to_qchar(char_type const ucs4)
74 BOOST_ASSERT(is_utf16(ucs4));
75 return QChar(static_cast<unsigned short>(ucs4));
80 /// Maximum valid UCS4 code point
81 char_type const ucs4_max = 0x10ffff;
85 bool isLetterChar(char_type c)
89 // outside the UCS4 range
91 // assume that all non-utf16 characters are letters
94 return ucs4_to_qchar(c).isLetter();
98 bool isAlphaASCII(char_type c)
100 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
104 bool isPrintable(char_type c)
108 // outside the UCS4 range
110 // assume that all non-utf16 characters are printable
113 return ucs4_to_qchar(c).isPrint();
117 bool isPrintableNonspace(char_type c)
121 // outside the UCS4 range
123 // assume that all non-utf16 characters are printable and
127 QChar const qc = ucs4_to_qchar(c);
128 return qc.isPrint() && !qc.isSpace();
132 bool isSpace(char_type c)
135 // assume that no non-utf16 character is a space
136 // c outside the UCS4 range is catched as well
139 QChar const qc = ucs4_to_qchar(c);
144 bool isDigit(char_type c)
147 // assume that no non-utf16 character is a digit
148 // c outside the UCS4 range is catched as well
150 return ucs4_to_qchar(c).isDigit();
154 bool isDigitASCII(char_type c)
156 return '0' <= c && c <= '9';
161 int compare_no_case(docstring const & s, docstring const & s2)
163 docstring::const_iterator p = s.begin();
164 docstring::const_iterator p2 = s2.begin();
166 while (p != s.end() && p2 != s2.end()) {
167 char_type const lc1 = lowercase(*p);
168 char_type const lc2 = lowercase(*p2);
170 return (lc1 < lc2) ? -1 : 1;
175 if (s.size() == s2.size())
177 if (s.size() < s2.size())
185 template<typename Char>
186 Char ascii_tolower(Char c) {
187 if (c >= 'A' && c <= 'Z')
188 return c - 'A' + 'a';
195 int compare_ascii_no_case(string const & s, string const & s2)
197 string::const_iterator p = s.begin();
198 string::const_iterator p2 = s2.begin();
200 while (p != s.end() && p2 != s2.end()) {
201 int const lc1 = ascii_tolower(*p);
202 int const lc2 = ascii_tolower(*p2);
204 return (lc1 < lc2) ? -1 : 1;
209 if (s.size() == s2.size())
211 if (s.size() < s2.size())
217 int compare_ascii_no_case(docstring const & s, docstring const & s2)
219 docstring::const_iterator p = s.begin();
220 docstring::const_iterator p2 = s2.begin();
222 while (p != s.end() && p2 != s2.end()) {
223 char_type const lc1 = ascii_tolower(*p);
224 char_type const lc2 = ascii_tolower(*p2);
226 return (lc1 < lc2) ? -1 : 1;
231 if (s.size() == s2.size())
233 if (s.size() < s2.size())
239 bool isStrInt(string const & str)
244 // Remove leading and trailing white space chars.
245 string const tmpstr = trim(str);
249 string::const_iterator cit = tmpstr.begin();
253 string::const_iterator end = tmpstr.end();
254 for (; cit != end; ++cit)
255 if (!isdigit((*cit)))
262 bool isStrUnsignedInt(string const & str)
267 // Remove leading and trailing white space chars.
268 string const tmpstr = trim(str);
272 string::const_iterator cit = tmpstr.begin();
273 string::const_iterator end = tmpstr.end();
274 for (; cit != end; ++cit)
275 if (!isdigit((*cit)))
282 bool isStrDbl(string const & str)
287 // Remove leading and trailing white space chars.
288 string const tmpstr = trim(str);
291 // if (tmpstr.count('.') > 1) return false;
293 string::const_iterator cit = tmpstr.begin();
294 bool found_dot = false;
297 string::const_iterator end = tmpstr.end();
298 for (; cit != end; ++cit) {
299 if (!isdigit(*cit) && *cit != '.')
311 static bool isHexChar(char_type c)
323 c == 'a' || c == 'A' ||
324 c == 'b' || c == 'B' ||
325 c == 'c' || c == 'C' ||
326 c == 'd' || c == 'D' ||
327 c == 'e' || c == 'E' ||
328 c == 'f' || c == 'F';
332 bool isHex(docstring const & str)
336 if (str.length() > 2 && str[0] == '0' &&
337 (str[1] == 'x' || str[1] == 'X'))
340 int const len = str.length();
342 for (; index < len; ++index) {
343 if (!isHexChar(str[index]))
350 int hexToInt(docstring const & str)
352 string s = to_ascii(str);
354 sscanf(s.c_str(), "%x", &h);
359 bool isAscii(docstring const & str)
361 int const len = str.length();
362 for (int i = 0; i < len; ++i)
369 bool isAscii(string const & str)
371 int const len = str.length();
372 for (int i = 0; i < len; ++i)
373 if (static_cast<unsigned char>(str[i]) >= 0x80)
379 char lowercase(char c)
381 BOOST_ASSERT(static_cast<unsigned char>(c) < 0x80);
382 return char(tolower(c));
386 char uppercase(char c)
388 BOOST_ASSERT(static_cast<unsigned char>(c) < 0x80);
389 return char(toupper(c));
393 char_type lowercase(char_type c)
396 // We don't know how to lowercase a non-utf16 char
398 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
402 char_type uppercase(char_type c)
405 // We don't know how to uppercase a non-utf16 char
407 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
413 // since we cannot use tolower and toupper directly in the
414 // calls to transform yet, we use these helper clases. (Lgb)
416 struct local_lowercase {
417 char_type operator()(char_type c) const {
419 // We don't know how to lowercase a non-utf16 char
421 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
425 struct local_uppercase {
426 char_type operator()(char_type c) const {
428 // We don't know how to uppercase a non-utf16 char
430 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
434 template<typename Char> struct local_ascii_lowercase {
435 Char operator()(Char c) const { return ascii_tolower(c); }
438 } // end of anon namespace
440 docstring const lowercase(docstring const & a)
443 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
448 docstring const uppercase(docstring const & a)
451 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
456 string const ascii_lowercase(string const & a)
459 transform(tmp.begin(), tmp.end(), tmp.begin(),
460 local_ascii_lowercase<char>());
465 docstring const ascii_lowercase(docstring const & a)
468 transform(tmp.begin(), tmp.end(), tmp.begin(),
469 local_ascii_lowercase<char_type>());
474 bool prefixIs(docstring const & a, char_type c)
482 bool prefixIs(string const & a, string const & pre)
484 size_t const prelen = pre.length();
485 size_t const alen = a.length();
487 if (prelen > alen || a.empty())
489 #if defined(STD_STRING_IS_GOOD)
490 return a.compare(0, prelen, pre) == 0;
492 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
497 bool prefixIs(docstring const & a, docstring const & pre)
499 size_t const prelen = pre.length();
500 size_t const alen = a.length();
502 if (prelen > alen || a.empty())
505 return a.compare(0, prelen, pre) == 0;
509 bool suffixIs(string const & a, char c)
511 if (a.empty()) return false;
512 return a[a.length() - 1] == c;
516 bool suffixIs(docstring const & a, char_type c)
520 return a[a.length() - 1] == c;
524 bool suffixIs(string const & a, string const & suf)
526 size_t const suflen = suf.length();
527 size_t const alen = a.length();
532 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
533 string tmp(a, alen - suflen);
534 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
536 return a.compare(alen - suflen, suflen, suf) == 0;
541 bool containsOnly(string const & s, string const & cset)
543 return s.find_first_not_of(cset) == string::npos;
547 // ale970405+lasgoutt-970425
548 // rewritten to use new string (Lgb)
549 string const token(string const & a, char delim, int n)
557 // Find delimiter or end of string
559 if ((i = a.find(delim, i)) == string::npos)
565 // i is now the n'th delim (or string::npos)
566 if (i == string::npos)
569 k = a.find(delim, i);
570 // k is now the n'th + 1 delim (or string::npos)
572 return a.substr(i, k - i);
576 docstring const token(docstring const & a, char_type delim, int n)
584 // Find delimiter or end of string
586 if ((i = a.find(delim, i)) == docstring::npos)
592 // i is now the n'th delim (or string::npos)
593 if (i == docstring::npos)
596 k = a.find(delim, i);
597 // k is now the n'th + 1 delim (or string::npos)
599 return a.substr(i, k - i);
603 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
604 // rewritten to use new string (Lgb)
605 int tokenPos(string const & a, char delim, string const & tok)
611 while (!str.empty()) {
612 str = split(str, tmptok, delim);
623 /// Substitute all \a oldchar with \a newchar
624 template<typename Ch> inline
625 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
626 Ch oldchar, Ch newchar)
628 typedef basic_string<Ch> String;
630 typename String::iterator lit = tmp.begin();
631 typename String::iterator end = tmp.end();
632 for (; lit != end; ++lit)
633 if ((*lit) == oldchar)
638 /// Substitute all \a oldchar with \a newchar
639 docstring const subst_char(docstring const & a,
640 docstring::value_type oldchar, docstring::value_type newchar)
643 docstring::iterator lit = tmp.begin();
644 docstring::iterator end = tmp.end();
645 for (; lit != end; ++lit)
646 if ((*lit) == oldchar)
652 /// substitutes all instances of \a oldstr with \a newstr
653 template<typename String> inline
654 String const subst_string(String const & a,
655 String const & oldstr, String const & newstr)
657 BOOST_ASSERT(!oldstr.empty());
660 size_t const olen = oldstr.length();
661 while ((i = lstr.find(oldstr, i)) != string::npos) {
662 lstr.replace(i, olen, newstr);
663 i += newstr.length(); // We need to be sure that we dont
664 // use the same i over and over again.
669 docstring const subst_string(docstring const & a,
670 docstring const & oldstr, docstring const & newstr)
672 BOOST_ASSERT(!oldstr.empty());
675 size_t const olen = oldstr.length();
676 while ((i = lstr.find(oldstr, i)) != string::npos) {
677 lstr.replace(i, olen, newstr);
678 i += newstr.length(); // We need to be sure that we dont
679 // use the same i over and over again.
687 string const subst(string const & a, char oldchar, char newchar)
689 return subst_char(a, oldchar, newchar);
693 docstring const subst(docstring const & a,
694 char_type oldchar, char_type newchar)
696 return subst_char(a, oldchar, newchar);
700 string const subst(string const & a,
701 string const & oldstr, string const & newstr)
703 return subst_string(a, oldstr, newstr);
707 docstring const subst(docstring const & a,
708 docstring const & oldstr, docstring const & newstr)
710 return subst_string(a, oldstr, newstr);
714 docstring const trim(docstring const & a, char const * p)
718 if (a.empty() || !*p)
721 docstring s = from_ascii(p);
722 size_t r = a.find_last_not_of(s);
723 size_t l = a.find_first_not_of(s);
725 // Is this the minimal test? (lgb)
726 if (r == docstring::npos && l == docstring::npos)
729 return a.substr(l, r - l + 1);
733 string const trim(string const & a, char const * p)
737 if (a.empty() || !*p)
740 size_t r = a.find_last_not_of(p);
741 size_t l = a.find_first_not_of(p);
743 // Is this the minimal test? (lgb)
744 if (r == string::npos && l == string::npos)
747 return a.substr(l, r - l + 1);
751 string const rtrim(string const & a, char const * p)
755 if (a.empty() || !*p)
758 size_t r = a.find_last_not_of(p);
760 // Is this test really needed? (Lgb)
761 if (r == string::npos)
764 return a.substr(0, r + 1);
768 docstring const rtrim(docstring const & a, char const * p)
772 if (a.empty() || !*p)
775 size_t r = a.find_last_not_of(from_ascii(p));
777 // Is this test really needed? (Lgb)
778 if (r == docstring::npos)
781 return a.substr(0, r + 1);
785 string const ltrim(string const & a, char const * p)
788 if (a.empty() || !*p)
790 size_t l = a.find_first_not_of(p);
791 if (l == string::npos)
793 return a.substr(l, string::npos);
797 docstring const ltrim(docstring const & a, char const * p)
800 if (a.empty() || !*p)
802 size_t l = a.find_first_not_of(from_ascii(p));
803 if (l == docstring::npos)
805 return a.substr(l, docstring::npos);
810 template<typename String, typename Char> inline
811 String const doSplit(String const & a, String & piece, Char delim)
814 size_t i = a.find(delim);
815 if (i == a.length() - 1) {
816 piece = a.substr(0, i);
817 } else if (i != String::npos) {
818 piece = a.substr(0, i);
819 tmp = a.substr(i + 1);
822 tmp = a.substr(i + 1);
829 template<typename Char> inline
830 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
833 size_t i = a.find(delim);
834 if (i == a.length() - 1) {
835 piece = a.substr(0, i);
836 } else if (i != docstring::npos) {
837 piece = a.substr(0, i);
838 tmp = a.substr(i + 1);
841 tmp = a.substr(i + 1);
851 string const split(string const & a, string & piece, char delim)
853 return doSplit(a, piece, delim);
857 docstring const split(docstring const & a, docstring & piece, char_type delim)
859 return doSplit(a, piece, delim);
863 string const split(string const & a, char delim)
866 size_t i = a.find(delim);
867 if (i != string::npos) // found delim
868 tmp = a.substr(i + 1);
874 string const rsplit(string const & a, string & piece, char delim)
877 size_t i = a.rfind(delim);
878 if (i != string::npos) { // delimiter was found
879 piece = a.substr(0, i);
880 tmp = a.substr(i + 1);
881 } else { // delimiter was not found
888 docstring const escape(docstring const & lab)
890 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
891 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
893 for (size_t i = 0; i < lab.length(); ++i) {
894 char_type c = lab[i];
895 if (c >= 128 || c == '=' || c == '%') {
896 // Although char_type is a 32 bit type we know that
897 // UCS4 occupies only 21 bits, so we don't need to
898 // encode bigger values. Test for 2^24 because we
899 // can encode that with the 6 hex digits that are
900 // needed for 21 bits anyway.
901 BOOST_ASSERT(c < (1 << 24));
903 enc += hexdigit[(c>>20) & 15];
904 enc += hexdigit[(c>>16) & 15];
905 enc += hexdigit[(c>>12) & 15];
906 enc += hexdigit[(c>> 8) & 15];
907 enc += hexdigit[(c>> 4) & 15];
908 enc += hexdigit[ c & 15];
919 template<typename String> vector<String> const
920 getVectorFromStringT(String const & str, String const & delim)
922 // Lars would like this code to go, but for now his replacement (below)
923 // doesn't fullfil the same function. I have, therefore, reactivated the
924 // old code for now. Angus 11 Nov 2002.
929 String keys = rtrim(str);
931 size_t const idx = keys.find(delim);
932 if (idx == String::npos) {
933 vec.push_back(ltrim(keys));
936 String const key = trim(keys.substr(0, idx));
939 size_t const start = idx + delim.size();
940 keys = keys.substr(start);
944 typedef boost::char_separator<typename String::value_type> Separator;
945 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
946 Separator sep(delim.c_str());
947 Tokenizer tokens(str, sep);
948 return vector<String>(tokens.begin(), tokens.end());
955 vector<string> const getVectorFromString(string const & str,
956 string const & delim)
958 return getVectorFromStringT<string>(str, delim);
962 vector<docstring> const getVectorFromString(docstring const & str,
963 docstring const & delim)
965 return getVectorFromStringT<docstring>(str, delim);
969 // the same vice versa
970 string const getStringFromVector(vector<string> const & vec,
971 string const & delim)
975 for (vector<string>::const_iterator it = vec.begin();
976 it != vec.end(); ++it) {
977 string item = trim(*it);
988 int findToken(char const * const str[], string const & search_token)
992 while (str[i][0] && str[i] != search_token)
1000 docstring const externalLineEnding(docstring const & str)
1002 #if defined(__APPLE__)
1003 // The MAC clipboard uses \r for lineendings, and we use \n
1004 return subst(str, '\n', '\r');
1005 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
1006 // Windows clipboard uses \r\n for lineendings, and we use \n
1007 return subst(str, from_ascii("\n"), from_ascii("\r\n"));
1014 docstring const internalLineEnding(docstring const & str)
1016 docstring const s = subst(str, from_ascii("\r\n"), from_ascii("\n"));
1017 return subst(s, '\r', '\n');
1022 docstring bformat(docstring const & fmt, int arg1)
1024 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1025 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1026 return subst(str, from_ascii("%%"), from_ascii("%"));
1031 docstring bformat(docstring const & fmt, long arg1)
1033 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1034 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1035 return subst(str, from_ascii("%%"), from_ascii("%"));
1040 docstring bformat(docstring const & fmt, unsigned int arg1)
1042 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1043 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1044 return subst(str, from_ascii("%%"), from_ascii("%"));
1049 docstring bformat(docstring const & fmt, docstring arg1)
1051 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1052 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1053 return subst(str, from_ascii("%%"), from_ascii("%"));
1058 docstring bformat(docstring const & fmt, char * arg1)
1060 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1061 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1062 return subst(str, from_ascii("%%"), from_ascii("%"));
1067 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1069 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1070 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1071 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1072 str = subst(str, from_ascii("%2$s"), arg2);
1073 return subst(str, from_ascii("%%"), from_ascii("%"));
1078 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1080 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1081 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1082 docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1083 str = subst(fmt, from_ascii("%2$s"), arg2);
1084 return subst(str, from_ascii("%%"), from_ascii("%"));
1089 docstring bformat(docstring const & fmt, int arg1, int arg2)
1091 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1092 BOOST_ASSERT(contains(fmt, from_ascii("%2$d")));
1093 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1094 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1095 return subst(str, from_ascii("%%"), from_ascii("%"));
1100 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1102 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1103 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1104 BOOST_ASSERT(contains(fmt, from_ascii("%3$s")));
1105 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1106 str = subst(str, from_ascii("%2$s"), arg2);
1107 str = subst(str, from_ascii("%3$s"), arg3);
1108 return subst(str, from_ascii("%%"), from_ascii("%"));
1113 docstring bformat(docstring const & fmt,
1114 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1116 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1117 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1118 BOOST_ASSERT(contains(fmt, from_ascii("%3$s")));
1119 BOOST_ASSERT(contains(fmt, from_ascii("%4$s")));
1120 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1121 str = subst(str, from_ascii("%2$s"), arg2);
1122 str = subst(str, from_ascii("%3$s"), arg3);
1123 str = subst(str, from_ascii("%4$s"), arg4);
1124 return subst(str, from_ascii("%%"), from_ascii("%"));
1127 } // namespace support