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)
187 int compare_no_case(docstring const & s, docstring const & s2)
189 docstring::const_iterator p = s.begin();
190 docstring::const_iterator p2 = s2.begin();
192 while (p != s.end() && p2 != s2.end()) {
193 char_type const lc1 = lowercase(*p);
194 char_type const lc2 = lowercase(*p2);
196 return (lc1 < lc2) ? -1 : 1;
201 if (s.size() == s2.size())
203 if (s.size() < s2.size())
209 int compare_locale(docstring const & s, docstring const & s2)
211 // FIXME We have a report that this does not work on windows (bug 9030)
214 string const l = to_local8bit(s);
215 string const r = to_local8bit(s2);
216 return strcoll(l.c_str(), r.c_str());
220 // fall back to builtin sorting
221 LYXERR0("Could not compare using the current locale: "
222 << e.what() << ", using fallback.");
234 template<typename Char>
235 Char ascii_tolower(Char c) {
236 if (c >= 'A' && c <= 'Z')
237 return c - 'A' + 'a';
244 int compare_ascii_no_case(string const & s, string const & s2)
246 string::const_iterator p = s.begin();
247 string::const_iterator p2 = s2.begin();
249 while (p != s.end() && p2 != s2.end()) {
250 int const lc1 = ascii_tolower(*p);
251 int const lc2 = ascii_tolower(*p2);
253 return (lc1 < lc2) ? -1 : 1;
258 if (s.size() == s2.size())
260 if (s.size() < s2.size())
266 int compare_ascii_no_case(docstring const & s, docstring const & s2)
268 docstring::const_iterator p = s.begin();
269 docstring::const_iterator p2 = s2.begin();
271 while (p != s.end() && p2 != s2.end()) {
272 char_type const lc1 = ascii_tolower(*p);
273 char_type const lc2 = ascii_tolower(*p2);
275 return (lc1 < lc2) ? -1 : 1;
280 if (s.size() == s2.size())
282 if (s.size() < s2.size())
288 bool isStrInt(string const & str)
293 // Remove leading and trailing white space chars.
294 string const tmpstr = trim(str);
298 string::const_iterator cit = tmpstr.begin();
302 string::const_iterator end = tmpstr.end();
303 for (; cit != end; ++cit)
304 if (!isDigitASCII(*cit))
311 bool isStrUnsignedInt(string const & str)
316 // Remove leading and trailing white space chars.
317 string const tmpstr = trim(str);
321 string::const_iterator cit = tmpstr.begin();
322 string::const_iterator end = tmpstr.end();
323 for (; cit != end; ++cit)
324 if (!isDigitASCII(*cit))
331 bool isStrDbl(string const & str)
336 // Remove leading and trailing white space chars.
337 string const tmpstr = trim(str);
340 // if (tmpstr.count('.') > 1) return false;
342 string::const_iterator cit = tmpstr.begin();
343 bool found_dot = false;
346 string::const_iterator end = tmpstr.end();
347 for (; cit != end; ++cit) {
348 if (!isDigitASCII(*cit) && *cit != '.')
360 bool hasDigitASCII(docstring const & str)
362 docstring::const_iterator cit = str.begin();
363 docstring::const_iterator const end = str.end();
364 for (; cit != end; ++cit)
365 if (isDigitASCII(*cit))
371 bool isHexChar(char_type c)
383 c == 'a' || c == 'A' ||
384 c == 'b' || c == 'B' ||
385 c == 'c' || c == 'C' ||
386 c == 'd' || c == 'D' ||
387 c == 'e' || c == 'E' ||
388 c == 'f' || c == 'F';
392 bool isHex(docstring const & str)
396 if (str.length() > 2 && str[0] == '0' &&
397 (str[1] == 'x' || str[1] == 'X'))
400 int const len = str.length();
402 for (; index < len; ++index) {
403 if (!isHexChar(str[index]))
410 int hexToInt(docstring const & str)
412 string s = to_ascii(str);
414 sscanf(s.c_str(), "%x", &h);
419 bool isAscii(docstring const & str)
421 int const len = str.length();
422 for (int i = 0; i < len; ++i)
429 bool isAscii(string const & str)
431 int const len = str.length();
432 for (int i = 0; i < len; ++i)
433 if (static_cast<unsigned char>(str[i]) >= 0x80)
439 char lowercase(char c)
441 LASSERT(isASCII(c), return '?');
442 return char(tolower(c));
446 char uppercase(char c)
448 LASSERT(isASCII(c), return '?');
449 return char(toupper(c));
453 char_type lowercase(char_type c)
456 // We don't know how to lowercase a non-utf16 char
458 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
462 char_type uppercase(char_type c)
465 // We don't know how to uppercase a non-utf16 char
467 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
471 bool isLowerCase(char_type ch) {
472 return lowercase(ch) == ch;
476 bool isUpperCase(char_type ch) {
477 return uppercase(ch) == ch;
483 // since we cannot use tolower and toupper directly in the
484 // calls to transform yet, we use these helper clases. (Lgb)
486 struct local_lowercase {
487 char_type operator()(char_type c) const {
492 struct local_uppercase {
493 char_type operator()(char_type c) const {
498 template<typename Char> struct local_ascii_lowercase {
499 Char operator()(Char c) const { return ascii_tolower(c); }
502 } // end of anon namespace
505 docstring const lowercase(docstring const & a)
508 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
513 /* Uncomment here and in lstrings.h if you should need this.
514 string const lowercase(string const & a)
517 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
523 docstring const uppercase(docstring const & a)
526 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
531 string const ascii_lowercase(string const & a)
534 transform(tmp.begin(), tmp.end(), tmp.begin(),
535 local_ascii_lowercase<char>());
540 docstring const ascii_lowercase(docstring const & a)
543 transform(tmp.begin(), tmp.end(), tmp.begin(),
544 local_ascii_lowercase<char_type>());
549 char_type superscript(char_type c)
552 case '2': return 0x00b2;
553 case '3': return 0x00b3;
554 case '1': return 0x00b9;
555 case '0': return 0x2070;
556 case 'i': return 0x2071;
557 case '4': return 0x2074;
558 case '5': return 0x2075;
559 case '6': return 0x2076;
560 case '7': return 0x2077;
561 case '8': return 0x2078;
562 case '9': return 0x2079;
563 case '+': return 0x207a;
564 case '-': return 0x207b;
565 case '=': return 0x207c;
566 case '(': return 0x207d;
567 case ')': return 0x207e;
568 case 'n': return 0x207f;
569 case 'h': return 0x02b0;
570 case 0x0266: return 0x02b1; // LATIN SMALL LETTER H WITH HOOK
571 case 'j': return 0x02b2;
572 case 'r': return 0x02b3;
573 case 0x0279: return 0x02b4; // LATIN SMALL LETTER TURNED R
574 case 0x027b: return 0x02b5; // LATIN SMALL LETTER TURNED R WITH HOOK
575 case 0x0281: return 0x02b6; // LATIN SMALL LETTER CAPITAL INVERTED R
576 case 'w': return 0x02b7;
577 case 'y': return 0x02b8;
578 // case 0x0294: return 0x02c0; // LATIN LETTER GLOTTAL STOP)
579 // case 0x0295: return 0x02c1; // LATIN LETTER PHARYNGEAL VOICED FRICATIVE
580 // (= LATIN LETTER REVERSED GLOTTAL STOP)
581 case 'l': return 0x02e1;
582 case 's': return 0x02e2;
583 case 'x': return 0x02e3;
584 // case 0x0295: return 0x02e4; // LATIN SMALL LETTER REVERSED GLOTTAL STOP
585 case 'A': return 0x1d2c;
586 case 0x00c6: return 0x1d2d; // LATIN CAPITAL LETTER AE
587 case 'B': return 0x1d2e;
588 case 'D': return 0x1d30;
589 case 'E': return 0x1d31;
590 case 'G': return 0x1d33;
591 case 'H': return 0x1d34;
592 case 'I': return 0x1d35;
593 case 'J': return 0x1d36;
594 case 'K': return 0x1d37;
595 case 'L': return 0x1d38;
596 case 'M': return 0x1d39;
597 case 'N': return 0x1d3a;
598 case 'O': return 0x1d3c;
599 case 'P': return 0x1d3e;
600 case 'R': return 0x1d3f;
601 case 'T': return 0x1d40;
602 case 'U': return 0x1d41;
603 case 'W': return 0x1d42;
604 case 'a': return 0x1d43;
605 case 0x0250: return 0x1d44; // LATIN SMALL LETTER TURNED A
606 case 0x0251: return 0x1d45; // LATIN SMALL LETTER ALPHA
607 case 'b': return 0x1d47;
608 case 'd': return 0x1d48;
609 case 'e': return 0x1d49;
610 case 0x0259: return 0x1d4a; // LATIN SMALL LETTER SCHWA
611 case 0x025b: return 0x1d4b; // LATIN SMALL LETTER OPEN E
612 case 0x1d08: return 0x1d4c; // LATIN SMALL LETTER TURNED OPEN E
613 case 'g': return 0x1d4d;
614 case 0x1d09: return 0x1d4e; // LATIN SMALL LETTER TURNED I
615 case 'k': return 0x1d4f;
616 case 'm': return 0x1d50;
617 case 0x014b: return 0x1d51; // LATIN SMALL LETTER ENG
618 case 'o': return 0x1d52;
619 case 0x0254: return 0x1d53; // LATIN SMALL LETTER OPEN O
620 case 0x1d16: return 0x1d54; // LATIN SMALL LETTER TOP HALF O
621 case 0x1d17: return 0x1d55; // LATIN SMALL LETTER BOTTOM HALF O
622 case 'p': return 0x1d56;
623 case 't': return 0x1d57;
624 case 'u': return 0x1d58;
625 case 0x1d1d: return 0x1d59; // LATIN SMALL LETTER SIDEWAYS U
626 case 0x1d1f: return 0x1d5a; // LATIN SMALL LETTER SIDEWAYS TURNED M
627 case 'v': return 0x1d5b;
628 case 0x03b2: return 0x1d5d; // GREEK SMALL LETTER BETA
629 case 0x03b3: return 0x1d5e; // GREEK SMALL LETTER GAMMA
630 case 0x03b4: return 0x1d5f; // GREEK SMALL LETTER DELTA
631 case 0x03c6: return 0x1d60; // GREEK SMALL LETTER PHI
632 case 0x03c7: return 0x1d61; // GREEK SMALL LETTER CHI
638 char_type subscript(char_type c)
641 case 'i': return 0x1d62;
642 case 'r': return 0x1d63;
643 case 'u': return 0x1d64;
644 case 'v': return 0x1d65;
645 case 0x03b2: return 0x1d66; // GREEK SMALL LETTER BETA
646 case 0x03b3: return 0x1d67; // GREEK SMALL LETTER GAMMA
647 case 0x03c1: return 0x1d68; // GREEK SMALL LETTER RHO
648 case 0x03c6: return 0x1d69; // GREEK SMALL LETTER PHI
649 case 0x03c7: return 0x1d6a; // GREEK SMALL LETTER CHI
650 case '0': return 0x2080;
651 case '1': return 0x2081;
652 case '2': return 0x2082;
653 case '3': return 0x2083;
654 case '4': return 0x2084;
655 case '5': return 0x2085;
656 case '6': return 0x2086;
657 case '7': return 0x2087;
658 case '8': return 0x2088;
659 case '9': return 0x2089;
660 case '+': return 0x208a;
661 case '-': return 0x208b;
662 case '=': return 0x208c;
663 case '(': return 0x208d;
664 case ')': return 0x208e;
665 case 'a': return 0x2090;
666 case 'e': return 0x2091;
667 case 'o': return 0x2092;
668 case 'x': return 0x2093;
669 case 0x0259: return 0x2093; // LATIN SMALL LETTER SCHWA
675 bool prefixIs(docstring const & a, char_type c)
683 bool prefixIs(string const & a, string const & pre)
685 size_t const prelen = pre.length();
686 size_t const alen = a.length();
687 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
691 bool prefixIs(docstring const & a, docstring const & pre)
693 size_t const prelen = pre.length();
694 size_t const alen = a.length();
695 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
699 bool suffixIs(string const & a, char c)
703 return a[a.length() - 1] == c;
707 bool suffixIs(docstring const & a, char_type c)
711 return a[a.length() - 1] == c;
715 bool suffixIs(string const & a, string const & suf)
717 size_t const suflen = suf.length();
718 size_t const alen = a.length();
719 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
723 bool suffixIs(docstring const & a, docstring const & suf)
725 size_t const suflen = suf.length();
726 size_t const alen = a.length();
727 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
731 bool containsOnly(string const & s, string const & cset)
733 return s.find_first_not_of(cset) == string::npos;
737 // ale970405+lasgoutt-970425
738 // rewritten to use new string (Lgb)
739 string const token(string const & a, char delim, int n)
747 // Find delimiter or end of string
749 if ((i = a.find(delim, i)) == string::npos)
755 // i is now the n'th delim (or string::npos)
756 if (i == string::npos)
759 k = a.find(delim, i);
760 // k is now the n'th + 1 delim (or string::npos)
762 return a.substr(i, k - i);
766 docstring const token(docstring const & a, char_type delim, int n)
774 // Find delimiter or end of string
776 if ((i = a.find(delim, i)) == docstring::npos)
782 // i is now the n'th delim (or string::npos)
783 if (i == docstring::npos)
786 k = a.find(delim, i);
787 // k is now the n'th + 1 delim (or string::npos)
789 return a.substr(i, k - i);
793 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
794 // rewritten to use new string (Lgb)
795 int tokenPos(string const & a, char delim, string const & tok)
801 while (!str.empty()) {
802 str = split(str, tmptok, delim);
811 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
812 // rewritten to use new string (Lgb)
813 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
819 while (!str.empty()) {
820 str = split(str, tmptok, delim);
831 /// Substitute all \a oldchar with \a newchar
832 template<typename Ch> inline
833 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
834 Ch oldchar, Ch newchar)
836 typedef basic_string<Ch> String;
838 typename String::iterator lit = tmp.begin();
839 typename String::iterator end = tmp.end();
840 for (; lit != end; ++lit)
841 if ((*lit) == oldchar)
847 /// Substitute all \a oldchar with \a newchar
848 docstring const subst_char(docstring const & a,
849 docstring::value_type oldchar, docstring::value_type newchar)
852 docstring::iterator lit = tmp.begin();
853 docstring::iterator end = tmp.end();
854 for (; lit != end; ++lit)
855 if ((*lit) == oldchar)
861 /// substitutes all instances of \a oldstr with \a newstr
862 template<typename String> inline
863 String const subst_string(String const & a,
864 String const & oldstr, String const & newstr)
866 LASSERT(!oldstr.empty(), return a);
869 size_t const olen = oldstr.length();
870 while ((i = lstr.find(oldstr, i)) != string::npos) {
871 lstr.replace(i, olen, newstr);
872 i += newstr.length(); // We need to be sure that we dont
873 // use the same i over and over again.
879 docstring const subst_string(docstring const & a,
880 docstring const & oldstr, docstring const & newstr)
882 LASSERT(!oldstr.empty(), return a);
885 size_t const olen = oldstr.length();
886 while ((i = lstr.find(oldstr, i)) != string::npos) {
887 lstr.replace(i, olen, newstr);
888 i += newstr.length(); // We need to be sure that we dont
889 // use the same i over and over again.
897 string const subst(string const & a, char oldchar, char newchar)
899 return subst_char(a, oldchar, newchar);
903 docstring const subst(docstring const & a,
904 char_type oldchar, char_type newchar)
906 return subst_char(a, oldchar, newchar);
910 string const subst(string const & a,
911 string const & oldstr, string const & newstr)
913 return subst_string(a, oldstr, newstr);
917 docstring const subst(docstring const & a,
918 docstring const & oldstr, docstring const & newstr)
920 return subst_string(a, oldstr, newstr);
924 int count_char(string const & str, char chr)
927 string::const_iterator lit = str.begin();
928 string::const_iterator end = str.end();
929 for (; lit != end; ++lit)
936 /// Count all occurences of char \a chr inside \a str
937 int count_char(docstring const & str, docstring::value_type chr)
940 docstring::const_iterator lit = str.begin();
941 docstring::const_iterator end = str.end();
942 for (; lit != end; ++lit)
949 int count_bin_chars(string const & str)
951 QString const qstr = toqstr(str).simplified();
953 QString::const_iterator cit = qstr.begin();
954 QString::const_iterator end = qstr.end();
955 for (; cit != end; ++cit) {
956 switch (cit->category()) {
957 case QChar::Separator_Line:
958 case QChar::Separator_Paragraph:
959 case QChar::Other_Control:
960 case QChar::Other_Format:
961 case QChar::Other_Surrogate:
962 case QChar::Other_PrivateUse:
963 case QChar::Other_NotAssigned:
974 docstring const trim(docstring const & a, char const * p)
976 LASSERT(p, return a);
978 if (a.empty() || !*p)
981 docstring s = from_ascii(p);
982 size_t r = a.find_last_not_of(s);
983 size_t l = a.find_first_not_of(s);
985 // Is this the minimal test? (lgb)
986 if (r == docstring::npos && l == docstring::npos)
989 return a.substr(l, r - l + 1);
993 string const trim(string const & a, char const * p)
995 LASSERT(p, return a);
997 if (a.empty() || !*p)
1000 size_t r = a.find_last_not_of(p);
1001 size_t l = a.find_first_not_of(p);
1003 // Is this the minimal test? (lgb)
1004 if (r == string::npos && l == string::npos)
1007 return a.substr(l, r - l + 1);
1011 string const rtrim(string const & a, char const * p)
1013 LASSERT(p, return a);
1015 if (a.empty() || !*p)
1018 size_t r = a.find_last_not_of(p);
1020 // Is this test really needed? (Lgb)
1021 if (r == string::npos)
1024 return a.substr(0, r + 1);
1028 docstring const rtrim(docstring const & a, char const * p)
1030 LASSERT(p, return a);
1032 if (a.empty() || !*p)
1035 size_t r = a.find_last_not_of(from_ascii(p));
1037 // Is this test really needed? (Lgb)
1038 if (r == docstring::npos)
1041 return a.substr(0, r + 1);
1045 string const ltrim(string const & a, char const * p)
1047 LASSERT(p, return a);
1048 if (a.empty() || !*p)
1050 size_t l = a.find_first_not_of(p);
1051 if (l == string::npos)
1053 return a.substr(l, string::npos);
1057 docstring const ltrim(docstring const & a, char const * p)
1059 LASSERT(p, return a);
1060 if (a.empty() || !*p)
1062 size_t l = a.find_first_not_of(from_ascii(p));
1063 if (l == docstring::npos)
1065 return a.substr(l, docstring::npos);
1070 template<typename String, typename Char> inline
1071 String const doSplit(String const & a, String & piece, Char delim)
1074 size_t i = a.find(delim);
1075 if (i == a.length() - 1) {
1076 piece = a.substr(0, i);
1077 } else if (i == 0) {
1079 tmp = a.substr(i + 1);
1080 } else if (i != String::npos) {
1081 piece = a.substr(0, i);
1082 tmp = a.substr(i + 1);
1090 // FIXME: why is this specialization needed?
1091 template<typename Char> inline
1092 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
1095 size_t i = a.find(delim);
1096 if (i == a.length() - 1) {
1097 piece = a.substr(0, i);
1098 } else if (i == 0) {
1100 tmp = a.substr(i + 1);
1101 } else if (i != docstring::npos) {
1102 piece = a.substr(0, i);
1103 tmp = a.substr(i + 1);
1113 string const split(string const & a, string & piece, char delim)
1115 return doSplit(a, piece, delim);
1119 docstring const split(docstring const & a, docstring & piece, char_type delim)
1121 return doSplit(a, piece, delim);
1125 string const split(string const & a, char delim)
1128 size_t i = a.find(delim);
1129 if (i != string::npos) // found delim
1130 tmp = a.substr(i + 1);
1136 string const rsplit(string const & a, string & piece, char delim)
1139 size_t i = a.rfind(delim);
1140 if (i != string::npos) { // delimiter was found
1141 piece = a.substr(0, i);
1142 tmp = a.substr(i + 1);
1143 } else { // delimiter was not found
1150 docstring const rsplit(docstring const & a, docstring & piece, char_type delim)
1153 size_t i = a.rfind(delim);
1154 if (i != string::npos) { // delimiter was found
1155 piece = a.substr(0, i);
1156 tmp = a.substr(i + 1);
1157 } else { // delimiter was not found
1164 docstring const rsplit(docstring const & a, char_type delim)
1167 size_t i = a.rfind(delim);
1168 if (i != string::npos)
1169 tmp = a.substr(i + 1);
1174 docstring const escape(docstring const & lab)
1176 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
1177 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1179 for (size_t i = 0; i < lab.length(); ++i) {
1180 char_type c = lab[i];
1181 if (c >= 128 || c == '=' || c == '%' || c == '#' || c == '$'
1182 || c == '}' || c == '{' || c == ']' || c == '[' || c == '&') {
1183 // Although char_type is a 32 bit type we know that
1184 // UCS4 occupies only 21 bits, so we don't need to
1185 // encode bigger values. Test for 2^24 because we
1186 // can encode that with the 6 hex digits that are
1187 // needed for 21 bits anyway.
1188 LASSERT(c < (1 << 24), continue);
1190 enc += hexdigit[(c>>20) & 15];
1191 enc += hexdigit[(c>>16) & 15];
1192 enc += hexdigit[(c>>12) & 15];
1193 enc += hexdigit[(c>> 8) & 15];
1194 enc += hexdigit[(c>> 4) & 15];
1195 enc += hexdigit[ c & 15];
1204 bool truncateWithEllipsis(docstring & str, size_t const len)
1206 if (str.size() <= len)
1210 str[len - 1] = 0x2026;// HORIZONTAL ELLIPSIS
1217 // this doesn't check whether str is empty, so do that first.
1218 vector<docstring> wrapToVec(docstring const & str, int ind,
1221 docstring s = trim(str);
1223 return vector<docstring>();
1227 indent.insert(0, -ind, ' ');
1230 s.insert(0, ind, ' ');
1232 vector<docstring> retval;
1233 while (s.size() > width) {
1234 // find the last space within the first 'width' chars
1235 size_t const i = s.find_last_of(' ', width - 1);
1236 if (i == docstring::npos || i <= size_t(ind)) {
1238 truncateWithEllipsis(s, width);
1241 retval.push_back(s.substr(0, i));
1242 s = indent + s.substr(i);
1243 ind = indent.size();
1246 retval.push_back(s);
1253 docstring wrap(docstring const & str, int const ind, size_t const width)
1255 docstring s = trim(str);
1259 vector<docstring> const svec = wrapToVec(str, ind, width);
1260 return getStringFromVector(svec, from_ascii("\n"));
1264 docstring wrapParas(docstring const & str, int const indent,
1265 size_t const width, size_t const maxlines)
1270 vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1271 vector<docstring> retval;
1273 vector<docstring>::const_iterator it = pars.begin();
1274 vector<docstring>::const_iterator const en = pars.end();
1275 for (; it != en; ++it) {
1276 vector<docstring> tmp = wrapToVec(*it, indent, width);
1277 size_t const nlines = tmp.size();
1280 size_t const curlines = retval.size();
1281 if (maxlines > 0 && curlines + nlines > maxlines) {
1282 tmp.resize(maxlines - curlines);
1283 docstring last = tmp.back();
1284 size_t const lsize = last.size();
1285 if (lsize > width - 1) {
1286 size_t const i = last.find_last_of(' ', width - 1);
1287 if (i == docstring::npos || i <= size_t(indent))
1289 truncateWithEllipsis(last, lsize);
1291 truncateWithEllipsis(last, i);
1293 last.push_back(0x2026);//HORIZONTAL ELLIPSIS
1295 tmp.push_back(last);
1297 retval.insert(retval.end(), tmp.begin(), tmp.end());
1298 if (maxlines > 0 && retval.size() >= maxlines)
1301 return getStringFromVector(retval, from_ascii("\n"));
1307 template<typename String> vector<String> const
1308 getVectorFromStringT(String const & str, String const & delim, bool keepempty)
1310 // Lars would like this code to go, but for now his replacement (below)
1311 // doesn't fullfil the same function. I have, therefore, reactivated the
1312 // old code for now. Angus 11 Nov 2002.
1317 String keys = rtrim(str);
1319 size_t const idx = keys.find(delim);
1320 if (idx == String::npos) {
1321 vec.push_back(ltrim(keys));
1324 String const key = trim(keys.substr(0, idx));
1325 if (!key.empty() || keepempty)
1327 size_t const start = idx + delim.size();
1328 keys = keys.substr(start);
1332 typedef boost::char_separator<typename String::value_type> Separator;
1333 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1334 Separator sep(delim.c_str());
1335 Tokenizer tokens(str, sep);
1336 return vector<String>(tokens.begin(), tokens.end());
1341 template<typename String> const String
1342 getStringFromVector(vector<String> const & vec, String const & delim)
1345 typename vector<String>::const_iterator it = vec.begin();
1346 typename vector<String>::const_iterator en = vec.end();
1347 for (; it != en; ++it) {
1348 String item = trim(*it);
1361 vector<string> const getVectorFromString(string const & str,
1362 string const & delim,
1365 return getVectorFromStringT<string>(str, delim, keepempty);
1369 vector<docstring> const getVectorFromString(docstring const & str,
1370 docstring const & delim,
1373 return getVectorFromStringT<docstring>(str, delim, keepempty);
1377 string const getStringFromVector(vector<string> const & vec,
1378 string const & delim)
1380 return getStringFromVector<string>(vec, delim);
1384 docstring const getStringFromVector(vector<docstring> const & vec,
1385 docstring const & delim)
1387 return getStringFromVector<docstring>(vec, delim);
1391 int findToken(char const * const str[], string const & search_token)
1395 while (str[i][0] && str[i] != search_token)
1403 std::string formatFPNumber(double x)
1405 // Need manual tweaking, QString::number(x, 'f', 16) does not work either
1408 // Prevent outputs of 23.4200000000000017 but output small numbers
1409 // with at least 6 significant digits.
1410 double const logarithm = log10(fabs(x));
1411 os << std::setprecision(max(6 - iround(logarithm), 0)) << x;
1412 string result = os.str();
1413 if (result.find('.') != string::npos) {
1414 result = rtrim(result, "0");
1415 if (result[result.length()-1] == '.')
1416 result = rtrim(result, ".");
1423 docstring bformat(docstring const & fmt, int arg1)
1425 LATTEST(contains(fmt, from_ascii("%1$d")));
1426 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1427 return subst(str, from_ascii("%%"), from_ascii("%"));
1432 docstring bformat(docstring const & fmt, long arg1)
1434 LATTEST(contains(fmt, from_ascii("%1$d")));
1435 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1436 return subst(str, from_ascii("%%"), from_ascii("%"));
1440 #ifdef LYX_USE_LONG_LONG
1442 docstring bformat(docstring const & fmt, long 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("%"));
1452 docstring bformat(docstring const & fmt, unsigned int arg1)
1454 LATTEST(contains(fmt, from_ascii("%1$d")));
1455 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1456 return subst(str, from_ascii("%%"), from_ascii("%"));
1461 docstring bformat(docstring const & fmt, docstring arg1)
1463 LATTEST(contains(fmt, from_ascii("%1$s")));
1464 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1465 return subst(str, from_ascii("%%"), from_ascii("%"));
1470 docstring bformat(docstring const & fmt, char * arg1)
1472 LATTEST(contains(fmt, from_ascii("%1$s")));
1473 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1474 return subst(str, from_ascii("%%"), from_ascii("%"));
1479 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1481 LATTEST(contains(fmt, from_ascii("%1$s")));
1482 LATTEST(contains(fmt, from_ascii("%2$s")));
1483 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1484 str = subst(str, from_ascii("%2$s"), arg2);
1485 return subst(str, from_ascii("%%"), from_ascii("%"));
1490 docstring bformat(docstring const & fmt, docstring arg1, int arg2)
1492 LATTEST(contains(fmt, from_ascii("%1$s")));
1493 LATTEST(contains(fmt, from_ascii("%2$d")));
1494 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1495 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1496 return subst(str, from_ascii("%%"), from_ascii("%"));
1501 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1503 LATTEST(contains(fmt, from_ascii("%1$s")));
1504 LATTEST(contains(fmt, from_ascii("%2$s")));
1505 docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1506 str = subst(str, from_ascii("%2$s"), arg2);
1507 return subst(str, from_ascii("%%"), from_ascii("%"));
1512 docstring bformat(docstring const & fmt, int arg1, int arg2)
1514 LATTEST(contains(fmt, from_ascii("%1$d")));
1515 LATTEST(contains(fmt, from_ascii("%2$d")));
1516 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1517 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1518 return subst(str, from_ascii("%%"), from_ascii("%"));
1523 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1525 LATTEST(contains(fmt, from_ascii("%1$s")));
1526 LATTEST(contains(fmt, from_ascii("%2$s")));
1527 LATTEST(contains(fmt, from_ascii("%3$s")));
1528 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1529 str = subst(str, from_ascii("%2$s"), arg2);
1530 str = subst(str, from_ascii("%3$s"), arg3);
1531 return subst(str, from_ascii("%%"), from_ascii("%"));
1536 docstring bformat(docstring const & fmt,
1537 docstring arg1, docstring arg2, docstring arg3, docstring 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