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 caught 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 caught as well
163 return ucs4_to_qchar(c).isNumber();
167 bool isCommonNumberSeparator(char_type c)
170 // assume that no non-utf16 character is a numeral
171 // c outside the UCS4 range is caught as well
173 return ucs4_to_qchar(c).direction() == QChar::DirCS;
177 bool isEuropeanNumberTerminator(char_type c)
180 // assume that no non-utf16 character is a numeral
181 // c outside the UCS4 range is caught 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 caught 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 docstring capitalize(docstring const & s) {
548 char_type t = uppercase(ret[0]);
554 string const ascii_lowercase(string const & a)
557 transform(tmp.begin(), tmp.end(), tmp.begin(),
558 local_ascii_lowercase<char>());
563 docstring const ascii_lowercase(docstring const & a)
566 transform(tmp.begin(), tmp.end(), tmp.begin(),
567 local_ascii_lowercase<char_type>());
572 char_type superscript(char_type c)
575 case '2': return 0x00b2;
576 case '3': return 0x00b3;
577 case '1': return 0x00b9;
578 case '0': return 0x2070;
579 case 'i': return 0x2071;
580 case '4': return 0x2074;
581 case '5': return 0x2075;
582 case '6': return 0x2076;
583 case '7': return 0x2077;
584 case '8': return 0x2078;
585 case '9': return 0x2079;
586 case '+': return 0x207a;
587 case '-': return 0x207b;
588 case '=': return 0x207c;
589 case '(': return 0x207d;
590 case ')': return 0x207e;
591 case 'n': return 0x207f;
592 case 'h': return 0x02b0;
593 case 0x0266: return 0x02b1; // LATIN SMALL LETTER H WITH HOOK
594 case 'j': return 0x02b2;
595 case 'r': return 0x02b3;
596 case 0x0279: return 0x02b4; // LATIN SMALL LETTER TURNED R
597 case 0x027b: return 0x02b5; // LATIN SMALL LETTER TURNED R WITH HOOK
598 case 0x0281: return 0x02b6; // LATIN SMALL LETTER CAPITAL INVERTED R
599 case 'w': return 0x02b7;
600 case 'y': return 0x02b8;
601 // case 0x0294: return 0x02c0; // LATIN LETTER GLOTTAL STOP)
602 // case 0x0295: return 0x02c1; // LATIN LETTER PHARYNGEAL VOICED FRICATIVE
603 // (= LATIN LETTER REVERSED GLOTTAL STOP)
604 case 'l': return 0x02e1;
605 case 's': return 0x02e2;
606 case 'x': return 0x02e3;
607 // case 0x0295: return 0x02e4; // LATIN SMALL LETTER REVERSED GLOTTAL STOP
608 case 'A': return 0x1d2c;
609 case 0x00c6: return 0x1d2d; // LATIN CAPITAL LETTER AE
610 case 'B': return 0x1d2e;
611 case 'D': return 0x1d30;
612 case 'E': return 0x1d31;
613 case 'G': return 0x1d33;
614 case 'H': return 0x1d34;
615 case 'I': return 0x1d35;
616 case 'J': return 0x1d36;
617 case 'K': return 0x1d37;
618 case 'L': return 0x1d38;
619 case 'M': return 0x1d39;
620 case 'N': return 0x1d3a;
621 case 'O': return 0x1d3c;
622 case 'P': return 0x1d3e;
623 case 'R': return 0x1d3f;
624 case 'T': return 0x1d40;
625 case 'U': return 0x1d41;
626 case 'W': return 0x1d42;
627 case 'a': return 0x1d43;
628 case 0x0250: return 0x1d44; // LATIN SMALL LETTER TURNED A
629 case 0x0251: return 0x1d45; // LATIN SMALL LETTER ALPHA
630 case 'b': return 0x1d47;
631 case 'd': return 0x1d48;
632 case 'e': return 0x1d49;
633 case 0x0259: return 0x1d4a; // LATIN SMALL LETTER SCHWA
634 case 0x025b: return 0x1d4b; // LATIN SMALL LETTER OPEN E
635 case 0x1d08: return 0x1d4c; // LATIN SMALL LETTER TURNED OPEN E
636 case 'g': return 0x1d4d;
637 case 0x1d09: return 0x1d4e; // LATIN SMALL LETTER TURNED I
638 case 'k': return 0x1d4f;
639 case 'm': return 0x1d50;
640 case 0x014b: return 0x1d51; // LATIN SMALL LETTER ENG
641 case 'o': return 0x1d52;
642 case 0x0254: return 0x1d53; // LATIN SMALL LETTER OPEN O
643 case 0x1d16: return 0x1d54; // LATIN SMALL LETTER TOP HALF O
644 case 0x1d17: return 0x1d55; // LATIN SMALL LETTER BOTTOM HALF O
645 case 'p': return 0x1d56;
646 case 't': return 0x1d57;
647 case 'u': return 0x1d58;
648 case 0x1d1d: return 0x1d59; // LATIN SMALL LETTER SIDEWAYS U
649 case 0x1d1f: return 0x1d5a; // LATIN SMALL LETTER SIDEWAYS TURNED M
650 case 'v': return 0x1d5b;
651 case 0x03b2: return 0x1d5d; // GREEK SMALL LETTER BETA
652 case 0x03b3: return 0x1d5e; // GREEK SMALL LETTER GAMMA
653 case 0x03b4: return 0x1d5f; // GREEK SMALL LETTER DELTA
654 case 0x03c6: return 0x1d60; // GREEK SMALL LETTER PHI
655 case 0x03c7: return 0x1d61; // GREEK SMALL LETTER CHI
661 char_type subscript(char_type c)
664 case 'i': return 0x1d62;
665 case 'r': return 0x1d63;
666 case 'u': return 0x1d64;
667 case 'v': return 0x1d65;
668 case 0x03b2: return 0x1d66; // GREEK SMALL LETTER BETA
669 case 0x03b3: return 0x1d67; // GREEK SMALL LETTER GAMMA
670 case 0x03c1: return 0x1d68; // GREEK SMALL LETTER RHO
671 case 0x03c6: return 0x1d69; // GREEK SMALL LETTER PHI
672 case 0x03c7: return 0x1d6a; // GREEK SMALL LETTER CHI
673 case '0': return 0x2080;
674 case '1': return 0x2081;
675 case '2': return 0x2082;
676 case '3': return 0x2083;
677 case '4': return 0x2084;
678 case '5': return 0x2085;
679 case '6': return 0x2086;
680 case '7': return 0x2087;
681 case '8': return 0x2088;
682 case '9': return 0x2089;
683 case '+': return 0x208a;
684 case '-': return 0x208b;
685 case '=': return 0x208c;
686 case '(': return 0x208d;
687 case ')': return 0x208e;
688 case 'a': return 0x2090;
689 case 'e': return 0x2091;
690 case 'o': return 0x2092;
691 case 'x': return 0x2093;
692 case 0x0259: return 0x2093; // LATIN SMALL LETTER SCHWA
698 bool prefixIs(docstring const & a, char_type c)
706 bool prefixIs(string const & a, string 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 prefixIs(docstring const & a, docstring const & pre)
716 size_t const prelen = pre.length();
717 size_t const alen = a.length();
718 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
722 bool suffixIs(string const & a, char c)
726 return a[a.length() - 1] == c;
730 bool suffixIs(docstring const & a, char_type c)
734 return a[a.length() - 1] == c;
738 bool suffixIs(string const & a, string 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 suffixIs(docstring const & a, docstring const & suf)
748 size_t const suflen = suf.length();
749 size_t const alen = a.length();
750 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
754 bool containsOnly(string const & s, string const & cset)
756 return s.find_first_not_of(cset) == string::npos;
760 bool containsOnly(docstring const & s, string const & cset)
762 return s.find_first_not_of(from_ascii(cset)) == string::npos;
766 // ale970405+lasgoutt-970425
767 // rewritten to use new string (Lgb)
768 string const token(string const & a, char delim, int n)
776 // Find delimiter or end of string
778 if ((i = a.find(delim, i)) == string::npos)
784 // i is now the n'th delim (or string::npos)
785 if (i == string::npos)
788 k = a.find(delim, i);
789 // k is now the n'th + 1 delim (or string::npos)
791 return a.substr(i, k - i);
795 docstring const token(docstring const & a, char_type delim, int n)
803 // Find delimiter or end of string
805 if ((i = a.find(delim, i)) == docstring::npos)
811 // i is now the n'th delim (or string::npos)
812 if (i == docstring::npos)
815 k = a.find(delim, i);
816 // k is now the n'th + 1 delim (or string::npos)
818 return a.substr(i, k - i);
822 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
823 // rewritten to use new string (Lgb)
824 int tokenPos(string const & a, char delim, string const & tok)
830 while (!str.empty()) {
831 str = split(str, tmptok, delim);
840 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
841 // rewritten to use new string (Lgb)
842 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
848 while (!str.empty()) {
849 str = split(str, tmptok, delim);
860 /// Substitute all \a oldchar with \a newchar
861 template<typename Ch> inline
862 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
863 Ch oldchar, Ch newchar)
865 typedef basic_string<Ch> String;
867 typename String::iterator lit = tmp.begin();
868 typename String::iterator end = tmp.end();
869 for (; lit != end; ++lit)
870 if ((*lit) == oldchar)
876 /// Substitute all \a oldchar with \a newchar
877 docstring const subst_char(docstring const & a,
878 docstring::value_type oldchar, docstring::value_type newchar)
881 docstring::iterator lit = tmp.begin();
882 docstring::iterator end = tmp.end();
883 for (; lit != end; ++lit)
884 if ((*lit) == oldchar)
890 /// substitutes all instances of \a oldstr with \a newstr
891 template<typename String> inline
892 String const subst_string(String const & a,
893 String const & oldstr, String const & newstr)
895 LASSERT(!oldstr.empty(), return a);
898 size_t const olen = oldstr.length();
899 while ((i = lstr.find(oldstr, i)) != string::npos) {
900 lstr.replace(i, olen, newstr);
901 i += newstr.length(); // We need to be sure that we don't
902 // use the same i over and over again.
908 docstring const subst_string(docstring const & a,
909 docstring const & oldstr, docstring const & newstr)
911 LASSERT(!oldstr.empty(), return a);
914 size_t const olen = oldstr.length();
915 while ((i = lstr.find(oldstr, i)) != string::npos) {
916 lstr.replace(i, olen, newstr);
917 i += newstr.length(); // We need to be sure that we don't
918 // use the same i over and over again.
926 string const subst(string const & a, char oldchar, char newchar)
928 return subst_char(a, oldchar, newchar);
932 docstring const subst(docstring const & a,
933 char_type oldchar, char_type newchar)
935 return subst_char(a, oldchar, newchar);
939 string const subst(string const & a,
940 string const & oldstr, string const & newstr)
942 return subst_string(a, oldstr, newstr);
946 docstring const subst(docstring const & a,
947 docstring const & oldstr, docstring const & newstr)
949 return subst_string(a, oldstr, newstr);
953 int count_char(string const & str, char chr)
956 string::const_iterator lit = str.begin();
957 string::const_iterator end = str.end();
958 for (; lit != end; ++lit)
965 /// Count all occurrences of char \a chr inside \a str
966 int count_char(docstring const & str, docstring::value_type chr)
969 docstring::const_iterator lit = str.begin();
970 docstring::const_iterator end = str.end();
971 for (; lit != end; ++lit)
978 int count_bin_chars(string const & str)
980 QString const qstr = toqstr(str).simplified();
982 QString::const_iterator cit = qstr.begin();
983 QString::const_iterator end = qstr.end();
984 for (; cit != end; ++cit) {
985 switch (cit->category()) {
986 case QChar::Separator_Line:
987 case QChar::Separator_Paragraph:
988 case QChar::Other_Control:
989 case QChar::Other_Format:
990 case QChar::Other_Surrogate:
991 case QChar::Other_PrivateUse:
992 case QChar::Other_NotAssigned:
1003 docstring const trim(docstring const & a, char const * p)
1005 LASSERT(p, return a);
1007 if (a.empty() || !*p)
1010 docstring s = from_ascii(p);
1011 size_t r = a.find_last_not_of(s);
1012 size_t l = a.find_first_not_of(s);
1014 // Is this the minimal test? (lgb)
1015 if (r == docstring::npos && l == docstring::npos)
1018 return a.substr(l, r - l + 1);
1022 string const trim(string const & a, char const * p)
1024 LASSERT(p, return a);
1026 if (a.empty() || !*p)
1029 size_t r = a.find_last_not_of(p);
1030 size_t l = a.find_first_not_of(p);
1032 // Is this the minimal test? (lgb)
1033 if (r == string::npos && l == string::npos)
1036 return a.substr(l, r - l + 1);
1040 string const rtrim(string const & a, char const * p)
1042 LASSERT(p, return a);
1044 if (a.empty() || !*p)
1047 size_t r = a.find_last_not_of(p);
1049 // Is this test really needed? (Lgb)
1050 if (r == string::npos)
1053 return a.substr(0, r + 1);
1057 docstring const rtrim(docstring const & a, char const * p)
1059 LASSERT(p, return a);
1061 if (a.empty() || !*p)
1064 size_t r = a.find_last_not_of(from_ascii(p));
1066 // Is this test really needed? (Lgb)
1067 if (r == docstring::npos)
1070 return a.substr(0, r + 1);
1074 string const ltrim(string const & a, char const * p)
1076 LASSERT(p, return a);
1077 if (a.empty() || !*p)
1079 size_t l = a.find_first_not_of(p);
1080 if (l == string::npos)
1082 return a.substr(l, string::npos);
1086 docstring const ltrim(docstring const & a, char const * p)
1088 LASSERT(p, return a);
1089 if (a.empty() || !*p)
1091 size_t l = a.find_first_not_of(from_ascii(p));
1092 if (l == docstring::npos)
1094 return a.substr(l, docstring::npos);
1099 template<typename String, typename Char> inline
1100 String const doSplit(String const & a, String & piece, Char delim)
1103 size_t i = a.find(delim);
1104 if (i == a.length() - 1) {
1105 piece = a.substr(0, i);
1106 } else if (i == 0) {
1108 tmp = a.substr(i + 1);
1109 } else if (i != String::npos) {
1110 piece = a.substr(0, i);
1111 tmp = a.substr(i + 1);
1119 // FIXME: why is this specialization needed?
1120 template<typename Char> inline
1121 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
1124 size_t i = a.find(delim);
1125 if (i == a.length() - 1) {
1126 piece = a.substr(0, i);
1127 } else if (i == 0) {
1129 tmp = a.substr(i + 1);
1130 } else if (i != docstring::npos) {
1131 piece = a.substr(0, i);
1132 tmp = a.substr(i + 1);
1142 string const split(string const & a, string & piece, char delim)
1144 return doSplit(a, piece, delim);
1148 docstring const split(docstring const & a, docstring & piece, char_type delim)
1150 return doSplit(a, piece, delim);
1154 string const split(string const & a, char delim)
1157 size_t i = a.find(delim);
1158 if (i != string::npos) // found delim
1159 tmp = a.substr(i + 1);
1165 string const rsplit(string const & a, string & piece, char delim)
1168 size_t i = a.rfind(delim);
1169 if (i != string::npos) { // delimiter was found
1170 piece = a.substr(0, i);
1171 tmp = a.substr(i + 1);
1172 } else { // delimiter was not found
1179 docstring const rsplit(docstring const & a, docstring & piece, char_type delim)
1182 size_t i = a.rfind(delim);
1183 if (i != string::npos) { // delimiter was found
1184 piece = a.substr(0, i);
1185 tmp = a.substr(i + 1);
1186 } else { // delimiter was not found
1193 docstring const rsplit(docstring const & a, char_type delim)
1196 size_t i = a.rfind(delim);
1197 if (i != string::npos)
1198 tmp = a.substr(i + 1);
1203 docstring const escape(docstring const & lab)
1205 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
1206 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1208 for (char_type const c : lab) {
1209 if (c >= 128 || c == '=' || c == '%' || c == '#' || c == '$'
1210 || c == '}' || c == '{' || c == ']' || c == '[' || c == '&'
1212 // Although char_type is a 32 bit type we know that
1213 // UCS4 occupies only 21 bits, so we don't need to
1214 // encode bigger values. Test for 2^24 because we
1215 // can encode that with the 6 hex digits that are
1216 // needed for 21 bits anyway.
1217 LASSERT(c < (1 << 24), continue);
1219 enc += hexdigit[(c>>20) & 15];
1220 enc += hexdigit[(c>>16) & 15];
1221 enc += hexdigit[(c>>12) & 15];
1222 enc += hexdigit[(c>> 8) & 15];
1223 enc += hexdigit[(c>> 4) & 15];
1224 enc += hexdigit[ c & 15];
1233 docstring const protectArgument(docstring & arg, char const l,
1236 if (contains(arg, l) || contains(arg, r))
1238 arg = '{' + arg + '}';
1243 bool truncateWithEllipsis(docstring & str, size_t const len, bool const mid)
1245 if (str.size() <= len)
1247 if (mid && len > 0) {
1248 size_t const hlen = len / 2;
1249 docstring suffix = str.substr(str.size() - hlen);
1251 str[hlen - 1] = 0x2026;// HORIZONTAL ELLIPSIS
1256 str[len - 1] = 0x2026;// HORIZONTAL ELLIPSIS
1264 // this doesn't check whether str is empty, so do that first.
1265 vector<docstring> wrapToVec(docstring const & str, int ind,
1268 docstring s = trim(str);
1270 return vector<docstring>();
1274 indent.insert(0, -ind, ' ');
1277 s.insert(0, ind, ' ');
1279 vector<docstring> retval;
1280 while (s.size() > width) {
1281 // find the last space within the first 'width' chars
1282 size_t const i = s.find_last_of(' ', width - 1);
1283 if (i == docstring::npos || i <= size_t(ind)) {
1285 truncateWithEllipsis(s, width);
1288 retval.push_back(s.substr(0, i));
1289 s = indent + s.substr(i);
1290 ind = indent.size();
1293 retval.push_back(s);
1300 docstring wrap(docstring const & str, int const ind, size_t const width)
1302 docstring s = trim(str);
1306 vector<docstring> const svec = wrapToVec(str, ind, width);
1307 return getStringFromVector(svec, from_ascii("\n"));
1311 docstring wrapParas(docstring const & str, int const indent,
1312 size_t const width, size_t const maxlines)
1317 vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1318 vector<docstring> retval;
1320 vector<docstring>::const_iterator it = pars.begin();
1321 vector<docstring>::const_iterator const en = pars.end();
1322 for (; it != en; ++it) {
1323 vector<docstring> tmp = wrapToVec(*it, indent, width);
1324 size_t const nlines = tmp.size();
1327 size_t const curlines = retval.size();
1328 if (maxlines > 0 && curlines + nlines > maxlines) {
1329 tmp.resize(maxlines - curlines);
1330 docstring last = tmp.back();
1331 size_t const lsize = last.size();
1332 if (lsize > width - 1) {
1333 size_t const i = last.find_last_of(' ', width - 1);
1334 if (i == docstring::npos || i <= size_t(indent))
1336 truncateWithEllipsis(last, lsize);
1338 truncateWithEllipsis(last, i);
1340 last.push_back(0x2026);//HORIZONTAL ELLIPSIS
1342 tmp.push_back(last);
1344 retval.insert(retval.end(), tmp.begin(), tmp.end());
1345 if (maxlines > 0 && retval.size() >= maxlines)
1348 return getStringFromVector(retval, from_ascii("\n"));
1354 template<typename String> vector<String> const
1355 getVectorFromStringT(String const & str, String const & delim,
1356 bool keepempty, bool trimit)
1358 // Lars would like this code to go, but for now his replacement (below)
1359 // doesn't fullfil the same function. I have, therefore, reactivated the
1360 // old code for now. Angus 11 Nov 2002.
1365 String keys = trimit ? rtrim(str) : str;
1367 size_t const idx = keys.find(delim);
1368 if (idx == String::npos) {
1369 vec.push_back(trimit ? ltrim(keys) : keys);
1372 String const key = trimit ?
1373 trim(keys.substr(0, idx)) : keys.substr(0, idx);
1374 if (!key.empty() || keepempty)
1376 size_t const start = idx + delim.size();
1377 keys = keys.substr(start);
1381 typedef boost::char_separator<typename String::value_type> Separator;
1382 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1383 Separator sep(delim.c_str());
1384 Tokenizer tokens(str, sep);
1385 return vector<String>(tokens.begin(), tokens.end());
1390 template<typename String> const String
1391 getStringFromVector(vector<String> const & vec, String const & delim)
1394 typename vector<String>::const_iterator it = vec.begin();
1395 typename vector<String>::const_iterator en = vec.end();
1396 for (; it != en; ++it) {
1397 String item = trim(*it);
1410 vector<string> const getVectorFromString(string const & str,
1411 string const & delim, bool keepempty, bool trimit)
1413 return getVectorFromStringT<string>(str, delim, keepempty, trimit);
1417 vector<docstring> const getVectorFromString(docstring const & str,
1418 docstring const & delim, bool keepempty, bool trimit)
1420 return getVectorFromStringT<docstring>(str, delim, keepempty, trimit);
1424 string const getStringFromVector(vector<string> const & vec,
1425 string const & delim)
1427 return getStringFromVector<string>(vec, delim);
1431 docstring const getStringFromVector(vector<docstring> const & vec,
1432 docstring const & delim)
1434 return getStringFromVector<docstring>(vec, delim);
1438 int findToken(char const * const str[], string const & search_token)
1442 while (str[i][0] && str[i] != search_token)
1450 std::string formatFPNumber(double x)
1452 // Need manual tweaking, QString::number(x, 'f', 16) does not work either
1455 // Prevent outputs of 23.4200000000000017 but output small numbers
1456 // with at least 6 significant digits.
1457 int const precision = (x == 0.0) ? 0 : max(6 - iround(log10(fabs(x))), 0);
1458 os << std::setprecision(precision) << x;
1459 string result = os.str();
1460 if (result.find('.') != string::npos) {
1461 result = rtrim(result, "0");
1462 if (result[result.length()-1] == '.')
1463 result = rtrim(result, ".");
1469 docstring to_percent_encoding(docstring const & in, docstring const & ex)
1471 QByteArray input = to_utf8(in).c_str();
1472 QByteArray excludes = to_utf8(ex).c_str();
1473 return from_utf8(string(input.toPercentEncoding(excludes).data()));
1477 string from_percent_encoding(string const & in)
1479 return QByteArray::fromPercentEncoding(in.c_str()).data();
1483 docstring bformat(docstring const & fmt, int arg1)
1485 LATTEST(contains(fmt, from_ascii("%1$d")));
1486 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1487 return subst(str, from_ascii("%%"), from_ascii("%"));
1491 docstring bformat(docstring const & fmt, long arg1)
1493 LATTEST(contains(fmt, from_ascii("%1$d")));
1494 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1495 return subst(str, from_ascii("%%"), from_ascii("%"));
1499 #ifdef HAVE_LONG_LONG_INT
1500 docstring bformat(docstring const & fmt, long long arg1)
1502 LATTEST(contains(fmt, from_ascii("%1$d")));
1503 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1504 return subst(str, from_ascii("%%"), from_ascii("%"));
1509 docstring bformat(docstring const & fmt, unsigned int arg1)
1511 LATTEST(contains(fmt, from_ascii("%1$d")));
1512 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1513 return subst(str, from_ascii("%%"), from_ascii("%"));
1517 docstring bformat(docstring const & fmt, docstring const & arg1)
1519 LATTEST(contains(fmt, from_ascii("%1$s")));
1520 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1521 return subst(str, from_ascii("%%"), from_ascii("%"));
1525 docstring bformat(docstring const & fmt, char * arg1)
1527 LATTEST(contains(fmt, from_ascii("%1$s")));
1528 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1529 return subst(str, from_ascii("%%"), from_ascii("%"));
1533 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2)
1535 LATTEST(contains(fmt, from_ascii("%1$s")));
1536 LATTEST(contains(fmt, from_ascii("%2$s")));
1537 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1538 str = subst(str, from_ascii("%2$s"), arg2);
1539 return subst(str, from_ascii("%%"), from_ascii("%"));
1543 docstring bformat(docstring const & fmt, docstring const & arg1, int arg2)
1545 LATTEST(contains(fmt, from_ascii("%1$s")));
1546 LATTEST(contains(fmt, from_ascii("%2$d")));
1547 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1548 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1549 return subst(str, from_ascii("%%"), from_ascii("%"));
1553 docstring bformat(docstring const & fmt, char const * arg1, docstring const & arg2)
1555 return bformat(fmt, from_ascii(arg1), arg2);
1559 docstring bformat(docstring const & fmt, int arg1, int arg2)
1561 LATTEST(contains(fmt, from_ascii("%1$d")));
1562 LATTEST(contains(fmt, from_ascii("%2$d")));
1563 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1564 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1565 return subst(str, from_ascii("%%"), from_ascii("%"));
1569 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2, docstring const & arg3)
1571 LATTEST(contains(fmt, from_ascii("%1$s")));
1572 LATTEST(contains(fmt, from_ascii("%2$s")));
1573 LATTEST(contains(fmt, from_ascii("%3$s")));
1574 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1575 str = subst(str, from_ascii("%2$s"), arg2);
1576 str = subst(str, from_ascii("%3$s"), arg3);
1577 return subst(str, from_ascii("%%"), from_ascii("%"));
1581 docstring bformat(docstring const & fmt,
1582 docstring const & arg1, docstring const & arg2, docstring const & arg3, docstring const & arg4)
1584 LATTEST(contains(fmt, from_ascii("%1$s")));
1585 LATTEST(contains(fmt, from_ascii("%2$s")));
1586 LATTEST(contains(fmt, from_ascii("%3$s")));
1587 LATTEST(contains(fmt, from_ascii("%4$s")));
1588 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1589 str = subst(str, from_ascii("%2$s"), arg2);
1590 str = subst(str, from_ascii("%3$s"), arg3);
1591 str = subst(str, from_ascii("%4$s"), arg4);
1592 return subst(str, from_ascii("%%"), from_ascii("%"));
1595 docstring bformat(docstring const & fmt, docstring const & arg1,
1596 docstring const & arg2, docstring const & arg3,
1597 docstring const & arg4, docstring const & arg5)
1599 LATTEST(contains(fmt, from_ascii("%1$s")));
1600 LATTEST(contains(fmt, from_ascii("%2$s")));
1601 LATTEST(contains(fmt, from_ascii("%3$s")));
1602 LATTEST(contains(fmt, from_ascii("%4$s")));
1603 LATTEST(contains(fmt, from_ascii("%5$s")));
1604 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1605 str = subst(str, from_ascii("%2$s"), arg2);
1606 str = subst(str, from_ascii("%3$s"), arg3);
1607 str = subst(str, from_ascii("%4$s"), arg4);
1608 str = subst(str, from_ascii("%5$s"), arg5);
1609 return subst(str, from_ascii("%%"), from_ascii("%"));
1612 } // namespace support