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/gettext.h"
19 #include "support/qstring_helpers.h"
20 #include "support/textutils.h"
22 #include "support/lassert.h"
34 // Using this allows us to have docstring default arguments in headers
35 // without #include "support/docstring" there.
36 docstring const & empty_docstring()
42 // Using this allows us to have string default arguments in headers
43 // without #include <string>
44 string const & empty_string()
52 * Convert a QChar into a UCS4 character.
53 * This is a hack (it does only make sense for the common part of the UCS4
54 * and UTF16 encodings) and should not be used.
55 * This does only exist because of performance reasons (a real conversion
56 * using iconv is too slow on windows).
58 inline char_type qchar_to_ucs4(QChar const & qchar)
60 LASSERT(is_utf16(static_cast<char_type>(qchar.unicode())), /**/);
61 return static_cast<char_type>(qchar.unicode());
65 * Convert a UCS4 character into a QChar.
66 * This is a hack (it does only make sense for the common part of the UCS4
67 * and UTF16 encodings) and should not be used.
68 * This does only exist because of performance reasons (a real conversion
69 * using iconv is too slow on windows).
71 inline QChar const ucs4_to_qchar(char_type const ucs4)
73 LASSERT(is_utf16(ucs4), /**/);
74 return QChar(static_cast<unsigned short>(ucs4));
77 /// Maximum valid UCS4 code point
78 char_type const ucs4_max = 0x10ffff;
82 bool isLetterChar(char_type c)
86 // outside the UCS4 range
88 // assume that all non-utf16 characters are letters
91 return ucs4_to_qchar(c).isLetter();
95 bool isLower(char_type c)
99 return ucs4_to_qchar(c).isLower();
103 bool isAlphaASCII(char_type c)
105 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
109 bool isPrintable(char_type c)
113 // outside the UCS4 range
115 // assume that all non-utf16 characters are printable
118 // Not yet recognized by QChar::isPrint()
119 // See https://bugreports.qt-project.org/browse/QTBUG-12144
120 // LATIN CAPITAL LETTER SHARP S
121 else if (c == 0x1e9e)
123 return ucs4_to_qchar(c).isPrint();
127 bool isPrintableNonspace(char_type c)
131 // outside the UCS4 range
133 // assume that all non-utf16 characters are printable and
137 QChar const qc = ucs4_to_qchar(c);
138 return qc.isPrint() && !qc.isSpace();
142 bool isSpace(char_type c)
145 // assume that no non-utf16 character is a space
146 // c outside the UCS4 range is catched as well
149 QChar const qc = ucs4_to_qchar(c);
154 bool isNumber(char_type c)
157 // assume that no non-utf16 character is a numeral
158 // c outside the UCS4 range is catched as well
160 return ucs4_to_qchar(c).isNumber();
164 bool isDigitASCII(char_type c)
166 return '0' <= c && c <= '9';
170 bool isAlnumASCII(char_type c)
172 return isAlphaASCII(c) || isDigitASCII(c);
178 int compare_no_case(docstring const & s, docstring const & s2)
180 docstring::const_iterator p = s.begin();
181 docstring::const_iterator p2 = s2.begin();
183 while (p != s.end() && p2 != s2.end()) {
184 char_type const lc1 = lowercase(*p);
185 char_type const lc2 = lowercase(*p2);
187 return (lc1 < lc2) ? -1 : 1;
192 if (s.size() == s2.size())
194 if (s.size() < s2.size())
202 template<typename Char>
203 Char ascii_tolower(Char c) {
204 if (c >= 'A' && c <= 'Z')
205 return c - 'A' + 'a';
212 int compare_ascii_no_case(string const & s, string const & s2)
214 string::const_iterator p = s.begin();
215 string::const_iterator p2 = s2.begin();
217 while (p != s.end() && p2 != s2.end()) {
218 int const lc1 = ascii_tolower(*p);
219 int const lc2 = ascii_tolower(*p2);
221 return (lc1 < lc2) ? -1 : 1;
226 if (s.size() == s2.size())
228 if (s.size() < s2.size())
234 int compare_ascii_no_case(docstring const & s, docstring const & s2)
236 docstring::const_iterator p = s.begin();
237 docstring::const_iterator p2 = s2.begin();
239 while (p != s.end() && p2 != s2.end()) {
240 char_type const lc1 = ascii_tolower(*p);
241 char_type const lc2 = ascii_tolower(*p2);
243 return (lc1 < lc2) ? -1 : 1;
248 if (s.size() == s2.size())
250 if (s.size() < s2.size())
256 bool isStrInt(string const & str)
261 // Remove leading and trailing white space chars.
262 string const tmpstr = trim(str);
266 string::const_iterator cit = tmpstr.begin();
270 string::const_iterator end = tmpstr.end();
271 for (; cit != end; ++cit)
272 if (!isDigitASCII(*cit))
279 bool isStrUnsignedInt(string const & str)
284 // Remove leading and trailing white space chars.
285 string const tmpstr = trim(str);
289 string::const_iterator cit = tmpstr.begin();
290 string::const_iterator end = tmpstr.end();
291 for (; cit != end; ++cit)
292 if (!isDigitASCII(*cit))
299 bool isStrDbl(string const & str)
304 // Remove leading and trailing white space chars.
305 string const tmpstr = trim(str);
308 // if (tmpstr.count('.') > 1) return false;
310 string::const_iterator cit = tmpstr.begin();
311 bool found_dot = false;
314 string::const_iterator end = tmpstr.end();
315 for (; cit != end; ++cit) {
316 if (!isDigitASCII(*cit) && *cit != '.')
328 bool hasDigitASCII(docstring const & str)
330 docstring::const_iterator cit = str.begin();
331 docstring::const_iterator const end = str.end();
332 for (; cit != end; ++cit)
333 if (isDigitASCII(*cit))
339 bool isHexChar(char_type c)
351 c == 'a' || c == 'A' ||
352 c == 'b' || c == 'B' ||
353 c == 'c' || c == 'C' ||
354 c == 'd' || c == 'D' ||
355 c == 'e' || c == 'E' ||
356 c == 'f' || c == 'F';
360 bool isHex(docstring const & str)
364 if (str.length() > 2 && str[0] == '0' &&
365 (str[1] == 'x' || str[1] == 'X'))
368 int const len = str.length();
370 for (; index < len; ++index) {
371 if (!isHexChar(str[index]))
378 int hexToInt(docstring const & str)
380 string s = to_ascii(str);
382 sscanf(s.c_str(), "%x", &h);
387 bool isAscii(docstring const & str)
389 int const len = str.length();
390 for (int i = 0; i < len; ++i)
397 bool isAscii(string const & str)
399 int const len = str.length();
400 for (int i = 0; i < len; ++i)
401 if (static_cast<unsigned char>(str[i]) >= 0x80)
407 char lowercase(char c)
409 LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
410 return char(tolower(c));
414 char uppercase(char c)
416 LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
417 return char(toupper(c));
421 char_type lowercase(char_type c)
424 // We don't know how to lowercase a non-utf16 char
426 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
430 char_type uppercase(char_type c)
433 // We don't know how to uppercase a non-utf16 char
435 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
439 bool isLowerCase(char_type ch) {
440 return lowercase(ch) == ch;
444 bool isUpperCase(char_type ch) {
445 return uppercase(ch) == ch;
451 // since we cannot use tolower and toupper directly in the
452 // calls to transform yet, we use these helper clases. (Lgb)
454 struct local_lowercase {
455 char_type operator()(char_type c) const {
460 struct local_uppercase {
461 char_type operator()(char_type c) const {
466 template<typename Char> struct local_ascii_lowercase {
467 Char operator()(Char c) const { return ascii_tolower(c); }
470 } // end of anon namespace
473 docstring const lowercase(docstring const & a)
476 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
481 docstring const uppercase(docstring const & a)
484 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
489 string const ascii_lowercase(string const & a)
492 transform(tmp.begin(), tmp.end(), tmp.begin(),
493 local_ascii_lowercase<char>());
498 docstring const ascii_lowercase(docstring const & a)
501 transform(tmp.begin(), tmp.end(), tmp.begin(),
502 local_ascii_lowercase<char_type>());
507 char_type superscript(char_type c)
510 case '2': return 0x00b2;
511 case '3': return 0x00b3;
512 case '1': return 0x00b9;
513 case '0': return 0x2070;
514 case 'i': return 0x2071;
515 case '4': return 0x2074;
516 case '5': return 0x2075;
517 case '6': return 0x2076;
518 case '7': return 0x2077;
519 case '8': return 0x2078;
520 case '9': return 0x2079;
521 case '+': return 0x207a;
522 case '-': return 0x207b;
523 case '=': return 0x207c;
524 case '(': return 0x207d;
525 case ')': return 0x207e;
526 case 'n': return 0x207f;
527 case 'h': return 0x02b0;
528 case 0x0266: return 0x02b1; // LATIN SMALL LETTER H WITH HOOK
529 case 'j': return 0x02b2;
530 case 'r': return 0x02b3;
531 case 0x0279: return 0x02b4; // LATIN SMALL LETTER TURNED R
532 case 0x027b: return 0x02b5; // LATIN SMALL LETTER TURNED R WITH HOOK
533 case 0x0281: return 0x02b6; // LATIN SMALL LETTER CAPITAL INVERTED R
534 case 'w': return 0x02b7;
535 case 'y': return 0x02b8;
536 // case 0x0294: return 0x02c0; // LATIN LETTER GLOTTAL STOP)
537 // case 0x0295: return 0x02c1; // LATIN LETTER PHARYNGEAL VOICED FRICATIVE
538 // (= LATIN LETTER REVERSED GLOTTAL STOP)
539 case 'l': return 0x02e1;
540 case 's': return 0x02e2;
541 case 'x': return 0x02e3;
542 // case 0x0295: return 0x02e4; // LATIN SMALL LETTER REVERSED GLOTTAL STOP
543 case 'A': return 0x1d2c;
544 case 0x00c6: return 0x1d2d; // LATIN CAPITAL LETTER AE
545 case 'B': return 0x1d2e;
546 case 'D': return 0x1d30;
547 case 'E': return 0x1d31;
548 case 'G': return 0x1d33;
549 case 'H': return 0x1d34;
550 case 'I': return 0x1d35;
551 case 'J': return 0x1d36;
552 case 'K': return 0x1d37;
553 case 'L': return 0x1d38;
554 case 'M': return 0x1d39;
555 case 'N': return 0x1d3a;
556 case 'O': return 0x1d3c;
557 case 'P': return 0x1d3e;
558 case 'R': return 0x1d3f;
559 case 'T': return 0x1d40;
560 case 'U': return 0x1d41;
561 case 'W': return 0x1d42;
562 case 'a': return 0x1d43;
563 case 0x0250: return 0x1d44; // LATIN SMALL LETTER TURNED A
564 case 0x0251: return 0x1d45; // LATIN SMALL LETTER ALPHA
565 case 'b': return 0x1d47;
566 case 'd': return 0x1d48;
567 case 'e': return 0x1d49;
568 case 0x0259: return 0x1d4a; // LATIN SMALL LETTER SCHWA
569 case 0x025b: return 0x1d4b; // LATIN SMALL LETTER OPEN E
570 case 0x1d08: return 0x1d4c; // LATIN SMALL LETTER TURNED OPEN E
571 case 'g': return 0x1d4d;
572 case 0x1d09: return 0x1d4e; // LATIN SMALL LETTER TURNED I
573 case 'k': return 0x1d4f;
574 case 'm': return 0x1d50;
575 case 0x014b: return 0x1d51; // LATIN SMALL LETTER ENG
576 case 'o': return 0x1d52;
577 case 0x0254: return 0x1d53; // LATIN SMALL LETTER OPEN O
578 case 0x1d16: return 0x1d54; // LATIN SMALL LETTER TOP HALF O
579 case 0x1d17: return 0x1d55; // LATIN SMALL LETTER BOTTOM HALF O
580 case 'p': return 0x1d56;
581 case 't': return 0x1d57;
582 case 'u': return 0x1d58;
583 case 0x1d1d: return 0x1d59; // LATIN SMALL LETTER SIDEWAYS U
584 case 0x1d1f: return 0x1d5a; // LATIN SMALL LETTER SIDEWAYS TURNED M
585 case 'v': return 0x1d5b;
586 case 0x03b2: return 0x1d5d; // GREEK SMALL LETTER BETA
587 case 0x03b3: return 0x1d5e; // GREEK SMALL LETTER GAMMA
588 case 0x03b4: return 0x1d5f; // GREEK SMALL LETTER DELTA
589 case 0x03c6: return 0x1d60; // GREEK SMALL LETTER PHI
590 case 0x03c7: return 0x1d61; // GREEK SMALL LETTER CHI
596 char_type subscript(char_type c)
599 case 'i': return 0x1d62;
600 case 'r': return 0x1d63;
601 case 'u': return 0x1d64;
602 case 'v': return 0x1d65;
603 case 0x03b2: return 0x1d66; // GREEK SMALL LETTER BETA
604 case 0x03b3: return 0x1d67; // GREEK SMALL LETTER GAMMA
605 case 0x03c1: return 0x1d68; // GREEK SMALL LETTER RHO
606 case 0x03c6: return 0x1d69; // GREEK SMALL LETTER PHI
607 case 0x03c7: return 0x1d6a; // GREEK SMALL LETTER CHI
608 case '0': return 0x2080;
609 case '1': return 0x2081;
610 case '2': return 0x2082;
611 case '3': return 0x2083;
612 case '4': return 0x2084;
613 case '5': return 0x2085;
614 case '6': return 0x2086;
615 case '7': return 0x2087;
616 case '8': return 0x2088;
617 case '9': return 0x2089;
618 case '+': return 0x208a;
619 case '-': return 0x208b;
620 case '=': return 0x208c;
621 case '(': return 0x208d;
622 case ')': return 0x208e;
623 case 'a': return 0x2090;
624 case 'e': return 0x2091;
625 case 'o': return 0x2092;
626 case 'x': return 0x2093;
627 case 0x0259: return 0x2093; // LATIN SMALL LETTER SCHWA
633 bool prefixIs(docstring const & a, char_type c)
641 bool prefixIs(string const & a, string const & pre)
643 size_t const prelen = pre.length();
644 size_t const alen = a.length();
645 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
649 bool prefixIs(docstring const & a, docstring const & pre)
651 size_t const prelen = pre.length();
652 size_t const alen = a.length();
653 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
657 bool suffixIs(string const & a, char c)
661 return a[a.length() - 1] == c;
665 bool suffixIs(docstring const & a, char_type c)
669 return a[a.length() - 1] == c;
673 bool suffixIs(string const & a, string const & suf)
675 size_t const suflen = suf.length();
676 size_t const alen = a.length();
677 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
681 bool suffixIs(docstring const & a, docstring const & suf)
683 size_t const suflen = suf.length();
684 size_t const alen = a.length();
685 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
689 bool containsOnly(string const & s, string const & cset)
691 return s.find_first_not_of(cset) == string::npos;
695 // ale970405+lasgoutt-970425
696 // rewritten to use new string (Lgb)
697 string const token(string const & a, char delim, int n)
705 // Find delimiter or end of string
707 if ((i = a.find(delim, i)) == string::npos)
713 // i is now the n'th delim (or string::npos)
714 if (i == string::npos)
717 k = a.find(delim, i);
718 // k is now the n'th + 1 delim (or string::npos)
720 return a.substr(i, k - i);
724 docstring const token(docstring const & a, char_type delim, int n)
732 // Find delimiter or end of string
734 if ((i = a.find(delim, i)) == docstring::npos)
740 // i is now the n'th delim (or string::npos)
741 if (i == docstring::npos)
744 k = a.find(delim, i);
745 // k is now the n'th + 1 delim (or string::npos)
747 return a.substr(i, k - i);
751 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
752 // rewritten to use new string (Lgb)
753 int tokenPos(string const & a, char delim, string const & tok)
759 while (!str.empty()) {
760 str = split(str, tmptok, delim);
769 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
770 // rewritten to use new string (Lgb)
771 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
777 while (!str.empty()) {
778 str = split(str, tmptok, delim);
789 /// Substitute all \a oldchar with \a newchar
790 template<typename Ch> inline
791 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
792 Ch oldchar, Ch newchar)
794 typedef basic_string<Ch> String;
796 typename String::iterator lit = tmp.begin();
797 typename String::iterator end = tmp.end();
798 for (; lit != end; ++lit)
799 if ((*lit) == oldchar)
805 /// Substitute all \a oldchar with \a newchar
806 docstring const subst_char(docstring const & a,
807 docstring::value_type oldchar, docstring::value_type newchar)
810 docstring::iterator lit = tmp.begin();
811 docstring::iterator end = tmp.end();
812 for (; lit != end; ++lit)
813 if ((*lit) == oldchar)
819 /// substitutes all instances of \a oldstr with \a newstr
820 template<typename String> inline
821 String const subst_string(String const & a,
822 String const & oldstr, String const & newstr)
824 LASSERT(!oldstr.empty(), /**/);
827 size_t const olen = oldstr.length();
828 while ((i = lstr.find(oldstr, i)) != string::npos) {
829 lstr.replace(i, olen, newstr);
830 i += newstr.length(); // We need to be sure that we dont
831 // use the same i over and over again.
837 docstring const subst_string(docstring const & a,
838 docstring const & oldstr, docstring const & newstr)
840 LASSERT(!oldstr.empty(), /**/);
843 size_t const olen = oldstr.length();
844 while ((i = lstr.find(oldstr, i)) != string::npos) {
845 lstr.replace(i, olen, newstr);
846 i += newstr.length(); // We need to be sure that we dont
847 // use the same i over and over again.
855 string const subst(string const & a, char oldchar, char newchar)
857 return subst_char(a, oldchar, newchar);
861 docstring const subst(docstring const & a,
862 char_type oldchar, char_type newchar)
864 return subst_char(a, oldchar, newchar);
868 string const subst(string const & a,
869 string const & oldstr, string const & newstr)
871 return subst_string(a, oldstr, newstr);
875 docstring const subst(docstring const & a,
876 docstring const & oldstr, docstring const & newstr)
878 return subst_string(a, oldstr, newstr);
882 /// Count all occurences of char \a chr inside \a str
883 int count_char(docstring const & str, docstring::value_type chr)
886 docstring::const_iterator lit = str.begin();
887 docstring::const_iterator end = str.end();
888 for (; lit != end; ++lit)
895 docstring const trim(docstring const & a, char const * p)
899 if (a.empty() || !*p)
902 docstring s = from_ascii(p);
903 size_t r = a.find_last_not_of(s);
904 size_t l = a.find_first_not_of(s);
906 // Is this the minimal test? (lgb)
907 if (r == docstring::npos && l == docstring::npos)
910 return a.substr(l, r - l + 1);
914 string const trim(string const & a, char const * p)
918 if (a.empty() || !*p)
921 size_t r = a.find_last_not_of(p);
922 size_t l = a.find_first_not_of(p);
924 // Is this the minimal test? (lgb)
925 if (r == string::npos && l == string::npos)
928 return a.substr(l, r - l + 1);
932 string const rtrim(string const & a, char const * p)
936 if (a.empty() || !*p)
939 size_t r = a.find_last_not_of(p);
941 // Is this test really needed? (Lgb)
942 if (r == string::npos)
945 return a.substr(0, r + 1);
949 docstring const rtrim(docstring const & a, char const * p)
953 if (a.empty() || !*p)
956 size_t r = a.find_last_not_of(from_ascii(p));
958 // Is this test really needed? (Lgb)
959 if (r == docstring::npos)
962 return a.substr(0, r + 1);
966 string const ltrim(string const & a, char const * p)
969 if (a.empty() || !*p)
971 size_t l = a.find_first_not_of(p);
972 if (l == string::npos)
974 return a.substr(l, string::npos);
978 docstring const ltrim(docstring const & a, char const * p)
981 if (a.empty() || !*p)
983 size_t l = a.find_first_not_of(from_ascii(p));
984 if (l == docstring::npos)
986 return a.substr(l, docstring::npos);
991 template<typename String, typename Char> inline
992 String const doSplit(String const & a, String & piece, Char delim)
995 size_t i = a.find(delim);
996 if (i == a.length() - 1) {
997 piece = a.substr(0, i);
998 } else if (i != String::npos) {
999 piece = a.substr(0, i);
1000 tmp = a.substr(i + 1);
1001 } else if (i == 0) {
1003 tmp = a.substr(i + 1);
1010 template<typename Char> inline
1011 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
1014 size_t i = a.find(delim);
1015 if (i == a.length() - 1) {
1016 piece = a.substr(0, i);
1017 } else if (i != docstring::npos) {
1018 piece = a.substr(0, i);
1019 tmp = a.substr(i + 1);
1020 } else if (i == 0) {
1022 tmp = a.substr(i + 1);
1032 string const split(string const & a, string & piece, char delim)
1034 return doSplit(a, piece, delim);
1038 docstring const split(docstring const & a, docstring & piece, char_type delim)
1040 return doSplit(a, piece, delim);
1044 string const split(string const & a, char delim)
1047 size_t i = a.find(delim);
1048 if (i != string::npos) // found delim
1049 tmp = a.substr(i + 1);
1055 string const rsplit(string const & a, string & piece, char delim)
1058 size_t i = a.rfind(delim);
1059 if (i != string::npos) { // delimiter was found
1060 piece = a.substr(0, i);
1061 tmp = a.substr(i + 1);
1062 } else { // delimiter was not found
1069 docstring const rsplit(docstring const & a, char_type delim)
1072 size_t i = a.rfind(delim);
1073 if (i != string::npos)
1074 tmp = a.substr(i + 1);
1079 docstring const escape(docstring const & lab)
1081 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
1082 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1084 for (size_t i = 0; i < lab.length(); ++i) {
1085 char_type c = lab[i];
1086 if (c >= 128 || c == '=' || c == '%' || c == '#' || c == '$'
1087 || c == '}' || c == '{' || c == ']' || c == '[' || c == '&') {
1088 // Although char_type is a 32 bit type we know that
1089 // UCS4 occupies only 21 bits, so we don't need to
1090 // encode bigger values. Test for 2^24 because we
1091 // can encode that with the 6 hex digits that are
1092 // needed for 21 bits anyway.
1093 LASSERT(c < (1 << 24), /**/);
1095 enc += hexdigit[(c>>20) & 15];
1096 enc += hexdigit[(c>>16) & 15];
1097 enc += hexdigit[(c>>12) & 15];
1098 enc += hexdigit[(c>> 8) & 15];
1099 enc += hexdigit[(c>> 4) & 15];
1100 enc += hexdigit[ c & 15];
1111 // this doesn't check whether str is empty, so do that first.
1112 vector<docstring> wrapToVec(docstring const & str, int ind,
1115 docstring s = trim(str);
1117 return vector<docstring>();
1121 indent.insert(0, -ind, ' ');
1124 s.insert(0, ind, ' ');
1126 vector<docstring> retval;
1127 while (s.size() > width) {
1128 // find the last space within the first 'width' chars
1129 size_t const i = s.find_last_of(' ', width - 1);
1130 if (i == docstring::npos || i <= size_t(ind)) {
1132 s = s.substr(0, width - 3) + "...";
1135 retval.push_back(s.substr(0, i));
1136 s = indent + s.substr(i);
1137 ind = indent.size();
1140 retval.push_back(s);
1147 docstring wrap(docstring const & str, int const ind, size_t const width)
1149 docstring s = trim(str);
1153 vector<docstring> const svec = wrapToVec(str, ind, width);
1154 return getStringFromVector(svec, from_ascii("\n"));
1158 docstring wrapParas(docstring const & str, int const indent,
1159 size_t const width, size_t const maxlines)
1161 docstring const dots = from_ascii("...");
1165 vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1166 vector<docstring> retval;
1168 vector<docstring>::const_iterator it = pars.begin();
1169 vector<docstring>::const_iterator const en = pars.end();
1170 for (; it != en; ++it) {
1171 vector<docstring> tmp = wrapToVec(*it, indent, width);
1172 size_t const nlines = tmp.size();
1175 size_t const curlines = retval.size();
1176 if (maxlines > 0 && curlines + nlines > maxlines) {
1177 tmp.resize(maxlines - curlines);
1178 docstring last = tmp.back();
1179 size_t const lsize = last.size();
1180 if (lsize > width - 3) {
1181 size_t const i = last.find_last_of(' ', width - 3);
1182 if (i == docstring::npos || i <= size_t(indent))
1184 last = last.substr(0, lsize - 3) + dots;
1186 last = last.substr(0, i) + dots;
1190 tmp.push_back(last);
1192 retval.insert(retval.end(), tmp.begin(), tmp.end());
1193 if (maxlines > 0 && retval.size() >= maxlines)
1196 return getStringFromVector(retval, from_ascii("\n"));
1202 template<typename String> vector<String> const
1203 getVectorFromStringT(String const & str, String const & delim, bool keepempty)
1205 // Lars would like this code to go, but for now his replacement (below)
1206 // doesn't fullfil the same function. I have, therefore, reactivated the
1207 // old code for now. Angus 11 Nov 2002.
1212 String keys = rtrim(str);
1214 size_t const idx = keys.find(delim);
1215 if (idx == String::npos) {
1216 vec.push_back(ltrim(keys));
1219 String const key = trim(keys.substr(0, idx));
1220 if (!key.empty() || keepempty)
1222 size_t const start = idx + delim.size();
1223 keys = keys.substr(start);
1227 typedef boost::char_separator<typename String::value_type> Separator;
1228 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1229 Separator sep(delim.c_str());
1230 Tokenizer tokens(str, sep);
1231 return vector<String>(tokens.begin(), tokens.end());
1236 template<typename String> const String
1237 getStringFromVector(vector<String> const & vec, String const & delim)
1240 typename vector<String>::const_iterator it = vec.begin();
1241 typename vector<String>::const_iterator en = vec.end();
1242 for (; it != en; ++it) {
1243 String item = trim(*it);
1256 vector<string> const getVectorFromString(string const & str,
1257 string const & delim,
1260 return getVectorFromStringT<string>(str, delim, keepempty);
1264 vector<docstring> const getVectorFromString(docstring const & str,
1265 docstring const & delim,
1268 return getVectorFromStringT<docstring>(str, delim, keepempty);
1272 string const getStringFromVector(vector<string> const & vec,
1273 string const & delim)
1275 return getStringFromVector<string>(vec, delim);
1279 docstring const getStringFromVector(vector<docstring> const & vec,
1280 docstring const & delim)
1282 return getStringFromVector<docstring>(vec, delim);
1286 int findToken(char const * const str[], string const & search_token)
1290 while (str[i][0] && str[i] != search_token)
1299 docstring bformat(docstring const & fmt, int arg1)
1301 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1302 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1303 return subst(str, from_ascii("%%"), from_ascii("%"));
1308 docstring bformat(docstring const & fmt, long arg1)
1310 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1311 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1312 return subst(str, from_ascii("%%"), from_ascii("%"));
1317 docstring bformat(docstring const & fmt, unsigned int arg1)
1319 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1320 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1321 return subst(str, from_ascii("%%"), from_ascii("%"));
1326 docstring bformat(docstring const & fmt, docstring arg1)
1328 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1329 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1330 return subst(str, from_ascii("%%"), from_ascii("%"));
1335 docstring bformat(docstring const & fmt, char * arg1)
1337 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1338 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1339 return subst(str, from_ascii("%%"), from_ascii("%"));
1344 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1346 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1347 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1348 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1349 str = subst(str, from_ascii("%2$s"), arg2);
1350 return subst(str, from_ascii("%%"), from_ascii("%"));
1355 docstring bformat(docstring const & fmt, docstring arg1, int arg2)
1357 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1358 LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1359 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1360 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1361 return subst(str, from_ascii("%%"), from_ascii("%"));
1366 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1368 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1369 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1370 docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1371 str = subst(fmt, from_ascii("%2$s"), arg2);
1372 return subst(str, from_ascii("%%"), from_ascii("%"));
1377 docstring bformat(docstring const & fmt, int arg1, int arg2)
1379 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1380 LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1381 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1382 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1383 return subst(str, from_ascii("%%"), from_ascii("%"));
1388 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1390 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1391 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1392 LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1393 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1394 str = subst(str, from_ascii("%2$s"), arg2);
1395 str = subst(str, from_ascii("%3$s"), arg3);
1396 return subst(str, from_ascii("%%"), from_ascii("%"));
1401 docstring bformat(docstring const & fmt,
1402 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1404 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1405 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1406 LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1407 LASSERT(contains(fmt, from_ascii("%4$s")), /**/);
1408 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1409 str = subst(str, from_ascii("%2$s"), arg2);
1410 str = subst(str, from_ascii("%3$s"), arg3);
1411 str = subst(str, from_ascii("%4$s"), arg4);
1412 return subst(str, from_ascii("%%"), from_ascii("%"));
1415 } // namespace support