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/qstring_helpers.h"
20 #include "support/lassert.h"
31 // Using this allows us to have docstring default arguments in headers
32 // without #include "support/docstring" there.
33 docstring const & empty_docstring()
39 // Using this allows us to have string default arguments in headers
40 // without #include <string>
41 string const & empty_string()
49 * Convert a QChar into a UCS4 character.
50 * This is a hack (it does only make sense for the common part of the UCS4
51 * and UTF16 encodings) and should not be used.
52 * This does only exist because of performance reasons (a real conversion
53 * using iconv is too slow on windows).
55 inline char_type qchar_to_ucs4(QChar const & qchar)
57 LASSERT(is_utf16(static_cast<char_type>(qchar.unicode())), /**/);
58 return static_cast<char_type>(qchar.unicode());
62 * Convert a UCS4 character into a QChar.
63 * This is a hack (it does only make sense for the common part of the UCS4
64 * and UTF16 encodings) and should not be used.
65 * This does only exist because of performance reasons (a real conversion
66 * using iconv is too slow on windows).
68 inline QChar const ucs4_to_qchar(char_type const ucs4)
70 LASSERT(is_utf16(ucs4), /**/);
71 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 isLower(char_type c)
96 return ucs4_to_qchar(c).isLower();
100 bool isAlphaASCII(char_type c)
102 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
106 bool isPrintable(char_type c)
110 // outside the UCS4 range
112 // assume that all non-utf16 characters are printable
115 // Not yet recognized by QChar::isPrint()
116 // See https://bugreports.qt-project.org/browse/QTBUG-12144
117 // LATIN CAPITAL LETTER SHARP S
118 else if (c == 0x1e9e)
120 return ucs4_to_qchar(c).isPrint();
124 bool isPrintableNonspace(char_type c)
128 // outside the UCS4 range
130 // assume that all non-utf16 characters are printable and
134 QChar const qc = ucs4_to_qchar(c);
135 return qc.isPrint() && !qc.isSpace();
139 bool isSpace(char_type c)
142 // assume that no non-utf16 character is a space
143 // c outside the UCS4 range is catched as well
146 QChar const qc = ucs4_to_qchar(c);
151 bool isNumber(char_type c)
154 // assume that no non-utf16 character is a numeral
155 // c outside the UCS4 range is catched as well
157 return ucs4_to_qchar(c).isNumber();
161 bool isDigitASCII(char_type c)
163 return '0' <= c && c <= '9';
167 bool isAlnumASCII(char_type c)
169 return isAlphaASCII(c) || isDigitASCII(c);
173 bool isASCII(char_type c)
181 int compare_no_case(docstring const & s, docstring const & s2)
183 docstring::const_iterator p = s.begin();
184 docstring::const_iterator p2 = s2.begin();
186 while (p != s.end() && p2 != s2.end()) {
187 char_type const lc1 = lowercase(*p);
188 char_type const lc2 = lowercase(*p2);
190 return (lc1 < lc2) ? -1 : 1;
195 if (s.size() == s2.size())
197 if (s.size() < s2.size())
205 template<typename Char>
206 Char ascii_tolower(Char c) {
207 if (c >= 'A' && c <= 'Z')
208 return c - 'A' + 'a';
215 int compare_ascii_no_case(string const & s, string const & s2)
217 string::const_iterator p = s.begin();
218 string::const_iterator p2 = s2.begin();
220 while (p != s.end() && p2 != s2.end()) {
221 int const lc1 = ascii_tolower(*p);
222 int const lc2 = ascii_tolower(*p2);
224 return (lc1 < lc2) ? -1 : 1;
229 if (s.size() == s2.size())
231 if (s.size() < s2.size())
237 int compare_ascii_no_case(docstring const & s, docstring const & s2)
239 docstring::const_iterator p = s.begin();
240 docstring::const_iterator p2 = s2.begin();
242 while (p != s.end() && p2 != s2.end()) {
243 char_type const lc1 = ascii_tolower(*p);
244 char_type const lc2 = ascii_tolower(*p2);
246 return (lc1 < lc2) ? -1 : 1;
251 if (s.size() == s2.size())
253 if (s.size() < s2.size())
259 bool isStrInt(string const & str)
264 // Remove leading and trailing white space chars.
265 string const tmpstr = trim(str);
269 string::const_iterator cit = tmpstr.begin();
273 string::const_iterator end = tmpstr.end();
274 for (; cit != end; ++cit)
275 if (!isDigitASCII(*cit))
282 bool isStrUnsignedInt(string const & str)
287 // Remove leading and trailing white space chars.
288 string const tmpstr = trim(str);
292 string::const_iterator cit = tmpstr.begin();
293 string::const_iterator end = tmpstr.end();
294 for (; cit != end; ++cit)
295 if (!isDigitASCII(*cit))
302 bool isStrDbl(string const & str)
307 // Remove leading and trailing white space chars.
308 string const tmpstr = trim(str);
311 // if (tmpstr.count('.') > 1) return false;
313 string::const_iterator cit = tmpstr.begin();
314 bool found_dot = false;
317 string::const_iterator end = tmpstr.end();
318 for (; cit != end; ++cit) {
319 if (!isDigitASCII(*cit) && *cit != '.')
331 bool hasDigitASCII(docstring const & str)
333 docstring::const_iterator cit = str.begin();
334 docstring::const_iterator const end = str.end();
335 for (; cit != end; ++cit)
336 if (isDigitASCII(*cit))
342 bool isHexChar(char_type c)
354 c == 'a' || c == 'A' ||
355 c == 'b' || c == 'B' ||
356 c == 'c' || c == 'C' ||
357 c == 'd' || c == 'D' ||
358 c == 'e' || c == 'E' ||
359 c == 'f' || c == 'F';
363 bool isHex(docstring const & str)
367 if (str.length() > 2 && str[0] == '0' &&
368 (str[1] == 'x' || str[1] == 'X'))
371 int const len = str.length();
373 for (; index < len; ++index) {
374 if (!isHexChar(str[index]))
381 int hexToInt(docstring const & str)
383 string s = to_ascii(str);
385 sscanf(s.c_str(), "%x", &h);
390 bool isAscii(docstring const & str)
392 int const len = str.length();
393 for (int i = 0; i < len; ++i)
400 bool isAscii(string const & str)
402 int const len = str.length();
403 for (int i = 0; i < len; ++i)
404 if (static_cast<unsigned char>(str[i]) >= 0x80)
410 char lowercase(char c)
412 LASSERT(isASCII(c), /**/);
413 return char(tolower(c));
417 char uppercase(char c)
419 LASSERT(isASCII(c), /**/);
420 return char(toupper(c));
424 char_type lowercase(char_type c)
427 // We don't know how to lowercase a non-utf16 char
429 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
433 char_type uppercase(char_type c)
436 // We don't know how to uppercase a non-utf16 char
438 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
442 bool isLowerCase(char_type ch) {
443 return lowercase(ch) == ch;
447 bool isUpperCase(char_type ch) {
448 return uppercase(ch) == ch;
454 // since we cannot use tolower and toupper directly in the
455 // calls to transform yet, we use these helper clases. (Lgb)
457 struct local_lowercase {
458 char_type operator()(char_type c) const {
463 struct local_uppercase {
464 char_type operator()(char_type c) const {
469 template<typename Char> struct local_ascii_lowercase {
470 Char operator()(Char c) const { return ascii_tolower(c); }
473 } // end of anon namespace
476 docstring const lowercase(docstring const & a)
479 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
484 docstring const uppercase(docstring const & a)
487 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
492 string const ascii_lowercase(string const & a)
495 transform(tmp.begin(), tmp.end(), tmp.begin(),
496 local_ascii_lowercase<char>());
501 docstring const ascii_lowercase(docstring const & a)
504 transform(tmp.begin(), tmp.end(), tmp.begin(),
505 local_ascii_lowercase<char_type>());
510 char_type superscript(char_type c)
513 case '2': return 0x00b2;
514 case '3': return 0x00b3;
515 case '1': return 0x00b9;
516 case '0': return 0x2070;
517 case 'i': return 0x2071;
518 case '4': return 0x2074;
519 case '5': return 0x2075;
520 case '6': return 0x2076;
521 case '7': return 0x2077;
522 case '8': return 0x2078;
523 case '9': return 0x2079;
524 case '+': return 0x207a;
525 case '-': return 0x207b;
526 case '=': return 0x207c;
527 case '(': return 0x207d;
528 case ')': return 0x207e;
529 case 'n': return 0x207f;
530 case 'h': return 0x02b0;
531 case 0x0266: return 0x02b1; // LATIN SMALL LETTER H WITH HOOK
532 case 'j': return 0x02b2;
533 case 'r': return 0x02b3;
534 case 0x0279: return 0x02b4; // LATIN SMALL LETTER TURNED R
535 case 0x027b: return 0x02b5; // LATIN SMALL LETTER TURNED R WITH HOOK
536 case 0x0281: return 0x02b6; // LATIN SMALL LETTER CAPITAL INVERTED R
537 case 'w': return 0x02b7;
538 case 'y': return 0x02b8;
539 // case 0x0294: return 0x02c0; // LATIN LETTER GLOTTAL STOP)
540 // case 0x0295: return 0x02c1; // LATIN LETTER PHARYNGEAL VOICED FRICATIVE
541 // (= LATIN LETTER REVERSED GLOTTAL STOP)
542 case 'l': return 0x02e1;
543 case 's': return 0x02e2;
544 case 'x': return 0x02e3;
545 // case 0x0295: return 0x02e4; // LATIN SMALL LETTER REVERSED GLOTTAL STOP
546 case 'A': return 0x1d2c;
547 case 0x00c6: return 0x1d2d; // LATIN CAPITAL LETTER AE
548 case 'B': return 0x1d2e;
549 case 'D': return 0x1d30;
550 case 'E': return 0x1d31;
551 case 'G': return 0x1d33;
552 case 'H': return 0x1d34;
553 case 'I': return 0x1d35;
554 case 'J': return 0x1d36;
555 case 'K': return 0x1d37;
556 case 'L': return 0x1d38;
557 case 'M': return 0x1d39;
558 case 'N': return 0x1d3a;
559 case 'O': return 0x1d3c;
560 case 'P': return 0x1d3e;
561 case 'R': return 0x1d3f;
562 case 'T': return 0x1d40;
563 case 'U': return 0x1d41;
564 case 'W': return 0x1d42;
565 case 'a': return 0x1d43;
566 case 0x0250: return 0x1d44; // LATIN SMALL LETTER TURNED A
567 case 0x0251: return 0x1d45; // LATIN SMALL LETTER ALPHA
568 case 'b': return 0x1d47;
569 case 'd': return 0x1d48;
570 case 'e': return 0x1d49;
571 case 0x0259: return 0x1d4a; // LATIN SMALL LETTER SCHWA
572 case 0x025b: return 0x1d4b; // LATIN SMALL LETTER OPEN E
573 case 0x1d08: return 0x1d4c; // LATIN SMALL LETTER TURNED OPEN E
574 case 'g': return 0x1d4d;
575 case 0x1d09: return 0x1d4e; // LATIN SMALL LETTER TURNED I
576 case 'k': return 0x1d4f;
577 case 'm': return 0x1d50;
578 case 0x014b: return 0x1d51; // LATIN SMALL LETTER ENG
579 case 'o': return 0x1d52;
580 case 0x0254: return 0x1d53; // LATIN SMALL LETTER OPEN O
581 case 0x1d16: return 0x1d54; // LATIN SMALL LETTER TOP HALF O
582 case 0x1d17: return 0x1d55; // LATIN SMALL LETTER BOTTOM HALF O
583 case 'p': return 0x1d56;
584 case 't': return 0x1d57;
585 case 'u': return 0x1d58;
586 case 0x1d1d: return 0x1d59; // LATIN SMALL LETTER SIDEWAYS U
587 case 0x1d1f: return 0x1d5a; // LATIN SMALL LETTER SIDEWAYS TURNED M
588 case 'v': return 0x1d5b;
589 case 0x03b2: return 0x1d5d; // GREEK SMALL LETTER BETA
590 case 0x03b3: return 0x1d5e; // GREEK SMALL LETTER GAMMA
591 case 0x03b4: return 0x1d5f; // GREEK SMALL LETTER DELTA
592 case 0x03c6: return 0x1d60; // GREEK SMALL LETTER PHI
593 case 0x03c7: return 0x1d61; // GREEK SMALL LETTER CHI
599 char_type subscript(char_type c)
602 case 'i': return 0x1d62;
603 case 'r': return 0x1d63;
604 case 'u': return 0x1d64;
605 case 'v': return 0x1d65;
606 case 0x03b2: return 0x1d66; // GREEK SMALL LETTER BETA
607 case 0x03b3: return 0x1d67; // GREEK SMALL LETTER GAMMA
608 case 0x03c1: return 0x1d68; // GREEK SMALL LETTER RHO
609 case 0x03c6: return 0x1d69; // GREEK SMALL LETTER PHI
610 case 0x03c7: return 0x1d6a; // GREEK SMALL LETTER CHI
611 case '0': return 0x2080;
612 case '1': return 0x2081;
613 case '2': return 0x2082;
614 case '3': return 0x2083;
615 case '4': return 0x2084;
616 case '5': return 0x2085;
617 case '6': return 0x2086;
618 case '7': return 0x2087;
619 case '8': return 0x2088;
620 case '9': return 0x2089;
621 case '+': return 0x208a;
622 case '-': return 0x208b;
623 case '=': return 0x208c;
624 case '(': return 0x208d;
625 case ')': return 0x208e;
626 case 'a': return 0x2090;
627 case 'e': return 0x2091;
628 case 'o': return 0x2092;
629 case 'x': return 0x2093;
630 case 0x0259: return 0x2093; // LATIN SMALL LETTER SCHWA
636 bool prefixIs(docstring const & a, char_type c)
644 bool prefixIs(string const & a, string const & pre)
646 size_t const prelen = pre.length();
647 size_t const alen = a.length();
648 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
652 bool prefixIs(docstring const & a, docstring const & pre)
654 size_t const prelen = pre.length();
655 size_t const alen = a.length();
656 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
660 bool suffixIs(string const & a, char c)
664 return a[a.length() - 1] == c;
668 bool suffixIs(docstring const & a, char_type c)
672 return a[a.length() - 1] == c;
676 bool suffixIs(string const & a, string const & suf)
678 size_t const suflen = suf.length();
679 size_t const alen = a.length();
680 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
684 bool suffixIs(docstring const & a, docstring const & suf)
686 size_t const suflen = suf.length();
687 size_t const alen = a.length();
688 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
692 bool containsOnly(string const & s, string const & cset)
694 return s.find_first_not_of(cset) == string::npos;
698 // ale970405+lasgoutt-970425
699 // rewritten to use new string (Lgb)
700 string const token(string const & a, char delim, int n)
708 // Find delimiter or end of string
710 if ((i = a.find(delim, i)) == string::npos)
716 // i is now the n'th delim (or string::npos)
717 if (i == string::npos)
720 k = a.find(delim, i);
721 // k is now the n'th + 1 delim (or string::npos)
723 return a.substr(i, k - i);
727 docstring const token(docstring const & a, char_type delim, int n)
735 // Find delimiter or end of string
737 if ((i = a.find(delim, i)) == docstring::npos)
743 // i is now the n'th delim (or string::npos)
744 if (i == docstring::npos)
747 k = a.find(delim, i);
748 // k is now the n'th + 1 delim (or string::npos)
750 return a.substr(i, k - i);
754 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
755 // rewritten to use new string (Lgb)
756 int tokenPos(string const & a, char delim, string const & tok)
762 while (!str.empty()) {
763 str = split(str, tmptok, delim);
772 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
773 // rewritten to use new string (Lgb)
774 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
780 while (!str.empty()) {
781 str = split(str, tmptok, delim);
792 /// Substitute all \a oldchar with \a newchar
793 template<typename Ch> inline
794 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
795 Ch oldchar, Ch newchar)
797 typedef basic_string<Ch> String;
799 typename String::iterator lit = tmp.begin();
800 typename String::iterator end = tmp.end();
801 for (; lit != end; ++lit)
802 if ((*lit) == oldchar)
808 /// Substitute all \a oldchar with \a newchar
809 docstring const subst_char(docstring const & a,
810 docstring::value_type oldchar, docstring::value_type newchar)
813 docstring::iterator lit = tmp.begin();
814 docstring::iterator end = tmp.end();
815 for (; lit != end; ++lit)
816 if ((*lit) == oldchar)
822 /// substitutes all instances of \a oldstr with \a newstr
823 template<typename String> inline
824 String const subst_string(String const & a,
825 String const & oldstr, String const & newstr)
827 LASSERT(!oldstr.empty(), /**/);
830 size_t const olen = oldstr.length();
831 while ((i = lstr.find(oldstr, i)) != string::npos) {
832 lstr.replace(i, olen, newstr);
833 i += newstr.length(); // We need to be sure that we dont
834 // use the same i over and over again.
840 docstring const subst_string(docstring const & a,
841 docstring const & oldstr, docstring const & newstr)
843 LASSERT(!oldstr.empty(), /**/);
846 size_t const olen = oldstr.length();
847 while ((i = lstr.find(oldstr, i)) != string::npos) {
848 lstr.replace(i, olen, newstr);
849 i += newstr.length(); // We need to be sure that we dont
850 // use the same i over and over again.
858 string const subst(string const & a, char oldchar, char newchar)
860 return subst_char(a, oldchar, newchar);
864 docstring const subst(docstring const & a,
865 char_type oldchar, char_type newchar)
867 return subst_char(a, oldchar, newchar);
871 string const subst(string const & a,
872 string const & oldstr, string const & newstr)
874 return subst_string(a, oldstr, newstr);
878 docstring const subst(docstring const & a,
879 docstring const & oldstr, docstring const & newstr)
881 return subst_string(a, oldstr, newstr);
885 /// Count all occurences of char \a chr inside \a str
886 int count_char(docstring const & str, docstring::value_type chr)
889 docstring::const_iterator lit = str.begin();
890 docstring::const_iterator end = str.end();
891 for (; lit != end; ++lit)
898 docstring const trim(docstring const & a, char const * p)
902 if (a.empty() || !*p)
905 docstring s = from_ascii(p);
906 size_t r = a.find_last_not_of(s);
907 size_t l = a.find_first_not_of(s);
909 // Is this the minimal test? (lgb)
910 if (r == docstring::npos && l == docstring::npos)
913 return a.substr(l, r - l + 1);
917 string const trim(string const & a, char const * p)
921 if (a.empty() || !*p)
924 size_t r = a.find_last_not_of(p);
925 size_t l = a.find_first_not_of(p);
927 // Is this the minimal test? (lgb)
928 if (r == string::npos && l == string::npos)
931 return a.substr(l, r - l + 1);
935 string const rtrim(string const & a, char const * p)
939 if (a.empty() || !*p)
942 size_t r = a.find_last_not_of(p);
944 // Is this test really needed? (Lgb)
945 if (r == string::npos)
948 return a.substr(0, r + 1);
952 docstring const rtrim(docstring const & a, char const * p)
956 if (a.empty() || !*p)
959 size_t r = a.find_last_not_of(from_ascii(p));
961 // Is this test really needed? (Lgb)
962 if (r == docstring::npos)
965 return a.substr(0, r + 1);
969 string const ltrim(string const & a, char const * p)
972 if (a.empty() || !*p)
974 size_t l = a.find_first_not_of(p);
975 if (l == string::npos)
977 return a.substr(l, string::npos);
981 docstring const ltrim(docstring const & a, char const * p)
984 if (a.empty() || !*p)
986 size_t l = a.find_first_not_of(from_ascii(p));
987 if (l == docstring::npos)
989 return a.substr(l, docstring::npos);
994 template<typename String, typename Char> inline
995 String const doSplit(String const & a, String & piece, Char delim)
998 size_t i = a.find(delim);
999 if (i == a.length() - 1) {
1000 piece = a.substr(0, i);
1001 } else if (i != String::npos) {
1002 piece = a.substr(0, i);
1003 tmp = a.substr(i + 1);
1004 } else if (i == 0) {
1006 tmp = a.substr(i + 1);
1013 template<typename Char> inline
1014 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
1017 size_t i = a.find(delim);
1018 if (i == a.length() - 1) {
1019 piece = a.substr(0, i);
1020 } else if (i != docstring::npos) {
1021 piece = a.substr(0, i);
1022 tmp = a.substr(i + 1);
1023 } else if (i == 0) {
1025 tmp = a.substr(i + 1);
1035 string const split(string const & a, string & piece, char delim)
1037 return doSplit(a, piece, delim);
1041 docstring const split(docstring const & a, docstring & piece, char_type delim)
1043 return doSplit(a, piece, delim);
1047 string const split(string const & a, char delim)
1050 size_t i = a.find(delim);
1051 if (i != string::npos) // found delim
1052 tmp = a.substr(i + 1);
1058 string const rsplit(string const & a, string & piece, char delim)
1061 size_t i = a.rfind(delim);
1062 if (i != string::npos) { // delimiter was found
1063 piece = a.substr(0, i);
1064 tmp = a.substr(i + 1);
1065 } else { // delimiter was not found
1072 docstring const rsplit(docstring const & a, docstring & piece, char_type delim)
1075 size_t i = a.rfind(delim);
1076 if (i != string::npos) { // delimiter was found
1077 piece = a.substr(0, i);
1078 tmp = a.substr(i + 1);
1079 } else { // delimiter was not found
1086 docstring const rsplit(docstring const & a, char_type delim)
1089 size_t i = a.rfind(delim);
1090 if (i != string::npos)
1091 tmp = a.substr(i + 1);
1096 docstring const escape(docstring const & lab)
1098 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
1099 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1101 for (size_t i = 0; i < lab.length(); ++i) {
1102 char_type c = lab[i];
1103 if (c >= 128 || c == '=' || c == '%' || c == '#' || c == '$'
1104 || c == '}' || c == '{' || c == ']' || c == '[' || c == '&') {
1105 // Although char_type is a 32 bit type we know that
1106 // UCS4 occupies only 21 bits, so we don't need to
1107 // encode bigger values. Test for 2^24 because we
1108 // can encode that with the 6 hex digits that are
1109 // needed for 21 bits anyway.
1110 LASSERT(c < (1 << 24), /**/);
1112 enc += hexdigit[(c>>20) & 15];
1113 enc += hexdigit[(c>>16) & 15];
1114 enc += hexdigit[(c>>12) & 15];
1115 enc += hexdigit[(c>> 8) & 15];
1116 enc += hexdigit[(c>> 4) & 15];
1117 enc += hexdigit[ c & 15];
1128 // this doesn't check whether str is empty, so do that first.
1129 vector<docstring> wrapToVec(docstring const & str, int ind,
1132 docstring s = trim(str);
1134 return vector<docstring>();
1138 indent.insert(0, -ind, ' ');
1141 s.insert(0, ind, ' ');
1143 vector<docstring> retval;
1144 while (s.size() > width) {
1145 // find the last space within the first 'width' chars
1146 size_t const i = s.find_last_of(' ', width - 1);
1147 if (i == docstring::npos || i <= size_t(ind)) {
1149 s = s.substr(0, width - 3) + "...";
1152 retval.push_back(s.substr(0, i));
1153 s = indent + s.substr(i);
1154 ind = indent.size();
1157 retval.push_back(s);
1164 docstring wrap(docstring const & str, int const ind, size_t const width)
1166 docstring s = trim(str);
1170 vector<docstring> const svec = wrapToVec(str, ind, width);
1171 return getStringFromVector(svec, from_ascii("\n"));
1175 docstring wrapParas(docstring const & str, int const indent,
1176 size_t const width, size_t const maxlines)
1178 docstring const dots = from_ascii("...");
1182 vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1183 vector<docstring> retval;
1185 vector<docstring>::const_iterator it = pars.begin();
1186 vector<docstring>::const_iterator const en = pars.end();
1187 for (; it != en; ++it) {
1188 vector<docstring> tmp = wrapToVec(*it, indent, width);
1189 size_t const nlines = tmp.size();
1192 size_t const curlines = retval.size();
1193 if (maxlines > 0 && curlines + nlines > maxlines) {
1194 tmp.resize(maxlines - curlines);
1195 docstring last = tmp.back();
1196 size_t const lsize = last.size();
1197 if (lsize > width - 3) {
1198 size_t const i = last.find_last_of(' ', width - 3);
1199 if (i == docstring::npos || i <= size_t(indent))
1201 last = last.substr(0, lsize - 3) + dots;
1203 last = last.substr(0, i) + dots;
1207 tmp.push_back(last);
1209 retval.insert(retval.end(), tmp.begin(), tmp.end());
1210 if (maxlines > 0 && retval.size() >= maxlines)
1213 return getStringFromVector(retval, from_ascii("\n"));
1219 template<typename String> vector<String> const
1220 getVectorFromStringT(String const & str, String const & delim, bool keepempty)
1222 // Lars would like this code to go, but for now his replacement (below)
1223 // doesn't fullfil the same function. I have, therefore, reactivated the
1224 // old code for now. Angus 11 Nov 2002.
1229 String keys = rtrim(str);
1231 size_t const idx = keys.find(delim);
1232 if (idx == String::npos) {
1233 vec.push_back(ltrim(keys));
1236 String const key = trim(keys.substr(0, idx));
1237 if (!key.empty() || keepempty)
1239 size_t const start = idx + delim.size();
1240 keys = keys.substr(start);
1244 typedef boost::char_separator<typename String::value_type> Separator;
1245 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1246 Separator sep(delim.c_str());
1247 Tokenizer tokens(str, sep);
1248 return vector<String>(tokens.begin(), tokens.end());
1253 template<typename String> const String
1254 getStringFromVector(vector<String> const & vec, String const & delim)
1257 typename vector<String>::const_iterator it = vec.begin();
1258 typename vector<String>::const_iterator en = vec.end();
1259 for (; it != en; ++it) {
1260 String item = trim(*it);
1273 vector<string> const getVectorFromString(string const & str,
1274 string const & delim,
1277 return getVectorFromStringT<string>(str, delim, keepempty);
1281 vector<docstring> const getVectorFromString(docstring const & str,
1282 docstring const & delim,
1285 return getVectorFromStringT<docstring>(str, delim, keepempty);
1289 string const getStringFromVector(vector<string> const & vec,
1290 string const & delim)
1292 return getStringFromVector<string>(vec, delim);
1296 docstring const getStringFromVector(vector<docstring> const & vec,
1297 docstring const & delim)
1299 return getStringFromVector<docstring>(vec, delim);
1303 int findToken(char const * const str[], string const & search_token)
1307 while (str[i][0] && str[i] != search_token)
1316 docstring bformat(docstring const & fmt, int arg1)
1318 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1319 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1320 return subst(str, from_ascii("%%"), from_ascii("%"));
1325 docstring bformat(docstring const & fmt, long arg1)
1327 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1328 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1329 return subst(str, from_ascii("%%"), from_ascii("%"));
1334 docstring bformat(docstring const & fmt, unsigned int arg1)
1336 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1337 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1338 return subst(str, from_ascii("%%"), from_ascii("%"));
1343 docstring bformat(docstring const & fmt, docstring arg1)
1345 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1346 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1347 return subst(str, from_ascii("%%"), from_ascii("%"));
1352 docstring bformat(docstring const & fmt, char * arg1)
1354 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1355 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1356 return subst(str, from_ascii("%%"), from_ascii("%"));
1361 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1363 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1364 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1365 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1366 str = subst(str, from_ascii("%2$s"), arg2);
1367 return subst(str, from_ascii("%%"), from_ascii("%"));
1372 docstring bformat(docstring const & fmt, docstring arg1, int arg2)
1374 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1375 LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1376 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1377 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1378 return subst(str, from_ascii("%%"), from_ascii("%"));
1383 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1385 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1386 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1387 docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1388 str = subst(fmt, from_ascii("%2$s"), arg2);
1389 return subst(str, from_ascii("%%"), from_ascii("%"));
1394 docstring bformat(docstring const & fmt, int arg1, int arg2)
1396 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1397 LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1398 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1399 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1400 return subst(str, from_ascii("%%"), from_ascii("%"));
1405 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1407 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1408 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1409 LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1410 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1411 str = subst(str, from_ascii("%2$s"), arg2);
1412 str = subst(str, from_ascii("%3$s"), arg3);
1413 return subst(str, from_ascii("%%"), from_ascii("%"));
1418 docstring bformat(docstring const & fmt,
1419 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1421 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1422 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1423 LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1424 LASSERT(contains(fmt, from_ascii("%4$s")), /**/);
1425 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1426 str = subst(str, from_ascii("%2$s"), arg2);
1427 str = subst(str, from_ascii("%3$s"), arg3);
1428 str = subst(str, from_ascii("%4$s"), arg4);
1429 return subst(str, from_ascii("%%"), from_ascii("%"));
1432 } // namespace support