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)
500 return a[a.length() - 1] == c;
504 bool suffixIs(docstring const & a, char_type c)
508 return a[a.length() - 1] == c;
512 bool suffixIs(string const & a, string const & suf)
514 size_t const suflen = suf.length();
515 size_t const alen = a.length();
516 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
520 bool suffixIs(docstring const & a, docstring const & suf)
522 size_t const suflen = suf.length();
523 size_t const alen = a.length();
524 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
528 bool containsOnly(string const & s, string const & cset)
530 return s.find_first_not_of(cset) == string::npos;
534 // ale970405+lasgoutt-970425
535 // rewritten to use new string (Lgb)
536 string const token(string const & a, char delim, int n)
544 // Find delimiter or end of string
546 if ((i = a.find(delim, i)) == string::npos)
552 // i is now the n'th delim (or string::npos)
553 if (i == string::npos)
556 k = a.find(delim, i);
557 // k is now the n'th + 1 delim (or string::npos)
559 return a.substr(i, k - i);
563 docstring const token(docstring const & a, char_type delim, int n)
571 // Find delimiter or end of string
573 if ((i = a.find(delim, i)) == docstring::npos)
579 // i is now the n'th delim (or string::npos)
580 if (i == docstring::npos)
583 k = a.find(delim, i);
584 // k is now the n'th + 1 delim (or string::npos)
586 return a.substr(i, k - i);
590 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
591 // rewritten to use new string (Lgb)
592 int tokenPos(string const & a, char delim, string const & tok)
598 while (!str.empty()) {
599 str = split(str, tmptok, delim);
608 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
609 // rewritten to use new string (Lgb)
610 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
616 while (!str.empty()) {
617 str = split(str, tmptok, delim);
628 /// Substitute all \a oldchar with \a newchar
629 template<typename Ch> inline
630 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
631 Ch oldchar, Ch newchar)
633 typedef basic_string<Ch> String;
635 typename String::iterator lit = tmp.begin();
636 typename String::iterator end = tmp.end();
637 for (; lit != end; ++lit)
638 if ((*lit) == oldchar)
643 /// Substitute all \a oldchar with \a newchar
644 docstring const subst_char(docstring const & a,
645 docstring::value_type oldchar, docstring::value_type newchar)
648 docstring::iterator lit = tmp.begin();
649 docstring::iterator end = tmp.end();
650 for (; lit != end; ++lit)
651 if ((*lit) == oldchar)
657 /// substitutes all instances of \a oldstr with \a newstr
658 template<typename String> inline
659 String const subst_string(String const & a,
660 String const & oldstr, String const & newstr)
662 LASSERT(!oldstr.empty(), /**/);
665 size_t const olen = oldstr.length();
666 while ((i = lstr.find(oldstr, i)) != string::npos) {
667 lstr.replace(i, olen, newstr);
668 i += newstr.length(); // We need to be sure that we dont
669 // use the same i over and over again.
674 docstring const subst_string(docstring const & a,
675 docstring const & oldstr, docstring const & newstr)
677 LASSERT(!oldstr.empty(), /**/);
680 size_t const olen = oldstr.length();
681 while ((i = lstr.find(oldstr, i)) != string::npos) {
682 lstr.replace(i, olen, newstr);
683 i += newstr.length(); // We need to be sure that we dont
684 // use the same i over and over again.
692 string const subst(string const & a, char oldchar, char newchar)
694 return subst_char(a, oldchar, newchar);
698 docstring const subst(docstring const & a,
699 char_type oldchar, char_type newchar)
701 return subst_char(a, oldchar, newchar);
705 string const subst(string const & a,
706 string const & oldstr, string const & newstr)
708 return subst_string(a, oldstr, newstr);
712 docstring const subst(docstring const & a,
713 docstring const & oldstr, docstring const & newstr)
715 return subst_string(a, oldstr, newstr);
719 docstring const trim(docstring const & a, char const * p)
723 if (a.empty() || !*p)
726 docstring s = from_ascii(p);
727 size_t r = a.find_last_not_of(s);
728 size_t l = a.find_first_not_of(s);
730 // Is this the minimal test? (lgb)
731 if (r == docstring::npos && l == docstring::npos)
734 return a.substr(l, r - l + 1);
738 string const trim(string const & a, char const * p)
742 if (a.empty() || !*p)
745 size_t r = a.find_last_not_of(p);
746 size_t l = a.find_first_not_of(p);
748 // Is this the minimal test? (lgb)
749 if (r == string::npos && l == string::npos)
752 return a.substr(l, r - l + 1);
756 string const rtrim(string const & a, char const * p)
760 if (a.empty() || !*p)
763 size_t r = a.find_last_not_of(p);
765 // Is this test really needed? (Lgb)
766 if (r == string::npos)
769 return a.substr(0, r + 1);
773 docstring const rtrim(docstring const & a, char const * p)
777 if (a.empty() || !*p)
780 size_t r = a.find_last_not_of(from_ascii(p));
782 // Is this test really needed? (Lgb)
783 if (r == docstring::npos)
786 return a.substr(0, r + 1);
790 string const ltrim(string const & a, char const * p)
793 if (a.empty() || !*p)
795 size_t l = a.find_first_not_of(p);
796 if (l == string::npos)
798 return a.substr(l, string::npos);
802 docstring const ltrim(docstring const & a, char const * p)
805 if (a.empty() || !*p)
807 size_t l = a.find_first_not_of(from_ascii(p));
808 if (l == docstring::npos)
810 return a.substr(l, docstring::npos);
815 template<typename String, typename Char> inline
816 String const doSplit(String const & a, String & piece, Char delim)
819 size_t i = a.find(delim);
820 if (i == a.length() - 1) {
821 piece = a.substr(0, i);
822 } else if (i != String::npos) {
823 piece = a.substr(0, i);
824 tmp = a.substr(i + 1);
827 tmp = a.substr(i + 1);
834 template<typename Char> inline
835 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
838 size_t i = a.find(delim);
839 if (i == a.length() - 1) {
840 piece = a.substr(0, i);
841 } else if (i != docstring::npos) {
842 piece = a.substr(0, i);
843 tmp = a.substr(i + 1);
846 tmp = a.substr(i + 1);
856 string const split(string const & a, string & piece, char delim)
858 return doSplit(a, piece, delim);
862 docstring const split(docstring const & a, docstring & piece, char_type delim)
864 return doSplit(a, piece, delim);
868 string const split(string const & a, char delim)
871 size_t i = a.find(delim);
872 if (i != string::npos) // found delim
873 tmp = a.substr(i + 1);
879 string const rsplit(string const & a, string & piece, char delim)
882 size_t i = a.rfind(delim);
883 if (i != string::npos) { // delimiter was found
884 piece = a.substr(0, i);
885 tmp = a.substr(i + 1);
886 } else { // delimiter was not found
893 docstring const escape(docstring const & lab)
895 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
896 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
898 for (size_t i = 0; i < lab.length(); ++i) {
899 char_type c = lab[i];
900 if (c >= 128 || c == '=' || c == '%') {
901 // Although char_type is a 32 bit type we know that
902 // UCS4 occupies only 21 bits, so we don't need to
903 // encode bigger values. Test for 2^24 because we
904 // can encode that with the 6 hex digits that are
905 // needed for 21 bits anyway.
906 LASSERT(c < (1 << 24), /**/);
908 enc += hexdigit[(c>>20) & 15];
909 enc += hexdigit[(c>>16) & 15];
910 enc += hexdigit[(c>>12) & 15];
911 enc += hexdigit[(c>> 8) & 15];
912 enc += hexdigit[(c>> 4) & 15];
913 enc += hexdigit[ c & 15];
924 template<typename String> vector<String> const
925 getVectorFromStringT(String const & str, String const & delim, bool keepempty)
927 // Lars would like this code to go, but for now his replacement (below)
928 // doesn't fullfil the same function. I have, therefore, reactivated the
929 // old code for now. Angus 11 Nov 2002.
934 String keys = rtrim(str);
936 size_t const idx = keys.find(delim);
937 if (idx == String::npos) {
938 vec.push_back(ltrim(keys));
941 String const key = trim(keys.substr(0, idx));
942 if (!key.empty() || keepempty)
944 size_t const start = idx + delim.size();
945 keys = keys.substr(start);
949 typedef boost::char_separator<typename String::value_type> Separator;
950 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
951 Separator sep(delim.c_str());
952 Tokenizer tokens(str, sep);
953 return vector<String>(tokens.begin(), tokens.end());
960 vector<string> const getVectorFromString(string const & str,
961 string const & delim,
964 return getVectorFromStringT<string>(str, delim, keepempty);
968 vector<docstring> const getVectorFromString(docstring const & str,
969 docstring const & delim,
972 return getVectorFromStringT<docstring>(str, delim, keepempty);
976 // the same vice versa
977 string const getStringFromVector(vector<string> const & vec,
978 string const & delim)
982 for (vector<string>::const_iterator it = vec.begin();
983 it != vec.end(); ++it) {
984 string item = trim(*it);
995 int findToken(char const * const str[], string const & search_token)
999 while (str[i][0] && str[i] != search_token)
1007 docstring const externalLineEnding(docstring const & str)
1009 #if defined(__APPLE__)
1010 // The MAC clipboard uses \r for lineendings, and we use \n
1011 return subst(str, '\n', '\r');
1012 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
1013 // Windows clipboard uses \r\n for lineendings, and we use \n
1014 return subst(str, from_ascii("\n"), from_ascii("\r\n"));
1021 docstring const internalLineEnding(docstring const & str)
1023 docstring const s = subst(str, from_ascii("\r\n"), from_ascii("\n"));
1024 return subst(s, '\r', '\n');
1029 docstring bformat(docstring const & fmt, int arg1)
1031 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1032 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1033 return subst(str, from_ascii("%%"), from_ascii("%"));
1038 docstring bformat(docstring const & fmt, long arg1)
1040 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1041 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1042 return subst(str, from_ascii("%%"), from_ascii("%"));
1047 docstring bformat(docstring const & fmt, unsigned int arg1)
1049 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1050 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1051 return subst(str, from_ascii("%%"), from_ascii("%"));
1056 docstring bformat(docstring const & fmt, docstring arg1)
1058 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1059 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1060 return subst(str, from_ascii("%%"), from_ascii("%"));
1065 docstring bformat(docstring const & fmt, char * arg1)
1067 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1068 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1069 return subst(str, from_ascii("%%"), from_ascii("%"));
1074 docstring bformat(docstring const & fmt, docstring 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"), arg1);
1079 str = subst(str, from_ascii("%2$s"), arg2);
1080 return subst(str, from_ascii("%%"), from_ascii("%"));
1085 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1087 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1088 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1089 docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1090 str = subst(fmt, from_ascii("%2$s"), arg2);
1091 return subst(str, from_ascii("%%"), from_ascii("%"));
1096 docstring bformat(docstring const & fmt, int arg1, int arg2)
1098 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1099 LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1100 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1101 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1102 return subst(str, from_ascii("%%"), from_ascii("%"));
1107 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1109 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1110 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1111 LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1112 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1113 str = subst(str, from_ascii("%2$s"), arg2);
1114 str = subst(str, from_ascii("%3$s"), arg3);
1115 return subst(str, from_ascii("%%"), from_ascii("%"));
1120 docstring bformat(docstring const & fmt,
1121 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1123 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1124 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1125 LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1126 LASSERT(contains(fmt, from_ascii("%4$s")), /**/);
1127 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1128 str = subst(str, from_ascii("%2$s"), arg2);
1129 str = subst(str, from_ascii("%3$s"), arg3);
1130 str = subst(str, from_ascii("%4$s"), arg4);
1131 return subst(str, from_ascii("%%"), from_ascii("%"));
1134 } // namespace support