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/qstring_helpers.h"
21 #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()
38 static const docstring s;
42 // Using this allows us to have string default arguments in headers
43 // without #include <string>
44 string const & empty_string()
46 static const string s;
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())), return '?');
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), return QChar('?'));
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 isRTL(char_type c)
167 // assume that no non-utf16 character is right-to-left
168 // c outside the UCS4 range is catched as well
170 QChar::Direction direction = ucs4_to_qchar(c).direction();
173 * http://en.wikipedia.org/wiki/Template:Bidi_Class_%28Unicode%29.
174 * Here we only handle the easy cases:
175 * * R: Hebrew alphabet and related punctuation
176 * * AL: Arabic, Thaana and Syriac alphabets, and most
177 * punctuation specific to those scripts
179 * FIXME: testing show that this does not work (see
180 * RowPainter::paintChars), but my knowledge of unicode is too
181 * poor to understand why.
183 return direction == QChar::DirR || direction ==QChar::DirAL;
187 bool isDigitASCII(char_type c)
189 return '0' <= c && c <= '9';
193 bool isAlnumASCII(char_type c)
195 return isAlphaASCII(c) || isDigitASCII(c);
199 bool isASCII(char_type c)
207 int compare_no_case(docstring const & s, docstring const & s2)
209 docstring::const_iterator p = s.begin();
210 docstring::const_iterator p2 = s2.begin();
212 while (p != s.end() && p2 != s2.end()) {
213 char_type const lc1 = lowercase(*p);
214 char_type const lc2 = lowercase(*p2);
216 return (lc1 < lc2) ? -1 : 1;
221 if (s.size() == s2.size())
223 if (s.size() < s2.size())
229 int compare_locale(docstring const & s, docstring const & s2)
231 // FIXME We have a report that this does not work on windows (bug 9030)
234 string const l = to_local8bit(s);
235 string const r = to_local8bit(s2);
236 return strcoll(l.c_str(), r.c_str());
240 // fall back to builtin sorting
241 LYXERR0("Could not compare using the current locale: "
242 << e.what() << ", using fallback.");
254 template<typename Char>
255 Char ascii_tolower(Char c) {
256 if (c >= 'A' && c <= 'Z')
257 return c - 'A' + 'a';
264 int compare_ascii_no_case(string const & s, string const & s2)
266 string::const_iterator p = s.begin();
267 string::const_iterator p2 = s2.begin();
269 while (p != s.end() && p2 != s2.end()) {
270 int const lc1 = ascii_tolower(*p);
271 int const lc2 = ascii_tolower(*p2);
273 return (lc1 < lc2) ? -1 : 1;
278 if (s.size() == s2.size())
280 if (s.size() < s2.size())
286 int compare_ascii_no_case(docstring const & s, docstring const & s2)
288 docstring::const_iterator p = s.begin();
289 docstring::const_iterator p2 = s2.begin();
291 while (p != s.end() && p2 != s2.end()) {
292 char_type const lc1 = ascii_tolower(*p);
293 char_type const lc2 = ascii_tolower(*p2);
295 return (lc1 < lc2) ? -1 : 1;
300 if (s.size() == s2.size())
302 if (s.size() < s2.size())
308 bool isStrInt(string const & str)
313 // Remove leading and trailing white space chars.
314 string const tmpstr = trim(str);
318 string::const_iterator cit = tmpstr.begin();
322 string::const_iterator end = tmpstr.end();
323 for (; cit != end; ++cit)
324 if (!isDigitASCII(*cit))
331 bool isStrUnsignedInt(string const & str)
336 // Remove leading and trailing white space chars.
337 string const tmpstr = trim(str);
341 string::const_iterator cit = tmpstr.begin();
342 string::const_iterator end = tmpstr.end();
343 for (; cit != end; ++cit)
344 if (!isDigitASCII(*cit))
351 bool isStrDbl(string const & str)
356 // Remove leading and trailing white space chars.
357 string const tmpstr = trim(str);
360 // if (tmpstr.count('.') > 1) return false;
362 string::const_iterator cit = tmpstr.begin();
363 bool found_dot = false;
366 string::const_iterator end = tmpstr.end();
367 for (; cit != end; ++cit) {
368 if (!isDigitASCII(*cit) && *cit != '.')
380 bool hasDigitASCII(docstring const & str)
382 docstring::const_iterator cit = str.begin();
383 docstring::const_iterator const end = str.end();
384 for (; cit != end; ++cit)
385 if (isDigitASCII(*cit))
391 bool isHexChar(char_type c)
403 c == 'a' || c == 'A' ||
404 c == 'b' || c == 'B' ||
405 c == 'c' || c == 'C' ||
406 c == 'd' || c == 'D' ||
407 c == 'e' || c == 'E' ||
408 c == 'f' || c == 'F';
412 bool isHex(docstring const & str)
416 if (str.length() > 2 && str[0] == '0' &&
417 (str[1] == 'x' || str[1] == 'X'))
420 int const len = str.length();
422 for (; index < len; ++index) {
423 if (!isHexChar(str[index]))
430 int hexToInt(docstring const & str)
432 string s = to_ascii(str);
434 sscanf(s.c_str(), "%x", &h);
439 bool isAscii(docstring const & str)
441 int const len = str.length();
442 for (int i = 0; i < len; ++i)
449 bool isAscii(string const & str)
451 int const len = str.length();
452 for (int i = 0; i < len; ++i)
453 if (static_cast<unsigned char>(str[i]) >= 0x80)
459 char lowercase(char c)
461 LASSERT(isASCII(c), return '?');
462 return char(tolower(c));
466 char uppercase(char c)
468 LASSERT(isASCII(c), return '?');
469 return char(toupper(c));
473 char_type lowercase(char_type c)
476 // We don't know how to lowercase a non-utf16 char
478 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
482 char_type uppercase(char_type c)
485 // We don't know how to uppercase a non-utf16 char
487 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
491 bool isLowerCase(char_type ch) {
492 return lowercase(ch) == ch;
496 bool isUpperCase(char_type ch) {
497 return uppercase(ch) == ch;
503 // since we cannot use tolower and toupper directly in the
504 // calls to transform yet, we use these helper clases. (Lgb)
506 struct local_lowercase {
507 char_type operator()(char_type c) const {
512 struct local_uppercase {
513 char_type operator()(char_type c) const {
518 template<typename Char> struct local_ascii_lowercase {
519 Char operator()(Char c) const { return ascii_tolower(c); }
522 } // end of anon namespace
525 docstring const lowercase(docstring const & a)
528 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
533 /* Uncomment here and in lstrings.h if you should need this.
534 string const lowercase(string const & a)
537 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
543 docstring const uppercase(docstring const & a)
546 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
551 string const ascii_lowercase(string const & a)
554 transform(tmp.begin(), tmp.end(), tmp.begin(),
555 local_ascii_lowercase<char>());
560 docstring const ascii_lowercase(docstring const & a)
563 transform(tmp.begin(), tmp.end(), tmp.begin(),
564 local_ascii_lowercase<char_type>());
569 char_type superscript(char_type c)
572 case '2': return 0x00b2;
573 case '3': return 0x00b3;
574 case '1': return 0x00b9;
575 case '0': return 0x2070;
576 case 'i': return 0x2071;
577 case '4': return 0x2074;
578 case '5': return 0x2075;
579 case '6': return 0x2076;
580 case '7': return 0x2077;
581 case '8': return 0x2078;
582 case '9': return 0x2079;
583 case '+': return 0x207a;
584 case '-': return 0x207b;
585 case '=': return 0x207c;
586 case '(': return 0x207d;
587 case ')': return 0x207e;
588 case 'n': return 0x207f;
589 case 'h': return 0x02b0;
590 case 0x0266: return 0x02b1; // LATIN SMALL LETTER H WITH HOOK
591 case 'j': return 0x02b2;
592 case 'r': return 0x02b3;
593 case 0x0279: return 0x02b4; // LATIN SMALL LETTER TURNED R
594 case 0x027b: return 0x02b5; // LATIN SMALL LETTER TURNED R WITH HOOK
595 case 0x0281: return 0x02b6; // LATIN SMALL LETTER CAPITAL INVERTED R
596 case 'w': return 0x02b7;
597 case 'y': return 0x02b8;
598 // case 0x0294: return 0x02c0; // LATIN LETTER GLOTTAL STOP)
599 // case 0x0295: return 0x02c1; // LATIN LETTER PHARYNGEAL VOICED FRICATIVE
600 // (= LATIN LETTER REVERSED GLOTTAL STOP)
601 case 'l': return 0x02e1;
602 case 's': return 0x02e2;
603 case 'x': return 0x02e3;
604 // case 0x0295: return 0x02e4; // LATIN SMALL LETTER REVERSED GLOTTAL STOP
605 case 'A': return 0x1d2c;
606 case 0x00c6: return 0x1d2d; // LATIN CAPITAL LETTER AE
607 case 'B': return 0x1d2e;
608 case 'D': return 0x1d30;
609 case 'E': return 0x1d31;
610 case 'G': return 0x1d33;
611 case 'H': return 0x1d34;
612 case 'I': return 0x1d35;
613 case 'J': return 0x1d36;
614 case 'K': return 0x1d37;
615 case 'L': return 0x1d38;
616 case 'M': return 0x1d39;
617 case 'N': return 0x1d3a;
618 case 'O': return 0x1d3c;
619 case 'P': return 0x1d3e;
620 case 'R': return 0x1d3f;
621 case 'T': return 0x1d40;
622 case 'U': return 0x1d41;
623 case 'W': return 0x1d42;
624 case 'a': return 0x1d43;
625 case 0x0250: return 0x1d44; // LATIN SMALL LETTER TURNED A
626 case 0x0251: return 0x1d45; // LATIN SMALL LETTER ALPHA
627 case 'b': return 0x1d47;
628 case 'd': return 0x1d48;
629 case 'e': return 0x1d49;
630 case 0x0259: return 0x1d4a; // LATIN SMALL LETTER SCHWA
631 case 0x025b: return 0x1d4b; // LATIN SMALL LETTER OPEN E
632 case 0x1d08: return 0x1d4c; // LATIN SMALL LETTER TURNED OPEN E
633 case 'g': return 0x1d4d;
634 case 0x1d09: return 0x1d4e; // LATIN SMALL LETTER TURNED I
635 case 'k': return 0x1d4f;
636 case 'm': return 0x1d50;
637 case 0x014b: return 0x1d51; // LATIN SMALL LETTER ENG
638 case 'o': return 0x1d52;
639 case 0x0254: return 0x1d53; // LATIN SMALL LETTER OPEN O
640 case 0x1d16: return 0x1d54; // LATIN SMALL LETTER TOP HALF O
641 case 0x1d17: return 0x1d55; // LATIN SMALL LETTER BOTTOM HALF O
642 case 'p': return 0x1d56;
643 case 't': return 0x1d57;
644 case 'u': return 0x1d58;
645 case 0x1d1d: return 0x1d59; // LATIN SMALL LETTER SIDEWAYS U
646 case 0x1d1f: return 0x1d5a; // LATIN SMALL LETTER SIDEWAYS TURNED M
647 case 'v': return 0x1d5b;
648 case 0x03b2: return 0x1d5d; // GREEK SMALL LETTER BETA
649 case 0x03b3: return 0x1d5e; // GREEK SMALL LETTER GAMMA
650 case 0x03b4: return 0x1d5f; // GREEK SMALL LETTER DELTA
651 case 0x03c6: return 0x1d60; // GREEK SMALL LETTER PHI
652 case 0x03c7: return 0x1d61; // GREEK SMALL LETTER CHI
658 char_type subscript(char_type c)
661 case 'i': return 0x1d62;
662 case 'r': return 0x1d63;
663 case 'u': return 0x1d64;
664 case 'v': return 0x1d65;
665 case 0x03b2: return 0x1d66; // GREEK SMALL LETTER BETA
666 case 0x03b3: return 0x1d67; // GREEK SMALL LETTER GAMMA
667 case 0x03c1: return 0x1d68; // GREEK SMALL LETTER RHO
668 case 0x03c6: return 0x1d69; // GREEK SMALL LETTER PHI
669 case 0x03c7: return 0x1d6a; // GREEK SMALL LETTER CHI
670 case '0': return 0x2080;
671 case '1': return 0x2081;
672 case '2': return 0x2082;
673 case '3': return 0x2083;
674 case '4': return 0x2084;
675 case '5': return 0x2085;
676 case '6': return 0x2086;
677 case '7': return 0x2087;
678 case '8': return 0x2088;
679 case '9': return 0x2089;
680 case '+': return 0x208a;
681 case '-': return 0x208b;
682 case '=': return 0x208c;
683 case '(': return 0x208d;
684 case ')': return 0x208e;
685 case 'a': return 0x2090;
686 case 'e': return 0x2091;
687 case 'o': return 0x2092;
688 case 'x': return 0x2093;
689 case 0x0259: return 0x2093; // LATIN SMALL LETTER SCHWA
695 bool prefixIs(docstring const & a, char_type c)
703 bool prefixIs(string const & a, string const & pre)
705 size_t const prelen = pre.length();
706 size_t const alen = a.length();
707 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
711 bool prefixIs(docstring const & a, docstring const & pre)
713 size_t const prelen = pre.length();
714 size_t const alen = a.length();
715 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
719 bool suffixIs(string const & a, char c)
723 return a[a.length() - 1] == c;
727 bool suffixIs(docstring const & a, char_type c)
731 return a[a.length() - 1] == c;
735 bool suffixIs(string const & a, string const & suf)
737 size_t const suflen = suf.length();
738 size_t const alen = a.length();
739 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
743 bool suffixIs(docstring const & a, docstring const & suf)
745 size_t const suflen = suf.length();
746 size_t const alen = a.length();
747 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
751 bool containsOnly(string const & s, string const & cset)
753 return s.find_first_not_of(cset) == string::npos;
757 // ale970405+lasgoutt-970425
758 // rewritten to use new string (Lgb)
759 string const token(string const & a, char delim, int n)
767 // Find delimiter or end of string
769 if ((i = a.find(delim, i)) == string::npos)
775 // i is now the n'th delim (or string::npos)
776 if (i == string::npos)
779 k = a.find(delim, i);
780 // k is now the n'th + 1 delim (or string::npos)
782 return a.substr(i, k - i);
786 docstring const token(docstring const & a, char_type delim, int n)
794 // Find delimiter or end of string
796 if ((i = a.find(delim, i)) == docstring::npos)
802 // i is now the n'th delim (or string::npos)
803 if (i == docstring::npos)
806 k = a.find(delim, i);
807 // k is now the n'th + 1 delim (or string::npos)
809 return a.substr(i, k - i);
813 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
814 // rewritten to use new string (Lgb)
815 int tokenPos(string const & a, char delim, string const & tok)
821 while (!str.empty()) {
822 str = split(str, tmptok, delim);
831 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
832 // rewritten to use new string (Lgb)
833 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
839 while (!str.empty()) {
840 str = split(str, tmptok, delim);
851 /// Substitute all \a oldchar with \a newchar
852 template<typename Ch> inline
853 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
854 Ch oldchar, Ch newchar)
856 typedef basic_string<Ch> String;
858 typename String::iterator lit = tmp.begin();
859 typename String::iterator end = tmp.end();
860 for (; lit != end; ++lit)
861 if ((*lit) == oldchar)
867 /// Substitute all \a oldchar with \a newchar
868 docstring const subst_char(docstring const & a,
869 docstring::value_type oldchar, docstring::value_type newchar)
872 docstring::iterator lit = tmp.begin();
873 docstring::iterator end = tmp.end();
874 for (; lit != end; ++lit)
875 if ((*lit) == oldchar)
881 /// substitutes all instances of \a oldstr with \a newstr
882 template<typename String> inline
883 String const subst_string(String const & a,
884 String const & oldstr, String const & newstr)
886 LASSERT(!oldstr.empty(), return a);
889 size_t const olen = oldstr.length();
890 while ((i = lstr.find(oldstr, i)) != string::npos) {
891 lstr.replace(i, olen, newstr);
892 i += newstr.length(); // We need to be sure that we dont
893 // use the same i over and over again.
899 docstring const subst_string(docstring const & a,
900 docstring const & oldstr, docstring const & newstr)
902 LASSERT(!oldstr.empty(), return a);
905 size_t const olen = oldstr.length();
906 while ((i = lstr.find(oldstr, i)) != string::npos) {
907 lstr.replace(i, olen, newstr);
908 i += newstr.length(); // We need to be sure that we dont
909 // use the same i over and over again.
917 string const subst(string const & a, char oldchar, char newchar)
919 return subst_char(a, oldchar, newchar);
923 docstring const subst(docstring const & a,
924 char_type oldchar, char_type newchar)
926 return subst_char(a, oldchar, newchar);
930 string const subst(string const & a,
931 string const & oldstr, string const & newstr)
933 return subst_string(a, oldstr, newstr);
937 docstring const subst(docstring const & a,
938 docstring const & oldstr, docstring const & newstr)
940 return subst_string(a, oldstr, newstr);
944 int count_char(string const & str, char chr)
947 string::const_iterator lit = str.begin();
948 string::const_iterator end = str.end();
949 for (; lit != end; ++lit)
956 /// Count all occurences of char \a chr inside \a str
957 int count_char(docstring const & str, docstring::value_type chr)
960 docstring::const_iterator lit = str.begin();
961 docstring::const_iterator end = str.end();
962 for (; lit != end; ++lit)
969 int count_bin_chars(string const & str)
971 QString const qstr = toqstr(str).simplified();
973 QString::const_iterator cit = qstr.begin();
974 QString::const_iterator end = qstr.end();
975 for (; cit != end; ++cit) {
976 switch (cit->category()) {
977 case QChar::Separator_Line:
978 case QChar::Separator_Paragraph:
979 case QChar::Other_Control:
980 case QChar::Other_Format:
981 case QChar::Other_Surrogate:
982 case QChar::Other_PrivateUse:
983 case QChar::Other_NotAssigned:
994 docstring const trim(docstring const & a, char const * p)
996 LASSERT(p, return a);
998 if (a.empty() || !*p)
1001 docstring s = from_ascii(p);
1002 size_t r = a.find_last_not_of(s);
1003 size_t l = a.find_first_not_of(s);
1005 // Is this the minimal test? (lgb)
1006 if (r == docstring::npos && l == docstring::npos)
1009 return a.substr(l, r - l + 1);
1013 string const trim(string const & a, char const * p)
1015 LASSERT(p, return a);
1017 if (a.empty() || !*p)
1020 size_t r = a.find_last_not_of(p);
1021 size_t l = a.find_first_not_of(p);
1023 // Is this the minimal test? (lgb)
1024 if (r == string::npos && l == string::npos)
1027 return a.substr(l, r - l + 1);
1031 string const rtrim(string const & a, char const * p)
1033 LASSERT(p, return a);
1035 if (a.empty() || !*p)
1038 size_t r = a.find_last_not_of(p);
1040 // Is this test really needed? (Lgb)
1041 if (r == string::npos)
1044 return a.substr(0, r + 1);
1048 docstring const rtrim(docstring const & a, char const * p)
1050 LASSERT(p, return a);
1052 if (a.empty() || !*p)
1055 size_t r = a.find_last_not_of(from_ascii(p));
1057 // Is this test really needed? (Lgb)
1058 if (r == docstring::npos)
1061 return a.substr(0, r + 1);
1065 string const ltrim(string const & a, char const * p)
1067 LASSERT(p, return a);
1068 if (a.empty() || !*p)
1070 size_t l = a.find_first_not_of(p);
1071 if (l == string::npos)
1073 return a.substr(l, string::npos);
1077 docstring const ltrim(docstring const & a, char const * p)
1079 LASSERT(p, return a);
1080 if (a.empty() || !*p)
1082 size_t l = a.find_first_not_of(from_ascii(p));
1083 if (l == docstring::npos)
1085 return a.substr(l, docstring::npos);
1090 template<typename String, typename Char> inline
1091 String const doSplit(String const & a, String & piece, Char delim)
1094 size_t i = a.find(delim);
1095 if (i == a.length() - 1) {
1096 piece = a.substr(0, i);
1097 } else if (i != String::npos) {
1098 piece = a.substr(0, i);
1099 tmp = a.substr(i + 1);
1100 } else if (i == 0) {
1102 tmp = a.substr(i + 1);
1109 template<typename Char> inline
1110 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
1113 size_t i = a.find(delim);
1114 if (i == a.length() - 1) {
1115 piece = a.substr(0, i);
1116 } else if (i != docstring::npos) {
1117 piece = a.substr(0, i);
1118 tmp = a.substr(i + 1);
1119 } else if (i == 0) {
1121 tmp = a.substr(i + 1);
1131 string const split(string const & a, string & piece, char delim)
1133 return doSplit(a, piece, delim);
1137 docstring const split(docstring const & a, docstring & piece, char_type delim)
1139 return doSplit(a, piece, delim);
1143 string const split(string const & a, char delim)
1146 size_t i = a.find(delim);
1147 if (i != string::npos) // found delim
1148 tmp = a.substr(i + 1);
1154 string const rsplit(string const & a, string & piece, char delim)
1157 size_t i = a.rfind(delim);
1158 if (i != string::npos) { // delimiter was found
1159 piece = a.substr(0, i);
1160 tmp = a.substr(i + 1);
1161 } else { // delimiter was not found
1168 docstring const rsplit(docstring const & a, docstring & piece, char_type delim)
1171 size_t i = a.rfind(delim);
1172 if (i != string::npos) { // delimiter was found
1173 piece = a.substr(0, i);
1174 tmp = a.substr(i + 1);
1175 } else { // delimiter was not found
1182 docstring const rsplit(docstring const & a, char_type delim)
1185 size_t i = a.rfind(delim);
1186 if (i != string::npos)
1187 tmp = a.substr(i + 1);
1192 docstring const escape(docstring const & lab)
1194 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
1195 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1197 for (size_t i = 0; i < lab.length(); ++i) {
1198 char_type c = lab[i];
1199 if (c >= 128 || c == '=' || c == '%' || c == '#' || c == '$'
1200 || c == '}' || c == '{' || c == ']' || c == '[' || c == '&') {
1201 // Although char_type is a 32 bit type we know that
1202 // UCS4 occupies only 21 bits, so we don't need to
1203 // encode bigger values. Test for 2^24 because we
1204 // can encode that with the 6 hex digits that are
1205 // needed for 21 bits anyway.
1206 LASSERT(c < (1 << 24), continue);
1208 enc += hexdigit[(c>>20) & 15];
1209 enc += hexdigit[(c>>16) & 15];
1210 enc += hexdigit[(c>>12) & 15];
1211 enc += hexdigit[(c>> 8) & 15];
1212 enc += hexdigit[(c>> 4) & 15];
1213 enc += hexdigit[ c & 15];
1224 // this doesn't check whether str is empty, so do that first.
1225 vector<docstring> wrapToVec(docstring const & str, int ind,
1228 docstring s = trim(str);
1230 return vector<docstring>();
1234 indent.insert(0, -ind, ' ');
1237 s.insert(0, ind, ' ');
1239 vector<docstring> retval;
1240 while (s.size() > width) {
1241 // find the last space within the first 'width' chars
1242 size_t const i = s.find_last_of(' ', width - 1);
1243 if (i == docstring::npos || i <= size_t(ind)) {
1245 s = s.substr(0, width - 3) + "...";
1248 retval.push_back(s.substr(0, i));
1249 s = indent + s.substr(i);
1250 ind = indent.size();
1253 retval.push_back(s);
1260 docstring wrap(docstring const & str, int const ind, size_t const width)
1262 docstring s = trim(str);
1266 vector<docstring> const svec = wrapToVec(str, ind, width);
1267 return getStringFromVector(svec, from_ascii("\n"));
1271 docstring wrapParas(docstring const & str, int const indent,
1272 size_t const width, size_t const maxlines)
1274 docstring const dots = from_ascii("...");
1278 vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1279 vector<docstring> retval;
1281 vector<docstring>::const_iterator it = pars.begin();
1282 vector<docstring>::const_iterator const en = pars.end();
1283 for (; it != en; ++it) {
1284 vector<docstring> tmp = wrapToVec(*it, indent, width);
1285 size_t const nlines = tmp.size();
1288 size_t const curlines = retval.size();
1289 if (maxlines > 0 && curlines + nlines > maxlines) {
1290 tmp.resize(maxlines - curlines);
1291 docstring last = tmp.back();
1292 size_t const lsize = last.size();
1293 if (lsize > width - 3) {
1294 size_t const i = last.find_last_of(' ', width - 3);
1295 if (i == docstring::npos || i <= size_t(indent))
1297 last = last.substr(0, lsize - 3) + dots;
1299 last = last.substr(0, i) + dots;
1303 tmp.push_back(last);
1305 retval.insert(retval.end(), tmp.begin(), tmp.end());
1306 if (maxlines > 0 && retval.size() >= maxlines)
1309 return getStringFromVector(retval, from_ascii("\n"));
1315 template<typename String> vector<String> const
1316 getVectorFromStringT(String const & str, String const & delim, bool keepempty)
1318 // Lars would like this code to go, but for now his replacement (below)
1319 // doesn't fullfil the same function. I have, therefore, reactivated the
1320 // old code for now. Angus 11 Nov 2002.
1325 String keys = rtrim(str);
1327 size_t const idx = keys.find(delim);
1328 if (idx == String::npos) {
1329 vec.push_back(ltrim(keys));
1332 String const key = trim(keys.substr(0, idx));
1333 if (!key.empty() || keepempty)
1335 size_t const start = idx + delim.size();
1336 keys = keys.substr(start);
1340 typedef boost::char_separator<typename String::value_type> Separator;
1341 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1342 Separator sep(delim.c_str());
1343 Tokenizer tokens(str, sep);
1344 return vector<String>(tokens.begin(), tokens.end());
1349 template<typename String> const String
1350 getStringFromVector(vector<String> const & vec, String const & delim)
1353 typename vector<String>::const_iterator it = vec.begin();
1354 typename vector<String>::const_iterator en = vec.end();
1355 for (; it != en; ++it) {
1356 String item = trim(*it);
1369 vector<string> const getVectorFromString(string const & str,
1370 string const & delim,
1373 return getVectorFromStringT<string>(str, delim, keepempty);
1377 vector<docstring> const getVectorFromString(docstring const & str,
1378 docstring const & delim,
1381 return getVectorFromStringT<docstring>(str, delim, keepempty);
1385 string const getStringFromVector(vector<string> const & vec,
1386 string const & delim)
1388 return getStringFromVector<string>(vec, delim);
1392 docstring const getStringFromVector(vector<docstring> const & vec,
1393 docstring const & delim)
1395 return getStringFromVector<docstring>(vec, delim);
1399 int findToken(char const * const str[], string const & search_token)
1403 while (str[i][0] && str[i] != search_token)
1412 docstring bformat(docstring const & fmt, int arg1)
1414 LATTEST(contains(fmt, from_ascii("%1$d")));
1415 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1416 return subst(str, from_ascii("%%"), from_ascii("%"));
1421 docstring bformat(docstring const & fmt, long arg1)
1423 LATTEST(contains(fmt, from_ascii("%1$d")));
1424 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1425 return subst(str, from_ascii("%%"), from_ascii("%"));
1430 docstring bformat(docstring const & fmt, unsigned int arg1)
1432 LATTEST(contains(fmt, from_ascii("%1$d")));
1433 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1434 return subst(str, from_ascii("%%"), from_ascii("%"));
1439 docstring bformat(docstring const & fmt, docstring arg1)
1441 LATTEST(contains(fmt, from_ascii("%1$s")));
1442 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1443 return subst(str, from_ascii("%%"), from_ascii("%"));
1448 docstring bformat(docstring const & fmt, char * arg1)
1450 LATTEST(contains(fmt, from_ascii("%1$s")));
1451 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1452 return subst(str, from_ascii("%%"), from_ascii("%"));
1457 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1459 LATTEST(contains(fmt, from_ascii("%1$s")));
1460 LATTEST(contains(fmt, from_ascii("%2$s")));
1461 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1462 str = subst(str, from_ascii("%2$s"), arg2);
1463 return subst(str, from_ascii("%%"), from_ascii("%"));
1468 docstring bformat(docstring const & fmt, docstring arg1, int arg2)
1470 LATTEST(contains(fmt, from_ascii("%1$s")));
1471 LATTEST(contains(fmt, from_ascii("%2$d")));
1472 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1473 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1474 return subst(str, from_ascii("%%"), from_ascii("%"));
1479 docstring bformat(docstring const & fmt, char const * 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"), from_ascii(arg1));
1484 str = subst(fmt, from_ascii("%2$s"), arg2);
1485 return subst(str, from_ascii("%%"), from_ascii("%"));
1490 docstring bformat(docstring const & fmt, int arg1, int arg2)
1492 LATTEST(contains(fmt, from_ascii("%1$d")));
1493 LATTEST(contains(fmt, from_ascii("%2$d")));
1494 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(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, docstring arg1, docstring arg2, docstring arg3)
1503 LATTEST(contains(fmt, from_ascii("%1$s")));
1504 LATTEST(contains(fmt, from_ascii("%2$s")));
1505 LATTEST(contains(fmt, from_ascii("%3$s")));
1506 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1507 str = subst(str, from_ascii("%2$s"), arg2);
1508 str = subst(str, from_ascii("%3$s"), arg3);
1509 return subst(str, from_ascii("%%"), from_ascii("%"));
1514 docstring bformat(docstring const & fmt,
1515 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1517 LATTEST(contains(fmt, from_ascii("%1$s")));
1518 LATTEST(contains(fmt, from_ascii("%2$s")));
1519 LATTEST(contains(fmt, from_ascii("%3$s")));
1520 LATTEST(contains(fmt, from_ascii("%4$s")));
1521 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1522 str = subst(str, from_ascii("%2$s"), arg2);
1523 str = subst(str, from_ascii("%3$s"), arg3);
1524 str = subst(str, from_ascii("%4$s"), arg4);
1525 return subst(str, from_ascii("%%"), from_ascii("%"));
1528 } // namespace support