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/qstring_helpers.h"
19 #include "support/textutils.h"
21 #include <boost/tokenizer.hpp>
22 #include <boost/assert.hpp>
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 BOOST_ASSERT(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 BOOST_ASSERT(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 BOOST_ASSERT(static_cast<unsigned char>(c) < 0x80);
381 return char(tolower(c));
385 char uppercase(char c)
387 BOOST_ASSERT(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();
486 if (prelen > alen || a.empty())
488 #if defined(STD_STRING_IS_GOOD)
489 return a.compare(0, prelen, pre) == 0;
491 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
496 bool prefixIs(docstring const & a, docstring const & pre)
498 size_t const prelen = pre.length();
499 size_t const alen = a.length();
501 if (prelen > alen || a.empty())
504 return a.compare(0, prelen, pre) == 0;
508 bool suffixIs(string const & a, char c)
510 if (a.empty()) return false;
511 return a[a.length() - 1] == c;
515 bool suffixIs(docstring const & a, char_type c)
519 return a[a.length() - 1] == c;
523 bool suffixIs(string const & a, string const & suf)
525 size_t const suflen = suf.length();
526 size_t const alen = a.length();
531 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
532 string tmp(a, alen - suflen);
533 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
535 return a.compare(alen - suflen, suflen, suf) == 0;
540 bool containsOnly(string const & s, string const & cset)
542 return s.find_first_not_of(cset) == string::npos;
546 // ale970405+lasgoutt-970425
547 // rewritten to use new string (Lgb)
548 string const token(string const & a, char delim, int n)
556 // Find delimiter or end of string
558 if ((i = a.find(delim, i)) == string::npos)
564 // i is now the n'th delim (or string::npos)
565 if (i == string::npos)
568 k = a.find(delim, i);
569 // k is now the n'th + 1 delim (or string::npos)
571 return a.substr(i, k - i);
575 docstring const token(docstring const & a, char_type delim, int n)
583 // Find delimiter or end of string
585 if ((i = a.find(delim, i)) == docstring::npos)
591 // i is now the n'th delim (or string::npos)
592 if (i == docstring::npos)
595 k = a.find(delim, i);
596 // k is now the n'th + 1 delim (or string::npos)
598 return a.substr(i, k - i);
602 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
603 // rewritten to use new string (Lgb)
604 int tokenPos(string const & a, char delim, string const & tok)
610 while (!str.empty()) {
611 str = split(str, tmptok, delim);
622 /// Substitute all \a oldchar with \a newchar
623 template<typename Ch> inline
624 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
625 Ch oldchar, Ch newchar)
627 typedef basic_string<Ch> String;
629 typename String::iterator lit = tmp.begin();
630 typename String::iterator end = tmp.end();
631 for (; lit != end; ++lit)
632 if ((*lit) == oldchar)
637 /// Substitute all \a oldchar with \a newchar
638 docstring const subst_char(docstring const & a,
639 docstring::value_type oldchar, docstring::value_type newchar)
642 docstring::iterator lit = tmp.begin();
643 docstring::iterator end = tmp.end();
644 for (; lit != end; ++lit)
645 if ((*lit) == oldchar)
651 /// substitutes all instances of \a oldstr with \a newstr
652 template<typename String> inline
653 String const subst_string(String const & a,
654 String const & oldstr, String const & newstr)
656 BOOST_ASSERT(!oldstr.empty());
659 size_t const olen = oldstr.length();
660 while ((i = lstr.find(oldstr, i)) != string::npos) {
661 lstr.replace(i, olen, newstr);
662 i += newstr.length(); // We need to be sure that we dont
663 // use the same i over and over again.
668 docstring const subst_string(docstring const & a,
669 docstring const & oldstr, docstring const & newstr)
671 BOOST_ASSERT(!oldstr.empty());
674 size_t const olen = oldstr.length();
675 while ((i = lstr.find(oldstr, i)) != string::npos) {
676 lstr.replace(i, olen, newstr);
677 i += newstr.length(); // We need to be sure that we dont
678 // use the same i over and over again.
686 string const subst(string const & a, char oldchar, char newchar)
688 return subst_char(a, oldchar, newchar);
692 docstring const subst(docstring const & a,
693 char_type oldchar, char_type newchar)
695 return subst_char(a, oldchar, newchar);
699 string const subst(string const & a,
700 string const & oldstr, string const & newstr)
702 return subst_string(a, oldstr, newstr);
706 docstring const subst(docstring const & a,
707 docstring const & oldstr, docstring const & newstr)
709 return subst_string(a, oldstr, newstr);
713 docstring const trim(docstring const & a, char const * p)
717 if (a.empty() || !*p)
720 docstring s = from_ascii(p);
721 size_t r = a.find_last_not_of(s);
722 size_t l = a.find_first_not_of(s);
724 // Is this the minimal test? (lgb)
725 if (r == docstring::npos && l == docstring::npos)
728 return a.substr(l, r - l + 1);
732 string const trim(string const & a, char const * p)
736 if (a.empty() || !*p)
739 size_t r = a.find_last_not_of(p);
740 size_t l = a.find_first_not_of(p);
742 // Is this the minimal test? (lgb)
743 if (r == string::npos && l == string::npos)
746 return a.substr(l, r - l + 1);
750 string const rtrim(string const & a, char const * p)
754 if (a.empty() || !*p)
757 size_t r = a.find_last_not_of(p);
759 // Is this test really needed? (Lgb)
760 if (r == string::npos)
763 return a.substr(0, r + 1);
767 docstring const rtrim(docstring const & a, char const * p)
771 if (a.empty() || !*p)
774 size_t r = a.find_last_not_of(from_ascii(p));
776 // Is this test really needed? (Lgb)
777 if (r == docstring::npos)
780 return a.substr(0, r + 1);
784 string const ltrim(string const & a, char const * p)
787 if (a.empty() || !*p)
789 size_t l = a.find_first_not_of(p);
790 if (l == string::npos)
792 return a.substr(l, string::npos);
796 docstring const ltrim(docstring const & a, char const * p)
799 if (a.empty() || !*p)
801 size_t l = a.find_first_not_of(from_ascii(p));
802 if (l == docstring::npos)
804 return a.substr(l, docstring::npos);
809 template<typename String, typename Char> inline
810 String const doSplit(String const & a, String & piece, Char delim)
813 size_t i = a.find(delim);
814 if (i == a.length() - 1) {
815 piece = a.substr(0, i);
816 } else if (i != String::npos) {
817 piece = a.substr(0, i);
818 tmp = a.substr(i + 1);
821 tmp = a.substr(i + 1);
828 template<typename Char> inline
829 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
832 size_t i = a.find(delim);
833 if (i == a.length() - 1) {
834 piece = a.substr(0, i);
835 } else if (i != docstring::npos) {
836 piece = a.substr(0, i);
837 tmp = a.substr(i + 1);
840 tmp = a.substr(i + 1);
850 string const split(string const & a, string & piece, char delim)
852 return doSplit(a, piece, delim);
856 docstring const split(docstring const & a, docstring & piece, char_type delim)
858 return doSplit(a, piece, delim);
862 string const split(string const & a, char delim)
865 size_t i = a.find(delim);
866 if (i != string::npos) // found delim
867 tmp = a.substr(i + 1);
873 string const rsplit(string const & a, string & piece, char delim)
876 size_t i = a.rfind(delim);
877 if (i != string::npos) { // delimiter was found
878 piece = a.substr(0, i);
879 tmp = a.substr(i + 1);
880 } else { // delimiter was not found
887 docstring const escape(docstring const & lab)
889 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
890 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
892 for (size_t i = 0; i < lab.length(); ++i) {
893 char_type c = lab[i];
894 if (c >= 128 || c == '=' || c == '%') {
895 // Although char_type is a 32 bit type we know that
896 // UCS4 occupies only 21 bits, so we don't need to
897 // encode bigger values. Test for 2^24 because we
898 // can encode that with the 6 hex digits that are
899 // needed for 21 bits anyway.
900 BOOST_ASSERT(c < (1 << 24));
902 enc += hexdigit[(c>>20) & 15];
903 enc += hexdigit[(c>>16) & 15];
904 enc += hexdigit[(c>>12) & 15];
905 enc += hexdigit[(c>> 8) & 15];
906 enc += hexdigit[(c>> 4) & 15];
907 enc += hexdigit[ c & 15];
918 template<typename String> vector<String> const
919 getVectorFromStringT(String const & str, String const & delim)
921 // Lars would like this code to go, but for now his replacement (below)
922 // doesn't fullfil the same function. I have, therefore, reactivated the
923 // old code for now. Angus 11 Nov 2002.
928 String keys = rtrim(str);
930 size_t const idx = keys.find(delim);
931 if (idx == String::npos) {
932 vec.push_back(ltrim(keys));
935 String const key = trim(keys.substr(0, idx));
938 size_t const start = idx + delim.size();
939 keys = keys.substr(start);
943 typedef boost::char_separator<typename String::value_type> Separator;
944 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
945 Separator sep(delim.c_str());
946 Tokenizer tokens(str, sep);
947 return vector<String>(tokens.begin(), tokens.end());
954 vector<string> const getVectorFromString(string const & str,
955 string const & delim)
957 return getVectorFromStringT<string>(str, delim);
961 vector<docstring> const getVectorFromString(docstring const & str,
962 docstring const & delim)
964 return getVectorFromStringT<docstring>(str, delim);
968 // the same vice versa
969 string const getStringFromVector(vector<string> const & vec,
970 string const & delim)
974 for (vector<string>::const_iterator it = vec.begin();
975 it != vec.end(); ++it) {
976 string item = trim(*it);
987 int findToken(char const * const str[], string const & search_token)
991 while (str[i][0] && str[i] != search_token)
999 docstring const externalLineEnding(docstring const & str)
1001 #if defined(__APPLE__)
1002 // The MAC clipboard uses \r for lineendings, and we use \n
1003 return subst(str, '\n', '\r');
1004 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
1005 // Windows clipboard uses \r\n for lineendings, and we use \n
1006 return subst(str, from_ascii("\n"), from_ascii("\r\n"));
1013 docstring const internalLineEnding(docstring const & str)
1015 docstring const s = subst(str, from_ascii("\r\n"), from_ascii("\n"));
1016 return subst(s, '\r', '\n');
1021 docstring bformat(docstring const & fmt, int arg1)
1023 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1024 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1025 return subst(str, from_ascii("%%"), from_ascii("%"));
1030 docstring bformat(docstring const & fmt, long arg1)
1032 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1033 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1034 return subst(str, from_ascii("%%"), from_ascii("%"));
1039 docstring bformat(docstring const & fmt, unsigned int arg1)
1041 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1042 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1043 return subst(str, from_ascii("%%"), from_ascii("%"));
1048 docstring bformat(docstring const & fmt, docstring arg1)
1050 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1051 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1052 return subst(str, from_ascii("%%"), from_ascii("%"));
1057 docstring bformat(docstring const & fmt, char * arg1)
1059 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1060 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1061 return subst(str, from_ascii("%%"), from_ascii("%"));
1066 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1068 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1069 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1070 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1071 str = subst(str, from_ascii("%2$s"), arg2);
1072 return subst(str, from_ascii("%%"), from_ascii("%"));
1077 docstring bformat(docstring const & fmt, char const * 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"), from_ascii(arg1));
1082 str = subst(fmt, from_ascii("%2$s"), arg2);
1083 return subst(str, from_ascii("%%"), from_ascii("%"));
1088 docstring bformat(docstring const & fmt, int arg1, int arg2)
1090 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1091 BOOST_ASSERT(contains(fmt, from_ascii("%2$d")));
1092 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1093 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1094 return subst(str, from_ascii("%%"), from_ascii("%"));
1099 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1101 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1102 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1103 BOOST_ASSERT(contains(fmt, from_ascii("%3$s")));
1104 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1105 str = subst(str, from_ascii("%2$s"), arg2);
1106 str = subst(str, from_ascii("%3$s"), arg3);
1107 return subst(str, from_ascii("%%"), from_ascii("%"));
1112 docstring bformat(docstring const & fmt,
1113 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1115 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1116 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1117 BOOST_ASSERT(contains(fmt, from_ascii("%3$s")));
1118 BOOST_ASSERT(contains(fmt, from_ascii("%4$s")));
1119 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1120 str = subst(str, from_ascii("%2$s"), arg2);
1121 str = subst(str, from_ascii("%3$s"), arg3);
1122 str = subst(str, from_ascii("%4$s"), arg4);
1123 return subst(str, from_ascii("%%"), from_ascii("%"));
1126 } // namespace support