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/lyxlib.h"
20 #include "support/qstring_helpers.h"
22 #include "support/lassert.h"
37 // Using this allows us to have docstring default arguments in headers
38 // without #include "support/docstring" there.
39 docstring const & empty_docstring()
41 static const docstring s;
45 // Using this allows us to have string default arguments in headers
46 // without #include <string>
47 string const & empty_string()
49 static const string s;
55 * Convert a QChar into a UCS4 character.
56 * This is a hack (it does only make sense for the common part of the UCS4
57 * and UTF16 encodings) and should not be used.
58 * This does only exist because of performance reasons (a real conversion
59 * using iconv is too slow on windows).
61 inline char_type qchar_to_ucs4(QChar const & qchar)
63 LASSERT(is_utf16(static_cast<char_type>(qchar.unicode())), return '?');
64 return static_cast<char_type>(qchar.unicode());
68 * Convert a UCS4 character into a QChar.
69 * This is a hack (it does only make sense for the common part of the UCS4
70 * and UTF16 encodings) and should not be used.
71 * This does only exist because of performance reasons (a real conversion
72 * using iconv is too slow on windows).
74 inline QChar const ucs4_to_qchar(char_type const ucs4)
76 LASSERT(is_utf16(ucs4), return QChar('?'));
77 return QChar(static_cast<unsigned short>(ucs4));
80 /// Maximum valid UCS4 code point
81 char_type const ucs4_max = 0x10ffff;
85 bool isLetterChar(char_type c)
89 // outside the UCS4 range
91 // assume that all non-utf16 characters are letters
94 return ucs4_to_qchar(c).isLetter();
98 bool isLower(char_type c)
102 return ucs4_to_qchar(c).isLower();
106 bool isAlphaASCII(char_type c)
108 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
112 bool isPrintable(char_type c)
116 // outside the UCS4 range
118 // assume that all non-utf16 characters are printable
121 // Not yet recognized by QChar::isPrint()
122 // See https://bugreports.qt-project.org/browse/QTBUG-12144
123 // LATIN CAPITAL LETTER SHARP S
124 else if (c == 0x1e9e)
126 return ucs4_to_qchar(c).isPrint();
130 bool isPrintableNonspace(char_type c)
134 // outside the UCS4 range
136 // assume that all non-utf16 characters are printable and
140 QChar const qc = ucs4_to_qchar(c);
141 return qc.isPrint() && !qc.isSpace();
145 bool isSpace(char_type c)
148 // assume that no non-utf16 character is a space
149 // c outside the UCS4 range is catched as well
152 QChar const qc = ucs4_to_qchar(c);
157 bool isNumber(char_type c)
160 // assume that no non-utf16 character is a numeral
161 // c outside the UCS4 range is catched as well
163 return ucs4_to_qchar(c).isNumber();
167 bool isEuropeanNumberSeparator(char_type c)
170 // assume that no non-utf16 character is a numeral
171 // c outside the UCS4 range is catched as well
173 return ucs4_to_qchar(c).direction() == QChar::DirES;
177 bool isEuropeanNumberTerminator(char_type c)
180 // assume that no non-utf16 character is a numeral
181 // c outside the UCS4 range is catched as well
183 return ucs4_to_qchar(c).direction() == QChar::DirET;
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)
205 bool isOpenPunctuation(char_type c)
208 // assume that no non-utf16 character is an op
209 // c outside the UCS4 range is catched as well
212 QChar const qc = ucs4_to_qchar(c);
213 return qc.category() == QChar::Punctuation_Open;
219 int compare_no_case(docstring const & s, docstring const & s2)
221 docstring::const_iterator p = s.begin();
222 docstring::const_iterator p2 = s2.begin();
224 while (p != s.end() && p2 != s2.end()) {
225 char_type const lc1 = lowercase(*p);
226 char_type const lc2 = lowercase(*p2);
228 return (lc1 < lc2) ? -1 : 1;
233 if (s.size() == s2.size())
235 if (s.size() < s2.size())
241 int compare_locale(docstring const & s, docstring const & s2)
243 return QString::localeAwareCompare(toqstr(s), toqstr(s2));
249 template<typename Char>
250 Char ascii_tolower(Char c) {
251 if (c >= 'A' && c <= 'Z')
252 return c - 'A' + 'a';
259 int compare_ascii_no_case(string const & s, string const & s2)
261 string::const_iterator p = s.begin();
262 string::const_iterator p2 = s2.begin();
264 while (p != s.end() && p2 != s2.end()) {
265 int const lc1 = ascii_tolower(*p);
266 int const lc2 = ascii_tolower(*p2);
268 return (lc1 < lc2) ? -1 : 1;
273 if (s.size() == s2.size())
275 if (s.size() < s2.size())
281 int compare_ascii_no_case(docstring const & s, docstring const & s2)
283 docstring::const_iterator p = s.begin();
284 docstring::const_iterator p2 = s2.begin();
286 while (p != s.end() && p2 != s2.end()) {
287 char_type const lc1 = ascii_tolower(*p);
288 char_type const lc2 = ascii_tolower(*p2);
290 return (lc1 < lc2) ? -1 : 1;
295 if (s.size() == s2.size())
297 if (s.size() < s2.size())
303 bool isStrInt(string const & str)
308 // Remove leading and trailing white space chars.
309 string const tmpstr = trim(str);
313 string::const_iterator cit = tmpstr.begin();
317 string::const_iterator end = tmpstr.end();
318 for (; cit != end; ++cit)
319 if (!isDigitASCII(*cit))
326 bool isStrUnsignedInt(string const & str)
331 // Remove leading and trailing white space chars.
332 string const tmpstr = trim(str);
336 string::const_iterator cit = tmpstr.begin();
337 string::const_iterator end = tmpstr.end();
338 for (; cit != end; ++cit)
339 if (!isDigitASCII(*cit))
346 bool isStrDbl(string const & str)
351 // Remove leading and trailing white space chars.
352 string const tmpstr = trim(str);
355 // if (tmpstr.count('.') > 1) return false;
357 string::const_iterator cit = tmpstr.begin();
358 bool found_dot = false;
361 string::const_iterator end = tmpstr.end();
362 for (; cit != end; ++cit) {
363 if (!isDigitASCII(*cit) && *cit != '.')
375 bool hasDigitASCII(docstring const & str)
377 docstring::const_iterator cit = str.begin();
378 docstring::const_iterator const end = str.end();
379 for (; cit != end; ++cit)
380 if (isDigitASCII(*cit))
386 bool isHexChar(char_type c)
398 c == 'a' || c == 'A' ||
399 c == 'b' || c == 'B' ||
400 c == 'c' || c == 'C' ||
401 c == 'd' || c == 'D' ||
402 c == 'e' || c == 'E' ||
403 c == 'f' || c == 'F';
407 bool isHex(docstring const & str)
411 if (str.length() > 2 && str[0] == '0' &&
412 (str[1] == 'x' || str[1] == 'X'))
415 size_t const len = str.length();
417 for (; index < len; ++index) {
418 if (!isHexChar(str[index]))
425 unsigned int hexToInt(docstring const & str)
427 string s = to_ascii(str);
429 sscanf(s.c_str(), "%x", &h);
434 bool isAscii(docstring const & str)
436 size_t const len = str.length();
437 for (size_t i = 0; i < len; ++i)
444 bool isAscii(string const & str)
446 size_t const len = str.length();
447 for (size_t i = 0; i < len; ++i)
448 if (static_cast<unsigned char>(str[i]) >= 0x80)
454 char lowercase(char c)
456 LASSERT(isASCII(c), return '?');
457 return char(tolower(c));
461 char uppercase(char c)
463 LASSERT(isASCII(c), return '?');
464 return char(toupper(c));
468 char_type lowercase(char_type c)
471 // We don't know how to lowercase a non-utf16 char
473 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
477 char_type uppercase(char_type c)
480 // We don't know how to uppercase a non-utf16 char
482 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
486 bool isLowerCase(char_type ch) {
487 return lowercase(ch) == ch;
491 bool isUpperCase(char_type ch) {
492 return uppercase(ch) == ch;
498 // since we cannot use tolower and toupper directly in the
499 // calls to transform yet, we use these helper clases. (Lgb)
501 struct local_lowercase {
502 char_type operator()(char_type c) const {
507 struct local_uppercase {
508 char_type operator()(char_type c) const {
513 template<typename Char> struct local_ascii_lowercase {
514 Char operator()(Char c) const { return ascii_tolower(c); }
520 docstring const lowercase(docstring const & a)
523 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
528 /* Uncomment here and in lstrings.h if you should need this.
529 string const lowercase(string const & a)
532 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
538 docstring const uppercase(docstring const & a)
541 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
546 string const ascii_lowercase(string const & a)
549 transform(tmp.begin(), tmp.end(), tmp.begin(),
550 local_ascii_lowercase<char>());
555 docstring const ascii_lowercase(docstring const & a)
558 transform(tmp.begin(), tmp.end(), tmp.begin(),
559 local_ascii_lowercase<char_type>());
564 char_type superscript(char_type c)
567 case '2': return 0x00b2;
568 case '3': return 0x00b3;
569 case '1': return 0x00b9;
570 case '0': return 0x2070;
571 case 'i': return 0x2071;
572 case '4': return 0x2074;
573 case '5': return 0x2075;
574 case '6': return 0x2076;
575 case '7': return 0x2077;
576 case '8': return 0x2078;
577 case '9': return 0x2079;
578 case '+': return 0x207a;
579 case '-': return 0x207b;
580 case '=': return 0x207c;
581 case '(': return 0x207d;
582 case ')': return 0x207e;
583 case 'n': return 0x207f;
584 case 'h': return 0x02b0;
585 case 0x0266: return 0x02b1; // LATIN SMALL LETTER H WITH HOOK
586 case 'j': return 0x02b2;
587 case 'r': return 0x02b3;
588 case 0x0279: return 0x02b4; // LATIN SMALL LETTER TURNED R
589 case 0x027b: return 0x02b5; // LATIN SMALL LETTER TURNED R WITH HOOK
590 case 0x0281: return 0x02b6; // LATIN SMALL LETTER CAPITAL INVERTED R
591 case 'w': return 0x02b7;
592 case 'y': return 0x02b8;
593 // case 0x0294: return 0x02c0; // LATIN LETTER GLOTTAL STOP)
594 // case 0x0295: return 0x02c1; // LATIN LETTER PHARYNGEAL VOICED FRICATIVE
595 // (= LATIN LETTER REVERSED GLOTTAL STOP)
596 case 'l': return 0x02e1;
597 case 's': return 0x02e2;
598 case 'x': return 0x02e3;
599 // case 0x0295: return 0x02e4; // LATIN SMALL LETTER REVERSED GLOTTAL STOP
600 case 'A': return 0x1d2c;
601 case 0x00c6: return 0x1d2d; // LATIN CAPITAL LETTER AE
602 case 'B': return 0x1d2e;
603 case 'D': return 0x1d30;
604 case 'E': return 0x1d31;
605 case 'G': return 0x1d33;
606 case 'H': return 0x1d34;
607 case 'I': return 0x1d35;
608 case 'J': return 0x1d36;
609 case 'K': return 0x1d37;
610 case 'L': return 0x1d38;
611 case 'M': return 0x1d39;
612 case 'N': return 0x1d3a;
613 case 'O': return 0x1d3c;
614 case 'P': return 0x1d3e;
615 case 'R': return 0x1d3f;
616 case 'T': return 0x1d40;
617 case 'U': return 0x1d41;
618 case 'W': return 0x1d42;
619 case 'a': return 0x1d43;
620 case 0x0250: return 0x1d44; // LATIN SMALL LETTER TURNED A
621 case 0x0251: return 0x1d45; // LATIN SMALL LETTER ALPHA
622 case 'b': return 0x1d47;
623 case 'd': return 0x1d48;
624 case 'e': return 0x1d49;
625 case 0x0259: return 0x1d4a; // LATIN SMALL LETTER SCHWA
626 case 0x025b: return 0x1d4b; // LATIN SMALL LETTER OPEN E
627 case 0x1d08: return 0x1d4c; // LATIN SMALL LETTER TURNED OPEN E
628 case 'g': return 0x1d4d;
629 case 0x1d09: return 0x1d4e; // LATIN SMALL LETTER TURNED I
630 case 'k': return 0x1d4f;
631 case 'm': return 0x1d50;
632 case 0x014b: return 0x1d51; // LATIN SMALL LETTER ENG
633 case 'o': return 0x1d52;
634 case 0x0254: return 0x1d53; // LATIN SMALL LETTER OPEN O
635 case 0x1d16: return 0x1d54; // LATIN SMALL LETTER TOP HALF O
636 case 0x1d17: return 0x1d55; // LATIN SMALL LETTER BOTTOM HALF O
637 case 'p': return 0x1d56;
638 case 't': return 0x1d57;
639 case 'u': return 0x1d58;
640 case 0x1d1d: return 0x1d59; // LATIN SMALL LETTER SIDEWAYS U
641 case 0x1d1f: return 0x1d5a; // LATIN SMALL LETTER SIDEWAYS TURNED M
642 case 'v': return 0x1d5b;
643 case 0x03b2: return 0x1d5d; // GREEK SMALL LETTER BETA
644 case 0x03b3: return 0x1d5e; // GREEK SMALL LETTER GAMMA
645 case 0x03b4: return 0x1d5f; // GREEK SMALL LETTER DELTA
646 case 0x03c6: return 0x1d60; // GREEK SMALL LETTER PHI
647 case 0x03c7: return 0x1d61; // GREEK SMALL LETTER CHI
653 char_type subscript(char_type c)
656 case 'i': return 0x1d62;
657 case 'r': return 0x1d63;
658 case 'u': return 0x1d64;
659 case 'v': return 0x1d65;
660 case 0x03b2: return 0x1d66; // GREEK SMALL LETTER BETA
661 case 0x03b3: return 0x1d67; // GREEK SMALL LETTER GAMMA
662 case 0x03c1: return 0x1d68; // GREEK SMALL LETTER RHO
663 case 0x03c6: return 0x1d69; // GREEK SMALL LETTER PHI
664 case 0x03c7: return 0x1d6a; // GREEK SMALL LETTER CHI
665 case '0': return 0x2080;
666 case '1': return 0x2081;
667 case '2': return 0x2082;
668 case '3': return 0x2083;
669 case '4': return 0x2084;
670 case '5': return 0x2085;
671 case '6': return 0x2086;
672 case '7': return 0x2087;
673 case '8': return 0x2088;
674 case '9': return 0x2089;
675 case '+': return 0x208a;
676 case '-': return 0x208b;
677 case '=': return 0x208c;
678 case '(': return 0x208d;
679 case ')': return 0x208e;
680 case 'a': return 0x2090;
681 case 'e': return 0x2091;
682 case 'o': return 0x2092;
683 case 'x': return 0x2093;
684 case 0x0259: return 0x2093; // LATIN SMALL LETTER SCHWA
690 bool prefixIs(docstring const & a, char_type c)
698 bool prefixIs(string const & a, string const & pre)
700 size_t const prelen = pre.length();
701 size_t const alen = a.length();
702 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
706 bool prefixIs(docstring const & a, docstring const & pre)
708 size_t const prelen = pre.length();
709 size_t const alen = a.length();
710 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
714 bool suffixIs(string const & a, char c)
718 return a[a.length() - 1] == c;
722 bool suffixIs(docstring const & a, char_type c)
726 return a[a.length() - 1] == c;
730 bool suffixIs(string const & a, string const & suf)
732 size_t const suflen = suf.length();
733 size_t const alen = a.length();
734 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
738 bool suffixIs(docstring const & a, docstring const & suf)
740 size_t const suflen = suf.length();
741 size_t const alen = a.length();
742 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
746 bool containsOnly(string const & s, string const & cset)
748 return s.find_first_not_of(cset) == string::npos;
752 bool containsOnly(docstring const & s, string const & cset)
754 return s.find_first_not_of(from_ascii(cset)) == string::npos;
758 // ale970405+lasgoutt-970425
759 // rewritten to use new string (Lgb)
760 string const token(string const & a, char delim, int n)
768 // Find delimiter or end of string
770 if ((i = a.find(delim, i)) == string::npos)
776 // i is now the n'th delim (or string::npos)
777 if (i == string::npos)
780 k = a.find(delim, i);
781 // k is now the n'th + 1 delim (or string::npos)
783 return a.substr(i, k - i);
787 docstring const token(docstring const & a, char_type delim, int n)
795 // Find delimiter or end of string
797 if ((i = a.find(delim, i)) == docstring::npos)
803 // i is now the n'th delim (or string::npos)
804 if (i == docstring::npos)
807 k = a.find(delim, i);
808 // k is now the n'th + 1 delim (or string::npos)
810 return a.substr(i, k - i);
814 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
815 // rewritten to use new string (Lgb)
816 int tokenPos(string const & a, char delim, string const & tok)
822 while (!str.empty()) {
823 str = split(str, tmptok, delim);
832 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
833 // rewritten to use new string (Lgb)
834 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
840 while (!str.empty()) {
841 str = split(str, tmptok, delim);
852 /// Substitute all \a oldchar with \a newchar
853 template<typename Ch> inline
854 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
855 Ch oldchar, Ch newchar)
857 typedef basic_string<Ch> String;
859 typename String::iterator lit = tmp.begin();
860 typename String::iterator end = tmp.end();
861 for (; lit != end; ++lit)
862 if ((*lit) == oldchar)
868 /// Substitute all \a oldchar with \a newchar
869 docstring const subst_char(docstring const & a,
870 docstring::value_type oldchar, docstring::value_type newchar)
873 docstring::iterator lit = tmp.begin();
874 docstring::iterator end = tmp.end();
875 for (; lit != end; ++lit)
876 if ((*lit) == oldchar)
882 /// substitutes all instances of \a oldstr with \a newstr
883 template<typename String> inline
884 String const subst_string(String const & a,
885 String const & oldstr, String const & newstr)
887 LASSERT(!oldstr.empty(), return a);
890 size_t const olen = oldstr.length();
891 while ((i = lstr.find(oldstr, i)) != string::npos) {
892 lstr.replace(i, olen, newstr);
893 i += newstr.length(); // We need to be sure that we dont
894 // use the same i over and over again.
900 docstring const subst_string(docstring const & a,
901 docstring const & oldstr, docstring const & newstr)
903 LASSERT(!oldstr.empty(), return a);
906 size_t const olen = oldstr.length();
907 while ((i = lstr.find(oldstr, i)) != string::npos) {
908 lstr.replace(i, olen, newstr);
909 i += newstr.length(); // We need to be sure that we dont
910 // use the same i over and over again.
918 string const subst(string const & a, char oldchar, char newchar)
920 return subst_char(a, oldchar, newchar);
924 docstring const subst(docstring const & a,
925 char_type oldchar, char_type newchar)
927 return subst_char(a, oldchar, newchar);
931 string const subst(string const & a,
932 string const & oldstr, string const & newstr)
934 return subst_string(a, oldstr, newstr);
938 docstring const subst(docstring const & a,
939 docstring const & oldstr, docstring const & newstr)
941 return subst_string(a, oldstr, newstr);
945 int count_char(string const & str, char chr)
948 string::const_iterator lit = str.begin();
949 string::const_iterator end = str.end();
950 for (; lit != end; ++lit)
957 /// Count all occurrences of char \a chr inside \a str
958 int count_char(docstring const & str, docstring::value_type chr)
961 docstring::const_iterator lit = str.begin();
962 docstring::const_iterator end = str.end();
963 for (; lit != end; ++lit)
970 int count_bin_chars(string const & str)
972 QString const qstr = toqstr(str).simplified();
974 QString::const_iterator cit = qstr.begin();
975 QString::const_iterator end = qstr.end();
976 for (; cit != end; ++cit) {
977 switch (cit->category()) {
978 case QChar::Separator_Line:
979 case QChar::Separator_Paragraph:
980 case QChar::Other_Control:
981 case QChar::Other_Format:
982 case QChar::Other_Surrogate:
983 case QChar::Other_PrivateUse:
984 case QChar::Other_NotAssigned:
995 docstring const trim(docstring const & a, char const * p)
997 LASSERT(p, return a);
999 if (a.empty() || !*p)
1002 docstring s = from_ascii(p);
1003 size_t r = a.find_last_not_of(s);
1004 size_t l = a.find_first_not_of(s);
1006 // Is this the minimal test? (lgb)
1007 if (r == docstring::npos && l == docstring::npos)
1010 return a.substr(l, r - l + 1);
1014 string const trim(string const & a, char const * p)
1016 LASSERT(p, return a);
1018 if (a.empty() || !*p)
1021 size_t r = a.find_last_not_of(p);
1022 size_t l = a.find_first_not_of(p);
1024 // Is this the minimal test? (lgb)
1025 if (r == string::npos && l == string::npos)
1028 return a.substr(l, r - l + 1);
1032 string const rtrim(string const & a, char const * p)
1034 LASSERT(p, return a);
1036 if (a.empty() || !*p)
1039 size_t r = a.find_last_not_of(p);
1041 // Is this test really needed? (Lgb)
1042 if (r == string::npos)
1045 return a.substr(0, r + 1);
1049 docstring const rtrim(docstring const & a, char const * p)
1051 LASSERT(p, return a);
1053 if (a.empty() || !*p)
1056 size_t r = a.find_last_not_of(from_ascii(p));
1058 // Is this test really needed? (Lgb)
1059 if (r == docstring::npos)
1062 return a.substr(0, r + 1);
1066 string const ltrim(string const & a, char const * p)
1068 LASSERT(p, return a);
1069 if (a.empty() || !*p)
1071 size_t l = a.find_first_not_of(p);
1072 if (l == string::npos)
1074 return a.substr(l, string::npos);
1078 docstring const ltrim(docstring const & a, char const * p)
1080 LASSERT(p, return a);
1081 if (a.empty() || !*p)
1083 size_t l = a.find_first_not_of(from_ascii(p));
1084 if (l == docstring::npos)
1086 return a.substr(l, docstring::npos);
1091 template<typename String, typename Char> inline
1092 String const doSplit(String const & a, String & piece, Char delim)
1095 size_t i = a.find(delim);
1096 if (i == a.length() - 1) {
1097 piece = a.substr(0, i);
1098 } else if (i == 0) {
1100 tmp = a.substr(i + 1);
1101 } else if (i != String::npos) {
1102 piece = a.substr(0, i);
1103 tmp = a.substr(i + 1);
1111 // FIXME: why is this specialization needed?
1112 template<typename Char> inline
1113 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
1116 size_t i = a.find(delim);
1117 if (i == a.length() - 1) {
1118 piece = a.substr(0, i);
1119 } else if (i == 0) {
1121 tmp = a.substr(i + 1);
1122 } else if (i != docstring::npos) {
1123 piece = a.substr(0, i);
1124 tmp = a.substr(i + 1);
1134 string const split(string const & a, string & piece, char delim)
1136 return doSplit(a, piece, delim);
1140 docstring const split(docstring const & a, docstring & piece, char_type delim)
1142 return doSplit(a, piece, delim);
1146 string const split(string const & a, char delim)
1149 size_t i = a.find(delim);
1150 if (i != string::npos) // found delim
1151 tmp = a.substr(i + 1);
1157 string const rsplit(string const & a, string & piece, char delim)
1160 size_t i = a.rfind(delim);
1161 if (i != string::npos) { // delimiter was found
1162 piece = a.substr(0, i);
1163 tmp = a.substr(i + 1);
1164 } else { // delimiter was not found
1171 docstring const rsplit(docstring const & a, docstring & piece, char_type delim)
1174 size_t i = a.rfind(delim);
1175 if (i != string::npos) { // delimiter was found
1176 piece = a.substr(0, i);
1177 tmp = a.substr(i + 1);
1178 } else { // delimiter was not found
1185 docstring const rsplit(docstring const & a, char_type delim)
1188 size_t i = a.rfind(delim);
1189 if (i != string::npos)
1190 tmp = a.substr(i + 1);
1195 docstring const escape(docstring const & lab)
1197 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
1198 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1200 for (size_t i = 0; i < lab.length(); ++i) {
1201 char_type c = lab[i];
1202 if (c >= 128 || c == '=' || c == '%' || c == '#' || c == '$'
1203 || c == '}' || c == '{' || c == ']' || c == '[' || c == '&'
1205 // Although char_type is a 32 bit type we know that
1206 // UCS4 occupies only 21 bits, so we don't need to
1207 // encode bigger values. Test for 2^24 because we
1208 // can encode that with the 6 hex digits that are
1209 // needed for 21 bits anyway.
1210 LASSERT(c < (1 << 24), continue);
1212 enc += hexdigit[(c>>20) & 15];
1213 enc += hexdigit[(c>>16) & 15];
1214 enc += hexdigit[(c>>12) & 15];
1215 enc += hexdigit[(c>> 8) & 15];
1216 enc += hexdigit[(c>> 4) & 15];
1217 enc += hexdigit[ c & 15];
1226 docstring const protectArgument(docstring & arg, char const l,
1229 if (contains(arg, l) || contains(arg, r))
1231 arg = '{' + arg + '}';
1236 bool truncateWithEllipsis(docstring & str, size_t const len, bool const mid)
1238 if (str.size() <= len)
1240 if (mid && len > 0) {
1241 size_t const hlen = len / 2;
1242 docstring suffix = str.substr(str.size() - hlen);
1244 str[hlen - 1] = 0x2026;// HORIZONTAL ELLIPSIS
1249 str[len - 1] = 0x2026;// HORIZONTAL ELLIPSIS
1257 // this doesn't check whether str is empty, so do that first.
1258 vector<docstring> wrapToVec(docstring const & str, int ind,
1261 docstring s = trim(str);
1263 return vector<docstring>();
1267 indent.insert(0, -ind, ' ');
1270 s.insert(0, ind, ' ');
1272 vector<docstring> retval;
1273 while (s.size() > width) {
1274 // find the last space within the first 'width' chars
1275 size_t const i = s.find_last_of(' ', width - 1);
1276 if (i == docstring::npos || i <= size_t(ind)) {
1278 truncateWithEllipsis(s, width);
1281 retval.push_back(s.substr(0, i));
1282 s = indent + s.substr(i);
1283 ind = indent.size();
1286 retval.push_back(s);
1293 docstring wrap(docstring const & str, int const ind, size_t const width)
1295 docstring s = trim(str);
1299 vector<docstring> const svec = wrapToVec(str, ind, width);
1300 return getStringFromVector(svec, from_ascii("\n"));
1304 docstring wrapParas(docstring const & str, int const indent,
1305 size_t const width, size_t const maxlines)
1310 vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1311 vector<docstring> retval;
1313 vector<docstring>::const_iterator it = pars.begin();
1314 vector<docstring>::const_iterator const en = pars.end();
1315 for (; it != en; ++it) {
1316 vector<docstring> tmp = wrapToVec(*it, indent, width);
1317 size_t const nlines = tmp.size();
1320 size_t const curlines = retval.size();
1321 if (maxlines > 0 && curlines + nlines > maxlines) {
1322 tmp.resize(maxlines - curlines);
1323 docstring last = tmp.back();
1324 size_t const lsize = last.size();
1325 if (lsize > width - 1) {
1326 size_t const i = last.find_last_of(' ', width - 1);
1327 if (i == docstring::npos || i <= size_t(indent))
1329 truncateWithEllipsis(last, lsize);
1331 truncateWithEllipsis(last, i);
1333 last.push_back(0x2026);//HORIZONTAL ELLIPSIS
1335 tmp.push_back(last);
1337 retval.insert(retval.end(), tmp.begin(), tmp.end());
1338 if (maxlines > 0 && retval.size() >= maxlines)
1341 return getStringFromVector(retval, from_ascii("\n"));
1347 template<typename String> vector<String> const
1348 getVectorFromStringT(String const & str, String const & delim,
1349 bool keepempty, bool trimit)
1351 // Lars would like this code to go, but for now his replacement (below)
1352 // doesn't fullfil the same function. I have, therefore, reactivated the
1353 // old code for now. Angus 11 Nov 2002.
1358 String keys = trimit ? rtrim(str) : str;
1360 size_t const idx = keys.find(delim);
1361 if (idx == String::npos) {
1362 vec.push_back(trimit ? ltrim(keys) : keys);
1365 String const key = trimit ?
1366 trim(keys.substr(0, idx)) : keys.substr(0, idx);
1367 if (!key.empty() || keepempty)
1369 size_t const start = idx + delim.size();
1370 keys = keys.substr(start);
1374 typedef boost::char_separator<typename String::value_type> Separator;
1375 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1376 Separator sep(delim.c_str());
1377 Tokenizer tokens(str, sep);
1378 return vector<String>(tokens.begin(), tokens.end());
1383 template<typename String> const String
1384 getStringFromVector(vector<String> const & vec, String const & delim)
1387 typename vector<String>::const_iterator it = vec.begin();
1388 typename vector<String>::const_iterator en = vec.end();
1389 for (; it != en; ++it) {
1390 String item = trim(*it);
1403 vector<string> const getVectorFromString(string const & str,
1404 string const & delim, bool keepempty, bool trimit)
1406 return getVectorFromStringT<string>(str, delim, keepempty, trimit);
1410 vector<docstring> const getVectorFromString(docstring const & str,
1411 docstring const & delim, bool keepempty, bool trimit)
1413 return getVectorFromStringT<docstring>(str, delim, keepempty, trimit);
1417 string const getStringFromVector(vector<string> const & vec,
1418 string const & delim)
1420 return getStringFromVector<string>(vec, delim);
1424 docstring const getStringFromVector(vector<docstring> const & vec,
1425 docstring const & delim)
1427 return getStringFromVector<docstring>(vec, delim);
1431 int findToken(char const * const str[], string const & search_token)
1435 while (str[i][0] && str[i] != search_token)
1443 std::string formatFPNumber(double x)
1445 // Need manual tweaking, QString::number(x, 'f', 16) does not work either
1448 // Prevent outputs of 23.4200000000000017 but output small numbers
1449 // with at least 6 significant digits.
1450 double const logarithm = log10(fabs(x));
1451 os << std::setprecision(max(6 - iround(logarithm), 0)) << x;
1452 string result = os.str();
1453 if (result.find('.') != string::npos) {
1454 result = rtrim(result, "0");
1455 if (result[result.length()-1] == '.')
1456 result = rtrim(result, ".");
1462 docstring to_percent_encoding(docstring const & in, docstring const & ex)
1464 QByteArray input = to_utf8(in).c_str();
1465 QByteArray excludes = to_utf8(ex).c_str();
1466 return from_utf8(string(input.toPercentEncoding(excludes).data()));
1470 string from_percent_encoding(string const & in)
1472 return QByteArray::fromPercentEncoding(in.c_str()).data();
1476 docstring bformat(docstring const & fmt, int arg1)
1478 LATTEST(contains(fmt, from_ascii("%1$d")));
1479 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1480 return subst(str, from_ascii("%%"), from_ascii("%"));
1484 docstring bformat(docstring const & fmt, long arg1)
1486 LATTEST(contains(fmt, from_ascii("%1$d")));
1487 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1488 return subst(str, from_ascii("%%"), from_ascii("%"));
1492 #ifdef HAVE_LONG_LONG_INT
1493 docstring bformat(docstring const & fmt, long long arg1)
1495 LATTEST(contains(fmt, from_ascii("%1$d")));
1496 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1497 return subst(str, from_ascii("%%"), from_ascii("%"));
1502 docstring bformat(docstring const & fmt, unsigned int arg1)
1504 LATTEST(contains(fmt, from_ascii("%1$d")));
1505 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1506 return subst(str, from_ascii("%%"), from_ascii("%"));
1510 docstring bformat(docstring const & fmt, docstring const & arg1)
1512 LATTEST(contains(fmt, from_ascii("%1$s")));
1513 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1514 return subst(str, from_ascii("%%"), from_ascii("%"));
1518 docstring bformat(docstring const & fmt, char * arg1)
1520 LATTEST(contains(fmt, from_ascii("%1$s")));
1521 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1522 return subst(str, from_ascii("%%"), from_ascii("%"));
1526 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2)
1528 LATTEST(contains(fmt, from_ascii("%1$s")));
1529 LATTEST(contains(fmt, from_ascii("%2$s")));
1530 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1531 str = subst(str, from_ascii("%2$s"), arg2);
1532 return subst(str, from_ascii("%%"), from_ascii("%"));
1536 docstring bformat(docstring const & fmt, docstring const & arg1, int arg2)
1538 LATTEST(contains(fmt, from_ascii("%1$s")));
1539 LATTEST(contains(fmt, from_ascii("%2$d")));
1540 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1541 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1542 return subst(str, from_ascii("%%"), from_ascii("%"));
1546 docstring bformat(docstring const & fmt, char const * arg1, docstring const & arg2)
1548 LATTEST(contains(fmt, from_ascii("%1$s")));
1549 LATTEST(contains(fmt, from_ascii("%2$s")));
1550 docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1551 str = subst(str, from_ascii("%2$s"), arg2);
1552 return subst(str, from_ascii("%%"), from_ascii("%"));
1556 docstring bformat(docstring const & fmt, int arg1, int arg2)
1558 LATTEST(contains(fmt, from_ascii("%1$d")));
1559 LATTEST(contains(fmt, from_ascii("%2$d")));
1560 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1561 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1562 return subst(str, from_ascii("%%"), from_ascii("%"));
1566 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2, docstring const & arg3)
1568 LATTEST(contains(fmt, from_ascii("%1$s")));
1569 LATTEST(contains(fmt, from_ascii("%2$s")));
1570 LATTEST(contains(fmt, from_ascii("%3$s")));
1571 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1572 str = subst(str, from_ascii("%2$s"), arg2);
1573 str = subst(str, from_ascii("%3$s"), arg3);
1574 return subst(str, from_ascii("%%"), from_ascii("%"));
1578 docstring bformat(docstring const & fmt,
1579 docstring const & arg1, docstring const & arg2, docstring const & arg3, docstring const & arg4)
1581 LATTEST(contains(fmt, from_ascii("%1$s")));
1582 LATTEST(contains(fmt, from_ascii("%2$s")));
1583 LATTEST(contains(fmt, from_ascii("%3$s")));
1584 LATTEST(contains(fmt, from_ascii("%4$s")));
1585 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1586 str = subst(str, from_ascii("%2$s"), arg2);
1587 str = subst(str, from_ascii("%3$s"), arg3);
1588 str = subst(str, from_ascii("%4$s"), arg4);
1589 return subst(str, from_ascii("%%"), from_ascii("%"));
1592 docstring bformat(docstring const & fmt, docstring const & arg1,
1593 docstring const & arg2, docstring const & arg3,
1594 docstring const & arg4, docstring const & arg5)
1596 LATTEST(contains(fmt, from_ascii("%1$s")));
1597 LATTEST(contains(fmt, from_ascii("%2$s")));
1598 LATTEST(contains(fmt, from_ascii("%3$s")));
1599 LATTEST(contains(fmt, from_ascii("%4$s")));
1600 LATTEST(contains(fmt, from_ascii("%5$s")));
1601 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1602 str = subst(str, from_ascii("%2$s"), arg2);
1603 str = subst(str, from_ascii("%3$s"), arg3);
1604 str = subst(str, from_ascii("%4$s"), arg4);
1605 str = subst(str, from_ascii("%5$s"), arg5);
1606 return subst(str, from_ascii("%%"), from_ascii("%"));
1609 } // namespace support