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/lyxlib.h"
17 #include "support/convert.h"
18 #include "support/qstring_helpers.h"
19 #include "support/textutils.h"
23 #include <boost/tokenizer.hpp>
24 #include <boost/assert.hpp>
37 #ifndef CXX_GLOBAL_CSTD
47 * Convert a QChar into a UCS4 character.
48 * This is a hack (it does only make sense for the common part of the UCS4
49 * and UTF16 encodings) and should not be used.
50 * This does only exist because of performance reasons (a real conversion
51 * using iconv is too slow on windows).
53 static inline char_type qchar_to_ucs4(QChar const & qchar)
55 BOOST_ASSERT(is_utf16(static_cast<char_type>(qchar.unicode())));
56 return static_cast<char_type>(qchar.unicode());
61 * Convert a UCS4 character into a QChar.
62 * This is a hack (it does only make sense for the common part of the UCS4
63 * and UTF16 encodings) and should not be used.
64 * This does only exist because of performance reasons (a real conversion
65 * using iconv is too slow on windows).
67 static inline QChar const ucs4_to_qchar(char_type const ucs4)
69 BOOST_ASSERT(is_utf16(ucs4));
70 return QChar(static_cast<unsigned short>(ucs4));
75 /// Maximum valid UCS4 code point
76 char_type const ucs4_max = 0x10ffff;
80 bool isLetterChar(char_type c)
84 // outside the UCS4 range
86 // assume that all non-utf16 characters are letters
89 return ucs4_to_qchar(c).isLetter();
93 bool isAlphaASCII(char_type c)
95 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
99 bool isPrintable(char_type c)
103 // outside the UCS4 range
105 // assume that all non-utf16 characters are printable
108 return ucs4_to_qchar(c).isPrint();
112 bool isPrintableNonspace(char_type c)
116 // outside the UCS4 range
118 // assume that all non-utf16 characters are printable and
122 QChar const qc = ucs4_to_qchar(c);
123 return qc.isPrint() && !qc.isSpace();
127 bool isSpace(char_type c)
130 // assume that no non-utf16 character is a space
131 // c outside the UCS4 range is catched as well
134 QChar const qc = ucs4_to_qchar(c);
139 bool isDigit(char_type c)
142 // assume that no non-utf16 character is a digit
143 // c outside the UCS4 range is catched as well
145 return ucs4_to_qchar(c).isDigit();
149 bool isDigitASCII(char_type c)
151 return '0' <= c && c <= '9';
156 int compare_no_case(docstring const & s, docstring const & s2)
158 docstring::const_iterator p = s.begin();
159 docstring::const_iterator p2 = s2.begin();
161 while (p != s.end() && p2 != s2.end()) {
162 char_type const lc1 = lowercase(*p);
163 char_type const lc2 = lowercase(*p2);
165 return (lc1 < lc2) ? -1 : 1;
170 if (s.size() == s2.size())
172 if (s.size() < s2.size())
180 template<typename Char>
181 Char ascii_tolower(Char c) {
182 if (c >= 'A' && c <= 'Z')
183 return c - 'A' + 'a';
190 int compare_ascii_no_case(string const & s, string const & s2)
192 string::const_iterator p = s.begin();
193 string::const_iterator p2 = s2.begin();
195 while (p != s.end() && p2 != s2.end()) {
196 int const lc1 = ascii_tolower(*p);
197 int const lc2 = ascii_tolower(*p2);
199 return (lc1 < lc2) ? -1 : 1;
204 if (s.size() == s2.size())
206 if (s.size() < s2.size())
212 int compare_ascii_no_case(docstring const & s, docstring const & s2)
214 docstring::const_iterator p = s.begin();
215 docstring::const_iterator p2 = s2.begin();
217 while (p != s.end() && p2 != s2.end()) {
218 char_type const lc1 = ascii_tolower(*p);
219 char_type const lc2 = ascii_tolower(*p2);
221 return (lc1 < lc2) ? -1 : 1;
226 if (s.size() == s2.size())
228 if (s.size() < s2.size())
234 bool isStrInt(string const & str)
236 if (str.empty()) return false;
238 // Remove leading and trailing white space chars.
239 string const tmpstr = trim(str);
240 if (tmpstr.empty()) return false;
242 string::const_iterator cit = tmpstr.begin();
243 if ((*cit) == '-') ++cit;
244 string::const_iterator end = tmpstr.end();
245 for (; cit != end; ++cit) {
246 if (!isdigit((*cit))) return false;
252 bool isStrUnsignedInt(string const & str)
254 if (str.empty()) return false;
256 // Remove leading and trailing white space chars.
257 string const tmpstr = trim(str);
258 if (tmpstr.empty()) return false;
260 string::const_iterator cit = tmpstr.begin();
261 string::const_iterator end = tmpstr.end();
262 for (; cit != end; ++cit) {
263 if (!isdigit((*cit))) return false;
269 bool isStrDbl(string const & str)
271 if (str.empty()) return false;
273 // Remove leading and trailing white space chars.
274 string const tmpstr = trim(str);
275 if (tmpstr.empty()) return false;
276 // if (1 < tmpstr.count('.')) return false;
278 string::const_iterator cit = tmpstr.begin();
279 bool found_dot(false);
280 if ((*cit) == '-') ++cit;
281 string::const_iterator end = tmpstr.end();
282 for (; cit != end; ++cit) {
302 bool isHexChar(char_type c)
314 c == 'a' || c == 'A' ||
315 c == 'b' || c == 'B' ||
316 c == 'c' || c == 'C' ||
317 c == 'd' || c == 'D' ||
318 c == 'e' || c == 'E' ||
319 c == 'f' || c == 'F';
325 bool isHex(docstring const & str)
329 if (str.length() > 2 && str[0] == '0' &&
330 (str[1] == 'x' || str[1] == 'X'))
333 int const len = str.length();
335 for (; index < len; ++index) {
336 if (!isHexChar(str[index]))
343 int hexToInt(docstring const & str)
345 string s = to_ascii(str);
347 sscanf(s.c_str(), "%x", &h);
352 bool isAscii(docstring const & str)
354 int const len = str.length();
355 for (int i = 0; i < len; ++i)
362 bool isAscii(string const & str)
364 int const len = str.length();
365 for (int i = 0; i < len; ++i)
366 if (static_cast<unsigned char>(str[i]) >= 0x80)
372 char lowercase(char c)
374 BOOST_ASSERT(static_cast<unsigned char>(c) < 0x80);
375 return char(tolower(c));
379 char uppercase(char c)
381 BOOST_ASSERT(static_cast<unsigned char>(c) < 0x80);
382 return char(toupper(c));
386 char_type lowercase(char_type c)
389 // We don't know how to lowercase a non-utf16 char
391 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
395 char_type uppercase(char_type c)
398 // We don't know how to uppercase a non-utf16 char
400 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
406 // since we cannot use std::tolower and std::toupper directly in the
407 // calls to std::transform yet, we use these helper clases. (Lgb)
409 struct local_lowercase {
410 char_type operator()(char_type c) const {
412 // We don't know how to lowercase a non-utf16 char
414 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
418 struct local_uppercase {
419 char_type operator()(char_type c) const {
421 // We don't know how to uppercase a non-utf16 char
423 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
427 template<typename Char> struct local_ascii_lowercase {
428 Char operator()(Char c) const { return ascii_tolower(c); }
431 } // end of anon namespace
433 docstring const lowercase(docstring const & a)
436 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
441 docstring const uppercase(docstring const & a)
444 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
449 string const ascii_lowercase(string const & a)
452 transform(tmp.begin(), tmp.end(), tmp.begin(),
453 local_ascii_lowercase<char>());
458 docstring const ascii_lowercase(docstring const & a)
461 transform(tmp.begin(), tmp.end(), tmp.begin(),
462 local_ascii_lowercase<char_type>());
467 bool prefixIs(docstring const & a, char_type c)
475 bool prefixIs(string const & a, string const & pre)
477 size_t const prelen = pre.length();
478 size_t const alen = a.length();
480 if (prelen > alen || a.empty())
482 #if defined(STD_STRING_IS_GOOD)
483 return a.compare(0, prelen, pre) == 0;
485 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
490 bool prefixIs(docstring const & a, docstring const & pre)
492 size_t const prelen = pre.length();
493 size_t const alen = a.length();
495 if (prelen > alen || a.empty())
498 return a.compare(0, prelen, pre) == 0;
502 bool suffixIs(string const & a, char c)
504 if (a.empty()) return false;
505 return a[a.length() - 1] == c;
509 bool suffixIs(docstring const & a, char_type c)
513 return a[a.length() - 1] == c;
517 bool suffixIs(string const & a, string const & suf)
519 size_t const suflen = suf.length();
520 size_t const alen = a.length();
525 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
526 string tmp(a, alen - suflen);
527 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
529 return a.compare(alen - suflen, suflen, suf) == 0;
534 bool containsOnly(string const & s, string const & cset)
536 return s.find_first_not_of(cset) == string::npos;
540 // ale970405+lasgoutt-970425
541 // rewritten to use new string (Lgb)
542 string const token(string const & a, char delim, int n)
550 // Find delimiter or end of string
552 if ((i = a.find(delim, i)) == string::npos)
558 // i is now the n'th delim (or string::npos)
559 if (i == string::npos)
562 k = a.find(delim, i);
563 // k is now the n'th + 1 delim (or string::npos)
565 return a.substr(i, k - i);
569 docstring const token(docstring const & a, char_type delim, int n)
571 if (a.empty()) return docstring();
576 // Find delimiter or end of string
578 if ((i = a.find(delim, i)) == docstring::npos)
584 // i is now the n'th delim (or string::npos)
585 if (i == docstring::npos)
588 k = a.find(delim, i);
589 // k is now the n'th + 1 delim (or string::npos)
591 return a.substr(i, k - i);
595 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
596 // rewritten to use new string (Lgb)
597 int tokenPos(string const & a, char delim, string const & tok)
603 while (!str.empty()) {
604 str = split(str, tmptok, delim);
615 /// Substitute all \a oldchar with \a newchar
616 template<typename Ch> inline
617 std::basic_string<Ch> const subst_char(std::basic_string<Ch> const & a,
618 Ch oldchar, Ch newchar)
620 typedef std::basic_string<Ch> String;
622 typename String::iterator lit = tmp.begin();
623 typename String::iterator end = tmp.end();
624 for (; lit != end; ++lit)
625 if ((*lit) == oldchar)
630 /// Substitute all \a oldchar with \a newchar
631 docstring const subst_char(docstring const & a,
632 docstring::value_type oldchar, docstring::value_type newchar)
635 docstring::iterator lit = tmp.begin();
636 docstring::iterator end = tmp.end();
637 for (; lit != end; ++lit)
638 if ((*lit) == oldchar)
644 /// substitutes all instances of \a oldstr with \a newstr
645 template<typename String> inline
646 String const subst_string(String const & a,
647 String const & oldstr, String const & newstr)
649 BOOST_ASSERT(!oldstr.empty());
652 size_t const olen = oldstr.length();
653 while ((i = lstr.find(oldstr, i)) != string::npos) {
654 lstr.replace(i, olen, newstr);
655 i += newstr.length(); // We need to be sure that we dont
656 // use the same i over and over again.
661 docstring const subst_string(docstring const & a,
662 docstring const & oldstr, docstring const & newstr)
664 BOOST_ASSERT(!oldstr.empty());
667 size_t const olen = oldstr.length();
668 while ((i = lstr.find(oldstr, i)) != string::npos) {
669 lstr.replace(i, olen, newstr);
670 i += newstr.length(); // We need to be sure that we dont
671 // use the same i over and over again.
679 string const subst(string const & a, char oldchar, char newchar)
681 return subst_char(a, oldchar, newchar);
685 docstring const subst(docstring const & a,
686 char_type oldchar, char_type newchar)
688 return subst_char(a, oldchar, newchar);
692 string const subst(string const & a,
693 string const & oldstr, string const & newstr)
695 return subst_string(a, oldstr, newstr);
699 docstring const subst(docstring const & a,
700 docstring const & oldstr, docstring const & newstr)
702 return subst_string(a, oldstr, newstr);
706 docstring const trim(docstring const & a, char const * p)
710 if (a.empty() || !*p)
713 docstring s = from_ascii(p);
714 size_t r = a.find_last_not_of(s);
715 size_t l = a.find_first_not_of(s);
717 // Is this the minimal test? (lgb)
718 if (r == docstring::npos && l == docstring::npos)
721 return a.substr(l, r - l + 1);
725 string const trim(string const & a, char const * p)
729 if (a.empty() || !*p)
732 size_t r = a.find_last_not_of(p);
733 size_t l = a.find_first_not_of(p);
735 // Is this the minimal test? (lgb)
736 if (r == string::npos && l == string::npos)
739 return a.substr(l, r - l + 1);
743 string const rtrim(string const & a, char const * p)
747 if (a.empty() || !*p)
750 size_t r = a.find_last_not_of(p);
752 // Is this test really needed? (Lgb)
753 if (r == string::npos)
756 return a.substr(0, r + 1);
760 docstring const rtrim(docstring const & a, char const * p)
764 if (a.empty() || !*p)
767 size_t r = a.find_last_not_of(from_ascii(p));
769 // Is this test really needed? (Lgb)
770 if (r == docstring::npos)
773 return a.substr(0, r + 1);
777 string const ltrim(string const & a, char const * p)
780 if (a.empty() || !*p)
782 size_t l = a.find_first_not_of(p);
783 if (l == string::npos)
785 return a.substr(l, string::npos);
789 docstring const ltrim(docstring const & a, char const * p)
792 if (a.empty() || !*p)
794 size_t l = a.find_first_not_of(from_ascii(p));
795 if (l == docstring::npos)
797 return a.substr(l, docstring::npos);
802 template<typename String, typename Char> inline
803 String const doSplit(String const & a, String & piece, Char delim)
806 size_t i = a.find(delim);
807 if (i == a.length() - 1) {
808 piece = a.substr(0, i);
809 } else if (i != String::npos) {
810 piece = a.substr(0, i);
811 tmp = a.substr(i + 1);
814 tmp = a.substr(i + 1);
821 template<typename Char> inline
822 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
825 size_t i = a.find(delim);
826 if (i == a.length() - 1) {
827 piece = a.substr(0, i);
828 } else if (i != docstring::npos) {
829 piece = a.substr(0, i);
830 tmp = a.substr(i + 1);
833 tmp = a.substr(i + 1);
843 string const split(string const & a, string & piece, char delim)
845 return doSplit(a, piece, delim);
849 docstring const split(docstring const & a, docstring & piece, char_type delim)
851 return doSplit(a, piece, delim);
855 string const split(string const & a, char delim)
858 size_t i = a.find(delim);
859 if (i != string::npos) // found delim
860 tmp = a.substr(i + 1);
866 string const rsplit(string const & a, string & piece, char delim)
869 size_t i = a.rfind(delim);
870 if (i != string::npos) { // delimiter was found
871 piece = a.substr(0, i);
872 tmp = a.substr(i + 1);
873 } else { // delimiter was not found
880 docstring const escape(docstring const & lab)
882 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
883 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
885 for (size_t i = 0; i < lab.length(); ++i) {
886 char_type c = lab[i];
887 if (c >= 128 || c == '=' || c == '%') {
888 // Although char_type is a 32 bit type we know that
889 // UCS4 occupies only 21 bits, so we don't need to
890 // encode bigger values. Test for 2^24 because we
891 // can encode that with the 6 hex digits that are
892 // needed for 21 bits anyway.
893 BOOST_ASSERT(c < (1 << 24));
895 enc += hexdigit[(c>>20) & 15];
896 enc += hexdigit[(c>>16) & 15];
897 enc += hexdigit[(c>>12) & 15];
898 enc += hexdigit[(c>> 8) & 15];
899 enc += hexdigit[(c>> 4) & 15];
900 enc += hexdigit[ c & 15];
911 template<typename String> vector<String> const
912 getVectorFromStringT(String const & str, String const & delim)
914 // Lars would like this code to go, but for now his replacement (below)
915 // doesn't fullfil the same function. I have, therefore, reactivated the
916 // old code for now. Angus 11 Nov 2002.
921 String keys = rtrim(str);
923 size_t const idx = keys.find(delim);
924 if (idx == String::npos) {
925 vec.push_back(ltrim(keys));
928 String const key = trim(keys.substr(0, idx));
931 size_t const start = idx + delim.size();
932 keys = keys.substr(start);
936 typedef boost::char_separator<typename String::value_type> Separator;
937 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
938 Separator sep(delim.c_str());
939 Tokenizer tokens(str, sep);
940 return vector<String>(tokens.begin(), tokens.end());
947 vector<string> const getVectorFromString(string const & str,
948 string const & delim)
950 return getVectorFromStringT<string>(str, delim);
954 vector<docstring> const getVectorFromString(docstring const & str,
955 docstring const & delim)
957 return getVectorFromStringT<docstring>(str, delim);
961 // the same vice versa
962 string const getStringFromVector(vector<string> const & vec,
963 string const & delim)
967 for (vector<string>::const_iterator it = vec.begin();
968 it != vec.end(); ++it) {
969 string item = trim(*it);
980 int findToken(char const * const str[], string const & search_token)
984 while (str[i][0] && str[i] != search_token)
992 docstring const externalLineEnding(docstring const & str)
994 #if defined(__APPLE__)
995 // The MAC clipboard uses \r for lineendings, and we use \n
996 return subst(str, '\n', '\r');
997 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
998 // Windows clipboard uses \r\n for lineendings, and we use \n
999 return subst(str, from_ascii("\n"), from_ascii("\r\n"));
1006 docstring const internalLineEnding(docstring const & str)
1008 docstring const s = subst(str, from_ascii("\r\n"), from_ascii("\n"));
1009 return subst(s, '\r', '\n');
1014 docstring bformat(docstring const & fmt, int arg1)
1016 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1017 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1018 return subst(str, from_ascii("%%"), from_ascii("%"));
1023 docstring bformat(docstring const & fmt, long arg1)
1025 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1026 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1027 return subst(str, from_ascii("%%"), from_ascii("%"));
1032 docstring bformat(docstring const & fmt, unsigned int arg1)
1034 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1035 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1036 return subst(str, from_ascii("%%"), from_ascii("%"));
1041 docstring bformat(docstring const & fmt, docstring arg1)
1043 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1044 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1045 return subst(str, from_ascii("%%"), from_ascii("%"));
1050 docstring bformat(docstring const & fmt, char * arg1)
1052 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1053 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1054 return subst(str, from_ascii("%%"), from_ascii("%"));
1059 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1061 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1062 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1063 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1064 str = subst(str, from_ascii("%2$s"), arg2);
1065 return subst(str, from_ascii("%%"), from_ascii("%"));
1070 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1072 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1073 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1074 docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1075 str = subst(fmt, from_ascii("%2$s"), arg2);
1076 return subst(str, from_ascii("%%"), from_ascii("%"));
1081 docstring bformat(docstring const & fmt, int arg1, int arg2)
1083 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1084 BOOST_ASSERT(contains(fmt, from_ascii("%2$d")));
1085 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1086 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1087 return subst(str, from_ascii("%%"), from_ascii("%"));
1092 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1094 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1095 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1096 BOOST_ASSERT(contains(fmt, from_ascii("%3$s")));
1097 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1098 str = subst(str, from_ascii("%2$s"), arg2);
1099 str = subst(str, from_ascii("%3$s"), arg3);
1100 return subst(str, from_ascii("%%"), from_ascii("%"));
1105 docstring bformat(docstring const & fmt,
1106 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1108 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1109 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1110 BOOST_ASSERT(contains(fmt, from_ascii("%3$s")));
1111 BOOST_ASSERT(contains(fmt, from_ascii("%4$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 str = subst(str, from_ascii("%4$s"), arg4);
1116 return subst(str, from_ascii("%%"), from_ascii("%"));
1119 } // namespace support