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/qstring_helpers.h"
18 #include "support/textutils.h"
20 #include <boost/tokenizer.hpp>
21 #include "support/assert.h"
29 // Using this allows us to have docstring default arguments in headers
30 // without #include "support/docstring" there.
31 docstring const & empty_docstring()
37 // Using this allows us to have string default arguments in headers
38 // without #include <string>
39 string const & empty_string()
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 LASSERT(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 LASSERT(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)
238 // Remove leading and trailing white space chars.
239 string const tmpstr = trim(str);
243 string::const_iterator cit = tmpstr.begin();
247 string::const_iterator end = tmpstr.end();
248 for (; cit != end; ++cit)
249 if (!isdigit((*cit)))
256 bool isStrUnsignedInt(string const & str)
261 // Remove leading and trailing white space chars.
262 string const tmpstr = trim(str);
266 string::const_iterator cit = tmpstr.begin();
267 string::const_iterator end = tmpstr.end();
268 for (; cit != end; ++cit)
269 if (!isdigit((*cit)))
276 bool isStrDbl(string const & str)
281 // Remove leading and trailing white space chars.
282 string const tmpstr = trim(str);
285 // if (tmpstr.count('.') > 1) return false;
287 string::const_iterator cit = tmpstr.begin();
288 bool found_dot = false;
291 string::const_iterator end = tmpstr.end();
292 for (; cit != end; ++cit) {
293 if (!isdigit(*cit) && *cit != '.')
305 static bool isHexChar(char_type c)
317 c == 'a' || c == 'A' ||
318 c == 'b' || c == 'B' ||
319 c == 'c' || c == 'C' ||
320 c == 'd' || c == 'D' ||
321 c == 'e' || c == 'E' ||
322 c == 'f' || c == 'F';
326 bool isHex(docstring const & str)
330 if (str.length() > 2 && str[0] == '0' &&
331 (str[1] == 'x' || str[1] == 'X'))
334 int const len = str.length();
336 for (; index < len; ++index) {
337 if (!isHexChar(str[index]))
344 int hexToInt(docstring const & str)
346 string s = to_ascii(str);
348 sscanf(s.c_str(), "%x", &h);
353 bool isAscii(docstring const & str)
355 int const len = str.length();
356 for (int i = 0; i < len; ++i)
363 bool isAscii(string const & str)
365 int const len = str.length();
366 for (int i = 0; i < len; ++i)
367 if (static_cast<unsigned char>(str[i]) >= 0x80)
373 char lowercase(char c)
375 LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
376 return char(tolower(c));
380 char uppercase(char c)
382 LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
383 return char(toupper(c));
387 char_type lowercase(char_type c)
390 // We don't know how to lowercase a non-utf16 char
392 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
396 char_type uppercase(char_type c)
399 // We don't know how to uppercase a non-utf16 char
401 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
407 // since we cannot use tolower and toupper directly in the
408 // calls to transform yet, we use these helper clases. (Lgb)
410 struct local_lowercase {
411 char_type operator()(char_type c) const {
413 // We don't know how to lowercase a non-utf16 char
415 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
419 struct local_uppercase {
420 char_type operator()(char_type c) const {
422 // We don't know how to uppercase a non-utf16 char
424 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
428 template<typename Char> struct local_ascii_lowercase {
429 Char operator()(Char c) const { return ascii_tolower(c); }
432 } // end of anon namespace
434 docstring const lowercase(docstring const & a)
437 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
442 docstring const uppercase(docstring const & a)
445 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
450 string const ascii_lowercase(string const & a)
453 transform(tmp.begin(), tmp.end(), tmp.begin(),
454 local_ascii_lowercase<char>());
459 docstring const ascii_lowercase(docstring const & a)
462 transform(tmp.begin(), tmp.end(), tmp.begin(),
463 local_ascii_lowercase<char_type>());
468 bool prefixIs(docstring const & a, char_type c)
476 bool prefixIs(string const & a, string const & pre)
478 size_t const prelen = pre.length();
479 size_t const alen = a.length();
480 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
484 bool prefixIs(docstring const & a, docstring const & pre)
486 size_t const prelen = pre.length();
487 size_t const alen = a.length();
488 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
492 bool suffixIs(string const & a, char c)
494 if (a.empty()) return false;
495 return a[a.length() - 1] == c;
499 bool suffixIs(docstring const & a, char_type c)
503 return a[a.length() - 1] == c;
507 bool suffixIs(string const & a, string const & suf)
509 size_t const suflen = suf.length();
510 size_t const alen = a.length();
511 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
515 bool containsOnly(string const & s, string const & cset)
517 return s.find_first_not_of(cset) == string::npos;
521 // ale970405+lasgoutt-970425
522 // rewritten to use new string (Lgb)
523 string const token(string const & a, char delim, int n)
531 // Find delimiter or end of string
533 if ((i = a.find(delim, i)) == string::npos)
539 // i is now the n'th delim (or string::npos)
540 if (i == string::npos)
543 k = a.find(delim, i);
544 // k is now the n'th + 1 delim (or string::npos)
546 return a.substr(i, k - i);
550 docstring const token(docstring const & a, char_type delim, int n)
558 // Find delimiter or end of string
560 if ((i = a.find(delim, i)) == docstring::npos)
566 // i is now the n'th delim (or string::npos)
567 if (i == docstring::npos)
570 k = a.find(delim, i);
571 // k is now the n'th + 1 delim (or string::npos)
573 return a.substr(i, k - i);
577 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
578 // rewritten to use new string (Lgb)
579 int tokenPos(string const & a, char delim, string const & tok)
585 while (!str.empty()) {
586 str = split(str, tmptok, delim);
597 /// Substitute all \a oldchar with \a newchar
598 template<typename Ch> inline
599 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
600 Ch oldchar, Ch newchar)
602 typedef basic_string<Ch> String;
604 typename String::iterator lit = tmp.begin();
605 typename String::iterator end = tmp.end();
606 for (; lit != end; ++lit)
607 if ((*lit) == oldchar)
612 /// Substitute all \a oldchar with \a newchar
613 docstring const subst_char(docstring const & a,
614 docstring::value_type oldchar, docstring::value_type newchar)
617 docstring::iterator lit = tmp.begin();
618 docstring::iterator end = tmp.end();
619 for (; lit != end; ++lit)
620 if ((*lit) == oldchar)
626 /// substitutes all instances of \a oldstr with \a newstr
627 template<typename String> inline
628 String const subst_string(String const & a,
629 String const & oldstr, String const & newstr)
631 LASSERT(!oldstr.empty(), /**/);
634 size_t const olen = oldstr.length();
635 while ((i = lstr.find(oldstr, i)) != string::npos) {
636 lstr.replace(i, olen, newstr);
637 i += newstr.length(); // We need to be sure that we dont
638 // use the same i over and over again.
643 docstring const subst_string(docstring const & a,
644 docstring const & oldstr, docstring const & newstr)
646 LASSERT(!oldstr.empty(), /**/);
649 size_t const olen = oldstr.length();
650 while ((i = lstr.find(oldstr, i)) != string::npos) {
651 lstr.replace(i, olen, newstr);
652 i += newstr.length(); // We need to be sure that we dont
653 // use the same i over and over again.
661 string const subst(string const & a, char oldchar, char newchar)
663 return subst_char(a, oldchar, newchar);
667 docstring const subst(docstring const & a,
668 char_type oldchar, char_type newchar)
670 return subst_char(a, oldchar, newchar);
674 string const subst(string const & a,
675 string const & oldstr, string const & newstr)
677 return subst_string(a, oldstr, newstr);
681 docstring const subst(docstring const & a,
682 docstring const & oldstr, docstring const & newstr)
684 return subst_string(a, oldstr, newstr);
688 docstring const trim(docstring const & a, char const * p)
692 if (a.empty() || !*p)
695 docstring s = from_ascii(p);
696 size_t r = a.find_last_not_of(s);
697 size_t l = a.find_first_not_of(s);
699 // Is this the minimal test? (lgb)
700 if (r == docstring::npos && l == docstring::npos)
703 return a.substr(l, r - l + 1);
707 string const trim(string const & a, char const * p)
711 if (a.empty() || !*p)
714 size_t r = a.find_last_not_of(p);
715 size_t l = a.find_first_not_of(p);
717 // Is this the minimal test? (lgb)
718 if (r == string::npos && l == string::npos)
721 return a.substr(l, r - l + 1);
725 string const rtrim(string const & a, char const * p)
729 if (a.empty() || !*p)
732 size_t r = a.find_last_not_of(p);
734 // Is this test really needed? (Lgb)
735 if (r == string::npos)
738 return a.substr(0, r + 1);
742 docstring const rtrim(docstring const & a, char const * p)
746 if (a.empty() || !*p)
749 size_t r = a.find_last_not_of(from_ascii(p));
751 // Is this test really needed? (Lgb)
752 if (r == docstring::npos)
755 return a.substr(0, r + 1);
759 string const ltrim(string const & a, char const * p)
762 if (a.empty() || !*p)
764 size_t l = a.find_first_not_of(p);
765 if (l == string::npos)
767 return a.substr(l, string::npos);
771 docstring const ltrim(docstring const & a, char const * p)
774 if (a.empty() || !*p)
776 size_t l = a.find_first_not_of(from_ascii(p));
777 if (l == docstring::npos)
779 return a.substr(l, docstring::npos);
784 template<typename String, typename Char> inline
785 String const doSplit(String const & a, String & piece, Char delim)
788 size_t i = a.find(delim);
789 if (i == a.length() - 1) {
790 piece = a.substr(0, i);
791 } else if (i != String::npos) {
792 piece = a.substr(0, i);
793 tmp = a.substr(i + 1);
796 tmp = a.substr(i + 1);
803 template<typename Char> inline
804 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
807 size_t i = a.find(delim);
808 if (i == a.length() - 1) {
809 piece = a.substr(0, i);
810 } else if (i != docstring::npos) {
811 piece = a.substr(0, i);
812 tmp = a.substr(i + 1);
815 tmp = a.substr(i + 1);
825 string const split(string const & a, string & piece, char delim)
827 return doSplit(a, piece, delim);
831 docstring const split(docstring const & a, docstring & piece, char_type delim)
833 return doSplit(a, piece, delim);
837 string const split(string const & a, char delim)
840 size_t i = a.find(delim);
841 if (i != string::npos) // found delim
842 tmp = a.substr(i + 1);
848 string const rsplit(string const & a, string & piece, char delim)
851 size_t i = a.rfind(delim);
852 if (i != string::npos) { // delimiter was found
853 piece = a.substr(0, i);
854 tmp = a.substr(i + 1);
855 } else { // delimiter was not found
862 docstring const escape(docstring const & lab)
864 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
865 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
867 for (size_t i = 0; i < lab.length(); ++i) {
868 char_type c = lab[i];
869 if (c >= 128 || c == '=' || c == '%') {
870 // Although char_type is a 32 bit type we know that
871 // UCS4 occupies only 21 bits, so we don't need to
872 // encode bigger values. Test for 2^24 because we
873 // can encode that with the 6 hex digits that are
874 // needed for 21 bits anyway.
875 LASSERT(c < (1 << 24), /**/);
877 enc += hexdigit[(c>>20) & 15];
878 enc += hexdigit[(c>>16) & 15];
879 enc += hexdigit[(c>>12) & 15];
880 enc += hexdigit[(c>> 8) & 15];
881 enc += hexdigit[(c>> 4) & 15];
882 enc += hexdigit[ c & 15];
893 template<typename String> vector<String> const
894 getVectorFromStringT(String const & str, String const & delim)
896 // Lars would like this code to go, but for now his replacement (below)
897 // doesn't fullfil the same function. I have, therefore, reactivated the
898 // old code for now. Angus 11 Nov 2002.
903 String keys = rtrim(str);
905 size_t const idx = keys.find(delim);
906 if (idx == String::npos) {
907 vec.push_back(ltrim(keys));
910 String const key = trim(keys.substr(0, idx));
913 size_t const start = idx + delim.size();
914 keys = keys.substr(start);
918 typedef boost::char_separator<typename String::value_type> Separator;
919 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
920 Separator sep(delim.c_str());
921 Tokenizer tokens(str, sep);
922 return vector<String>(tokens.begin(), tokens.end());
929 vector<string> const getVectorFromString(string const & str,
930 string const & delim)
932 return getVectorFromStringT<string>(str, delim);
936 vector<docstring> const getVectorFromString(docstring const & str,
937 docstring const & delim)
939 return getVectorFromStringT<docstring>(str, delim);
943 // the same vice versa
944 string const getStringFromVector(vector<string> const & vec,
945 string const & delim)
949 for (vector<string>::const_iterator it = vec.begin();
950 it != vec.end(); ++it) {
951 string item = trim(*it);
962 int findToken(char const * const str[], string const & search_token)
966 while (str[i][0] && str[i] != search_token)
974 docstring const externalLineEnding(docstring const & str)
976 #if defined(__APPLE__)
977 // The MAC clipboard uses \r for lineendings, and we use \n
978 return subst(str, '\n', '\r');
979 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
980 // Windows clipboard uses \r\n for lineendings, and we use \n
981 return subst(str, from_ascii("\n"), from_ascii("\r\n"));
988 docstring const internalLineEnding(docstring const & str)
990 docstring const s = subst(str, from_ascii("\r\n"), from_ascii("\n"));
991 return subst(s, '\r', '\n');
996 docstring bformat(docstring const & fmt, int arg1)
998 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
999 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1000 return subst(str, from_ascii("%%"), from_ascii("%"));
1005 docstring bformat(docstring const & fmt, long arg1)
1007 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1008 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1009 return subst(str, from_ascii("%%"), from_ascii("%"));
1014 docstring bformat(docstring const & fmt, unsigned int arg1)
1016 LASSERT(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, docstring arg1)
1025 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1026 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1027 return subst(str, from_ascii("%%"), from_ascii("%"));
1032 docstring bformat(docstring const & fmt, char * arg1)
1034 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1035 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1036 return subst(str, from_ascii("%%"), from_ascii("%"));
1041 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1043 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1044 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1045 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1046 str = subst(str, from_ascii("%2$s"), arg2);
1047 return subst(str, from_ascii("%%"), from_ascii("%"));
1052 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1054 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1055 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1056 docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1057 str = subst(fmt, from_ascii("%2$s"), arg2);
1058 return subst(str, from_ascii("%%"), from_ascii("%"));
1063 docstring bformat(docstring const & fmt, int arg1, int arg2)
1065 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1066 LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1067 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1068 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1069 return subst(str, from_ascii("%%"), from_ascii("%"));
1074 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1076 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1077 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1078 LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1079 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1080 str = subst(str, from_ascii("%2$s"), arg2);
1081 str = subst(str, from_ascii("%3$s"), arg3);
1082 return subst(str, from_ascii("%%"), from_ascii("%"));
1087 docstring bformat(docstring const & fmt,
1088 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1090 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1091 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1092 LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1093 LASSERT(contains(fmt, from_ascii("%4$s")), /**/);
1094 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1095 str = subst(str, from_ascii("%2$s"), arg2);
1096 str = subst(str, from_ascii("%3$s"), arg3);
1097 str = subst(str, from_ascii("%4$s"), arg4);
1098 return subst(str, from_ascii("%%"), from_ascii("%"));
1101 } // namespace support