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"
33 // Using this allows us to have docstring default arguments in headers
34 // without #include "support/docstring" there.
35 docstring const & empty_docstring()
37 static const docstring s;
41 // Using this allows us to have string default arguments in headers
42 // without #include <string>
43 string const & empty_string()
45 static const string s;
51 * Convert a QChar into a UCS4 character.
52 * This is a hack (it does only make sense for the common part of the UCS4
53 * and UTF16 encodings) and should not be used.
54 * This does only exist because of performance reasons (a real conversion
55 * using iconv is too slow on windows).
57 inline char_type qchar_to_ucs4(QChar const & qchar)
59 LASSERT(is_utf16(static_cast<char_type>(qchar.unicode())), return '?');
60 return static_cast<char_type>(qchar.unicode());
64 * Convert a UCS4 character into a QChar.
65 * This is a hack (it does only make sense for the common part of the UCS4
66 * and UTF16 encodings) and should not be used.
67 * This does only exist because of performance reasons (a real conversion
68 * using iconv is too slow on windows).
70 inline QChar const ucs4_to_qchar(char_type const ucs4)
72 LASSERT(is_utf16(ucs4), return QChar('?'));
73 return QChar(static_cast<unsigned short>(ucs4));
76 /// Maximum valid UCS4 code point
77 char_type const ucs4_max = 0x10ffff;
81 bool isLetterChar(char_type c)
85 // outside the UCS4 range
87 // assume that all non-utf16 characters are letters
90 return ucs4_to_qchar(c).isLetter();
94 bool isLower(char_type c)
98 return ucs4_to_qchar(c).isLower();
102 bool isAlphaASCII(char_type c)
104 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
108 bool isPrintable(char_type c)
112 // outside the UCS4 range
114 // assume that all non-utf16 characters are printable
117 // Not yet recognized by QChar::isPrint()
118 // See https://bugreports.qt-project.org/browse/QTBUG-12144
119 // LATIN CAPITAL LETTER SHARP S
120 else if (c == 0x1e9e)
122 return ucs4_to_qchar(c).isPrint();
126 bool isPrintableNonspace(char_type c)
130 // outside the UCS4 range
132 // assume that all non-utf16 characters are printable and
136 QChar const qc = ucs4_to_qchar(c);
137 return qc.isPrint() && !qc.isSpace();
141 bool isSpace(char_type c)
144 // assume that no non-utf16 character is a space
145 // c outside the UCS4 range is catched as well
148 QChar const qc = ucs4_to_qchar(c);
153 bool isNumber(char_type c)
156 // assume that no non-utf16 character is a numeral
157 // c outside the UCS4 range is catched as well
159 return ucs4_to_qchar(c).isNumber();
163 bool isDigitASCII(char_type c)
165 return '0' <= c && c <= '9';
169 bool isAlnumASCII(char_type c)
171 return isAlphaASCII(c) || isDigitASCII(c);
175 bool isASCII(char_type c)
183 int compare_no_case(docstring const & s, docstring const & s2)
185 docstring::const_iterator p = s.begin();
186 docstring::const_iterator p2 = s2.begin();
188 while (p != s.end() && p2 != s2.end()) {
189 char_type const lc1 = lowercase(*p);
190 char_type const lc2 = lowercase(*p2);
192 return (lc1 < lc2) ? -1 : 1;
197 if (s.size() == s2.size())
199 if (s.size() < s2.size())
205 int compare_locale(docstring const & s, docstring const & s2)
207 // FIXME We have a report that this does not work on windows (bug 9030)
210 string const l = to_local8bit(s);
211 string const r = to_local8bit(s2);
212 return strcoll(l.c_str(), r.c_str());
216 // fall back to builtin sorting
217 LYXERR0("Could not compare using the current locale: "
218 << e.what() << ", using fallback.");
230 template<typename Char>
231 Char ascii_tolower(Char c) {
232 if (c >= 'A' && c <= 'Z')
233 return c - 'A' + 'a';
240 int compare_ascii_no_case(string const & s, string const & s2)
242 string::const_iterator p = s.begin();
243 string::const_iterator p2 = s2.begin();
245 while (p != s.end() && p2 != s2.end()) {
246 int const lc1 = ascii_tolower(*p);
247 int const lc2 = ascii_tolower(*p2);
249 return (lc1 < lc2) ? -1 : 1;
254 if (s.size() == s2.size())
256 if (s.size() < s2.size())
262 int compare_ascii_no_case(docstring const & s, docstring const & s2)
264 docstring::const_iterator p = s.begin();
265 docstring::const_iterator p2 = s2.begin();
267 while (p != s.end() && p2 != s2.end()) {
268 char_type const lc1 = ascii_tolower(*p);
269 char_type const lc2 = ascii_tolower(*p2);
271 return (lc1 < lc2) ? -1 : 1;
276 if (s.size() == s2.size())
278 if (s.size() < s2.size())
284 bool isStrInt(string const & str)
289 // Remove leading and trailing white space chars.
290 string const tmpstr = trim(str);
294 string::const_iterator cit = tmpstr.begin();
298 string::const_iterator end = tmpstr.end();
299 for (; cit != end; ++cit)
300 if (!isDigitASCII(*cit))
307 bool isStrUnsignedInt(string const & str)
312 // Remove leading and trailing white space chars.
313 string const tmpstr = trim(str);
317 string::const_iterator cit = tmpstr.begin();
318 string::const_iterator end = tmpstr.end();
319 for (; cit != end; ++cit)
320 if (!isDigitASCII(*cit))
327 bool isStrDbl(string const & str)
332 // Remove leading and trailing white space chars.
333 string const tmpstr = trim(str);
336 // if (tmpstr.count('.') > 1) return false;
338 string::const_iterator cit = tmpstr.begin();
339 bool found_dot = false;
342 string::const_iterator end = tmpstr.end();
343 for (; cit != end; ++cit) {
344 if (!isDigitASCII(*cit) && *cit != '.')
356 bool hasDigitASCII(docstring const & str)
358 docstring::const_iterator cit = str.begin();
359 docstring::const_iterator const end = str.end();
360 for (; cit != end; ++cit)
361 if (isDigitASCII(*cit))
367 bool isHexChar(char_type c)
379 c == 'a' || c == 'A' ||
380 c == 'b' || c == 'B' ||
381 c == 'c' || c == 'C' ||
382 c == 'd' || c == 'D' ||
383 c == 'e' || c == 'E' ||
384 c == 'f' || c == 'F';
388 bool isHex(docstring const & str)
392 if (str.length() > 2 && str[0] == '0' &&
393 (str[1] == 'x' || str[1] == 'X'))
396 int const len = str.length();
398 for (; index < len; ++index) {
399 if (!isHexChar(str[index]))
406 int hexToInt(docstring const & str)
408 string s = to_ascii(str);
410 sscanf(s.c_str(), "%x", &h);
415 bool isAscii(docstring const & str)
417 int const len = str.length();
418 for (int i = 0; i < len; ++i)
425 bool isAscii(string const & str)
427 int const len = str.length();
428 for (int i = 0; i < len; ++i)
429 if (static_cast<unsigned char>(str[i]) >= 0x80)
435 char lowercase(char c)
437 LASSERT(isASCII(c), return '?');
438 return char(tolower(c));
442 char uppercase(char c)
444 LASSERT(isASCII(c), return '?');
445 return char(toupper(c));
449 char_type lowercase(char_type c)
452 // We don't know how to lowercase a non-utf16 char
454 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
458 char_type uppercase(char_type c)
461 // We don't know how to uppercase a non-utf16 char
463 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
467 bool isLowerCase(char_type ch) {
468 return lowercase(ch) == ch;
472 bool isUpperCase(char_type ch) {
473 return uppercase(ch) == ch;
479 // since we cannot use tolower and toupper directly in the
480 // calls to transform yet, we use these helper clases. (Lgb)
482 struct local_lowercase {
483 char_type operator()(char_type c) const {
488 struct local_uppercase {
489 char_type operator()(char_type c) const {
494 template<typename Char> struct local_ascii_lowercase {
495 Char operator()(Char c) const { return ascii_tolower(c); }
498 } // end of anon namespace
501 docstring const lowercase(docstring const & a)
504 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
509 /* Uncomment here and in lstrings.h if you should need this.
510 string const lowercase(string const & a)
513 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
519 docstring const uppercase(docstring const & a)
522 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
527 string const ascii_lowercase(string const & a)
530 transform(tmp.begin(), tmp.end(), tmp.begin(),
531 local_ascii_lowercase<char>());
536 docstring const ascii_lowercase(docstring const & a)
539 transform(tmp.begin(), tmp.end(), tmp.begin(),
540 local_ascii_lowercase<char_type>());
545 char_type superscript(char_type c)
548 case '2': return 0x00b2;
549 case '3': return 0x00b3;
550 case '1': return 0x00b9;
551 case '0': return 0x2070;
552 case 'i': return 0x2071;
553 case '4': return 0x2074;
554 case '5': return 0x2075;
555 case '6': return 0x2076;
556 case '7': return 0x2077;
557 case '8': return 0x2078;
558 case '9': return 0x2079;
559 case '+': return 0x207a;
560 case '-': return 0x207b;
561 case '=': return 0x207c;
562 case '(': return 0x207d;
563 case ')': return 0x207e;
564 case 'n': return 0x207f;
565 case 'h': return 0x02b0;
566 case 0x0266: return 0x02b1; // LATIN SMALL LETTER H WITH HOOK
567 case 'j': return 0x02b2;
568 case 'r': return 0x02b3;
569 case 0x0279: return 0x02b4; // LATIN SMALL LETTER TURNED R
570 case 0x027b: return 0x02b5; // LATIN SMALL LETTER TURNED R WITH HOOK
571 case 0x0281: return 0x02b6; // LATIN SMALL LETTER CAPITAL INVERTED R
572 case 'w': return 0x02b7;
573 case 'y': return 0x02b8;
574 // case 0x0294: return 0x02c0; // LATIN LETTER GLOTTAL STOP)
575 // case 0x0295: return 0x02c1; // LATIN LETTER PHARYNGEAL VOICED FRICATIVE
576 // (= LATIN LETTER REVERSED GLOTTAL STOP)
577 case 'l': return 0x02e1;
578 case 's': return 0x02e2;
579 case 'x': return 0x02e3;
580 // case 0x0295: return 0x02e4; // LATIN SMALL LETTER REVERSED GLOTTAL STOP
581 case 'A': return 0x1d2c;
582 case 0x00c6: return 0x1d2d; // LATIN CAPITAL LETTER AE
583 case 'B': return 0x1d2e;
584 case 'D': return 0x1d30;
585 case 'E': return 0x1d31;
586 case 'G': return 0x1d33;
587 case 'H': return 0x1d34;
588 case 'I': return 0x1d35;
589 case 'J': return 0x1d36;
590 case 'K': return 0x1d37;
591 case 'L': return 0x1d38;
592 case 'M': return 0x1d39;
593 case 'N': return 0x1d3a;
594 case 'O': return 0x1d3c;
595 case 'P': return 0x1d3e;
596 case 'R': return 0x1d3f;
597 case 'T': return 0x1d40;
598 case 'U': return 0x1d41;
599 case 'W': return 0x1d42;
600 case 'a': return 0x1d43;
601 case 0x0250: return 0x1d44; // LATIN SMALL LETTER TURNED A
602 case 0x0251: return 0x1d45; // LATIN SMALL LETTER ALPHA
603 case 'b': return 0x1d47;
604 case 'd': return 0x1d48;
605 case 'e': return 0x1d49;
606 case 0x0259: return 0x1d4a; // LATIN SMALL LETTER SCHWA
607 case 0x025b: return 0x1d4b; // LATIN SMALL LETTER OPEN E
608 case 0x1d08: return 0x1d4c; // LATIN SMALL LETTER TURNED OPEN E
609 case 'g': return 0x1d4d;
610 case 0x1d09: return 0x1d4e; // LATIN SMALL LETTER TURNED I
611 case 'k': return 0x1d4f;
612 case 'm': return 0x1d50;
613 case 0x014b: return 0x1d51; // LATIN SMALL LETTER ENG
614 case 'o': return 0x1d52;
615 case 0x0254: return 0x1d53; // LATIN SMALL LETTER OPEN O
616 case 0x1d16: return 0x1d54; // LATIN SMALL LETTER TOP HALF O
617 case 0x1d17: return 0x1d55; // LATIN SMALL LETTER BOTTOM HALF O
618 case 'p': return 0x1d56;
619 case 't': return 0x1d57;
620 case 'u': return 0x1d58;
621 case 0x1d1d: return 0x1d59; // LATIN SMALL LETTER SIDEWAYS U
622 case 0x1d1f: return 0x1d5a; // LATIN SMALL LETTER SIDEWAYS TURNED M
623 case 'v': return 0x1d5b;
624 case 0x03b2: return 0x1d5d; // GREEK SMALL LETTER BETA
625 case 0x03b3: return 0x1d5e; // GREEK SMALL LETTER GAMMA
626 case 0x03b4: return 0x1d5f; // GREEK SMALL LETTER DELTA
627 case 0x03c6: return 0x1d60; // GREEK SMALL LETTER PHI
628 case 0x03c7: return 0x1d61; // GREEK SMALL LETTER CHI
634 char_type subscript(char_type c)
637 case 'i': return 0x1d62;
638 case 'r': return 0x1d63;
639 case 'u': return 0x1d64;
640 case 'v': return 0x1d65;
641 case 0x03b2: return 0x1d66; // GREEK SMALL LETTER BETA
642 case 0x03b3: return 0x1d67; // GREEK SMALL LETTER GAMMA
643 case 0x03c1: return 0x1d68; // GREEK SMALL LETTER RHO
644 case 0x03c6: return 0x1d69; // GREEK SMALL LETTER PHI
645 case 0x03c7: return 0x1d6a; // GREEK SMALL LETTER CHI
646 case '0': return 0x2080;
647 case '1': return 0x2081;
648 case '2': return 0x2082;
649 case '3': return 0x2083;
650 case '4': return 0x2084;
651 case '5': return 0x2085;
652 case '6': return 0x2086;
653 case '7': return 0x2087;
654 case '8': return 0x2088;
655 case '9': return 0x2089;
656 case '+': return 0x208a;
657 case '-': return 0x208b;
658 case '=': return 0x208c;
659 case '(': return 0x208d;
660 case ')': return 0x208e;
661 case 'a': return 0x2090;
662 case 'e': return 0x2091;
663 case 'o': return 0x2092;
664 case 'x': return 0x2093;
665 case 0x0259: return 0x2093; // LATIN SMALL LETTER SCHWA
671 bool prefixIs(docstring const & a, char_type c)
679 bool prefixIs(string const & a, string const & pre)
681 size_t const prelen = pre.length();
682 size_t const alen = a.length();
683 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
687 bool prefixIs(docstring const & a, docstring const & pre)
689 size_t const prelen = pre.length();
690 size_t const alen = a.length();
691 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
695 bool suffixIs(string const & a, char c)
699 return a[a.length() - 1] == c;
703 bool suffixIs(docstring const & a, char_type c)
707 return a[a.length() - 1] == c;
711 bool suffixIs(string const & a, string const & suf)
713 size_t const suflen = suf.length();
714 size_t const alen = a.length();
715 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
719 bool suffixIs(docstring const & a, docstring const & suf)
721 size_t const suflen = suf.length();
722 size_t const alen = a.length();
723 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
727 bool containsOnly(string const & s, string const & cset)
729 return s.find_first_not_of(cset) == string::npos;
733 // ale970405+lasgoutt-970425
734 // rewritten to use new string (Lgb)
735 string const token(string const & a, char delim, int n)
743 // Find delimiter or end of string
745 if ((i = a.find(delim, i)) == string::npos)
751 // i is now the n'th delim (or string::npos)
752 if (i == string::npos)
755 k = a.find(delim, i);
756 // k is now the n'th + 1 delim (or string::npos)
758 return a.substr(i, k - i);
762 docstring const token(docstring const & a, char_type delim, int n)
770 // Find delimiter or end of string
772 if ((i = a.find(delim, i)) == docstring::npos)
778 // i is now the n'th delim (or string::npos)
779 if (i == docstring::npos)
782 k = a.find(delim, i);
783 // k is now the n'th + 1 delim (or string::npos)
785 return a.substr(i, k - i);
789 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
790 // rewritten to use new string (Lgb)
791 int tokenPos(string const & a, char delim, string const & tok)
797 while (!str.empty()) {
798 str = split(str, tmptok, delim);
807 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
808 // rewritten to use new string (Lgb)
809 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
815 while (!str.empty()) {
816 str = split(str, tmptok, delim);
827 /// Substitute all \a oldchar with \a newchar
828 template<typename Ch> inline
829 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
830 Ch oldchar, Ch newchar)
832 typedef basic_string<Ch> String;
834 typename String::iterator lit = tmp.begin();
835 typename String::iterator end = tmp.end();
836 for (; lit != end; ++lit)
837 if ((*lit) == oldchar)
843 /// Substitute all \a oldchar with \a newchar
844 docstring const subst_char(docstring const & a,
845 docstring::value_type oldchar, docstring::value_type newchar)
848 docstring::iterator lit = tmp.begin();
849 docstring::iterator end = tmp.end();
850 for (; lit != end; ++lit)
851 if ((*lit) == oldchar)
857 /// substitutes all instances of \a oldstr with \a newstr
858 template<typename String> inline
859 String const subst_string(String const & a,
860 String const & oldstr, String const & newstr)
862 LASSERT(!oldstr.empty(), return a);
865 size_t const olen = oldstr.length();
866 while ((i = lstr.find(oldstr, i)) != string::npos) {
867 lstr.replace(i, olen, newstr);
868 i += newstr.length(); // We need to be sure that we dont
869 // use the same i over and over again.
875 docstring const subst_string(docstring const & a,
876 docstring const & oldstr, docstring const & newstr)
878 LASSERT(!oldstr.empty(), return a);
881 size_t const olen = oldstr.length();
882 while ((i = lstr.find(oldstr, i)) != string::npos) {
883 lstr.replace(i, olen, newstr);
884 i += newstr.length(); // We need to be sure that we dont
885 // use the same i over and over again.
893 string const subst(string const & a, char oldchar, char newchar)
895 return subst_char(a, oldchar, newchar);
899 docstring const subst(docstring const & a,
900 char_type oldchar, char_type newchar)
902 return subst_char(a, oldchar, newchar);
906 string const subst(string const & a,
907 string const & oldstr, string const & newstr)
909 return subst_string(a, oldstr, newstr);
913 docstring const subst(docstring const & a,
914 docstring const & oldstr, docstring const & newstr)
916 return subst_string(a, oldstr, newstr);
920 int count_char(string const & str, char chr)
923 string::const_iterator lit = str.begin();
924 string::const_iterator end = str.end();
925 for (; lit != end; ++lit)
932 /// Count all occurences of char \a chr inside \a str
933 int count_char(docstring const & str, docstring::value_type chr)
936 docstring::const_iterator lit = str.begin();
937 docstring::const_iterator end = str.end();
938 for (; lit != end; ++lit)
945 docstring const trim(docstring const & a, char const * p)
947 LASSERT(p, return a);
949 if (a.empty() || !*p)
952 docstring s = from_ascii(p);
953 size_t r = a.find_last_not_of(s);
954 size_t l = a.find_first_not_of(s);
956 // Is this the minimal test? (lgb)
957 if (r == docstring::npos && l == docstring::npos)
960 return a.substr(l, r - l + 1);
964 string const trim(string const & a, char const * p)
966 LASSERT(p, return a);
968 if (a.empty() || !*p)
971 size_t r = a.find_last_not_of(p);
972 size_t l = a.find_first_not_of(p);
974 // Is this the minimal test? (lgb)
975 if (r == string::npos && l == string::npos)
978 return a.substr(l, r - l + 1);
982 string const rtrim(string const & a, char const * p)
984 LASSERT(p, return a);
986 if (a.empty() || !*p)
989 size_t r = a.find_last_not_of(p);
991 // Is this test really needed? (Lgb)
992 if (r == string::npos)
995 return a.substr(0, r + 1);
999 docstring const rtrim(docstring const & a, char const * p)
1001 LASSERT(p, return a);
1003 if (a.empty() || !*p)
1006 size_t r = a.find_last_not_of(from_ascii(p));
1008 // Is this test really needed? (Lgb)
1009 if (r == docstring::npos)
1012 return a.substr(0, r + 1);
1016 string const ltrim(string const & a, char const * p)
1018 LASSERT(p, return a);
1019 if (a.empty() || !*p)
1021 size_t l = a.find_first_not_of(p);
1022 if (l == string::npos)
1024 return a.substr(l, string::npos);
1028 docstring const ltrim(docstring const & a, char const * p)
1030 LASSERT(p, return a);
1031 if (a.empty() || !*p)
1033 size_t l = a.find_first_not_of(from_ascii(p));
1034 if (l == docstring::npos)
1036 return a.substr(l, docstring::npos);
1041 template<typename String, typename Char> inline
1042 String const doSplit(String const & a, String & piece, Char delim)
1045 size_t i = a.find(delim);
1046 if (i == a.length() - 1) {
1047 piece = a.substr(0, i);
1048 } else if (i != String::npos) {
1049 piece = a.substr(0, i);
1050 tmp = a.substr(i + 1);
1051 } else if (i == 0) {
1053 tmp = a.substr(i + 1);
1060 template<typename Char> inline
1061 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
1064 size_t i = a.find(delim);
1065 if (i == a.length() - 1) {
1066 piece = a.substr(0, i);
1067 } else if (i != docstring::npos) {
1068 piece = a.substr(0, i);
1069 tmp = a.substr(i + 1);
1070 } else if (i == 0) {
1072 tmp = a.substr(i + 1);
1082 string const split(string const & a, string & piece, char delim)
1084 return doSplit(a, piece, delim);
1088 docstring const split(docstring const & a, docstring & piece, char_type delim)
1090 return doSplit(a, piece, delim);
1094 string const split(string const & a, char delim)
1097 size_t i = a.find(delim);
1098 if (i != string::npos) // found delim
1099 tmp = a.substr(i + 1);
1105 string const rsplit(string const & a, string & piece, char delim)
1108 size_t i = a.rfind(delim);
1109 if (i != string::npos) { // delimiter was found
1110 piece = a.substr(0, i);
1111 tmp = a.substr(i + 1);
1112 } else { // delimiter was not found
1119 docstring const rsplit(docstring const & a, docstring & piece, char_type delim)
1122 size_t i = a.rfind(delim);
1123 if (i != string::npos) { // delimiter was found
1124 piece = a.substr(0, i);
1125 tmp = a.substr(i + 1);
1126 } else { // delimiter was not found
1133 docstring const rsplit(docstring const & a, char_type delim)
1136 size_t i = a.rfind(delim);
1137 if (i != string::npos)
1138 tmp = a.substr(i + 1);
1143 docstring const escape(docstring const & lab)
1145 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
1146 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1148 for (size_t i = 0; i < lab.length(); ++i) {
1149 char_type c = lab[i];
1150 if (c >= 128 || c == '=' || c == '%' || c == '#' || c == '$'
1151 || c == '}' || c == '{' || c == ']' || c == '[' || c == '&') {
1152 // Although char_type is a 32 bit type we know that
1153 // UCS4 occupies only 21 bits, so we don't need to
1154 // encode bigger values. Test for 2^24 because we
1155 // can encode that with the 6 hex digits that are
1156 // needed for 21 bits anyway.
1157 LASSERT(c < (1 << 24), continue);
1159 enc += hexdigit[(c>>20) & 15];
1160 enc += hexdigit[(c>>16) & 15];
1161 enc += hexdigit[(c>>12) & 15];
1162 enc += hexdigit[(c>> 8) & 15];
1163 enc += hexdigit[(c>> 4) & 15];
1164 enc += hexdigit[ c & 15];
1175 // this doesn't check whether str is empty, so do that first.
1176 vector<docstring> wrapToVec(docstring const & str, int ind,
1179 docstring s = trim(str);
1181 return vector<docstring>();
1185 indent.insert(0, -ind, ' ');
1188 s.insert(0, ind, ' ');
1190 vector<docstring> retval;
1191 while (s.size() > width) {
1192 // find the last space within the first 'width' chars
1193 size_t const i = s.find_last_of(' ', width - 1);
1194 if (i == docstring::npos || i <= size_t(ind)) {
1196 s = s.substr(0, width - 3) + "...";
1199 retval.push_back(s.substr(0, i));
1200 s = indent + s.substr(i);
1201 ind = indent.size();
1204 retval.push_back(s);
1211 docstring wrap(docstring const & str, int const ind, size_t const width)
1213 docstring s = trim(str);
1217 vector<docstring> const svec = wrapToVec(str, ind, width);
1218 return getStringFromVector(svec, from_ascii("\n"));
1222 docstring wrapParas(docstring const & str, int const indent,
1223 size_t const width, size_t const maxlines)
1225 docstring const dots = from_ascii("...");
1229 vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1230 vector<docstring> retval;
1232 vector<docstring>::const_iterator it = pars.begin();
1233 vector<docstring>::const_iterator const en = pars.end();
1234 for (; it != en; ++it) {
1235 vector<docstring> tmp = wrapToVec(*it, indent, width);
1236 size_t const nlines = tmp.size();
1239 size_t const curlines = retval.size();
1240 if (maxlines > 0 && curlines + nlines > maxlines) {
1241 tmp.resize(maxlines - curlines);
1242 docstring last = tmp.back();
1243 size_t const lsize = last.size();
1244 if (lsize > width - 3) {
1245 size_t const i = last.find_last_of(' ', width - 3);
1246 if (i == docstring::npos || i <= size_t(indent))
1248 last = last.substr(0, lsize - 3) + dots;
1250 last = last.substr(0, i) + dots;
1254 tmp.push_back(last);
1256 retval.insert(retval.end(), tmp.begin(), tmp.end());
1257 if (maxlines > 0 && retval.size() >= maxlines)
1260 return getStringFromVector(retval, from_ascii("\n"));
1266 template<typename String> vector<String> const
1267 getVectorFromStringT(String const & str, String const & delim, bool keepempty)
1269 // Lars would like this code to go, but for now his replacement (below)
1270 // doesn't fullfil the same function. I have, therefore, reactivated the
1271 // old code for now. Angus 11 Nov 2002.
1276 String keys = rtrim(str);
1278 size_t const idx = keys.find(delim);
1279 if (idx == String::npos) {
1280 vec.push_back(ltrim(keys));
1283 String const key = trim(keys.substr(0, idx));
1284 if (!key.empty() || keepempty)
1286 size_t const start = idx + delim.size();
1287 keys = keys.substr(start);
1291 typedef boost::char_separator<typename String::value_type> Separator;
1292 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1293 Separator sep(delim.c_str());
1294 Tokenizer tokens(str, sep);
1295 return vector<String>(tokens.begin(), tokens.end());
1300 template<typename String> const String
1301 getStringFromVector(vector<String> const & vec, String const & delim)
1304 typename vector<String>::const_iterator it = vec.begin();
1305 typename vector<String>::const_iterator en = vec.end();
1306 for (; it != en; ++it) {
1307 String item = trim(*it);
1320 vector<string> const getVectorFromString(string const & str,
1321 string const & delim,
1324 return getVectorFromStringT<string>(str, delim, keepempty);
1328 vector<docstring> const getVectorFromString(docstring const & str,
1329 docstring const & delim,
1332 return getVectorFromStringT<docstring>(str, delim, keepempty);
1336 string const getStringFromVector(vector<string> const & vec,
1337 string const & delim)
1339 return getStringFromVector<string>(vec, delim);
1343 docstring const getStringFromVector(vector<docstring> const & vec,
1344 docstring const & delim)
1346 return getStringFromVector<docstring>(vec, delim);
1350 int findToken(char const * const str[], string const & search_token)
1354 while (str[i][0] && str[i] != search_token)
1363 docstring bformat(docstring const & fmt, int arg1)
1365 LATTEST(contains(fmt, from_ascii("%1$d")));
1366 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1367 return subst(str, from_ascii("%%"), from_ascii("%"));
1372 docstring bformat(docstring const & fmt, long arg1)
1374 LATTEST(contains(fmt, from_ascii("%1$d")));
1375 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1376 return subst(str, from_ascii("%%"), from_ascii("%"));
1381 docstring bformat(docstring const & fmt, unsigned int arg1)
1383 LATTEST(contains(fmt, from_ascii("%1$d")));
1384 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1385 return subst(str, from_ascii("%%"), from_ascii("%"));
1390 docstring bformat(docstring const & fmt, docstring arg1)
1392 LATTEST(contains(fmt, from_ascii("%1$s")));
1393 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1394 return subst(str, from_ascii("%%"), from_ascii("%"));
1399 docstring bformat(docstring const & fmt, char * arg1)
1401 LATTEST(contains(fmt, from_ascii("%1$s")));
1402 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1403 return subst(str, from_ascii("%%"), from_ascii("%"));
1408 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1410 LATTEST(contains(fmt, from_ascii("%1$s")));
1411 LATTEST(contains(fmt, from_ascii("%2$s")));
1412 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1413 str = subst(str, from_ascii("%2$s"), arg2);
1414 return subst(str, from_ascii("%%"), from_ascii("%"));
1419 docstring bformat(docstring const & fmt, docstring arg1, int arg2)
1421 LATTEST(contains(fmt, from_ascii("%1$s")));
1422 LATTEST(contains(fmt, from_ascii("%2$d")));
1423 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1424 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1425 return subst(str, from_ascii("%%"), from_ascii("%"));
1430 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1432 LATTEST(contains(fmt, from_ascii("%1$s")));
1433 LATTEST(contains(fmt, from_ascii("%2$s")));
1434 docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1435 str = subst(fmt, from_ascii("%2$s"), arg2);
1436 return subst(str, from_ascii("%%"), from_ascii("%"));
1441 docstring bformat(docstring const & fmt, int arg1, int arg2)
1443 LATTEST(contains(fmt, from_ascii("%1$d")));
1444 LATTEST(contains(fmt, from_ascii("%2$d")));
1445 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1446 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1447 return subst(str, from_ascii("%%"), from_ascii("%"));
1452 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1454 LATTEST(contains(fmt, from_ascii("%1$s")));
1455 LATTEST(contains(fmt, from_ascii("%2$s")));
1456 LATTEST(contains(fmt, from_ascii("%3$s")));
1457 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1458 str = subst(str, from_ascii("%2$s"), arg2);
1459 str = subst(str, from_ascii("%3$s"), arg3);
1460 return subst(str, from_ascii("%%"), from_ascii("%"));
1465 docstring bformat(docstring const & fmt,
1466 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1468 LATTEST(contains(fmt, from_ascii("%1$s")));
1469 LATTEST(contains(fmt, from_ascii("%2$s")));
1470 LATTEST(contains(fmt, from_ascii("%3$s")));
1471 LATTEST(contains(fmt, from_ascii("%4$s")));
1472 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1473 str = subst(str, from_ascii("%2$s"), arg2);
1474 str = subst(str, from_ascii("%3$s"), arg3);
1475 str = subst(str, from_ascii("%4$s"), arg4);
1476 return subst(str, from_ascii("%%"), from_ascii("%"));
1479 } // namespace support