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
10 * Full author contact details are available in file CREDITS.
15 #include "support/lstrings.h"
17 #include "support/convert.h"
18 #include "support/debug.h"
19 #include "support/lyxlib.h"
20 #include "support/qstring_helpers.h"
22 #include "support/lassert.h"
37 // Using this allows us to have docstring default arguments in headers
38 // without #include "support/docstring" there.
39 docstring const & empty_docstring()
41 static const docstring s;
45 // Using this allows us to have string default arguments in headers
46 // without #include <string>
47 string const & empty_string()
49 static const string s;
55 * Convert a QChar into a UCS4 character.
56 * This is a hack (it does only make sense for the common part of the UCS4
57 * and UTF16 encodings) and should not be used.
58 * This does only exist because of performance reasons (a real conversion
59 * using iconv is too slow on windows).
61 inline char_type qchar_to_ucs4(QChar const & qchar)
63 LASSERT(is_utf16(static_cast<char_type>(qchar.unicode())), return '?');
64 return static_cast<char_type>(qchar.unicode());
68 * Convert a UCS4 character into a QChar.
69 * This is a hack (it does only make sense for the common part of the UCS4
70 * and UTF16 encodings) and should not be used.
71 * This does only exist because of performance reasons (a real conversion
72 * using iconv is too slow on windows).
74 inline QChar const ucs4_to_qchar(char_type const ucs4)
76 LASSERT(is_utf16(ucs4), return QChar('?'));
77 return QChar(static_cast<unsigned short>(ucs4));
80 /// Maximum valid UCS4 code point
81 char_type const ucs4_max = 0x10ffff;
85 bool isLetterChar(char_type c)
89 // outside the UCS4 range
91 // assume that all non-utf16 characters are letters
94 return ucs4_to_qchar(c).isLetter();
98 bool isLower(char_type c)
102 return ucs4_to_qchar(c).isLower();
106 bool isAlphaASCII(char_type c)
108 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
112 bool isPrintable(char_type c)
116 // outside the UCS4 range
118 // assume that all non-utf16 characters are printable
121 // Not yet recognized by QChar::isPrint()
122 // See https://bugreports.qt-project.org/browse/QTBUG-12144
123 // LATIN CAPITAL LETTER SHARP S
124 else if (c == 0x1e9e)
126 return ucs4_to_qchar(c).isPrint();
130 bool isPrintableNonspace(char_type c)
134 // outside the UCS4 range
136 // assume that all non-utf16 characters are printable and
140 QChar const qc = ucs4_to_qchar(c);
141 return qc.isPrint() && !qc.isSpace();
145 bool isSpace(char_type c)
148 // assume that no non-utf16 character is a space
149 // c outside the UCS4 range is catched as well
152 QChar const qc = ucs4_to_qchar(c);
157 bool isNumber(char_type c)
160 // assume that no non-utf16 character is a numeral
161 // c outside the UCS4 range is catched as well
163 return ucs4_to_qchar(c).isNumber();
167 bool isDigitASCII(char_type c)
169 return '0' <= c && c <= '9';
173 bool isAlnumASCII(char_type c)
175 return isAlphaASCII(c) || isDigitASCII(c);
179 bool isASCII(char_type c)
185 bool isOpenPunctuation(char_type c)
188 // assume that no non-utf16 character is an op
189 // c outside the UCS4 range is catched as well
192 QChar const qc = ucs4_to_qchar(c);
193 return qc.category() == QChar::Punctuation_Open;
199 int compare_no_case(docstring const & s, docstring const & s2)
201 docstring::const_iterator p = s.begin();
202 docstring::const_iterator p2 = s2.begin();
204 while (p != s.end() && p2 != s2.end()) {
205 char_type const lc1 = lowercase(*p);
206 char_type const lc2 = lowercase(*p2);
208 return (lc1 < lc2) ? -1 : 1;
213 if (s.size() == s2.size())
215 if (s.size() < s2.size())
221 int compare_locale(docstring const & s, docstring const & s2)
223 return QString::localeAwareCompare(toqstr(s), toqstr(s2));
229 template<typename Char>
230 Char ascii_tolower(Char c) {
231 if (c >= 'A' && c <= 'Z')
232 return c - 'A' + 'a';
239 int compare_ascii_no_case(string const & s, string const & s2)
241 string::const_iterator p = s.begin();
242 string::const_iterator p2 = s2.begin();
244 while (p != s.end() && p2 != s2.end()) {
245 int const lc1 = ascii_tolower(*p);
246 int const lc2 = ascii_tolower(*p2);
248 return (lc1 < lc2) ? -1 : 1;
253 if (s.size() == s2.size())
255 if (s.size() < s2.size())
261 int compare_ascii_no_case(docstring const & s, docstring const & s2)
263 docstring::const_iterator p = s.begin();
264 docstring::const_iterator p2 = s2.begin();
266 while (p != s.end() && p2 != s2.end()) {
267 char_type const lc1 = ascii_tolower(*p);
268 char_type const lc2 = ascii_tolower(*p2);
270 return (lc1 < lc2) ? -1 : 1;
275 if (s.size() == s2.size())
277 if (s.size() < s2.size())
283 bool isStrInt(string const & str)
288 // Remove leading and trailing white space chars.
289 string const tmpstr = trim(str);
293 string::const_iterator cit = tmpstr.begin();
297 string::const_iterator end = tmpstr.end();
298 for (; cit != end; ++cit)
299 if (!isDigitASCII(*cit))
306 bool isStrUnsignedInt(string const & str)
311 // Remove leading and trailing white space chars.
312 string const tmpstr = trim(str);
316 string::const_iterator cit = tmpstr.begin();
317 string::const_iterator end = tmpstr.end();
318 for (; cit != end; ++cit)
319 if (!isDigitASCII(*cit))
326 bool isStrDbl(string const & str)
331 // Remove leading and trailing white space chars.
332 string const tmpstr = trim(str);
335 // if (tmpstr.count('.') > 1) return false;
337 string::const_iterator cit = tmpstr.begin();
338 bool found_dot = false;
341 string::const_iterator end = tmpstr.end();
342 for (; cit != end; ++cit) {
343 if (!isDigitASCII(*cit) && *cit != '.')
355 bool hasDigitASCII(docstring const & str)
357 docstring::const_iterator cit = str.begin();
358 docstring::const_iterator const end = str.end();
359 for (; cit != end; ++cit)
360 if (isDigitASCII(*cit))
366 bool isHexChar(char_type c)
378 c == 'a' || c == 'A' ||
379 c == 'b' || c == 'B' ||
380 c == 'c' || c == 'C' ||
381 c == 'd' || c == 'D' ||
382 c == 'e' || c == 'E' ||
383 c == 'f' || c == 'F';
387 bool isHex(docstring const & str)
391 if (str.length() > 2 && str[0] == '0' &&
392 (str[1] == 'x' || str[1] == 'X'))
395 int const len = str.length();
397 for (; index < len; ++index) {
398 if (!isHexChar(str[index]))
405 int hexToInt(docstring const & str)
407 string s = to_ascii(str);
409 sscanf(s.c_str(), "%x", &h);
414 bool isAscii(docstring const & str)
416 int const len = str.length();
417 for (int i = 0; i < len; ++i)
424 bool isAscii(string const & str)
426 int const len = str.length();
427 for (int i = 0; i < len; ++i)
428 if (static_cast<unsigned char>(str[i]) >= 0x80)
434 char lowercase(char c)
436 LASSERT(isASCII(c), return '?');
437 return char(tolower(c));
441 char uppercase(char c)
443 LASSERT(isASCII(c), return '?');
444 return char(toupper(c));
448 char_type lowercase(char_type c)
451 // We don't know how to lowercase a non-utf16 char
453 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
457 char_type uppercase(char_type c)
460 // We don't know how to uppercase a non-utf16 char
462 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
466 bool isLowerCase(char_type ch) {
467 return lowercase(ch) == ch;
471 bool isUpperCase(char_type ch) {
472 return uppercase(ch) == ch;
478 // since we cannot use tolower and toupper directly in the
479 // calls to transform yet, we use these helper clases. (Lgb)
481 struct local_lowercase {
482 char_type operator()(char_type c) const {
487 struct local_uppercase {
488 char_type operator()(char_type c) const {
493 template<typename Char> struct local_ascii_lowercase {
494 Char operator()(Char c) const { return ascii_tolower(c); }
497 } // end of anon namespace
500 docstring const lowercase(docstring const & a)
503 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
508 /* Uncomment here and in lstrings.h if you should need this.
509 string const lowercase(string const & a)
512 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
518 docstring const uppercase(docstring const & a)
521 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
526 string const ascii_lowercase(string const & a)
529 transform(tmp.begin(), tmp.end(), tmp.begin(),
530 local_ascii_lowercase<char>());
535 docstring const ascii_lowercase(docstring const & a)
538 transform(tmp.begin(), tmp.end(), tmp.begin(),
539 local_ascii_lowercase<char_type>());
544 char_type superscript(char_type c)
547 case '2': return 0x00b2;
548 case '3': return 0x00b3;
549 case '1': return 0x00b9;
550 case '0': return 0x2070;
551 case 'i': return 0x2071;
552 case '4': return 0x2074;
553 case '5': return 0x2075;
554 case '6': return 0x2076;
555 case '7': return 0x2077;
556 case '8': return 0x2078;
557 case '9': return 0x2079;
558 case '+': return 0x207a;
559 case '-': return 0x207b;
560 case '=': return 0x207c;
561 case '(': return 0x207d;
562 case ')': return 0x207e;
563 case 'n': return 0x207f;
564 case 'h': return 0x02b0;
565 case 0x0266: return 0x02b1; // LATIN SMALL LETTER H WITH HOOK
566 case 'j': return 0x02b2;
567 case 'r': return 0x02b3;
568 case 0x0279: return 0x02b4; // LATIN SMALL LETTER TURNED R
569 case 0x027b: return 0x02b5; // LATIN SMALL LETTER TURNED R WITH HOOK
570 case 0x0281: return 0x02b6; // LATIN SMALL LETTER CAPITAL INVERTED R
571 case 'w': return 0x02b7;
572 case 'y': return 0x02b8;
573 // case 0x0294: return 0x02c0; // LATIN LETTER GLOTTAL STOP)
574 // case 0x0295: return 0x02c1; // LATIN LETTER PHARYNGEAL VOICED FRICATIVE
575 // (= LATIN LETTER REVERSED GLOTTAL STOP)
576 case 'l': return 0x02e1;
577 case 's': return 0x02e2;
578 case 'x': return 0x02e3;
579 // case 0x0295: return 0x02e4; // LATIN SMALL LETTER REVERSED GLOTTAL STOP
580 case 'A': return 0x1d2c;
581 case 0x00c6: return 0x1d2d; // LATIN CAPITAL LETTER AE
582 case 'B': return 0x1d2e;
583 case 'D': return 0x1d30;
584 case 'E': return 0x1d31;
585 case 'G': return 0x1d33;
586 case 'H': return 0x1d34;
587 case 'I': return 0x1d35;
588 case 'J': return 0x1d36;
589 case 'K': return 0x1d37;
590 case 'L': return 0x1d38;
591 case 'M': return 0x1d39;
592 case 'N': return 0x1d3a;
593 case 'O': return 0x1d3c;
594 case 'P': return 0x1d3e;
595 case 'R': return 0x1d3f;
596 case 'T': return 0x1d40;
597 case 'U': return 0x1d41;
598 case 'W': return 0x1d42;
599 case 'a': return 0x1d43;
600 case 0x0250: return 0x1d44; // LATIN SMALL LETTER TURNED A
601 case 0x0251: return 0x1d45; // LATIN SMALL LETTER ALPHA
602 case 'b': return 0x1d47;
603 case 'd': return 0x1d48;
604 case 'e': return 0x1d49;
605 case 0x0259: return 0x1d4a; // LATIN SMALL LETTER SCHWA
606 case 0x025b: return 0x1d4b; // LATIN SMALL LETTER OPEN E
607 case 0x1d08: return 0x1d4c; // LATIN SMALL LETTER TURNED OPEN E
608 case 'g': return 0x1d4d;
609 case 0x1d09: return 0x1d4e; // LATIN SMALL LETTER TURNED I
610 case 'k': return 0x1d4f;
611 case 'm': return 0x1d50;
612 case 0x014b: return 0x1d51; // LATIN SMALL LETTER ENG
613 case 'o': return 0x1d52;
614 case 0x0254: return 0x1d53; // LATIN SMALL LETTER OPEN O
615 case 0x1d16: return 0x1d54; // LATIN SMALL LETTER TOP HALF O
616 case 0x1d17: return 0x1d55; // LATIN SMALL LETTER BOTTOM HALF O
617 case 'p': return 0x1d56;
618 case 't': return 0x1d57;
619 case 'u': return 0x1d58;
620 case 0x1d1d: return 0x1d59; // LATIN SMALL LETTER SIDEWAYS U
621 case 0x1d1f: return 0x1d5a; // LATIN SMALL LETTER SIDEWAYS TURNED M
622 case 'v': return 0x1d5b;
623 case 0x03b2: return 0x1d5d; // GREEK SMALL LETTER BETA
624 case 0x03b3: return 0x1d5e; // GREEK SMALL LETTER GAMMA
625 case 0x03b4: return 0x1d5f; // GREEK SMALL LETTER DELTA
626 case 0x03c6: return 0x1d60; // GREEK SMALL LETTER PHI
627 case 0x03c7: return 0x1d61; // GREEK SMALL LETTER CHI
633 char_type subscript(char_type c)
636 case 'i': return 0x1d62;
637 case 'r': return 0x1d63;
638 case 'u': return 0x1d64;
639 case 'v': return 0x1d65;
640 case 0x03b2: return 0x1d66; // GREEK SMALL LETTER BETA
641 case 0x03b3: return 0x1d67; // GREEK SMALL LETTER GAMMA
642 case 0x03c1: return 0x1d68; // GREEK SMALL LETTER RHO
643 case 0x03c6: return 0x1d69; // GREEK SMALL LETTER PHI
644 case 0x03c7: return 0x1d6a; // GREEK SMALL LETTER CHI
645 case '0': return 0x2080;
646 case '1': return 0x2081;
647 case '2': return 0x2082;
648 case '3': return 0x2083;
649 case '4': return 0x2084;
650 case '5': return 0x2085;
651 case '6': return 0x2086;
652 case '7': return 0x2087;
653 case '8': return 0x2088;
654 case '9': return 0x2089;
655 case '+': return 0x208a;
656 case '-': return 0x208b;
657 case '=': return 0x208c;
658 case '(': return 0x208d;
659 case ')': return 0x208e;
660 case 'a': return 0x2090;
661 case 'e': return 0x2091;
662 case 'o': return 0x2092;
663 case 'x': return 0x2093;
664 case 0x0259: return 0x2093; // LATIN SMALL LETTER SCHWA
670 bool prefixIs(docstring const & a, char_type c)
678 bool prefixIs(string const & a, string const & pre)
680 size_t const prelen = pre.length();
681 size_t const alen = a.length();
682 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
686 bool prefixIs(docstring const & a, docstring const & pre)
688 size_t const prelen = pre.length();
689 size_t const alen = a.length();
690 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
694 bool suffixIs(string const & a, char c)
698 return a[a.length() - 1] == c;
702 bool suffixIs(docstring const & a, char_type c)
706 return a[a.length() - 1] == c;
710 bool suffixIs(string const & a, string const & suf)
712 size_t const suflen = suf.length();
713 size_t const alen = a.length();
714 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
718 bool suffixIs(docstring const & a, docstring const & suf)
720 size_t const suflen = suf.length();
721 size_t const alen = a.length();
722 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
726 bool containsOnly(string const & s, string const & cset)
728 return s.find_first_not_of(cset) == string::npos;
732 bool containsOnly(docstring const & s, string const & cset)
734 return s.find_first_not_of(from_ascii(cset)) == string::npos;
738 // ale970405+lasgoutt-970425
739 // rewritten to use new string (Lgb)
740 string const token(string const & a, char delim, int n)
748 // Find delimiter or end of string
750 if ((i = a.find(delim, i)) == string::npos)
756 // i is now the n'th delim (or string::npos)
757 if (i == string::npos)
760 k = a.find(delim, i);
761 // k is now the n'th + 1 delim (or string::npos)
763 return a.substr(i, k - i);
767 docstring const token(docstring const & a, char_type delim, int n)
775 // Find delimiter or end of string
777 if ((i = a.find(delim, i)) == docstring::npos)
783 // i is now the n'th delim (or string::npos)
784 if (i == docstring::npos)
787 k = a.find(delim, i);
788 // k is now the n'th + 1 delim (or string::npos)
790 return a.substr(i, k - i);
794 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
795 // rewritten to use new string (Lgb)
796 int tokenPos(string const & a, char delim, string const & tok)
802 while (!str.empty()) {
803 str = split(str, tmptok, delim);
812 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
813 // rewritten to use new string (Lgb)
814 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
820 while (!str.empty()) {
821 str = split(str, tmptok, delim);
832 /// Substitute all \a oldchar with \a newchar
833 template<typename Ch> inline
834 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
835 Ch oldchar, Ch newchar)
837 typedef basic_string<Ch> String;
839 typename String::iterator lit = tmp.begin();
840 typename String::iterator end = tmp.end();
841 for (; lit != end; ++lit)
842 if ((*lit) == oldchar)
848 /// Substitute all \a oldchar with \a newchar
849 docstring const subst_char(docstring const & a,
850 docstring::value_type oldchar, docstring::value_type newchar)
853 docstring::iterator lit = tmp.begin();
854 docstring::iterator end = tmp.end();
855 for (; lit != end; ++lit)
856 if ((*lit) == oldchar)
862 /// substitutes all instances of \a oldstr with \a newstr
863 template<typename String> inline
864 String const subst_string(String const & a,
865 String const & oldstr, String const & newstr)
867 LASSERT(!oldstr.empty(), return a);
870 size_t const olen = oldstr.length();
871 while ((i = lstr.find(oldstr, i)) != string::npos) {
872 lstr.replace(i, olen, newstr);
873 i += newstr.length(); // We need to be sure that we dont
874 // use the same i over and over again.
880 docstring const subst_string(docstring const & a,
881 docstring const & oldstr, docstring const & newstr)
883 LASSERT(!oldstr.empty(), return a);
886 size_t const olen = oldstr.length();
887 while ((i = lstr.find(oldstr, i)) != string::npos) {
888 lstr.replace(i, olen, newstr);
889 i += newstr.length(); // We need to be sure that we dont
890 // use the same i over and over again.
898 string const subst(string const & a, char oldchar, char newchar)
900 return subst_char(a, oldchar, newchar);
904 docstring const subst(docstring const & a,
905 char_type oldchar, char_type newchar)
907 return subst_char(a, oldchar, newchar);
911 string const subst(string const & a,
912 string const & oldstr, string const & newstr)
914 return subst_string(a, oldstr, newstr);
918 docstring const subst(docstring const & a,
919 docstring const & oldstr, docstring const & newstr)
921 return subst_string(a, oldstr, newstr);
925 int count_char(string const & str, char chr)
928 string::const_iterator lit = str.begin();
929 string::const_iterator end = str.end();
930 for (; lit != end; ++lit)
937 /// Count all occurences of char \a chr inside \a str
938 int count_char(docstring const & str, docstring::value_type chr)
941 docstring::const_iterator lit = str.begin();
942 docstring::const_iterator end = str.end();
943 for (; lit != end; ++lit)
950 int count_bin_chars(string const & str)
952 QString const qstr = toqstr(str).simplified();
954 QString::const_iterator cit = qstr.begin();
955 QString::const_iterator end = qstr.end();
956 for (; cit != end; ++cit) {
957 switch (cit->category()) {
958 case QChar::Separator_Line:
959 case QChar::Separator_Paragraph:
960 case QChar::Other_Control:
961 case QChar::Other_Format:
962 case QChar::Other_Surrogate:
963 case QChar::Other_PrivateUse:
964 case QChar::Other_NotAssigned:
975 docstring const trim(docstring const & a, char const * p)
977 LASSERT(p, return a);
979 if (a.empty() || !*p)
982 docstring s = from_ascii(p);
983 size_t r = a.find_last_not_of(s);
984 size_t l = a.find_first_not_of(s);
986 // Is this the minimal test? (lgb)
987 if (r == docstring::npos && l == docstring::npos)
990 return a.substr(l, r - l + 1);
994 string const trim(string const & a, char const * p)
996 LASSERT(p, return a);
998 if (a.empty() || !*p)
1001 size_t r = a.find_last_not_of(p);
1002 size_t l = a.find_first_not_of(p);
1004 // Is this the minimal test? (lgb)
1005 if (r == string::npos && l == string::npos)
1008 return a.substr(l, r - l + 1);
1012 string const rtrim(string const & a, char const * p)
1014 LASSERT(p, return a);
1016 if (a.empty() || !*p)
1019 size_t r = a.find_last_not_of(p);
1021 // Is this test really needed? (Lgb)
1022 if (r == string::npos)
1025 return a.substr(0, r + 1);
1029 docstring const rtrim(docstring const & a, char const * p)
1031 LASSERT(p, return a);
1033 if (a.empty() || !*p)
1036 size_t r = a.find_last_not_of(from_ascii(p));
1038 // Is this test really needed? (Lgb)
1039 if (r == docstring::npos)
1042 return a.substr(0, r + 1);
1046 string const ltrim(string const & a, char const * p)
1048 LASSERT(p, return a);
1049 if (a.empty() || !*p)
1051 size_t l = a.find_first_not_of(p);
1052 if (l == string::npos)
1054 return a.substr(l, string::npos);
1058 docstring const ltrim(docstring const & a, char const * p)
1060 LASSERT(p, return a);
1061 if (a.empty() || !*p)
1063 size_t l = a.find_first_not_of(from_ascii(p));
1064 if (l == docstring::npos)
1066 return a.substr(l, docstring::npos);
1071 template<typename String, typename Char> inline
1072 String const doSplit(String const & a, String & piece, Char delim)
1075 size_t i = a.find(delim);
1076 if (i == a.length() - 1) {
1077 piece = a.substr(0, i);
1078 } else if (i == 0) {
1080 tmp = a.substr(i + 1);
1081 } else if (i != String::npos) {
1082 piece = a.substr(0, i);
1083 tmp = a.substr(i + 1);
1091 // FIXME: why is this specialization needed?
1092 template<typename Char> inline
1093 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
1096 size_t i = a.find(delim);
1097 if (i == a.length() - 1) {
1098 piece = a.substr(0, i);
1099 } else if (i == 0) {
1101 tmp = a.substr(i + 1);
1102 } else if (i != docstring::npos) {
1103 piece = a.substr(0, i);
1104 tmp = a.substr(i + 1);
1114 string const split(string const & a, string & piece, char delim)
1116 return doSplit(a, piece, delim);
1120 docstring const split(docstring const & a, docstring & piece, char_type delim)
1122 return doSplit(a, piece, delim);
1126 string const split(string const & a, char delim)
1129 size_t i = a.find(delim);
1130 if (i != string::npos) // found delim
1131 tmp = a.substr(i + 1);
1137 string const rsplit(string const & a, string & piece, char delim)
1140 size_t i = a.rfind(delim);
1141 if (i != string::npos) { // delimiter was found
1142 piece = a.substr(0, i);
1143 tmp = a.substr(i + 1);
1144 } else { // delimiter was not found
1151 docstring const rsplit(docstring const & a, docstring & piece, char_type delim)
1154 size_t i = a.rfind(delim);
1155 if (i != string::npos) { // delimiter was found
1156 piece = a.substr(0, i);
1157 tmp = a.substr(i + 1);
1158 } else { // delimiter was not found
1165 docstring const rsplit(docstring const & a, char_type delim)
1168 size_t i = a.rfind(delim);
1169 if (i != string::npos)
1170 tmp = a.substr(i + 1);
1175 docstring const escape(docstring const & lab)
1177 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
1178 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1180 for (size_t i = 0; i < lab.length(); ++i) {
1181 char_type c = lab[i];
1182 if (c >= 128 || c == '=' || c == '%' || c == '#' || c == '$'
1183 || c == '}' || c == '{' || c == ']' || c == '[' || c == '&'
1185 // Although char_type is a 32 bit type we know that
1186 // UCS4 occupies only 21 bits, so we don't need to
1187 // encode bigger values. Test for 2^24 because we
1188 // can encode that with the 6 hex digits that are
1189 // needed for 21 bits anyway.
1190 LASSERT(c < (1 << 24), continue);
1192 enc += hexdigit[(c>>20) & 15];
1193 enc += hexdigit[(c>>16) & 15];
1194 enc += hexdigit[(c>>12) & 15];
1195 enc += hexdigit[(c>> 8) & 15];
1196 enc += hexdigit[(c>> 4) & 15];
1197 enc += hexdigit[ c & 15];
1206 docstring const protectArgument(docstring & arg, char const l,
1209 if (contains(arg, l) || contains(arg, r))
1211 arg = '{' + arg + '}';
1216 bool truncateWithEllipsis(docstring & str, size_t const len)
1218 if (str.size() <= len)
1222 str[len - 1] = 0x2026;// HORIZONTAL ELLIPSIS
1229 // this doesn't check whether str is empty, so do that first.
1230 vector<docstring> wrapToVec(docstring const & str, int ind,
1233 docstring s = trim(str);
1235 return vector<docstring>();
1239 indent.insert(0, -ind, ' ');
1242 s.insert(0, ind, ' ');
1244 vector<docstring> retval;
1245 while (s.size() > width) {
1246 // find the last space within the first 'width' chars
1247 size_t const i = s.find_last_of(' ', width - 1);
1248 if (i == docstring::npos || i <= size_t(ind)) {
1250 truncateWithEllipsis(s, width);
1253 retval.push_back(s.substr(0, i));
1254 s = indent + s.substr(i);
1255 ind = indent.size();
1258 retval.push_back(s);
1265 docstring wrap(docstring const & str, int const ind, size_t const width)
1267 docstring s = trim(str);
1271 vector<docstring> const svec = wrapToVec(str, ind, width);
1272 return getStringFromVector(svec, from_ascii("\n"));
1276 docstring wrapParas(docstring const & str, int const indent,
1277 size_t const width, size_t const maxlines)
1282 vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1283 vector<docstring> retval;
1285 vector<docstring>::const_iterator it = pars.begin();
1286 vector<docstring>::const_iterator const en = pars.end();
1287 for (; it != en; ++it) {
1288 vector<docstring> tmp = wrapToVec(*it, indent, width);
1289 size_t const nlines = tmp.size();
1292 size_t const curlines = retval.size();
1293 if (maxlines > 0 && curlines + nlines > maxlines) {
1294 tmp.resize(maxlines - curlines);
1295 docstring last = tmp.back();
1296 size_t const lsize = last.size();
1297 if (lsize > width - 1) {
1298 size_t const i = last.find_last_of(' ', width - 1);
1299 if (i == docstring::npos || i <= size_t(indent))
1301 truncateWithEllipsis(last, lsize);
1303 truncateWithEllipsis(last, i);
1305 last.push_back(0x2026);//HORIZONTAL ELLIPSIS
1307 tmp.push_back(last);
1309 retval.insert(retval.end(), tmp.begin(), tmp.end());
1310 if (maxlines > 0 && retval.size() >= maxlines)
1313 return getStringFromVector(retval, from_ascii("\n"));
1319 template<typename String> vector<String> const
1320 getVectorFromStringT(String const & str, String const & delim, bool keepempty)
1322 // Lars would like this code to go, but for now his replacement (below)
1323 // doesn't fullfil the same function. I have, therefore, reactivated the
1324 // old code for now. Angus 11 Nov 2002.
1329 String keys = rtrim(str);
1331 size_t const idx = keys.find(delim);
1332 if (idx == String::npos) {
1333 vec.push_back(ltrim(keys));
1336 String const key = trim(keys.substr(0, idx));
1337 if (!key.empty() || keepempty)
1339 size_t const start = idx + delim.size();
1340 keys = keys.substr(start);
1344 typedef boost::char_separator<typename String::value_type> Separator;
1345 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1346 Separator sep(delim.c_str());
1347 Tokenizer tokens(str, sep);
1348 return vector<String>(tokens.begin(), tokens.end());
1353 template<typename String> const String
1354 getStringFromVector(vector<String> const & vec, String const & delim)
1357 typename vector<String>::const_iterator it = vec.begin();
1358 typename vector<String>::const_iterator en = vec.end();
1359 for (; it != en; ++it) {
1360 String item = trim(*it);
1373 vector<string> const getVectorFromString(string const & str,
1374 string const & delim,
1377 return getVectorFromStringT<string>(str, delim, keepempty);
1381 vector<docstring> const getVectorFromString(docstring const & str,
1382 docstring const & delim,
1385 return getVectorFromStringT<docstring>(str, delim, keepempty);
1389 string const getStringFromVector(vector<string> const & vec,
1390 string const & delim)
1392 return getStringFromVector<string>(vec, delim);
1396 docstring const getStringFromVector(vector<docstring> const & vec,
1397 docstring const & delim)
1399 return getStringFromVector<docstring>(vec, delim);
1403 int findToken(char const * const str[], string const & search_token)
1407 while (str[i][0] && str[i] != search_token)
1415 std::string formatFPNumber(double x)
1417 // Need manual tweaking, QString::number(x, 'f', 16) does not work either
1420 // Prevent outputs of 23.4200000000000017 but output small numbers
1421 // with at least 6 significant digits.
1422 double const logarithm = log10(fabs(x));
1423 os << std::setprecision(max(6 - iround(logarithm), 0)) << x;
1424 string result = os.str();
1425 if (result.find('.') != string::npos) {
1426 result = rtrim(result, "0");
1427 if (result[result.length()-1] == '.')
1428 result = rtrim(result, ".");
1434 docstring bformat(docstring const & fmt, int arg1)
1436 LATTEST(contains(fmt, from_ascii("%1$d")));
1437 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1438 return subst(str, from_ascii("%%"), from_ascii("%"));
1442 docstring bformat(docstring const & fmt, long arg1)
1444 LATTEST(contains(fmt, from_ascii("%1$d")));
1445 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1446 return subst(str, from_ascii("%%"), from_ascii("%"));
1450 #ifdef LYX_USE_LONG_LONG
1451 docstring bformat(docstring const & fmt, long long arg1)
1453 LATTEST(contains(fmt, from_ascii("%1$d")));
1454 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1455 return subst(str, from_ascii("%%"), from_ascii("%"));
1460 docstring bformat(docstring const & fmt, unsigned int arg1)
1462 LATTEST(contains(fmt, from_ascii("%1$d")));
1463 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1464 return subst(str, from_ascii("%%"), from_ascii("%"));
1468 docstring bformat(docstring const & fmt, docstring const & arg1)
1470 LATTEST(contains(fmt, from_ascii("%1$s")));
1471 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1472 return subst(str, from_ascii("%%"), from_ascii("%"));
1476 docstring bformat(docstring const & fmt, char * arg1)
1478 LATTEST(contains(fmt, from_ascii("%1$s")));
1479 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1480 return subst(str, from_ascii("%%"), from_ascii("%"));
1484 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2)
1486 LATTEST(contains(fmt, from_ascii("%1$s")));
1487 LATTEST(contains(fmt, from_ascii("%2$s")));
1488 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1489 str = subst(str, from_ascii("%2$s"), arg2);
1490 return subst(str, from_ascii("%%"), from_ascii("%"));
1494 docstring bformat(docstring const & fmt, docstring const & arg1, int arg2)
1496 LATTEST(contains(fmt, from_ascii("%1$s")));
1497 LATTEST(contains(fmt, from_ascii("%2$d")));
1498 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1499 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1500 return subst(str, from_ascii("%%"), from_ascii("%"));
1504 docstring bformat(docstring const & fmt, char const * arg1, docstring const & arg2)
1506 LATTEST(contains(fmt, from_ascii("%1$s")));
1507 LATTEST(contains(fmt, from_ascii("%2$s")));
1508 docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1509 str = subst(str, from_ascii("%2$s"), arg2);
1510 return subst(str, from_ascii("%%"), from_ascii("%"));
1514 docstring bformat(docstring const & fmt, int arg1, int arg2)
1516 LATTEST(contains(fmt, from_ascii("%1$d")));
1517 LATTEST(contains(fmt, from_ascii("%2$d")));
1518 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1519 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1520 return subst(str, from_ascii("%%"), from_ascii("%"));
1524 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2, docstring const & arg3)
1526 LATTEST(contains(fmt, from_ascii("%1$s")));
1527 LATTEST(contains(fmt, from_ascii("%2$s")));
1528 LATTEST(contains(fmt, from_ascii("%3$s")));
1529 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1530 str = subst(str, from_ascii("%2$s"), arg2);
1531 str = subst(str, from_ascii("%3$s"), arg3);
1532 return subst(str, from_ascii("%%"), from_ascii("%"));
1536 docstring bformat(docstring const & fmt,
1537 docstring const & arg1, docstring const & arg2, docstring const & arg3, docstring const & arg4)
1539 LATTEST(contains(fmt, from_ascii("%1$s")));
1540 LATTEST(contains(fmt, from_ascii("%2$s")));
1541 LATTEST(contains(fmt, from_ascii("%3$s")));
1542 LATTEST(contains(fmt, from_ascii("%4$s")));
1543 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1544 str = subst(str, from_ascii("%2$s"), arg2);
1545 str = subst(str, from_ascii("%3$s"), arg3);
1546 str = subst(str, from_ascii("%4$s"), arg4);
1547 return subst(str, from_ascii("%%"), from_ascii("%"));
1550 } // namespace support