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"
34 // Using this allows us to have docstring default arguments in headers
35 // without #include "support/docstring" there.
36 docstring const & empty_docstring()
42 // Using this allows us to have string default arguments in headers
43 // without #include <string>
44 string const & empty_string()
51 * Convert a QChar into a UCS4 character.
52 * This is a hack (it does only make sense for the common part of the UCS4
53 * and UTF16 encodings) and should not be used.
54 * This does only exist because of performance reasons (a real conversion
55 * using iconv is too slow on windows).
57 static inline char_type qchar_to_ucs4(QChar const & qchar)
59 LASSERT(is_utf16(static_cast<char_type>(qchar.unicode())), /**/);
60 return static_cast<char_type>(qchar.unicode());
65 * Convert a UCS4 character into a QChar.
66 * This is a hack (it does only make sense for the common part of the UCS4
67 * and UTF16 encodings) and should not be used.
68 * This does only exist because of performance reasons (a real conversion
69 * using iconv is too slow on windows).
71 static inline QChar const ucs4_to_qchar(char_type const ucs4)
73 LASSERT(is_utf16(ucs4), /**/);
74 return QChar(static_cast<unsigned short>(ucs4));
79 /// Maximum valid UCS4 code point
80 char_type const ucs4_max = 0x10ffff;
84 bool isLetterChar(char_type c)
88 // outside the UCS4 range
90 // assume that all non-utf16 characters are letters
93 return ucs4_to_qchar(c).isLetter();
97 bool isAlphaASCII(char_type c)
99 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
103 bool isPrintable(char_type c)
107 // outside the UCS4 range
109 // assume that all non-utf16 characters are printable
112 return ucs4_to_qchar(c).isPrint();
116 bool isPrintableNonspace(char_type c)
120 // outside the UCS4 range
122 // assume that all non-utf16 characters are printable and
126 QChar const qc = ucs4_to_qchar(c);
127 return qc.isPrint() && !qc.isSpace();
131 bool isSpace(char_type c)
134 // assume that no non-utf16 character is a space
135 // c outside the UCS4 range is catched as well
138 QChar const qc = ucs4_to_qchar(c);
143 bool isDigit(char_type c)
146 // assume that no non-utf16 character is a digit
147 // c outside the UCS4 range is catched as well
149 return ucs4_to_qchar(c).isDigit();
153 bool isDigitASCII(char_type c)
155 return '0' <= c && c <= '9';
160 int compare_no_case(docstring const & s, docstring const & s2)
162 docstring::const_iterator p = s.begin();
163 docstring::const_iterator p2 = s2.begin();
165 while (p != s.end() && p2 != s2.end()) {
166 char_type const lc1 = lowercase(*p);
167 char_type const lc2 = lowercase(*p2);
169 return (lc1 < lc2) ? -1 : 1;
174 if (s.size() == s2.size())
176 if (s.size() < s2.size())
184 template<typename Char>
185 Char ascii_tolower(Char c) {
186 if (c >= 'A' && c <= 'Z')
187 return c - 'A' + 'a';
194 int compare_ascii_no_case(string const & s, string const & s2)
196 string::const_iterator p = s.begin();
197 string::const_iterator p2 = s2.begin();
199 while (p != s.end() && p2 != s2.end()) {
200 int const lc1 = ascii_tolower(*p);
201 int const lc2 = ascii_tolower(*p2);
203 return (lc1 < lc2) ? -1 : 1;
208 if (s.size() == s2.size())
210 if (s.size() < s2.size())
216 int compare_ascii_no_case(docstring const & s, docstring const & s2)
218 docstring::const_iterator p = s.begin();
219 docstring::const_iterator p2 = s2.begin();
221 while (p != s.end() && p2 != s2.end()) {
222 char_type const lc1 = ascii_tolower(*p);
223 char_type const lc2 = ascii_tolower(*p2);
225 return (lc1 < lc2) ? -1 : 1;
230 if (s.size() == s2.size())
232 if (s.size() < s2.size())
238 bool isStrInt(string const & str)
243 // Remove leading and trailing white space chars.
244 string const tmpstr = trim(str);
248 string::const_iterator cit = tmpstr.begin();
252 string::const_iterator end = tmpstr.end();
253 for (; cit != end; ++cit)
254 if (!isdigit((*cit)))
261 bool isStrUnsignedInt(string const & str)
266 // Remove leading and trailing white space chars.
267 string const tmpstr = trim(str);
271 string::const_iterator cit = tmpstr.begin();
272 string::const_iterator end = tmpstr.end();
273 for (; cit != end; ++cit)
274 if (!isdigit((*cit)))
281 bool isStrDbl(string const & str)
286 // Remove leading and trailing white space chars.
287 string const tmpstr = trim(str);
290 // if (tmpstr.count('.') > 1) return false;
292 string::const_iterator cit = tmpstr.begin();
293 bool found_dot = false;
296 string::const_iterator end = tmpstr.end();
297 for (; cit != end; ++cit) {
298 if (!isdigit(*cit) && *cit != '.')
310 static bool isHexChar(char_type c)
322 c == 'a' || c == 'A' ||
323 c == 'b' || c == 'B' ||
324 c == 'c' || c == 'C' ||
325 c == 'd' || c == 'D' ||
326 c == 'e' || c == 'E' ||
327 c == 'f' || c == 'F';
331 bool isHex(docstring const & str)
335 if (str.length() > 2 && str[0] == '0' &&
336 (str[1] == 'x' || str[1] == 'X'))
339 int const len = str.length();
341 for (; index < len; ++index) {
342 if (!isHexChar(str[index]))
349 int hexToInt(docstring const & str)
351 string s = to_ascii(str);
353 sscanf(s.c_str(), "%x", &h);
358 bool isAscii(docstring const & str)
360 int const len = str.length();
361 for (int i = 0; i < len; ++i)
368 bool isAscii(string const & str)
370 int const len = str.length();
371 for (int i = 0; i < len; ++i)
372 if (static_cast<unsigned char>(str[i]) >= 0x80)
378 char lowercase(char c)
380 LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
381 return char(tolower(c));
385 char uppercase(char c)
387 LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
388 return char(toupper(c));
392 char_type lowercase(char_type c)
395 // We don't know how to lowercase a non-utf16 char
397 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
401 char_type uppercase(char_type c)
404 // We don't know how to uppercase a non-utf16 char
406 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
412 // since we cannot use tolower and toupper directly in the
413 // calls to transform yet, we use these helper clases. (Lgb)
415 struct local_lowercase {
416 char_type operator()(char_type c) const {
418 // We don't know how to lowercase a non-utf16 char
420 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
424 struct local_uppercase {
425 char_type operator()(char_type c) const {
427 // We don't know how to uppercase a non-utf16 char
429 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
433 template<typename Char> struct local_ascii_lowercase {
434 Char operator()(Char c) const { return ascii_tolower(c); }
437 } // end of anon namespace
439 docstring const lowercase(docstring const & a)
442 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
447 docstring const uppercase(docstring const & a)
450 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
455 string const ascii_lowercase(string const & a)
458 transform(tmp.begin(), tmp.end(), tmp.begin(),
459 local_ascii_lowercase<char>());
464 docstring const ascii_lowercase(docstring const & a)
467 transform(tmp.begin(), tmp.end(), tmp.begin(),
468 local_ascii_lowercase<char_type>());
473 bool prefixIs(docstring const & a, char_type c)
481 bool prefixIs(string const & a, string const & pre)
483 size_t const prelen = pre.length();
484 size_t const alen = a.length();
485 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
489 bool prefixIs(docstring const & a, docstring const & pre)
491 size_t const prelen = pre.length();
492 size_t const alen = a.length();
493 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
497 bool suffixIs(string const & a, char c)
501 return a[a.length() - 1] == c;
505 bool suffixIs(docstring const & a, char_type c)
509 return a[a.length() - 1] == c;
513 bool suffixIs(string const & a, string const & suf)
515 size_t const suflen = suf.length();
516 size_t const alen = a.length();
517 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
521 bool suffixIs(docstring const & a, docstring const & suf)
523 size_t const suflen = suf.length();
524 size_t const alen = a.length();
525 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
529 bool containsOnly(string const & s, string const & cset)
531 return s.find_first_not_of(cset) == string::npos;
535 // ale970405+lasgoutt-970425
536 // rewritten to use new string (Lgb)
537 string const token(string const & a, char delim, int n)
545 // Find delimiter or end of string
547 if ((i = a.find(delim, i)) == string::npos)
553 // i is now the n'th delim (or string::npos)
554 if (i == string::npos)
557 k = a.find(delim, i);
558 // k is now the n'th + 1 delim (or string::npos)
560 return a.substr(i, k - i);
564 docstring const token(docstring const & a, char_type delim, int n)
572 // Find delimiter or end of string
574 if ((i = a.find(delim, i)) == docstring::npos)
580 // i is now the n'th delim (or string::npos)
581 if (i == docstring::npos)
584 k = a.find(delim, i);
585 // k is now the n'th + 1 delim (or string::npos)
587 return a.substr(i, k - i);
591 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
592 // rewritten to use new string (Lgb)
593 int tokenPos(string const & a, char delim, string const & tok)
599 while (!str.empty()) {
600 str = split(str, tmptok, delim);
609 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
610 // rewritten to use new string (Lgb)
611 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
617 while (!str.empty()) {
618 str = split(str, tmptok, delim);
629 /// Substitute all \a oldchar with \a newchar
630 template<typename Ch> inline
631 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
632 Ch oldchar, Ch newchar)
634 typedef basic_string<Ch> String;
636 typename String::iterator lit = tmp.begin();
637 typename String::iterator end = tmp.end();
638 for (; lit != end; ++lit)
639 if ((*lit) == oldchar)
644 /// Substitute all \a oldchar with \a newchar
645 docstring const subst_char(docstring const & a,
646 docstring::value_type oldchar, docstring::value_type newchar)
649 docstring::iterator lit = tmp.begin();
650 docstring::iterator end = tmp.end();
651 for (; lit != end; ++lit)
652 if ((*lit) == oldchar)
658 /// substitutes all instances of \a oldstr with \a newstr
659 template<typename String> inline
660 String const subst_string(String const & a,
661 String const & oldstr, String const & newstr)
663 LASSERT(!oldstr.empty(), /**/);
666 size_t const olen = oldstr.length();
667 while ((i = lstr.find(oldstr, i)) != string::npos) {
668 lstr.replace(i, olen, newstr);
669 i += newstr.length(); // We need to be sure that we dont
670 // use the same i over and over again.
675 docstring const subst_string(docstring const & a,
676 docstring const & oldstr, docstring const & newstr)
678 LASSERT(!oldstr.empty(), /**/);
681 size_t const olen = oldstr.length();
682 while ((i = lstr.find(oldstr, i)) != string::npos) {
683 lstr.replace(i, olen, newstr);
684 i += newstr.length(); // We need to be sure that we dont
685 // use the same i over and over again.
693 string const subst(string const & a, char oldchar, char newchar)
695 return subst_char(a, oldchar, newchar);
699 docstring const subst(docstring const & a,
700 char_type oldchar, char_type newchar)
702 return subst_char(a, oldchar, newchar);
706 string const subst(string const & a,
707 string const & oldstr, string const & newstr)
709 return subst_string(a, oldstr, newstr);
713 docstring const subst(docstring const & a,
714 docstring const & oldstr, docstring const & newstr)
716 return subst_string(a, oldstr, newstr);
720 docstring const trim(docstring const & a, char const * p)
724 if (a.empty() || !*p)
727 docstring s = from_ascii(p);
728 size_t r = a.find_last_not_of(s);
729 size_t l = a.find_first_not_of(s);
731 // Is this the minimal test? (lgb)
732 if (r == docstring::npos && l == docstring::npos)
735 return a.substr(l, r - l + 1);
739 string const trim(string const & a, char const * p)
743 if (a.empty() || !*p)
746 size_t r = a.find_last_not_of(p);
747 size_t l = a.find_first_not_of(p);
749 // Is this the minimal test? (lgb)
750 if (r == string::npos && l == string::npos)
753 return a.substr(l, r - l + 1);
757 string const rtrim(string const & a, char const * p)
761 if (a.empty() || !*p)
764 size_t r = a.find_last_not_of(p);
766 // Is this test really needed? (Lgb)
767 if (r == string::npos)
770 return a.substr(0, r + 1);
774 docstring const rtrim(docstring const & a, char const * p)
778 if (a.empty() || !*p)
781 size_t r = a.find_last_not_of(from_ascii(p));
783 // Is this test really needed? (Lgb)
784 if (r == docstring::npos)
787 return a.substr(0, r + 1);
791 string const ltrim(string const & a, char const * p)
794 if (a.empty() || !*p)
796 size_t l = a.find_first_not_of(p);
797 if (l == string::npos)
799 return a.substr(l, string::npos);
803 docstring const ltrim(docstring const & a, char const * p)
806 if (a.empty() || !*p)
808 size_t l = a.find_first_not_of(from_ascii(p));
809 if (l == docstring::npos)
811 return a.substr(l, docstring::npos);
816 template<typename String, typename Char> inline
817 String const doSplit(String const & a, String & piece, Char delim)
820 size_t i = a.find(delim);
821 if (i == a.length() - 1) {
822 piece = a.substr(0, i);
823 } else if (i != String::npos) {
824 piece = a.substr(0, i);
825 tmp = a.substr(i + 1);
828 tmp = a.substr(i + 1);
835 template<typename Char> inline
836 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
839 size_t i = a.find(delim);
840 if (i == a.length() - 1) {
841 piece = a.substr(0, i);
842 } else if (i != docstring::npos) {
843 piece = a.substr(0, i);
844 tmp = a.substr(i + 1);
847 tmp = a.substr(i + 1);
857 string const split(string const & a, string & piece, char delim)
859 return doSplit(a, piece, delim);
863 docstring const split(docstring const & a, docstring & piece, char_type delim)
865 return doSplit(a, piece, delim);
869 string const split(string const & a, char delim)
872 size_t i = a.find(delim);
873 if (i != string::npos) // found delim
874 tmp = a.substr(i + 1);
880 string const rsplit(string const & a, string & piece, char delim)
883 size_t i = a.rfind(delim);
884 if (i != string::npos) { // delimiter was found
885 piece = a.substr(0, i);
886 tmp = a.substr(i + 1);
887 } else { // delimiter was not found
894 docstring const escape(docstring const & lab)
896 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
897 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
899 for (size_t i = 0; i < lab.length(); ++i) {
900 char_type c = lab[i];
901 if (c >= 128 || c == '=' || c == '%') {
902 // Although char_type is a 32 bit type we know that
903 // UCS4 occupies only 21 bits, so we don't need to
904 // encode bigger values. Test for 2^24 because we
905 // can encode that with the 6 hex digits that are
906 // needed for 21 bits anyway.
907 LASSERT(c < (1 << 24), /**/);
909 enc += hexdigit[(c>>20) & 15];
910 enc += hexdigit[(c>>16) & 15];
911 enc += hexdigit[(c>>12) & 15];
912 enc += hexdigit[(c>> 8) & 15];
913 enc += hexdigit[(c>> 4) & 15];
914 enc += hexdigit[ c & 15];
925 template<typename String> vector<String> const
926 getVectorFromStringT(String const & str, String const & delim, bool keepempty)
928 // Lars would like this code to go, but for now his replacement (below)
929 // doesn't fullfil the same function. I have, therefore, reactivated the
930 // old code for now. Angus 11 Nov 2002.
935 String keys = rtrim(str);
937 size_t const idx = keys.find(delim);
938 if (idx == String::npos) {
939 vec.push_back(ltrim(keys));
942 String const key = trim(keys.substr(0, idx));
943 if (!key.empty() || keepempty)
945 size_t const start = idx + delim.size();
946 keys = keys.substr(start);
950 typedef boost::char_separator<typename String::value_type> Separator;
951 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
952 Separator sep(delim.c_str());
953 Tokenizer tokens(str, sep);
954 return vector<String>(tokens.begin(), tokens.end());
961 vector<string> const getVectorFromString(string const & str,
962 string const & delim,
965 return getVectorFromStringT<string>(str, delim, keepempty);
969 vector<docstring> const getVectorFromString(docstring const & str,
970 docstring const & delim,
973 return getVectorFromStringT<docstring>(str, delim, keepempty);
977 // the same vice versa
978 string const getStringFromVector(vector<string> const & vec,
979 string const & delim)
983 for (vector<string>::const_iterator it = vec.begin();
984 it != vec.end(); ++it) {
985 string item = trim(*it);
996 int findToken(char const * const str[], string const & search_token)
1000 while (str[i][0] && str[i] != search_token)
1009 docstring bformat(docstring const & fmt, int arg1)
1011 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1012 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1013 return subst(str, from_ascii("%%"), from_ascii("%"));
1018 docstring bformat(docstring const & fmt, long 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, unsigned int 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, docstring arg1)
1038 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1039 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1040 return subst(str, from_ascii("%%"), from_ascii("%"));
1045 docstring bformat(docstring const & fmt, char * arg1)
1047 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1048 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1049 return subst(str, from_ascii("%%"), from_ascii("%"));
1054 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1056 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1057 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1058 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1059 str = subst(str, from_ascii("%2$s"), arg2);
1060 return subst(str, from_ascii("%%"), from_ascii("%"));
1065 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1067 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1068 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1069 docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1070 str = subst(fmt, from_ascii("%2$s"), arg2);
1071 return subst(str, from_ascii("%%"), from_ascii("%"));
1076 docstring bformat(docstring const & fmt, int arg1, int arg2)
1078 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1079 LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1080 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1081 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1082 return subst(str, from_ascii("%%"), from_ascii("%"));
1087 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1089 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1090 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1091 LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1092 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1093 str = subst(str, from_ascii("%2$s"), arg2);
1094 str = subst(str, from_ascii("%3$s"), arg3);
1095 return subst(str, from_ascii("%%"), from_ascii("%"));
1100 docstring bformat(docstring const & fmt,
1101 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1103 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1104 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1105 LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1106 LASSERT(contains(fmt, from_ascii("%4$s")), /**/);
1107 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1108 str = subst(str, from_ascii("%2$s"), arg2);
1109 str = subst(str, from_ascii("%3$s"), arg3);
1110 str = subst(str, from_ascii("%4$s"), arg4);
1111 return subst(str, from_ascii("%%"), from_ascii("%"));
1114 } // namespace support