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 {
429 return ascii_tolower(c);
433 } // end of anon namespace
435 docstring const lowercase(docstring const & a)
438 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
443 docstring const uppercase(docstring const & a)
446 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
451 string const ascii_lowercase(string const & a)
454 transform(tmp.begin(), tmp.end(), tmp.begin(),
455 local_ascii_lowercase<char>());
460 docstring const ascii_lowercase(docstring const & a)
463 transform(tmp.begin(), tmp.end(), tmp.begin(),
464 local_ascii_lowercase<char_type>());
469 bool prefixIs(docstring const & a, char_type c)
477 bool prefixIs(string const & a, string const & pre)
479 string::size_type const prelen = pre.length();
480 string::size_type const alen = a.length();
482 if (prelen > alen || a.empty())
485 #if defined(STD_STRING_IS_GOOD)
486 return a.compare(0, prelen, pre) == 0;
488 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
494 bool prefixIs(docstring const & a, docstring const & pre)
496 docstring::size_type const prelen = pre.length();
497 docstring::size_type const alen = a.length();
499 if (prelen > alen || a.empty())
502 return a.compare(0, prelen, pre) == 0;
506 bool suffixIs(string const & a, char c)
508 if (a.empty()) return false;
509 return a[a.length() - 1] == c;
513 bool suffixIs(docstring const & a, char_type c)
517 return a[a.length() - 1] == c;
521 bool suffixIs(string const & a, string const & suf)
523 string::size_type const suflen = suf.length();
524 string::size_type const alen = a.length();
529 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
530 string tmp(a, alen - suflen);
531 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
533 return a.compare(alen - suflen, suflen, suf) == 0;
539 bool containsOnly(string const & s, string const & cset)
541 return s.find_first_not_of(cset) == string::npos;
545 // ale970405+lasgoutt-970425
546 // rewritten to use new string (Lgb)
547 string const token(string const & a, char delim, int n)
549 if (a.empty()) return string();
551 string::size_type k = 0;
552 string::size_type i = 0;
554 // Find delimiter or end of string
556 if ((i = a.find(delim, i)) == string::npos)
560 // i is now the n'th delim (or string::npos)
561 if (i == string::npos) return string();
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();
573 string::size_type k = 0;
574 string::size_type i = 0;
576 // Find delimiter or end of string
578 if ((i = a.find(delim, i)) == docstring::npos)
582 // i is now the n'th delim (or string::npos)
583 if (i == docstring::npos) return docstring();
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);
611 /// Substitute all \a oldchar with \a newchar
612 template<typename Ch> inline
613 std::basic_string<Ch> const subst_char(std::basic_string<Ch> const & a,
614 Ch oldchar, Ch newchar)
616 typedef std::basic_string<Ch> String;
618 typename String::iterator lit = tmp.begin();
619 typename String::iterator end = tmp.end();
620 for (; lit != end; ++lit)
621 if ((*lit) == oldchar)
626 /// Substitute all \a oldchar with \a newchar
627 docstring const subst_char(docstring const & a,
628 docstring::value_type oldchar, docstring::value_type newchar)
631 docstring::iterator lit = tmp.begin();
632 docstring::iterator end = tmp.end();
633 for (; lit != end; ++lit)
634 if ((*lit) == oldchar)
640 /// substitutes all instances of \a oldstr with \a newstr
641 template<typename String> inline
642 String const subst_string(String const & a,
643 String const & oldstr, String const & newstr)
645 BOOST_ASSERT(!oldstr.empty());
647 typename String::size_type i = 0;
648 typename String::size_type const olen = oldstr.length();
649 while ((i = lstr.find(oldstr, i)) != string::npos) {
650 lstr.replace(i, olen, newstr);
651 i += newstr.length(); // We need to be sure that we dont
652 // use the same i over and over again.
657 docstring const subst_string(docstring const & a,
658 docstring const & oldstr, docstring const & newstr)
660 BOOST_ASSERT(!oldstr.empty());
662 docstring::size_type i = 0;
663 docstring::size_type const olen = oldstr.length();
664 while ((i = lstr.find(oldstr, i)) != string::npos) {
665 lstr.replace(i, olen, newstr);
666 i += newstr.length(); // We need to be sure that we dont
667 // use the same i over and over again.
675 string const subst(string const & a, char oldchar, char newchar)
677 return subst_char(a, oldchar, newchar);
681 docstring const subst(docstring const & a,
682 char_type oldchar, char_type newchar)
684 return subst_char(a, oldchar, newchar);
688 string const subst(string const & a,
689 string const & oldstr, string const & newstr)
691 return subst_string(a, oldstr, newstr);
695 docstring const subst(docstring const & a,
696 docstring const & oldstr, docstring const & newstr)
698 return subst_string(a, oldstr, newstr);
702 docstring const trim(docstring const & a, char const * p)
706 if (a.empty() || !*p)
709 docstring s = lyx::from_ascii(p);
710 docstring::size_type r = a.find_last_not_of(s);
711 docstring::size_type l = a.find_first_not_of(s);
713 // Is this the minimal test? (lgb)
714 if (r == docstring::npos && l == docstring::npos)
717 return a.substr(l, r - l + 1);
721 string const trim(string const & a, char const * p)
725 if (a.empty() || !*p)
728 string::size_type r = a.find_last_not_of(p);
729 string::size_type l = a.find_first_not_of(p);
731 // Is this the minimal test? (lgb)
732 if (r == string::npos && l == string::npos)
735 return a.substr(l, r - l + 1);
739 string const rtrim(string const & a, char const * p)
743 if (a.empty() || !*p)
746 string::size_type r = a.find_last_not_of(p);
748 // Is this test really needed? (Lgb)
749 if (r == string::npos)
752 return a.substr(0, r + 1);
756 docstring const rtrim(docstring const & a, char const * p)
760 if (a.empty() || !*p)
763 docstring::size_type r = a.find_last_not_of(from_ascii(p));
765 // Is this test really needed? (Lgb)
766 if (r == docstring::npos)
769 return a.substr(0, r + 1);
773 string const ltrim(string const & a, char const * p)
776 if (a.empty() || !*p)
778 string::size_type l = a.find_first_not_of(p);
779 if (l == string::npos)
781 return a.substr(l, string::npos);
785 docstring const ltrim(docstring const & a, char const * p)
788 if (a.empty() || !*p)
790 size_t l = a.find_first_not_of(from_ascii(p));
791 if (l == docstring::npos)
793 return a.substr(l, docstring::npos);
798 template<typename String, typename Char> inline
799 String const doSplit(String const & a, String & piece, Char delim)
802 typename String::size_type i = a.find(delim);
803 if (i == a.length() - 1) {
804 piece = a.substr(0, i);
805 } else if (i != String::npos) {
806 piece = a.substr(0, i);
807 tmp = a.substr(i + 1);
810 tmp = a.substr(i + 1);
817 template<typename Char> inline
818 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
821 typename docstring::size_type i = a.find(delim);
822 if (i == a.length() - 1) {
823 piece = a.substr(0, i);
824 } else if (i != docstring::npos) {
825 piece = a.substr(0, i);
826 tmp = a.substr(i + 1);
829 tmp = a.substr(i + 1);
839 string const split(string const & a, string & piece, char delim)
841 return doSplit(a, piece, delim);
845 docstring const split(docstring const & a, docstring & piece, char_type delim)
847 return doSplit(a, piece, delim);
851 string const split(string const & a, char delim)
854 string::size_type i = a.find(delim);
855 if (i != string::npos) // found delim
856 tmp = a.substr(i + 1);
862 string const rsplit(string const & a, string & piece, char delim)
865 string::size_type i = a.rfind(delim);
866 if (i != string::npos) { // delimiter was found
867 piece = a.substr(0, i);
868 tmp = a.substr(i + 1);
869 } else { // delimiter was not found
876 docstring const escape(docstring const & lab)
878 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
879 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
881 for (docstring::size_type i = 0; i < lab.length(); ++i) {
882 char_type c = lab[i];
883 if (c >= 128 || c == '=' || c == '%') {
884 // Although char_type is a 32 bit type we know that
885 // UCS4 occupies only 21 bits, so we don't need to
886 // encode bigger values. Test for 2^24 because we
887 // can encode that with the 6 hex digits that are
888 // needed for 21 bits anyway.
889 BOOST_ASSERT(c < (1 << 24));
891 enc += hexdigit[(c>>20) & 15];
892 enc += hexdigit[(c>>16) & 15];
893 enc += hexdigit[(c>>12) & 15];
894 enc += hexdigit[(c>> 8) & 15];
895 enc += hexdigit[(c>> 4) & 15];
896 enc += hexdigit[ c & 15];
907 template<typename String> vector<String> const
908 getVectorFromStringT(String const & str, String const & delim)
910 // Lars would like this code to go, but for now his replacement (below)
911 // doesn't fullfil the same function. I have, therefore, reactivated the
912 // old code for now. Angus 11 Nov 2002.
917 String keys = rtrim(str);
919 typename String::size_type const idx = keys.find(delim);
920 if (idx == String::npos) {
921 vec.push_back(ltrim(keys));
924 String const key = trim(keys.substr(0, idx));
927 typename String::size_type const start = idx + delim.size();
928 keys = keys.substr(start);
932 typedef boost::char_separator<typename String::value_type> Separator;
933 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
934 Separator sep(delim.c_str());
935 Tokenizer tokens(str, sep);
936 return vector<String>(tokens.begin(), tokens.end());
943 vector<string> const getVectorFromString(string const & str,
944 string const & delim)
946 return getVectorFromStringT<string>(str, delim);
950 vector<docstring> const getVectorFromString(docstring const & str,
951 docstring const & delim)
953 return getVectorFromStringT<docstring>(str, delim);
957 // the same vice versa
958 string const getStringFromVector(vector<string> const & vec,
959 string const & delim)
963 for (vector<string>::const_iterator it = vec.begin();
964 it != vec.end(); ++it) {
965 string item = trim(*it);
976 int findToken(char const * const str[], string const & search_token)
980 while (str[i][0] && str[i] != search_token)
988 docstring const externalLineEnding(docstring const & str)
990 #if defined(__APPLE__)
991 // The MAC clipboard uses \r for lineendings, and we use \n
992 return subst(str, '\n', '\r');
993 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
994 // Windows clipboard uses \r\n for lineendings, and we use \n
995 return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
1002 docstring const internalLineEnding(docstring const & str)
1004 docstring const s = subst(str,
1005 lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
1006 return subst(s, '\r', '\n');
1011 docstring bformat(docstring const & fmt, int arg1)
1013 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
1014 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
1015 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1020 docstring bformat(docstring const & fmt, long arg1)
1022 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
1023 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
1024 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1029 docstring bformat(docstring const & fmt, unsigned int arg1)
1031 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
1032 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
1033 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1038 docstring bformat(docstring const & fmt, docstring arg1)
1040 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1041 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
1042 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1047 docstring bformat(docstring const & fmt, char * arg1)
1049 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1050 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
1051 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1056 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1058 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1059 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
1060 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
1061 str = subst(str, lyx::from_ascii("%2$s"), arg2);
1062 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1067 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1069 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1070 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
1071 docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
1072 str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
1073 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1078 docstring bformat(docstring const & fmt, int arg1, int arg2)
1080 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
1081 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
1082 docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
1083 str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
1084 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1089 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1091 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1092 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
1093 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
1094 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
1095 str = subst(str, lyx::from_ascii("%2$s"), arg2);
1096 str = subst(str, lyx::from_ascii("%3$s"), arg3);
1097 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1102 docstring bformat(docstring const & fmt,
1103 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1105 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1106 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
1107 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
1108 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
1109 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
1110 str = subst(str, lyx::from_ascii("%2$s"), arg2);
1111 str = subst(str, lyx::from_ascii("%3$s"), arg3);
1112 str = subst(str, lyx::from_ascii("%4$s"), arg4);
1113 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1116 } // namespace support