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/lyxlib.h"
19 #include "support/qstring_helpers.h"
20 #include "support/textutils.h"
22 #include <boost/tokenizer.hpp>
23 #include <boost/assert.hpp>
36 #ifndef CXX_GLOBAL_CSTD
46 * Convert a QChar into a UCS4 character.
47 * This is a hack (it does only make sense for the common part of the UCS4
48 * and UTF16 encodings) and should not be used.
49 * This does only exist because of performance reasons (a real conversion
50 * using iconv is too slow on windows).
52 static inline char_type qchar_to_ucs4(QChar const & qchar)
54 BOOST_ASSERT(is_utf16(static_cast<char_type>(qchar.unicode())));
55 return static_cast<char_type>(qchar.unicode());
60 * Convert a UCS4 character into a QChar.
61 * This is a hack (it does only make sense for the common part of the UCS4
62 * and UTF16 encodings) and should not be used.
63 * This does only exist because of performance reasons (a real conversion
64 * using iconv is too slow on windows).
66 static inline QChar const ucs4_to_qchar(char_type const ucs4)
68 BOOST_ASSERT(is_utf16(ucs4));
69 return QChar(static_cast<unsigned short>(ucs4));
74 /// Maximum valid UCS4 code point
75 char_type const ucs4_max = 0x10ffff;
79 bool isLetterChar(char_type c)
83 // outside the UCS4 range
85 // assume that all non-utf16 characters are letters
88 return ucs4_to_qchar(c).isLetter();
92 bool isAlphaASCII(char_type c)
94 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
98 bool isPrintable(char_type c)
102 // outside the UCS4 range
104 // assume that all non-utf16 characters are printable
107 return ucs4_to_qchar(c).isPrint();
111 bool isPrintableNonspace(char_type c)
115 // outside the UCS4 range
117 // assume that all non-utf16 characters are printable and
121 QChar const qc = ucs4_to_qchar(c);
122 return qc.isPrint() && !qc.isSpace();
126 bool isSpace(char_type c)
129 // assume that no non-utf16 character is a space
130 // c outside the UCS4 range is catched as well
133 QChar const qc = ucs4_to_qchar(c);
138 bool isDigit(char_type c)
141 // assume that no non-utf16 character is a digit
142 // c outside the UCS4 range is catched as well
144 return ucs4_to_qchar(c).isDigit();
148 bool isDigitASCII(char_type c)
150 return '0' <= c && c <= '9';
155 int compare_no_case(docstring const & s, docstring const & s2)
157 docstring::const_iterator p = s.begin();
158 docstring::const_iterator p2 = s2.begin();
160 while (p != s.end() && p2 != s2.end()) {
161 char_type const lc1 = lowercase(*p);
162 char_type const lc2 = lowercase(*p2);
164 return (lc1 < lc2) ? -1 : 1;
169 if (s.size() == s2.size())
171 if (s.size() < s2.size())
179 template<typename Char>
180 Char ascii_tolower(Char c) {
181 if (c >= 'A' && c <= 'Z')
182 return c - 'A' + 'a';
189 int compare_ascii_no_case(string const & s, string const & s2)
191 string::const_iterator p = s.begin();
192 string::const_iterator p2 = s2.begin();
194 while (p != s.end() && p2 != s2.end()) {
195 int const lc1 = ascii_tolower(*p);
196 int const lc2 = ascii_tolower(*p2);
198 return (lc1 < lc2) ? -1 : 1;
203 if (s.size() == s2.size())
205 if (s.size() < s2.size())
211 int compare_ascii_no_case(docstring const & s, docstring const & s2)
213 docstring::const_iterator p = s.begin();
214 docstring::const_iterator p2 = s2.begin();
216 while (p != s.end() && p2 != s2.end()) {
217 char_type const lc1 = ascii_tolower(*p);
218 char_type const lc2 = ascii_tolower(*p2);
220 return (lc1 < lc2) ? -1 : 1;
225 if (s.size() == s2.size())
227 if (s.size() < s2.size())
233 bool isStrInt(string const & str)
235 if (str.empty()) return false;
237 // Remove leading and trailing white space chars.
238 string const tmpstr = trim(str);
239 if (tmpstr.empty()) return false;
241 string::const_iterator cit = tmpstr.begin();
242 if ((*cit) == '-') ++cit;
243 string::const_iterator end = tmpstr.end();
244 for (; cit != end; ++cit) {
245 if (!isdigit((*cit))) return false;
251 bool isStrUnsignedInt(string const & str)
253 if (str.empty()) return false;
255 // Remove leading and trailing white space chars.
256 string const tmpstr = trim(str);
257 if (tmpstr.empty()) return false;
259 string::const_iterator cit = tmpstr.begin();
260 string::const_iterator end = tmpstr.end();
261 for (; cit != end; ++cit) {
262 if (!isdigit((*cit))) return false;
268 bool isStrDbl(string const & str)
270 if (str.empty()) return false;
272 // Remove leading and trailing white space chars.
273 string const tmpstr = trim(str);
274 if (tmpstr.empty()) return false;
275 // if (1 < tmpstr.count('.')) return false;
277 string::const_iterator cit = tmpstr.begin();
278 bool found_dot(false);
279 if ((*cit) == '-') ++cit;
280 string::const_iterator end = tmpstr.end();
281 for (; cit != end; ++cit) {
301 bool isHexChar(char_type c)
313 c == 'a' || c == 'A' ||
314 c == 'b' || c == 'B' ||
315 c == 'c' || c == 'C' ||
316 c == 'd' || c == 'D' ||
317 c == 'e' || c == 'E' ||
318 c == 'f' || c == 'F';
324 bool isHex(docstring const & str)
328 if (str.length() > 2 && str[0] == '0' &&
329 (str[1] == 'x' || str[1] == 'X'))
332 int const len = str.length();
334 for (; index < len; ++index) {
335 if (!isHexChar(str[index]))
342 int hexToInt(docstring const & str)
344 string s = to_ascii(str);
346 sscanf(s.c_str(), "%x", &h);
351 bool isAscii(docstring const & str)
353 int const len = str.length();
354 for (int i = 0; i < len; ++i)
361 bool isAscii(string const & str)
363 int const len = str.length();
364 for (int i = 0; i < len; ++i)
365 if (static_cast<unsigned char>(str[i]) >= 0x80)
371 char lowercase(char c)
373 BOOST_ASSERT(static_cast<unsigned char>(c) < 0x80);
374 return char(tolower(c));
378 char uppercase(char c)
380 BOOST_ASSERT(static_cast<unsigned char>(c) < 0x80);
381 return char(toupper(c));
385 char_type lowercase(char_type c)
388 // We don't know how to lowercase a non-utf16 char
390 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
394 char_type uppercase(char_type c)
397 // We don't know how to uppercase a non-utf16 char
399 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
405 // since we cannot use std::tolower and std::toupper directly in the
406 // calls to std::transform yet, we use these helper clases. (Lgb)
408 struct local_lowercase {
409 char_type operator()(char_type c) const {
411 // We don't know how to lowercase a non-utf16 char
413 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
417 struct local_uppercase {
418 char_type operator()(char_type c) const {
420 // We don't know how to uppercase a non-utf16 char
422 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
426 template<typename Char> struct local_ascii_lowercase {
427 Char operator()(Char c) const { return ascii_tolower(c); }
430 } // end of anon namespace
432 docstring const lowercase(docstring const & a)
435 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
440 docstring const uppercase(docstring const & a)
443 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
448 string const ascii_lowercase(string const & a)
451 transform(tmp.begin(), tmp.end(), tmp.begin(),
452 local_ascii_lowercase<char>());
457 docstring const ascii_lowercase(docstring const & a)
460 transform(tmp.begin(), tmp.end(), tmp.begin(),
461 local_ascii_lowercase<char_type>());
466 bool prefixIs(docstring const & a, char_type c)
474 bool prefixIs(string const & a, string const & pre)
476 size_t const prelen = pre.length();
477 size_t const alen = a.length();
479 if (prelen > alen || a.empty())
481 #if defined(STD_STRING_IS_GOOD)
482 return a.compare(0, prelen, pre) == 0;
484 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
489 bool prefixIs(docstring const & a, docstring const & pre)
491 size_t const prelen = pre.length();
492 size_t const alen = a.length();
494 if (prelen > alen || a.empty())
497 return a.compare(0, prelen, pre) == 0;
501 bool suffixIs(string const & a, char c)
503 if (a.empty()) return false;
504 return a[a.length() - 1] == c;
508 bool suffixIs(docstring const & a, char_type c)
512 return a[a.length() - 1] == c;
516 bool suffixIs(string const & a, string const & suf)
518 size_t const suflen = suf.length();
519 size_t const alen = a.length();
524 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
525 string tmp(a, alen - suflen);
526 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
528 return a.compare(alen - suflen, suflen, suf) == 0;
533 bool containsOnly(string const & s, string const & cset)
535 return s.find_first_not_of(cset) == string::npos;
539 // ale970405+lasgoutt-970425
540 // rewritten to use new string (Lgb)
541 string const token(string const & a, char delim, int n)
549 // Find delimiter or end of string
551 if ((i = a.find(delim, i)) == string::npos)
557 // i is now the n'th delim (or string::npos)
558 if (i == string::npos)
561 k = a.find(delim, i);
562 // k is now the n'th + 1 delim (or string::npos)
564 return a.substr(i, k - i);
568 docstring const token(docstring const & a, char_type delim, int n)
570 if (a.empty()) return docstring();
575 // Find delimiter or end of string
577 if ((i = a.find(delim, i)) == docstring::npos)
583 // i is now the n'th delim (or string::npos)
584 if (i == docstring::npos)
587 k = a.find(delim, i);
588 // k is now the n'th + 1 delim (or string::npos)
590 return a.substr(i, k - i);
594 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
595 // rewritten to use new string (Lgb)
596 int tokenPos(string const & a, char delim, string const & tok)
602 while (!str.empty()) {
603 str = split(str, tmptok, delim);
614 /// Substitute all \a oldchar with \a newchar
615 template<typename Ch> inline
616 std::basic_string<Ch> const subst_char(std::basic_string<Ch> const & a,
617 Ch oldchar, Ch newchar)
619 typedef std::basic_string<Ch> String;
621 typename String::iterator lit = tmp.begin();
622 typename String::iterator end = tmp.end();
623 for (; lit != end; ++lit)
624 if ((*lit) == oldchar)
629 /// Substitute all \a oldchar with \a newchar
630 docstring const subst_char(docstring const & a,
631 docstring::value_type oldchar, docstring::value_type newchar)
634 docstring::iterator lit = tmp.begin();
635 docstring::iterator end = tmp.end();
636 for (; lit != end; ++lit)
637 if ((*lit) == oldchar)
643 /// substitutes all instances of \a oldstr with \a newstr
644 template<typename String> inline
645 String const subst_string(String const & a,
646 String const & oldstr, String const & newstr)
648 BOOST_ASSERT(!oldstr.empty());
651 size_t const olen = oldstr.length();
652 while ((i = lstr.find(oldstr, i)) != string::npos) {
653 lstr.replace(i, olen, newstr);
654 i += newstr.length(); // We need to be sure that we dont
655 // use the same i over and over again.
660 docstring const subst_string(docstring const & a,
661 docstring const & oldstr, docstring const & newstr)
663 BOOST_ASSERT(!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.
678 string const subst(string const & a, char oldchar, char newchar)
680 return subst_char(a, oldchar, newchar);
684 docstring const subst(docstring const & a,
685 char_type oldchar, char_type newchar)
687 return subst_char(a, oldchar, newchar);
691 string const subst(string const & a,
692 string const & oldstr, string const & newstr)
694 return subst_string(a, oldstr, newstr);
698 docstring const subst(docstring const & a,
699 docstring const & oldstr, docstring const & newstr)
701 return subst_string(a, oldstr, newstr);
705 docstring const trim(docstring const & a, char const * p)
709 if (a.empty() || !*p)
712 docstring s = from_ascii(p);
713 size_t r = a.find_last_not_of(s);
714 size_t l = a.find_first_not_of(s);
716 // Is this the minimal test? (lgb)
717 if (r == docstring::npos && l == docstring::npos)
720 return a.substr(l, r - l + 1);
724 string const trim(string const & a, char const * p)
728 if (a.empty() || !*p)
731 size_t r = a.find_last_not_of(p);
732 size_t l = a.find_first_not_of(p);
734 // Is this the minimal test? (lgb)
735 if (r == string::npos && l == string::npos)
738 return a.substr(l, r - l + 1);
742 string const rtrim(string const & a, char const * p)
746 if (a.empty() || !*p)
749 size_t r = a.find_last_not_of(p);
751 // Is this test really needed? (Lgb)
752 if (r == string::npos)
755 return a.substr(0, r + 1);
759 docstring const rtrim(docstring const & a, char const * p)
763 if (a.empty() || !*p)
766 size_t r = a.find_last_not_of(from_ascii(p));
768 // Is this test really needed? (Lgb)
769 if (r == docstring::npos)
772 return a.substr(0, r + 1);
776 string const ltrim(string const & a, char const * p)
779 if (a.empty() || !*p)
781 size_t l = a.find_first_not_of(p);
782 if (l == string::npos)
784 return a.substr(l, string::npos);
788 docstring const ltrim(docstring const & a, char const * p)
791 if (a.empty() || !*p)
793 size_t l = a.find_first_not_of(from_ascii(p));
794 if (l == docstring::npos)
796 return a.substr(l, docstring::npos);
801 template<typename String, typename Char> inline
802 String const doSplit(String const & a, String & piece, Char delim)
805 size_t i = a.find(delim);
806 if (i == a.length() - 1) {
807 piece = a.substr(0, i);
808 } else if (i != String::npos) {
809 piece = a.substr(0, i);
810 tmp = a.substr(i + 1);
813 tmp = a.substr(i + 1);
820 template<typename Char> inline
821 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
824 size_t i = a.find(delim);
825 if (i == a.length() - 1) {
826 piece = a.substr(0, i);
827 } else if (i != docstring::npos) {
828 piece = a.substr(0, i);
829 tmp = a.substr(i + 1);
832 tmp = a.substr(i + 1);
842 string const split(string const & a, string & piece, char delim)
844 return doSplit(a, piece, delim);
848 docstring const split(docstring const & a, docstring & piece, char_type delim)
850 return doSplit(a, piece, delim);
854 string const split(string const & a, char delim)
857 size_t i = a.find(delim);
858 if (i != string::npos) // found delim
859 tmp = a.substr(i + 1);
865 string const rsplit(string const & a, string & piece, char delim)
868 size_t i = a.rfind(delim);
869 if (i != string::npos) { // delimiter was found
870 piece = a.substr(0, i);
871 tmp = a.substr(i + 1);
872 } else { // delimiter was not found
879 docstring const escape(docstring const & lab)
881 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
882 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
884 for (size_t i = 0; i < lab.length(); ++i) {
885 char_type c = lab[i];
886 if (c >= 128 || c == '=' || c == '%') {
887 // Although char_type is a 32 bit type we know that
888 // UCS4 occupies only 21 bits, so we don't need to
889 // encode bigger values. Test for 2^24 because we
890 // can encode that with the 6 hex digits that are
891 // needed for 21 bits anyway.
892 BOOST_ASSERT(c < (1 << 24));
894 enc += hexdigit[(c>>20) & 15];
895 enc += hexdigit[(c>>16) & 15];
896 enc += hexdigit[(c>>12) & 15];
897 enc += hexdigit[(c>> 8) & 15];
898 enc += hexdigit[(c>> 4) & 15];
899 enc += hexdigit[ c & 15];
910 template<typename String> vector<String> const
911 getVectorFromStringT(String const & str, String const & delim)
913 // Lars would like this code to go, but for now his replacement (below)
914 // doesn't fullfil the same function. I have, therefore, reactivated the
915 // old code for now. Angus 11 Nov 2002.
920 String keys = rtrim(str);
922 size_t const idx = keys.find(delim);
923 if (idx == String::npos) {
924 vec.push_back(ltrim(keys));
927 String const key = trim(keys.substr(0, idx));
930 size_t const start = idx + delim.size();
931 keys = keys.substr(start);
935 typedef boost::char_separator<typename String::value_type> Separator;
936 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
937 Separator sep(delim.c_str());
938 Tokenizer tokens(str, sep);
939 return vector<String>(tokens.begin(), tokens.end());
946 vector<string> const getVectorFromString(string const & str,
947 string const & delim)
949 return getVectorFromStringT<string>(str, delim);
953 vector<docstring> const getVectorFromString(docstring const & str,
954 docstring const & delim)
956 return getVectorFromStringT<docstring>(str, delim);
960 // the same vice versa
961 string const getStringFromVector(vector<string> const & vec,
962 string const & delim)
966 for (vector<string>::const_iterator it = vec.begin();
967 it != vec.end(); ++it) {
968 string item = trim(*it);
979 int findToken(char const * const str[], string const & search_token)
983 while (str[i][0] && str[i] != search_token)
991 docstring const externalLineEnding(docstring const & str)
993 #if defined(__APPLE__)
994 // The MAC clipboard uses \r for lineendings, and we use \n
995 return subst(str, '\n', '\r');
996 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
997 // Windows clipboard uses \r\n for lineendings, and we use \n
998 return subst(str, from_ascii("\n"), from_ascii("\r\n"));
1005 docstring const internalLineEnding(docstring const & str)
1007 docstring const s = subst(str, from_ascii("\r\n"), from_ascii("\n"));
1008 return subst(s, '\r', '\n');
1013 docstring bformat(docstring const & fmt, int arg1)
1015 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1016 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1017 return subst(str, from_ascii("%%"), from_ascii("%"));
1022 docstring bformat(docstring const & fmt, long arg1)
1024 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1025 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1026 return subst(str, from_ascii("%%"), from_ascii("%"));
1031 docstring bformat(docstring const & fmt, unsigned int arg1)
1033 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1034 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1035 return subst(str, from_ascii("%%"), from_ascii("%"));
1040 docstring bformat(docstring const & fmt, docstring arg1)
1042 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1043 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1044 return subst(str, from_ascii("%%"), from_ascii("%"));
1049 docstring bformat(docstring const & fmt, char * arg1)
1051 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1052 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1053 return subst(str, from_ascii("%%"), from_ascii("%"));
1058 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1060 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1061 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1062 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1063 str = subst(str, from_ascii("%2$s"), arg2);
1064 return subst(str, from_ascii("%%"), from_ascii("%"));
1069 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1071 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1072 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1073 docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1074 str = subst(fmt, from_ascii("%2$s"), arg2);
1075 return subst(str, from_ascii("%%"), from_ascii("%"));
1080 docstring bformat(docstring const & fmt, int arg1, int arg2)
1082 BOOST_ASSERT(contains(fmt, from_ascii("%1$d")));
1083 BOOST_ASSERT(contains(fmt, from_ascii("%2$d")));
1084 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1085 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1086 return subst(str, from_ascii("%%"), from_ascii("%"));
1091 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1093 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1094 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1095 BOOST_ASSERT(contains(fmt, from_ascii("%3$s")));
1096 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1097 str = subst(str, from_ascii("%2$s"), arg2);
1098 str = subst(str, from_ascii("%3$s"), arg3);
1099 return subst(str, from_ascii("%%"), from_ascii("%"));
1104 docstring bformat(docstring const & fmt,
1105 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1107 BOOST_ASSERT(contains(fmt, from_ascii("%1$s")));
1108 BOOST_ASSERT(contains(fmt, from_ascii("%2$s")));
1109 BOOST_ASSERT(contains(fmt, from_ascii("%3$s")));
1110 BOOST_ASSERT(contains(fmt, from_ascii("%4$s")));
1111 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1112 str = subst(str, from_ascii("%2$s"), arg2);
1113 str = subst(str, from_ascii("%3$s"), arg3);
1114 str = subst(str, from_ascii("%4$s"), arg4);
1115 return subst(str, from_ascii("%%"), from_ascii("%"));
1118 } // namespace support