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
10 * Full author contact details are available in file CREDITS.
15 #include "support/lstrings.h"
17 #include "support/convert.h"
18 #include "support/qstring_helpers.h"
19 #include "support/textutils.h"
21 #include <boost/tokenizer.hpp>
22 #include "support/lassert.h"
33 // Using this allows us to have docstring default arguments in headers
34 // without #include "support/docstring" there.
35 docstring const & empty_docstring()
41 // Using this allows us to have string default arguments in headers
42 // without #include <string>
43 string const & empty_string()
50 * Convert a QChar into a UCS4 character.
51 * This is a hack (it does only make sense for the common part of the UCS4
52 * and UTF16 encodings) and should not be used.
53 * This does only exist because of performance reasons (a real conversion
54 * using iconv is too slow on windows).
56 static inline char_type qchar_to_ucs4(QChar const & qchar)
58 LASSERT(is_utf16(static_cast<char_type>(qchar.unicode())), /**/);
59 return static_cast<char_type>(qchar.unicode());
64 * Convert a UCS4 character into a QChar.
65 * This is a hack (it does only make sense for the common part of the UCS4
66 * and UTF16 encodings) and should not be used.
67 * This does only exist because of performance reasons (a real conversion
68 * using iconv is too slow on windows).
70 static inline QChar const ucs4_to_qchar(char_type const ucs4)
72 LASSERT(is_utf16(ucs4), /**/);
73 return QChar(static_cast<unsigned short>(ucs4));
78 /// Maximum valid UCS4 code point
79 char_type const ucs4_max = 0x10ffff;
83 bool isLetterChar(char_type c)
87 // outside the UCS4 range
89 // assume that all non-utf16 characters are letters
92 return ucs4_to_qchar(c).isLetter();
96 bool isAlphaASCII(char_type c)
98 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
102 bool isPrintable(char_type c)
106 // outside the UCS4 range
108 // assume that all non-utf16 characters are printable
111 return ucs4_to_qchar(c).isPrint();
115 bool isPrintableNonspace(char_type c)
119 // outside the UCS4 range
121 // assume that all non-utf16 characters are printable and
125 QChar const qc = ucs4_to_qchar(c);
126 return qc.isPrint() && !qc.isSpace();
130 bool isSpace(char_type c)
133 // assume that no non-utf16 character is a space
134 // c outside the UCS4 range is catched as well
137 QChar const qc = ucs4_to_qchar(c);
142 bool isDigit(char_type c)
145 // assume that no non-utf16 character is a digit
146 // c outside the UCS4 range is catched as well
148 return ucs4_to_qchar(c).isDigit();
152 bool isDigitASCII(char_type c)
154 return '0' <= c && c <= '9';
159 int compare_no_case(docstring const & s, docstring const & s2)
161 docstring::const_iterator p = s.begin();
162 docstring::const_iterator p2 = s2.begin();
164 while (p != s.end() && p2 != s2.end()) {
165 char_type const lc1 = lowercase(*p);
166 char_type const lc2 = lowercase(*p2);
168 return (lc1 < lc2) ? -1 : 1;
173 if (s.size() == s2.size())
175 if (s.size() < s2.size())
183 template<typename Char>
184 Char ascii_tolower(Char c) {
185 if (c >= 'A' && c <= 'Z')
186 return c - 'A' + 'a';
193 int compare_ascii_no_case(string const & s, string const & s2)
195 string::const_iterator p = s.begin();
196 string::const_iterator p2 = s2.begin();
198 while (p != s.end() && p2 != s2.end()) {
199 int const lc1 = ascii_tolower(*p);
200 int const lc2 = ascii_tolower(*p2);
202 return (lc1 < lc2) ? -1 : 1;
207 if (s.size() == s2.size())
209 if (s.size() < s2.size())
215 int compare_ascii_no_case(docstring const & s, docstring const & s2)
217 docstring::const_iterator p = s.begin();
218 docstring::const_iterator p2 = s2.begin();
220 while (p != s.end() && p2 != s2.end()) {
221 char_type const lc1 = ascii_tolower(*p);
222 char_type const lc2 = ascii_tolower(*p2);
224 return (lc1 < lc2) ? -1 : 1;
229 if (s.size() == s2.size())
231 if (s.size() < s2.size())
237 bool isStrInt(string const & str)
242 // Remove leading and trailing white space chars.
243 string const tmpstr = trim(str);
247 string::const_iterator cit = tmpstr.begin();
251 string::const_iterator end = tmpstr.end();
252 for (; cit != end; ++cit)
253 if (!isdigit((*cit)))
260 bool isStrUnsignedInt(string const & str)
265 // Remove leading and trailing white space chars.
266 string const tmpstr = trim(str);
270 string::const_iterator cit = tmpstr.begin();
271 string::const_iterator end = tmpstr.end();
272 for (; cit != end; ++cit)
273 if (!isdigit((*cit)))
280 bool isStrDbl(string const & str)
285 // Remove leading and trailing white space chars.
286 string const tmpstr = trim(str);
289 // if (tmpstr.count('.') > 1) return false;
291 string::const_iterator cit = tmpstr.begin();
292 bool found_dot = false;
295 string::const_iterator end = tmpstr.end();
296 for (; cit != end; ++cit) {
297 if (!isdigit(*cit) && *cit != '.')
309 static bool isHexChar(char_type c)
321 c == 'a' || c == 'A' ||
322 c == 'b' || c == 'B' ||
323 c == 'c' || c == 'C' ||
324 c == 'd' || c == 'D' ||
325 c == 'e' || c == 'E' ||
326 c == 'f' || c == 'F';
330 bool isHex(docstring const & str)
334 if (str.length() > 2 && str[0] == '0' &&
335 (str[1] == 'x' || str[1] == 'X'))
338 int const len = str.length();
340 for (; index < len; ++index) {
341 if (!isHexChar(str[index]))
348 int hexToInt(docstring const & str)
350 string s = to_ascii(str);
352 sscanf(s.c_str(), "%x", &h);
357 bool isAscii(docstring const & str)
359 int const len = str.length();
360 for (int i = 0; i < len; ++i)
367 bool isAscii(string const & str)
369 int const len = str.length();
370 for (int i = 0; i < len; ++i)
371 if (static_cast<unsigned char>(str[i]) >= 0x80)
377 char lowercase(char c)
379 LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
380 return char(tolower(c));
384 char uppercase(char c)
386 LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
387 return char(toupper(c));
391 char_type lowercase(char_type c)
394 // We don't know how to lowercase a non-utf16 char
396 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
400 char_type uppercase(char_type c)
403 // We don't know how to uppercase a non-utf16 char
405 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
411 // since we cannot use tolower and toupper directly in the
412 // calls to transform yet, we use these helper clases. (Lgb)
414 struct local_lowercase {
415 char_type operator()(char_type c) const {
417 // We don't know how to lowercase a non-utf16 char
419 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
423 struct local_uppercase {
424 char_type operator()(char_type c) const {
426 // We don't know how to uppercase a non-utf16 char
428 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
432 template<typename Char> struct local_ascii_lowercase {
433 Char operator()(Char c) const { return ascii_tolower(c); }
436 } // end of anon namespace
438 docstring const lowercase(docstring const & a)
441 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
446 docstring const uppercase(docstring const & a)
449 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
454 string const ascii_lowercase(string const & a)
457 transform(tmp.begin(), tmp.end(), tmp.begin(),
458 local_ascii_lowercase<char>());
463 docstring const ascii_lowercase(docstring const & a)
466 transform(tmp.begin(), tmp.end(), tmp.begin(),
467 local_ascii_lowercase<char_type>());
472 bool prefixIs(docstring const & a, char_type c)
480 bool prefixIs(string const & a, string const & pre)
482 size_t const prelen = pre.length();
483 size_t const alen = a.length();
484 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
488 bool prefixIs(docstring const & a, docstring const & pre)
490 size_t const prelen = pre.length();
491 size_t const alen = a.length();
492 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
496 bool suffixIs(string const & a, char c)
498 if (a.empty()) return false;
499 return a[a.length() - 1] == c;
503 bool suffixIs(docstring const & a, char_type c)
507 return a[a.length() - 1] == c;
511 bool suffixIs(string const & a, string const & suf)
513 size_t const suflen = suf.length();
514 size_t const alen = a.length();
515 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
519 bool containsOnly(string const & s, string const & cset)
521 return s.find_first_not_of(cset) == string::npos;
525 // ale970405+lasgoutt-970425
526 // rewritten to use new string (Lgb)
527 string const token(string const & a, char delim, int n)
535 // Find delimiter or end of string
537 if ((i = a.find(delim, i)) == string::npos)
543 // i is now the n'th delim (or string::npos)
544 if (i == string::npos)
547 k = a.find(delim, i);
548 // k is now the n'th + 1 delim (or string::npos)
550 return a.substr(i, k - i);
554 docstring const token(docstring const & a, char_type delim, int n)
562 // Find delimiter or end of string
564 if ((i = a.find(delim, i)) == docstring::npos)
570 // i is now the n'th delim (or string::npos)
571 if (i == docstring::npos)
574 k = a.find(delim, i);
575 // k is now the n'th + 1 delim (or string::npos)
577 return a.substr(i, k - i);
581 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
582 // rewritten to use new string (Lgb)
583 int tokenPos(string const & a, char delim, string const & tok)
589 while (!str.empty()) {
590 str = split(str, tmptok, delim);
599 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
600 // rewritten to use new string (Lgb)
601 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
607 while (!str.empty()) {
608 str = split(str, tmptok, delim);
619 /// Substitute all \a oldchar with \a newchar
620 template<typename Ch> inline
621 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
622 Ch oldchar, Ch newchar)
624 typedef basic_string<Ch> String;
626 typename String::iterator lit = tmp.begin();
627 typename String::iterator end = tmp.end();
628 for (; lit != end; ++lit)
629 if ((*lit) == oldchar)
634 /// Substitute all \a oldchar with \a newchar
635 docstring const subst_char(docstring const & a,
636 docstring::value_type oldchar, docstring::value_type newchar)
639 docstring::iterator lit = tmp.begin();
640 docstring::iterator end = tmp.end();
641 for (; lit != end; ++lit)
642 if ((*lit) == oldchar)
648 /// substitutes all instances of \a oldstr with \a newstr
649 template<typename String> inline
650 String const subst_string(String const & a,
651 String const & oldstr, String const & newstr)
653 LASSERT(!oldstr.empty(), /**/);
656 size_t const olen = oldstr.length();
657 while ((i = lstr.find(oldstr, i)) != string::npos) {
658 lstr.replace(i, olen, newstr);
659 i += newstr.length(); // We need to be sure that we dont
660 // use the same i over and over again.
665 docstring const subst_string(docstring const & a,
666 docstring const & oldstr, docstring const & newstr)
668 LASSERT(!oldstr.empty(), /**/);
671 size_t const olen = oldstr.length();
672 while ((i = lstr.find(oldstr, i)) != string::npos) {
673 lstr.replace(i, olen, newstr);
674 i += newstr.length(); // We need to be sure that we dont
675 // use the same i over and over again.
683 string const subst(string const & a, char oldchar, char newchar)
685 return subst_char(a, oldchar, newchar);
689 docstring const subst(docstring const & a,
690 char_type oldchar, char_type newchar)
692 return subst_char(a, oldchar, newchar);
696 string const subst(string const & a,
697 string const & oldstr, string const & newstr)
699 return subst_string(a, oldstr, newstr);
703 docstring const subst(docstring const & a,
704 docstring const & oldstr, docstring const & newstr)
706 return subst_string(a, oldstr, newstr);
710 docstring const trim(docstring const & a, char const * p)
714 if (a.empty() || !*p)
717 docstring s = from_ascii(p);
718 size_t r = a.find_last_not_of(s);
719 size_t l = a.find_first_not_of(s);
721 // Is this the minimal test? (lgb)
722 if (r == docstring::npos && l == docstring::npos)
725 return a.substr(l, r - l + 1);
729 string const trim(string const & a, char const * p)
733 if (a.empty() || !*p)
736 size_t r = a.find_last_not_of(p);
737 size_t l = a.find_first_not_of(p);
739 // Is this the minimal test? (lgb)
740 if (r == string::npos && l == string::npos)
743 return a.substr(l, r - l + 1);
747 string const rtrim(string const & a, char const * p)
751 if (a.empty() || !*p)
754 size_t r = a.find_last_not_of(p);
756 // Is this test really needed? (Lgb)
757 if (r == string::npos)
760 return a.substr(0, r + 1);
764 docstring const rtrim(docstring const & a, char const * p)
768 if (a.empty() || !*p)
771 size_t r = a.find_last_not_of(from_ascii(p));
773 // Is this test really needed? (Lgb)
774 if (r == docstring::npos)
777 return a.substr(0, r + 1);
781 string const ltrim(string const & a, char const * p)
784 if (a.empty() || !*p)
786 size_t l = a.find_first_not_of(p);
787 if (l == string::npos)
789 return a.substr(l, string::npos);
793 docstring const ltrim(docstring const & a, char const * p)
796 if (a.empty() || !*p)
798 size_t l = a.find_first_not_of(from_ascii(p));
799 if (l == docstring::npos)
801 return a.substr(l, docstring::npos);
806 template<typename String, typename Char> inline
807 String const doSplit(String const & a, String & piece, Char delim)
810 size_t i = a.find(delim);
811 if (i == a.length() - 1) {
812 piece = a.substr(0, i);
813 } else if (i != String::npos) {
814 piece = a.substr(0, i);
815 tmp = a.substr(i + 1);
818 tmp = a.substr(i + 1);
825 template<typename Char> inline
826 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
829 size_t i = a.find(delim);
830 if (i == a.length() - 1) {
831 piece = a.substr(0, i);
832 } else if (i != docstring::npos) {
833 piece = a.substr(0, i);
834 tmp = a.substr(i + 1);
837 tmp = a.substr(i + 1);
847 string const split(string const & a, string & piece, char delim)
849 return doSplit(a, piece, delim);
853 docstring const split(docstring const & a, docstring & piece, char_type delim)
855 return doSplit(a, piece, delim);
859 string const split(string const & a, char delim)
862 size_t i = a.find(delim);
863 if (i != string::npos) // found delim
864 tmp = a.substr(i + 1);
870 string const rsplit(string const & a, string & piece, char delim)
873 size_t i = a.rfind(delim);
874 if (i != string::npos) { // delimiter was found
875 piece = a.substr(0, i);
876 tmp = a.substr(i + 1);
877 } else { // delimiter was not found
884 docstring const escape(docstring const & lab)
886 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
887 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
889 for (size_t i = 0; i < lab.length(); ++i) {
890 char_type c = lab[i];
891 if (c >= 128 || c == '=' || c == '%') {
892 // Although char_type is a 32 bit type we know that
893 // UCS4 occupies only 21 bits, so we don't need to
894 // encode bigger values. Test for 2^24 because we
895 // can encode that with the 6 hex digits that are
896 // needed for 21 bits anyway.
897 LASSERT(c < (1 << 24), /**/);
899 enc += hexdigit[(c>>20) & 15];
900 enc += hexdigit[(c>>16) & 15];
901 enc += hexdigit[(c>>12) & 15];
902 enc += hexdigit[(c>> 8) & 15];
903 enc += hexdigit[(c>> 4) & 15];
904 enc += hexdigit[ c & 15];
915 template<typename String> vector<String> const
916 getVectorFromStringT(String const & str, String const & delim)
918 // Lars would like this code to go, but for now his replacement (below)
919 // doesn't fullfil the same function. I have, therefore, reactivated the
920 // old code for now. Angus 11 Nov 2002.
925 String keys = rtrim(str);
927 size_t const idx = keys.find(delim);
928 if (idx == String::npos) {
929 vec.push_back(ltrim(keys));
932 String const key = trim(keys.substr(0, idx));
935 size_t const start = idx + delim.size();
936 keys = keys.substr(start);
940 typedef boost::char_separator<typename String::value_type> Separator;
941 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
942 Separator sep(delim.c_str());
943 Tokenizer tokens(str, sep);
944 return vector<String>(tokens.begin(), tokens.end());
951 vector<string> const getVectorFromString(string const & str,
952 string const & delim)
954 return getVectorFromStringT<string>(str, delim);
958 vector<docstring> const getVectorFromString(docstring const & str,
959 docstring const & delim)
961 return getVectorFromStringT<docstring>(str, delim);
965 // the same vice versa
966 string const getStringFromVector(vector<string> const & vec,
967 string const & delim)
971 for (vector<string>::const_iterator it = vec.begin();
972 it != vec.end(); ++it) {
973 string item = trim(*it);
984 int findToken(char const * const str[], string const & search_token)
988 while (str[i][0] && str[i] != search_token)
996 docstring const externalLineEnding(docstring const & str)
998 #if defined(__APPLE__)
999 // The MAC clipboard uses \r for lineendings, and we use \n
1000 return subst(str, '\n', '\r');
1001 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
1002 // Windows clipboard uses \r\n for lineendings, and we use \n
1003 return subst(str, from_ascii("\n"), from_ascii("\r\n"));
1010 docstring const internalLineEnding(docstring const & str)
1012 docstring const s = subst(str, from_ascii("\r\n"), from_ascii("\n"));
1013 return subst(s, '\r', '\n');
1018 docstring bformat(docstring const & fmt, int arg1)
1020 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1021 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1022 return subst(str, from_ascii("%%"), from_ascii("%"));
1027 docstring bformat(docstring const & fmt, long arg1)
1029 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1030 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1031 return subst(str, from_ascii("%%"), from_ascii("%"));
1036 docstring bformat(docstring const & fmt, unsigned int arg1)
1038 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1039 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1040 return subst(str, from_ascii("%%"), from_ascii("%"));
1045 docstring bformat(docstring const & fmt, docstring arg1)
1047 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1048 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1049 return subst(str, from_ascii("%%"), from_ascii("%"));
1054 docstring bformat(docstring const & fmt, char * arg1)
1056 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1057 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1058 return subst(str, from_ascii("%%"), from_ascii("%"));
1063 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1065 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1066 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1067 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1068 str = subst(str, from_ascii("%2$s"), arg2);
1069 return subst(str, from_ascii("%%"), from_ascii("%"));
1074 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1076 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1077 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1078 docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1079 str = subst(fmt, from_ascii("%2$s"), arg2);
1080 return subst(str, from_ascii("%%"), from_ascii("%"));
1085 docstring bformat(docstring const & fmt, int arg1, int arg2)
1087 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1088 LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1089 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1090 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1091 return subst(str, from_ascii("%%"), from_ascii("%"));
1096 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1098 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1099 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1100 LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1101 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1102 str = subst(str, from_ascii("%2$s"), arg2);
1103 str = subst(str, from_ascii("%3$s"), arg3);
1104 return subst(str, from_ascii("%%"), from_ascii("%"));
1109 docstring bformat(docstring const & fmt,
1110 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1112 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1113 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1114 LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1115 LASSERT(contains(fmt, from_ascii("%4$s")), /**/);
1116 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1117 str = subst(str, from_ascii("%2$s"), arg2);
1118 str = subst(str, from_ascii("%3$s"), arg3);
1119 str = subst(str, from_ascii("%4$s"), arg4);
1120 return subst(str, from_ascii("%%"), from_ascii("%"));
1123 } // namespace support