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"
19 #include "support/textutils.h"
21 #include <boost/tokenizer.hpp>
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 return ucs4_to_qchar(c).isPrint();
122 bool isPrintableNonspace(char_type c)
126 // outside the UCS4 range
128 // assume that all non-utf16 characters are printable and
132 QChar const qc = ucs4_to_qchar(c);
133 return qc.isPrint() && !qc.isSpace();
137 bool isSpace(char_type c)
140 // assume that no non-utf16 character is a space
141 // c outside the UCS4 range is catched as well
144 QChar const qc = ucs4_to_qchar(c);
149 bool isDigit(char_type c)
152 // assume that no non-utf16 character is a digit
153 // c outside the UCS4 range is catched as well
155 return ucs4_to_qchar(c).isDigit();
159 bool isDigitASCII(char_type c)
161 return '0' <= c && c <= '9';
165 bool isAlnumASCII(char_type c)
167 return ('0' <= c && c <= '9')
168 || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
174 int compare_no_case(docstring const & s, docstring const & s2)
176 docstring::const_iterator p = s.begin();
177 docstring::const_iterator p2 = s2.begin();
179 while (p != s.end() && p2 != s2.end()) {
180 char_type const lc1 = lowercase(*p);
181 char_type const lc2 = lowercase(*p2);
183 return (lc1 < lc2) ? -1 : 1;
188 if (s.size() == s2.size())
190 if (s.size() < s2.size())
198 template<typename Char>
199 Char ascii_tolower(Char c) {
200 if (c >= 'A' && c <= 'Z')
201 return c - 'A' + 'a';
208 int compare_ascii_no_case(string const & s, string const & s2)
210 string::const_iterator p = s.begin();
211 string::const_iterator p2 = s2.begin();
213 while (p != s.end() && p2 != s2.end()) {
214 int const lc1 = ascii_tolower(*p);
215 int const lc2 = ascii_tolower(*p2);
217 return (lc1 < lc2) ? -1 : 1;
222 if (s.size() == s2.size())
224 if (s.size() < s2.size())
230 int compare_ascii_no_case(docstring const & s, docstring const & s2)
232 docstring::const_iterator p = s.begin();
233 docstring::const_iterator p2 = s2.begin();
235 while (p != s.end() && p2 != s2.end()) {
236 char_type const lc1 = ascii_tolower(*p);
237 char_type const lc2 = ascii_tolower(*p2);
239 return (lc1 < lc2) ? -1 : 1;
244 if (s.size() == s2.size())
246 if (s.size() < s2.size())
252 bool isStrInt(string const & str)
257 // Remove leading and trailing white space chars.
258 string const tmpstr = trim(str);
262 string::const_iterator cit = tmpstr.begin();
266 string::const_iterator end = tmpstr.end();
267 for (; cit != end; ++cit)
268 if (!isdigit((*cit)))
275 bool isStrUnsignedInt(string const & str)
280 // Remove leading and trailing white space chars.
281 string const tmpstr = trim(str);
285 string::const_iterator cit = tmpstr.begin();
286 string::const_iterator end = tmpstr.end();
287 for (; cit != end; ++cit)
288 if (!isdigit((*cit)))
295 bool isStrDbl(string const & str)
300 // Remove leading and trailing white space chars.
301 string const tmpstr = trim(str);
304 // if (tmpstr.count('.') > 1) return false;
306 string::const_iterator cit = tmpstr.begin();
307 bool found_dot = false;
310 string::const_iterator end = tmpstr.end();
311 for (; cit != end; ++cit) {
312 if (!isdigit(*cit) && *cit != '.')
324 bool hasDigit(docstring const & str)
329 docstring::const_iterator cit = str.begin();
330 docstring::const_iterator const end = str.end();
331 for (; cit != end; ++cit) {
341 static bool isHexChar(char_type c)
353 c == 'a' || c == 'A' ||
354 c == 'b' || c == 'B' ||
355 c == 'c' || c == 'C' ||
356 c == 'd' || c == 'D' ||
357 c == 'e' || c == 'E' ||
358 c == 'f' || c == 'F';
362 bool isHex(docstring const & str)
366 if (str.length() > 2 && str[0] == '0' &&
367 (str[1] == 'x' || str[1] == 'X'))
370 int const len = str.length();
372 for (; index < len; ++index) {
373 if (!isHexChar(str[index]))
380 int hexToInt(docstring const & str)
382 string s = to_ascii(str);
384 sscanf(s.c_str(), "%x", &h);
389 bool isAscii(docstring const & str)
391 int const len = str.length();
392 for (int i = 0; i < len; ++i)
399 bool isAscii(string const & str)
401 int const len = str.length();
402 for (int i = 0; i < len; ++i)
403 if (static_cast<unsigned char>(str[i]) >= 0x80)
409 char lowercase(char c)
411 LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
412 return char(tolower(c));
416 char uppercase(char c)
418 LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
419 return char(toupper(c));
423 char_type lowercase(char_type c)
426 // We don't know how to lowercase a non-utf16 char
428 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
432 char_type uppercase(char_type c)
435 // We don't know how to uppercase a non-utf16 char
437 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
441 bool isLowerCase(char_type ch) {
442 return lowercase(ch) == ch;
446 bool isUpperCase(char_type ch) {
447 return uppercase(ch) == ch;
453 // since we cannot use tolower and toupper directly in the
454 // calls to transform yet, we use these helper clases. (Lgb)
456 struct local_lowercase {
457 char_type operator()(char_type c) const {
462 struct local_uppercase {
463 char_type operator()(char_type c) const {
468 template<typename Char> struct local_ascii_lowercase {
469 Char operator()(Char c) const { return ascii_tolower(c); }
472 } // end of anon namespace
475 docstring const lowercase(docstring const & a)
478 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
483 docstring const uppercase(docstring const & a)
486 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
491 string const ascii_lowercase(string const & a)
494 transform(tmp.begin(), tmp.end(), tmp.begin(),
495 local_ascii_lowercase<char>());
500 docstring const ascii_lowercase(docstring const & a)
503 transform(tmp.begin(), tmp.end(), tmp.begin(),
504 local_ascii_lowercase<char_type>());
509 char_type superscript(char_type c)
512 case '2': return 0x00b2;
513 case '3': return 0x00b3;
514 case '1': return 0x00b9;
515 case '0': return 0x2070;
516 case 'i': return 0x2071;
517 case '4': return 0x2074;
518 case '5': return 0x2075;
519 case '6': return 0x2076;
520 case '7': return 0x2077;
521 case '8': return 0x2078;
522 case '9': return 0x2079;
523 case '+': return 0x207a;
524 case '-': return 0x207b;
525 case '=': return 0x207c;
526 case '(': return 0x207d;
527 case ')': return 0x207e;
528 case 'n': return 0x207f;
529 case 'h': return 0x02b0;
530 case 0x0266: return 0x02b1; // LATIN SMALL LETTER H WITH HOOK
531 case 'j': return 0x02b2;
532 case 'r': return 0x02b3;
533 case 0x0279: return 0x02b4; // LATIN SMALL LETTER TURNED R
534 case 0x027b: return 0x02b5; // LATIN SMALL LETTER TURNED R WITH HOOK
535 case 0x0281: return 0x02b6; // LATIN SMALL LETTER CAPITAL INVERTED R
536 case 'w': return 0x02b7;
537 case 'y': return 0x02b8;
538 // case 0x0294: return 0x02c0; // LATIN LETTER GLOTTAL STOP)
539 // case 0x0295: return 0x02c1; // LATIN LETTER PHARYNGEAL VOICED FRICATIVE
540 // (= LATIN LETTER REVERSED GLOTTAL STOP)
541 case 'l': return 0x02e1;
542 case 's': return 0x02e2;
543 case 'x': return 0x02e3;
544 // case 0x0295: return 0x02e4; // LATIN SMALL LETTER REVERSED GLOTTAL STOP
545 case 'A': return 0x1d2c;
546 case 0x00c6: return 0x1d2d; // LATIN CAPITAL LETTER AE
547 case 'B': return 0x1d2e;
548 case 'D': return 0x1d30;
549 case 'E': return 0x1d31;
550 case 'G': return 0x1d33;
551 case 'H': return 0x1d34;
552 case 'I': return 0x1d35;
553 case 'J': return 0x1d36;
554 case 'K': return 0x1d37;
555 case 'L': return 0x1d38;
556 case 'M': return 0x1d39;
557 case 'N': return 0x1d3a;
558 case 'O': return 0x1d3c;
559 case 'P': return 0x1d3e;
560 case 'R': return 0x1d3f;
561 case 'T': return 0x1d40;
562 case 'U': return 0x1d41;
563 case 'W': return 0x1d42;
564 case 'a': return 0x1d43;
565 case 0x0250: return 0x1d44; // LATIN SMALL LETTER TURNED A
566 case 0x0251: return 0x1d45; // LATIN SMALL LETTER ALPHA
567 case 'b': return 0x1d47;
568 case 'd': return 0x1d48;
569 case 'e': return 0x1d49;
570 case 0x0259: return 0x1d4a; // LATIN SMALL LETTER SCHWA
571 case 0x025b: return 0x1d4b; // LATIN SMALL LETTER OPEN E
572 case 0x1d08: return 0x1d4c; // LATIN SMALL LETTER TURNED OPEN E
573 case 'g': return 0x1d4d;
574 case 0x1d09: return 0x1d4e; // LATIN SMALL LETTER TURNED I
575 case 'k': return 0x1d4f;
576 case 'm': return 0x1d50;
577 case 0x014b: return 0x1d51; // LATIN SMALL LETTER ENG
578 case 'o': return 0x1d52;
579 case 0x0254: return 0x1d53; // LATIN SMALL LETTER OPEN O
580 case 0x1d16: return 0x1d54; // LATIN SMALL LETTER TOP HALF O
581 case 0x1d17: return 0x1d55; // LATIN SMALL LETTER BOTTOM HALF O
582 case 'p': return 0x1d56;
583 case 't': return 0x1d57;
584 case 'u': return 0x1d58;
585 case 0x1d1d: return 0x1d59; // LATIN SMALL LETTER SIDEWAYS U
586 case 0x1d1f: return 0x1d5a; // LATIN SMALL LETTER SIDEWAYS TURNED M
587 case 'v': return 0x1d5b;
588 case 0x03b2: return 0x1d5d; // GREEK SMALL LETTER BETA
589 case 0x03b3: return 0x1d5e; // GREEK SMALL LETTER GAMMA
590 case 0x03b4: return 0x1d5f; // GREEK SMALL LETTER DELTA
591 case 0x03c6: return 0x1d60; // GREEK SMALL LETTER PHI
592 case 0x03c7: return 0x1d61; // GREEK SMALL LETTER CHI
598 char_type subscript(char_type c)
601 case 'i': return 0x1d62;
602 case 'r': return 0x1d63;
603 case 'u': return 0x1d64;
604 case 'v': return 0x1d65;
605 case 0x03b2: return 0x1d66; // GREEK SMALL LETTER BETA
606 case 0x03b3: return 0x1d67; // GREEK SMALL LETTER GAMMA
607 case 0x03c1: return 0x1d68; // GREEK SMALL LETTER RHO
608 case 0x03c6: return 0x1d69; // GREEK SMALL LETTER PHI
609 case 0x03c7: return 0x1d6a; // GREEK SMALL LETTER CHI
610 case '0': return 0x2080;
611 case '1': return 0x2081;
612 case '2': return 0x2082;
613 case '3': return 0x2083;
614 case '4': return 0x2084;
615 case '5': return 0x2085;
616 case '6': return 0x2086;
617 case '7': return 0x2087;
618 case '8': return 0x2088;
619 case '9': return 0x2089;
620 case '+': return 0x208a;
621 case '-': return 0x208b;
622 case '=': return 0x208c;
623 case '(': return 0x208d;
624 case ')': return 0x208e;
625 case 'a': return 0x2090;
626 case 'e': return 0x2091;
627 case 'o': return 0x2092;
628 case 'x': return 0x2093;
629 case 0x0259: return 0x2093; // LATIN SMALL LETTER SCHWA
635 bool prefixIs(docstring const & a, char_type c)
643 bool prefixIs(string const & a, string const & pre)
645 size_t const prelen = pre.length();
646 size_t const alen = a.length();
647 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
651 bool prefixIs(docstring const & a, docstring const & pre)
653 size_t const prelen = pre.length();
654 size_t const alen = a.length();
655 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
659 bool suffixIs(string const & a, char c)
663 return a[a.length() - 1] == c;
667 bool suffixIs(docstring const & a, char_type c)
671 return a[a.length() - 1] == c;
675 bool suffixIs(string const & a, string const & suf)
677 size_t const suflen = suf.length();
678 size_t const alen = a.length();
679 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
683 bool suffixIs(docstring const & a, docstring const & suf)
685 size_t const suflen = suf.length();
686 size_t const alen = a.length();
687 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
691 bool containsOnly(string const & s, string const & cset)
693 return s.find_first_not_of(cset) == string::npos;
697 // ale970405+lasgoutt-970425
698 // rewritten to use new string (Lgb)
699 string const token(string const & a, char delim, int n)
707 // Find delimiter or end of string
709 if ((i = a.find(delim, i)) == string::npos)
715 // i is now the n'th delim (or string::npos)
716 if (i == string::npos)
719 k = a.find(delim, i);
720 // k is now the n'th + 1 delim (or string::npos)
722 return a.substr(i, k - i);
726 docstring const token(docstring const & a, char_type delim, int n)
734 // Find delimiter or end of string
736 if ((i = a.find(delim, i)) == docstring::npos)
742 // i is now the n'th delim (or string::npos)
743 if (i == docstring::npos)
746 k = a.find(delim, i);
747 // k is now the n'th + 1 delim (or string::npos)
749 return a.substr(i, k - i);
753 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
754 // rewritten to use new string (Lgb)
755 int tokenPos(string const & a, char delim, string const & tok)
761 while (!str.empty()) {
762 str = split(str, tmptok, delim);
771 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
772 // rewritten to use new string (Lgb)
773 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
779 while (!str.empty()) {
780 str = split(str, tmptok, delim);
791 /// Substitute all \a oldchar with \a newchar
792 template<typename Ch> inline
793 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
794 Ch oldchar, Ch newchar)
796 typedef basic_string<Ch> String;
798 typename String::iterator lit = tmp.begin();
799 typename String::iterator end = tmp.end();
800 for (; lit != end; ++lit)
801 if ((*lit) == oldchar)
807 /// Substitute all \a oldchar with \a newchar
808 docstring const subst_char(docstring const & a,
809 docstring::value_type oldchar, docstring::value_type newchar)
812 docstring::iterator lit = tmp.begin();
813 docstring::iterator end = tmp.end();
814 for (; lit != end; ++lit)
815 if ((*lit) == oldchar)
821 /// substitutes all instances of \a oldstr with \a newstr
822 template<typename String> inline
823 String const subst_string(String const & a,
824 String const & oldstr, String const & newstr)
826 LASSERT(!oldstr.empty(), /**/);
829 size_t const olen = oldstr.length();
830 while ((i = lstr.find(oldstr, i)) != string::npos) {
831 lstr.replace(i, olen, newstr);
832 i += newstr.length(); // We need to be sure that we dont
833 // use the same i over and over again.
839 docstring const subst_string(docstring const & a,
840 docstring const & oldstr, docstring const & newstr)
842 LASSERT(!oldstr.empty(), /**/);
845 size_t const olen = oldstr.length();
846 while ((i = lstr.find(oldstr, i)) != string::npos) {
847 lstr.replace(i, olen, newstr);
848 i += newstr.length(); // We need to be sure that we dont
849 // use the same i over and over again.
857 string const subst(string const & a, char oldchar, char newchar)
859 return subst_char(a, oldchar, newchar);
863 docstring const subst(docstring const & a,
864 char_type oldchar, char_type newchar)
866 return subst_char(a, oldchar, newchar);
870 string const subst(string const & a,
871 string const & oldstr, string const & newstr)
873 return subst_string(a, oldstr, newstr);
877 docstring const subst(docstring const & a,
878 docstring const & oldstr, docstring const & newstr)
880 return subst_string(a, oldstr, newstr);
884 /// Count all occurences of char \a chr inside \a str
885 int count_char(docstring const & str, docstring::value_type chr)
888 docstring::const_iterator lit = str.begin();
889 docstring::const_iterator end = str.end();
890 for (; lit != end; ++lit)
897 docstring const trim(docstring const & a, char const * p)
901 if (a.empty() || !*p)
904 docstring s = from_ascii(p);
905 size_t r = a.find_last_not_of(s);
906 size_t l = a.find_first_not_of(s);
908 // Is this the minimal test? (lgb)
909 if (r == docstring::npos && l == docstring::npos)
912 return a.substr(l, r - l + 1);
916 string const trim(string const & a, char const * p)
920 if (a.empty() || !*p)
923 size_t r = a.find_last_not_of(p);
924 size_t l = a.find_first_not_of(p);
926 // Is this the minimal test? (lgb)
927 if (r == string::npos && l == string::npos)
930 return a.substr(l, r - l + 1);
934 string const rtrim(string const & a, char const * p)
938 if (a.empty() || !*p)
941 size_t r = a.find_last_not_of(p);
943 // Is this test really needed? (Lgb)
944 if (r == string::npos)
947 return a.substr(0, r + 1);
951 docstring const rtrim(docstring const & a, char const * p)
955 if (a.empty() || !*p)
958 size_t r = a.find_last_not_of(from_ascii(p));
960 // Is this test really needed? (Lgb)
961 if (r == docstring::npos)
964 return a.substr(0, r + 1);
968 string const ltrim(string const & a, char const * p)
971 if (a.empty() || !*p)
973 size_t l = a.find_first_not_of(p);
974 if (l == string::npos)
976 return a.substr(l, string::npos);
980 docstring const ltrim(docstring const & a, char const * p)
983 if (a.empty() || !*p)
985 size_t l = a.find_first_not_of(from_ascii(p));
986 if (l == docstring::npos)
988 return a.substr(l, docstring::npos);
993 template<typename String, typename Char> inline
994 String const doSplit(String const & a, String & piece, Char delim)
997 size_t i = a.find(delim);
998 if (i == a.length() - 1) {
999 piece = a.substr(0, i);
1000 } else if (i != String::npos) {
1001 piece = a.substr(0, i);
1002 tmp = a.substr(i + 1);
1003 } else if (i == 0) {
1005 tmp = a.substr(i + 1);
1012 template<typename Char> inline
1013 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
1016 size_t i = a.find(delim);
1017 if (i == a.length() - 1) {
1018 piece = a.substr(0, i);
1019 } else if (i != docstring::npos) {
1020 piece = a.substr(0, i);
1021 tmp = a.substr(i + 1);
1022 } else if (i == 0) {
1024 tmp = a.substr(i + 1);
1034 string const split(string const & a, string & piece, char delim)
1036 return doSplit(a, piece, delim);
1040 docstring const split(docstring const & a, docstring & piece, char_type delim)
1042 return doSplit(a, piece, delim);
1046 string const split(string const & a, char delim)
1049 size_t i = a.find(delim);
1050 if (i != string::npos) // found delim
1051 tmp = a.substr(i + 1);
1057 string const rsplit(string const & a, string & piece, char delim)
1060 size_t i = a.rfind(delim);
1061 if (i != string::npos) { // delimiter was found
1062 piece = a.substr(0, i);
1063 tmp = a.substr(i + 1);
1064 } else { // delimiter was not found
1071 docstring const rsplit(docstring const & a, char_type delim)
1074 size_t i = a.rfind(delim);
1075 if (i != string::npos)
1076 tmp = a.substr(i + 1);
1081 docstring const escape(docstring const & lab)
1083 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
1084 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1086 for (size_t i = 0; i < lab.length(); ++i) {
1087 char_type c = lab[i];
1088 if (c >= 128 || c == '=' || c == '%') {
1089 // Although char_type is a 32 bit type we know that
1090 // UCS4 occupies only 21 bits, so we don't need to
1091 // encode bigger values. Test for 2^24 because we
1092 // can encode that with the 6 hex digits that are
1093 // needed for 21 bits anyway.
1094 LASSERT(c < (1 << 24), /**/);
1096 enc += hexdigit[(c>>20) & 15];
1097 enc += hexdigit[(c>>16) & 15];
1098 enc += hexdigit[(c>>12) & 15];
1099 enc += hexdigit[(c>> 8) & 15];
1100 enc += hexdigit[(c>> 4) & 15];
1101 enc += hexdigit[ c & 15];
1112 // this doesn't check whether str is empty, so do that first.
1113 vector<docstring> wrapToVec(docstring const & str, int ind,
1116 docstring s = trim(str);
1118 return vector<docstring>();
1122 indent.insert(0, -ind, ' ');
1125 s.insert(0, ind, ' ');
1127 vector<docstring> retval;
1128 while (s.size() > width) {
1129 // find the last space within the first 'width' chars
1130 size_t const i = s.find_last_of(' ', width - 1);
1131 if (i == docstring::npos || i <= size_t(ind)) {
1133 s = s.substr(0, width - 3) + "...";
1136 retval.push_back(s.substr(0, i));
1137 s = indent + s.substr(i);
1138 ind = indent.size();
1141 retval.push_back(s);
1148 docstring wrap(docstring const & str, int const ind, size_t const width)
1150 docstring s = trim(str);
1154 vector<docstring> const svec = wrapToVec(str, ind, width);
1155 return getStringFromVector(svec, from_ascii("\n"));
1159 docstring wrapParas(docstring const & str, int const indent,
1160 size_t const width, size_t const maxlines)
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 - 1);
1178 tmp.push_back(from_ascii("..."));
1180 retval.insert(retval.end(), tmp.begin(), tmp.end());
1181 if (maxlines > 0 && retval.size() >= maxlines)
1184 return getStringFromVector(retval, from_ascii("\n"));
1190 template<typename String> vector<String> const
1191 getVectorFromStringT(String const & str, String const & delim, bool keepempty)
1193 // Lars would like this code to go, but for now his replacement (below)
1194 // doesn't fullfil the same function. I have, therefore, reactivated the
1195 // old code for now. Angus 11 Nov 2002.
1200 String keys = rtrim(str);
1202 size_t const idx = keys.find(delim);
1203 if (idx == String::npos) {
1204 vec.push_back(ltrim(keys));
1207 String const key = trim(keys.substr(0, idx));
1208 if (!key.empty() || keepempty)
1210 size_t const start = idx + delim.size();
1211 keys = keys.substr(start);
1215 typedef boost::char_separator<typename String::value_type> Separator;
1216 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1217 Separator sep(delim.c_str());
1218 Tokenizer tokens(str, sep);
1219 return vector<String>(tokens.begin(), tokens.end());
1224 template<typename String> const String
1225 getStringFromVector(vector<String> const & vec, String const & delim)
1228 typename vector<String>::const_iterator it = vec.begin();
1229 typename vector<String>::const_iterator en = vec.end();
1230 for (; it != en; ++it) {
1231 String item = trim(*it);
1244 vector<string> const getVectorFromString(string const & str,
1245 string const & delim,
1248 return getVectorFromStringT<string>(str, delim, keepempty);
1252 vector<docstring> const getVectorFromString(docstring const & str,
1253 docstring const & delim,
1256 return getVectorFromStringT<docstring>(str, delim, keepempty);
1260 string const getStringFromVector(vector<string> const & vec,
1261 string const & delim)
1263 return getStringFromVector<string>(vec, delim);
1267 docstring const getStringFromVector(vector<docstring> const & vec,
1268 docstring const & delim)
1270 return getStringFromVector<docstring>(vec, delim);
1274 int findToken(char const * const str[], string const & search_token)
1278 while (str[i][0] && str[i] != search_token)
1287 docstring bformat(docstring const & fmt, int arg1)
1289 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1290 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1291 return subst(str, from_ascii("%%"), from_ascii("%"));
1296 docstring bformat(docstring const & fmt, long arg1)
1298 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1299 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1300 return subst(str, from_ascii("%%"), from_ascii("%"));
1305 docstring bformat(docstring const & fmt, unsigned int arg1)
1307 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1308 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1309 return subst(str, from_ascii("%%"), from_ascii("%"));
1314 docstring bformat(docstring const & fmt, docstring arg1)
1316 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1317 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1318 return subst(str, from_ascii("%%"), from_ascii("%"));
1323 docstring bformat(docstring const & fmt, char * arg1)
1325 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1326 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1327 return subst(str, from_ascii("%%"), from_ascii("%"));
1332 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1334 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1335 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1336 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1337 str = subst(str, from_ascii("%2$s"), arg2);
1338 return subst(str, from_ascii("%%"), from_ascii("%"));
1343 docstring bformat(docstring const & fmt, docstring arg1, int arg2)
1345 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1346 LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1347 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1348 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1349 return subst(str, from_ascii("%%"), from_ascii("%"));
1354 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1356 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1357 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1358 docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1359 str = subst(fmt, from_ascii("%2$s"), arg2);
1360 return subst(str, from_ascii("%%"), from_ascii("%"));
1365 docstring bformat(docstring const & fmt, int arg1, int arg2)
1367 LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1368 LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1369 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1370 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1371 return subst(str, from_ascii("%%"), from_ascii("%"));
1376 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1378 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1379 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1380 LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1381 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1382 str = subst(str, from_ascii("%2$s"), arg2);
1383 str = subst(str, from_ascii("%3$s"), arg3);
1384 return subst(str, from_ascii("%%"), from_ascii("%"));
1389 docstring bformat(docstring const & fmt,
1390 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1392 LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1393 LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1394 LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1395 LASSERT(contains(fmt, from_ascii("%4$s")), /**/);
1396 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1397 str = subst(str, from_ascii("%2$s"), arg2);
1398 str = subst(str, from_ascii("%3$s"), arg3);
1399 str = subst(str, from_ascii("%4$s"), arg4);
1400 return subst(str, from_ascii("%%"), from_ascii("%"));
1403 } // namespace support