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';
192 bool isNumberChar(char_type c)
195 // outside the UCS4 range
197 return ucs4_to_qchar(c).isNumber();
200 bool isAlnumASCII(char_type c)
202 return isAlphaASCII(c) || isDigitASCII(c);
206 bool isASCII(char_type c)
212 bool isOpenPunctuation(char_type c)
215 // assume that no non-utf16 character is an op
216 // c outside the UCS4 range is caught as well
219 QChar const qc = ucs4_to_qchar(c);
220 return qc.category() == QChar::Punctuation_Open;
226 int compare_no_case(docstring const & s, docstring const & s2)
228 docstring::const_iterator p = s.begin();
229 docstring::const_iterator p2 = s2.begin();
231 while (p != s.end() && p2 != s2.end()) {
232 char_type const lc1 = lowercase(*p);
233 char_type const lc2 = lowercase(*p2);
235 return (lc1 < lc2) ? -1 : 1;
240 if (s.size() == s2.size())
242 if (s.size() < s2.size())
248 int compare_locale(docstring const & s, docstring const & s2)
250 return QString::localeAwareCompare(toqstr(s), toqstr(s2));
256 template<typename Char>
257 Char ascii_tolower(Char c) {
258 if (c >= 'A' && c <= 'Z')
259 return c - 'A' + 'a';
266 int compare_ascii_no_case(string const & s, string const & s2)
268 string::const_iterator p = s.begin();
269 string::const_iterator p2 = s2.begin();
271 while (p != s.end() && p2 != s2.end()) {
272 int const lc1 = ascii_tolower(*p);
273 int const lc2 = ascii_tolower(*p2);
275 return (lc1 < lc2) ? -1 : 1;
280 if (s.size() == s2.size())
282 if (s.size() < s2.size())
288 int compare_ascii_no_case(docstring const & s, docstring const & s2)
290 docstring::const_iterator p = s.begin();
291 docstring::const_iterator p2 = s2.begin();
293 while (p != s.end() && p2 != s2.end()) {
294 char_type const lc1 = ascii_tolower(*p);
295 char_type const lc2 = ascii_tolower(*p2);
297 return (lc1 < lc2) ? -1 : 1;
302 if (s.size() == s2.size())
304 if (s.size() < s2.size())
310 bool isStrInt(string const & str)
315 // Remove leading and trailing white space chars.
316 string const tmpstr = trim(str);
320 string::const_iterator cit = tmpstr.begin();
324 string::const_iterator end = tmpstr.end();
325 for (; cit != end; ++cit)
326 if (!isDigitASCII(*cit))
333 bool isStrUnsignedInt(string const & str)
338 // Remove leading and trailing white space chars.
339 string const tmpstr = trim(str);
343 string::const_iterator cit = tmpstr.begin();
344 string::const_iterator end = tmpstr.end();
345 for (; cit != end; ++cit)
346 if (!isDigitASCII(*cit))
353 bool isStrDbl(string const & str)
358 // Remove leading and trailing white space chars.
359 string const tmpstr = trim(str);
362 // if (tmpstr.count('.') > 1) return false;
364 string::const_iterator cit = tmpstr.begin();
365 bool found_dot = false;
368 string::const_iterator end = tmpstr.end();
369 for (; cit != end; ++cit) {
370 if (!isDigitASCII(*cit) && *cit != '.')
382 bool hasDigitASCII(docstring const & str)
384 docstring::const_iterator cit = str.begin();
385 docstring::const_iterator const end = str.end();
386 for (; cit != end; ++cit)
387 if (isDigitASCII(*cit))
393 bool isHexChar(char_type c)
405 c == 'a' || c == 'A' ||
406 c == 'b' || c == 'B' ||
407 c == 'c' || c == 'C' ||
408 c == 'd' || c == 'D' ||
409 c == 'e' || c == 'E' ||
410 c == 'f' || c == 'F';
414 bool isHex(docstring const & str)
418 if (str.length() > 2 && str[0] == '0' &&
419 (str[1] == 'x' || str[1] == 'X'))
422 size_t const len = str.length();
424 for (; index < len; ++index) {
425 if (!isHexChar(str[index]))
432 unsigned int hexToInt(docstring const & str)
434 string s = to_ascii(str);
436 sscanf(s.c_str(), "%x", &h);
441 bool isAscii(docstring const & str)
443 size_t const len = str.length();
444 for (size_t i = 0; i < len; ++i)
451 bool isAscii(string const & str)
453 size_t const len = str.length();
454 for (size_t i = 0; i < len; ++i)
455 if (static_cast<unsigned char>(str[i]) >= 0x80)
461 char lowercase(char c)
463 LASSERT(isASCII(c), return '?');
464 return char(tolower(c));
468 char uppercase(char c)
470 LASSERT(isASCII(c), return '?');
471 return char(toupper(c));
475 char_type lowercase(char_type c)
478 // We don't know how to lowercase a non-utf16 char
480 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
484 char_type uppercase(char_type c)
487 // We don't know how to uppercase a non-utf16 char
489 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
493 bool isLowerCase(char_type ch) {
494 return lowercase(ch) == ch;
498 bool isUpperCase(char_type ch) {
499 return uppercase(ch) == ch;
505 // since we cannot use tolower and toupper directly in the
506 // calls to transform yet, we use these helper clases. (Lgb)
508 struct local_lowercase {
509 char_type operator()(char_type c) const {
514 struct local_uppercase {
515 char_type operator()(char_type c) const {
520 template<typename Char> struct local_ascii_lowercase {
521 Char operator()(Char c) const { return ascii_tolower(c); }
527 docstring const lowercase(docstring const & a)
530 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
535 /* Uncomment here and in lstrings.h if you should need this.
536 string const lowercase(string const & a)
539 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
545 docstring const uppercase(docstring const & a)
548 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
553 docstring capitalize(docstring const & s) {
555 char_type t = uppercase(ret[0]);
561 string const ascii_lowercase(string const & a)
564 transform(tmp.begin(), tmp.end(), tmp.begin(),
565 local_ascii_lowercase<char>());
570 docstring const ascii_lowercase(docstring const & a)
573 transform(tmp.begin(), tmp.end(), tmp.begin(),
574 local_ascii_lowercase<char_type>());
579 char_type superscript(char_type c)
582 case '2': return 0x00b2;
583 case '3': return 0x00b3;
584 case '1': return 0x00b9;
585 case '0': return 0x2070;
586 case 'i': return 0x2071;
587 case '4': return 0x2074;
588 case '5': return 0x2075;
589 case '6': return 0x2076;
590 case '7': return 0x2077;
591 case '8': return 0x2078;
592 case '9': return 0x2079;
593 case '+': return 0x207a;
594 case '-': return 0x207b;
595 case '=': return 0x207c;
596 case '(': return 0x207d;
597 case ')': return 0x207e;
598 case 'n': return 0x207f;
599 case 'h': return 0x02b0;
600 case 0x0266: return 0x02b1; // LATIN SMALL LETTER H WITH HOOK
601 case 'j': return 0x02b2;
602 case 'r': return 0x02b3;
603 case 0x0279: return 0x02b4; // LATIN SMALL LETTER TURNED R
604 case 0x027b: return 0x02b5; // LATIN SMALL LETTER TURNED R WITH HOOK
605 case 0x0281: return 0x02b6; // LATIN SMALL LETTER CAPITAL INVERTED R
606 case 'w': return 0x02b7;
607 case 'y': return 0x02b8;
608 // case 0x0294: return 0x02c0; // LATIN LETTER GLOTTAL STOP)
609 // case 0x0295: return 0x02c1; // LATIN LETTER PHARYNGEAL VOICED FRICATIVE
610 // (= LATIN LETTER REVERSED GLOTTAL STOP)
611 case 'l': return 0x02e1;
612 case 's': return 0x02e2;
613 case 'x': return 0x02e3;
614 // case 0x0295: return 0x02e4; // LATIN SMALL LETTER REVERSED GLOTTAL STOP
615 case 'A': return 0x1d2c;
616 case 0x00c6: return 0x1d2d; // LATIN CAPITAL LETTER AE
617 case 'B': return 0x1d2e;
618 case 'D': return 0x1d30;
619 case 'E': return 0x1d31;
620 case 'G': return 0x1d33;
621 case 'H': return 0x1d34;
622 case 'I': return 0x1d35;
623 case 'J': return 0x1d36;
624 case 'K': return 0x1d37;
625 case 'L': return 0x1d38;
626 case 'M': return 0x1d39;
627 case 'N': return 0x1d3a;
628 case 'O': return 0x1d3c;
629 case 'P': return 0x1d3e;
630 case 'R': return 0x1d3f;
631 case 'T': return 0x1d40;
632 case 'U': return 0x1d41;
633 case 'W': return 0x1d42;
634 case 'a': return 0x1d43;
635 case 0x0250: return 0x1d44; // LATIN SMALL LETTER TURNED A
636 case 0x0251: return 0x1d45; // LATIN SMALL LETTER ALPHA
637 case 'b': return 0x1d47;
638 case 'd': return 0x1d48;
639 case 'e': return 0x1d49;
640 case 0x0259: return 0x1d4a; // LATIN SMALL LETTER SCHWA
641 case 0x025b: return 0x1d4b; // LATIN SMALL LETTER OPEN E
642 case 0x1d08: return 0x1d4c; // LATIN SMALL LETTER TURNED OPEN E
643 case 'g': return 0x1d4d;
644 case 0x1d09: return 0x1d4e; // LATIN SMALL LETTER TURNED I
645 case 'k': return 0x1d4f;
646 case 'm': return 0x1d50;
647 case 0x014b: return 0x1d51; // LATIN SMALL LETTER ENG
648 case 'o': return 0x1d52;
649 case 0x0254: return 0x1d53; // LATIN SMALL LETTER OPEN O
650 case 0x1d16: return 0x1d54; // LATIN SMALL LETTER TOP HALF O
651 case 0x1d17: return 0x1d55; // LATIN SMALL LETTER BOTTOM HALF O
652 case 'p': return 0x1d56;
653 case 't': return 0x1d57;
654 case 'u': return 0x1d58;
655 case 0x1d1d: return 0x1d59; // LATIN SMALL LETTER SIDEWAYS U
656 case 0x1d1f: return 0x1d5a; // LATIN SMALL LETTER SIDEWAYS TURNED M
657 case 'v': return 0x1d5b;
658 case 0x03b2: return 0x1d5d; // GREEK SMALL LETTER BETA
659 case 0x03b3: return 0x1d5e; // GREEK SMALL LETTER GAMMA
660 case 0x03b4: return 0x1d5f; // GREEK SMALL LETTER DELTA
661 case 0x03c6: return 0x1d60; // GREEK SMALL LETTER PHI
662 case 0x03c7: return 0x1d61; // GREEK SMALL LETTER CHI
668 char_type subscript(char_type c)
671 case 'i': return 0x1d62;
672 case 'r': return 0x1d63;
673 case 'u': return 0x1d64;
674 case 'v': return 0x1d65;
675 case 0x03b2: return 0x1d66; // GREEK SMALL LETTER BETA
676 case 0x03b3: return 0x1d67; // GREEK SMALL LETTER GAMMA
677 case 0x03c1: return 0x1d68; // GREEK SMALL LETTER RHO
678 case 0x03c6: return 0x1d69; // GREEK SMALL LETTER PHI
679 case 0x03c7: return 0x1d6a; // GREEK SMALL LETTER CHI
680 case '0': return 0x2080;
681 case '1': return 0x2081;
682 case '2': return 0x2082;
683 case '3': return 0x2083;
684 case '4': return 0x2084;
685 case '5': return 0x2085;
686 case '6': return 0x2086;
687 case '7': return 0x2087;
688 case '8': return 0x2088;
689 case '9': return 0x2089;
690 case '+': return 0x208a;
691 case '-': return 0x208b;
692 case '=': return 0x208c;
693 case '(': return 0x208d;
694 case ')': return 0x208e;
695 case 'a': return 0x2090;
696 case 'e': return 0x2091;
697 case 'o': return 0x2092;
698 case 'x': return 0x2093;
699 case 0x0259: return 0x2093; // LATIN SMALL LETTER SCHWA
705 bool prefixIs(docstring const & a, char_type c)
713 bool prefixIs(string const & a, string const & pre)
715 size_t const prelen = pre.length();
716 size_t const alen = a.length();
717 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
721 bool prefixIs(docstring const & a, docstring const & pre)
723 size_t const prelen = pre.length();
724 size_t const alen = a.length();
725 return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
729 bool suffixIs(string const & a, char c)
733 return a[a.length() - 1] == c;
737 bool suffixIs(docstring const & a, char_type c)
741 return a[a.length() - 1] == c;
745 bool suffixIs(string const & a, string const & suf)
747 size_t const suflen = suf.length();
748 size_t const alen = a.length();
749 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
753 bool suffixIs(docstring const & a, docstring const & suf)
755 size_t const suflen = suf.length();
756 size_t const alen = a.length();
757 return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
761 bool containsOnly(string const & s, string const & cset)
763 return s.find_first_not_of(cset) == string::npos;
767 bool containsOnly(docstring const & s, string const & cset)
769 return s.find_first_not_of(from_ascii(cset)) == string::npos;
773 // ale970405+lasgoutt-970425
774 // rewritten to use new string (Lgb)
775 string const token(string const & a, char delim, int n)
783 // Find delimiter or end of string
785 if ((i = a.find(delim, i)) == string::npos)
791 // i is now the n'th delim (or string::npos)
792 if (i == string::npos)
795 k = a.find(delim, i);
796 // k is now the n'th + 1 delim (or string::npos)
798 return a.substr(i, k - i);
802 docstring const token(docstring const & a, char_type delim, int n)
810 // Find delimiter or end of string
812 if ((i = a.find(delim, i)) == docstring::npos)
818 // i is now the n'th delim (or string::npos)
819 if (i == docstring::npos)
822 k = a.find(delim, i);
823 // k is now the n'th + 1 delim (or string::npos)
825 return a.substr(i, k - i);
829 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
830 // rewritten to use new string (Lgb)
831 int tokenPos(string const & a, char delim, string const & tok)
837 while (!str.empty()) {
838 str = split(str, tmptok, delim);
847 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
848 // rewritten to use new string (Lgb)
849 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
855 while (!str.empty()) {
856 str = split(str, tmptok, delim);
867 /// Substitute all \a oldchar with \a newchar
868 template<typename Ch> inline
869 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
870 Ch oldchar, Ch newchar)
872 typedef basic_string<Ch> String;
874 typename String::iterator lit = tmp.begin();
875 typename String::iterator end = tmp.end();
876 for (; lit != end; ++lit)
877 if ((*lit) == oldchar)
883 /// Substitute all \a oldchar with \a newchar
884 docstring const subst_char(docstring const & a,
885 docstring::value_type oldchar, docstring::value_type newchar)
888 docstring::iterator lit = tmp.begin();
889 docstring::iterator end = tmp.end();
890 for (; lit != end; ++lit)
891 if ((*lit) == oldchar)
897 /// substitutes all instances of \a oldstr with \a newstr
898 template<typename String> inline
899 String const subst_string(String const & a,
900 String const & oldstr, String const & newstr)
902 LASSERT(!oldstr.empty(), return a);
905 size_t const olen = oldstr.length();
906 while ((i = lstr.find(oldstr, i)) != string::npos) {
907 lstr.replace(i, olen, newstr);
908 i += newstr.length(); // We need to be sure that we don't
909 // use the same i over and over again.
915 docstring const subst_string(docstring const & a,
916 docstring const & oldstr, docstring const & newstr)
918 LASSERT(!oldstr.empty(), return a);
921 size_t const olen = oldstr.length();
922 while ((i = lstr.find(oldstr, i)) != string::npos) {
923 lstr.replace(i, olen, newstr);
924 i += newstr.length(); // We need to be sure that we don't
925 // use the same i over and over again.
933 string const subst(string const & a, char oldchar, char newchar)
935 return subst_char(a, oldchar, newchar);
939 docstring const subst(docstring const & a,
940 char_type oldchar, char_type newchar)
942 return subst_char(a, oldchar, newchar);
946 string const subst(string const & a,
947 string const & oldstr, string const & newstr)
949 return subst_string(a, oldstr, newstr);
953 docstring const subst(docstring const & a,
954 docstring const & oldstr, docstring const & newstr)
956 return subst_string(a, oldstr, newstr);
960 int count_char(string const & str, char chr)
963 string::const_iterator lit = str.begin();
964 string::const_iterator end = str.end();
965 for (; lit != end; ++lit)
972 /// Count all occurrences of char \a chr inside \a str
973 int count_char(docstring const & str, docstring::value_type chr)
976 docstring::const_iterator lit = str.begin();
977 docstring::const_iterator end = str.end();
978 for (; lit != end; ++lit)
985 int wordCount(docstring const & d)
987 docstring dt = trim(d);
991 for (auto const & c : dt) {
1000 int count_bin_chars(string const & str)
1002 QString const qstr = toqstr(str).simplified();
1004 QString::const_iterator cit = qstr.begin();
1005 QString::const_iterator end = qstr.end();
1006 for (; cit != end; ++cit) {
1007 switch (cit->category()) {
1008 case QChar::Separator_Line:
1009 case QChar::Separator_Paragraph:
1010 case QChar::Other_Control:
1011 case QChar::Other_Format:
1012 case QChar::Other_Surrogate:
1013 case QChar::Other_PrivateUse:
1014 case QChar::Other_NotAssigned:
1025 docstring const trim(docstring const & a, char const * p)
1027 LASSERT(p, return a);
1029 if (a.empty() || !*p)
1032 docstring s = from_ascii(p);
1033 size_t r = a.find_last_not_of(s);
1034 size_t l = a.find_first_not_of(s);
1036 // Is this the minimal test? (lgb)
1037 if (r == docstring::npos && l == docstring::npos)
1040 return a.substr(l, r - l + 1);
1044 string const trim(string const & a, char const * p)
1046 LASSERT(p, return a);
1048 if (a.empty() || !*p)
1051 size_t r = a.find_last_not_of(p);
1052 size_t l = a.find_first_not_of(p);
1054 // Is this the minimal test? (lgb)
1055 if (r == string::npos && l == string::npos)
1058 return a.substr(l, r - l + 1);
1062 string const rtrim(string const & a, char const * p)
1064 LASSERT(p, return a);
1066 if (a.empty() || !*p)
1069 size_t r = a.find_last_not_of(p);
1071 // Is this test really needed? (Lgb)
1072 if (r == string::npos)
1075 return a.substr(0, r + 1);
1079 docstring const rtrim(docstring const & a, char const * p)
1081 LASSERT(p, return a);
1083 if (a.empty() || !*p)
1086 size_t r = a.find_last_not_of(from_ascii(p));
1088 // Is this test really needed? (Lgb)
1089 if (r == docstring::npos)
1092 return a.substr(0, r + 1);
1096 string const ltrim(string const & a, char const * p)
1098 LASSERT(p, return a);
1099 if (a.empty() || !*p)
1101 size_t l = a.find_first_not_of(p);
1102 if (l == string::npos)
1104 return a.substr(l, string::npos);
1108 docstring const ltrim(docstring const & a, char const * p)
1110 LASSERT(p, return a);
1111 if (a.empty() || !*p)
1113 size_t l = a.find_first_not_of(from_ascii(p));
1114 if (l == docstring::npos)
1116 return a.substr(l, docstring::npos);
1121 template<typename String, typename Char> inline
1122 String const doSplit(String const & a, String & piece, Char delim)
1125 size_t i = a.find(delim);
1126 if (i == a.length() - 1) {
1127 piece = a.substr(0, i);
1128 } else if (i == 0) {
1130 tmp = a.substr(i + 1);
1131 } else if (i != String::npos) {
1132 piece = a.substr(0, i);
1133 tmp = a.substr(i + 1);
1141 // FIXME: why is this specialization needed?
1142 template<typename Char> inline
1143 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
1146 size_t i = a.find(delim);
1147 if (i == a.length() - 1) {
1148 piece = a.substr(0, i);
1149 } else if (i == 0) {
1151 tmp = a.substr(i + 1);
1152 } else if (i != docstring::npos) {
1153 piece = a.substr(0, i);
1154 tmp = a.substr(i + 1);
1164 string const split(string const & a, string & piece, char delim)
1166 return doSplit(a, piece, delim);
1170 docstring const split(docstring const & a, docstring & piece, char_type delim)
1172 return doSplit(a, piece, delim);
1176 string const split(string const & a, char delim)
1179 size_t i = a.find(delim);
1180 if (i != string::npos) // found delim
1181 tmp = a.substr(i + 1);
1187 string const rsplit(string const & a, string & piece, char delim)
1190 size_t i = a.rfind(delim);
1191 if (i != string::npos) { // delimiter was found
1192 piece = a.substr(0, i);
1193 tmp = a.substr(i + 1);
1194 } else { // delimiter was not found
1201 docstring const rsplit(docstring const & a, docstring & piece, char_type delim)
1204 size_t i = a.rfind(delim);
1205 if (i != string::npos) { // delimiter was found
1206 piece = a.substr(0, i);
1207 tmp = a.substr(i + 1);
1208 } else { // delimiter was not found
1215 docstring const rsplit(docstring const & a, char_type delim)
1218 size_t i = a.rfind(delim);
1219 if (i != string::npos)
1220 tmp = a.substr(i + 1);
1225 docstring const escape(docstring const & lab)
1227 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
1228 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1230 for (char_type const c : lab) {
1231 if (c >= 128 || c == '=' || c == '%' || c == '#' || c == '$'
1232 || c == '}' || c == '{' || c == ']' || c == '[' || c == '&'
1234 // Although char_type is a 32 bit type we know that
1235 // UCS4 occupies only 21 bits, so we don't need to
1236 // encode bigger values. Test for 2^24 because we
1237 // can encode that with the 6 hex digits that are
1238 // needed for 21 bits anyway.
1239 LASSERT(c < (1 << 24), continue);
1241 enc += hexdigit[(c>>20) & 15];
1242 enc += hexdigit[(c>>16) & 15];
1243 enc += hexdigit[(c>>12) & 15];
1244 enc += hexdigit[(c>> 8) & 15];
1245 enc += hexdigit[(c>> 4) & 15];
1246 enc += hexdigit[ c & 15];
1255 docstring const protectArgument(docstring & arg, char const l,
1258 if (contains(arg, l) || contains(arg, r))
1260 arg = '{' + arg + '}';
1265 bool truncateWithEllipsis(docstring & str, size_t const len, bool const mid)
1267 if (str.size() <= len)
1269 if (mid && len > 0) {
1270 size_t const hlen = len / 2;
1271 docstring suffix = str.substr(str.size() - hlen);
1273 str[hlen - 1] = 0x2026;// HORIZONTAL ELLIPSIS
1278 str[len - 1] = 0x2026;// HORIZONTAL ELLIPSIS
1286 // this doesn't check whether str is empty, so do that first.
1287 vector<docstring> wrapToVec(docstring const & str, int ind,
1290 docstring s = trim(str);
1292 return vector<docstring>();
1296 indent.insert(0, -ind, ' ');
1299 s.insert(0, ind, ' ');
1301 vector<docstring> retval;
1302 while (s.size() > width) {
1303 // find the last space within the first 'width' chars
1304 size_t const i = s.find_last_of(' ', width - 1);
1305 if (i == docstring::npos || i <= size_t(ind)) {
1307 truncateWithEllipsis(s, width);
1310 retval.push_back(s.substr(0, i));
1311 s = indent + s.substr(i);
1312 ind = indent.size();
1315 retval.push_back(s);
1322 docstring wrap(docstring const & str, int const ind, size_t const width)
1324 docstring s = trim(str);
1328 vector<docstring> const svec = wrapToVec(str, ind, width);
1329 return getStringFromVector(svec, from_ascii("\n"));
1333 docstring wrapParas(docstring const & str, int const indent,
1334 size_t const width, size_t const maxlines)
1339 vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1340 vector<docstring> retval;
1342 vector<docstring>::const_iterator it = pars.begin();
1343 vector<docstring>::const_iterator const en = pars.end();
1344 for (; it != en; ++it) {
1345 vector<docstring> tmp = wrapToVec(*it, indent, width);
1346 size_t const nlines = tmp.size();
1349 size_t const curlines = retval.size();
1350 if (maxlines > 0 && curlines + nlines > maxlines) {
1351 tmp.resize(maxlines - curlines);
1352 docstring last = tmp.back();
1353 size_t const lsize = last.size();
1354 if (lsize > width - 1) {
1355 size_t const i = last.find_last_of(' ', width - 1);
1356 if (i == docstring::npos || i <= size_t(indent))
1358 truncateWithEllipsis(last, lsize);
1360 truncateWithEllipsis(last, i);
1362 last.push_back(0x2026);//HORIZONTAL ELLIPSIS
1364 tmp.push_back(last);
1366 retval.insert(retval.end(), tmp.begin(), tmp.end());
1367 if (maxlines > 0 && retval.size() >= maxlines)
1370 return getStringFromVector(retval, from_ascii("\n"));
1376 template<typename String> vector<String> const
1377 getVectorFromStringT(String const & str, String const & delim,
1378 bool keepempty, bool trimit)
1380 // Lars would like this code to go, but for now his replacement (below)
1381 // doesn't fullfil the same function. I have, therefore, reactivated the
1382 // old code for now. Angus 11 Nov 2002.
1387 String keys = trimit ? rtrim(str) : str;
1389 size_t const idx = keys.find(delim);
1390 if (idx == String::npos) {
1391 vec.push_back(trimit ? ltrim(keys) : keys);
1394 String const key = trimit ?
1395 trim(keys.substr(0, idx)) : keys.substr(0, idx);
1396 if (!key.empty() || keepempty)
1398 size_t const start = idx + delim.size();
1399 keys = keys.substr(start);
1403 typedef boost::char_separator<typename String::value_type> Separator;
1404 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1405 Separator sep(delim.c_str());
1406 Tokenizer tokens(str, sep);
1407 return vector<String>(tokens.begin(), tokens.end());
1412 template<typename String> const String
1413 getStringFromVector(vector<String> const & vec, String const & delim)
1416 typename vector<String>::const_iterator it = vec.begin();
1417 typename vector<String>::const_iterator en = vec.end();
1418 for (; it != en; ++it) {
1419 String item = trim(*it);
1432 vector<string> const getVectorFromString(string const & str,
1433 string const & delim, bool keepempty, bool trimit)
1435 return getVectorFromStringT<string>(str, delim, keepempty, trimit);
1439 vector<docstring> const getVectorFromString(docstring const & str,
1440 docstring const & delim, bool keepempty, bool trimit)
1442 return getVectorFromStringT<docstring>(str, delim, keepempty, trimit);
1446 string const getStringFromVector(vector<string> const & vec,
1447 string const & delim)
1449 return getStringFromVector<string>(vec, delim);
1453 docstring const getStringFromVector(vector<docstring> const & vec,
1454 docstring const & delim)
1456 return getStringFromVector<docstring>(vec, delim);
1460 int findToken(char const * const str[], string const & search_token)
1464 while (str[i][0] && str[i] != search_token)
1472 std::string formatFPNumber(double x)
1474 // Need manual tweaking, QString::number(x, 'f', 16) does not work either
1477 // Prevent outputs of 23.4200000000000017 but output small numbers
1478 // with at least 6 significant digits.
1479 int const precision = (x == 0.0) ? 0 : max(6 - iround(log10(fabs(x))), 0);
1480 os << std::setprecision(precision) << x;
1481 string result = os.str();
1482 if (result.find('.') != string::npos) {
1483 result = rtrim(result, "0");
1484 if (result[result.length()-1] == '.')
1485 result = rtrim(result, ".");
1491 docstring to_percent_encoding(docstring const & in, docstring const & ex)
1493 QByteArray input = to_utf8(in).c_str();
1494 QByteArray excludes = to_utf8(ex).c_str();
1495 return from_utf8(string(input.toPercentEncoding(excludes).data()));
1499 string from_percent_encoding(string const & in)
1501 return QByteArray::fromPercentEncoding(in.c_str()).data();
1505 int countExpanders(docstring const & str)
1507 // Numbers of characters that are expanded by inter-word spacing. These
1508 // characters are spaces, except for characters 09-0D which are treated
1509 // specially. (From a combination of testing with the notepad found in qt's
1510 // examples, and reading the source code.)
1512 for (char_type c : str)
1513 if (c > 0x0d && isSpace(c))
1519 docstring bformat(docstring const & fmt, int arg1)
1521 LATTEST(contains(fmt, from_ascii("%1$d")));
1522 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1523 return subst(str, from_ascii("%%"), from_ascii("%"));
1527 docstring bformat(docstring const & fmt, long arg1)
1529 LATTEST(contains(fmt, from_ascii("%1$d")));
1530 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1531 return subst(str, from_ascii("%%"), from_ascii("%"));
1535 #ifdef HAVE_LONG_LONG_INT
1536 docstring bformat(docstring const & fmt, long long arg1)
1538 LATTEST(contains(fmt, from_ascii("%1$d")));
1539 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1540 return subst(str, from_ascii("%%"), from_ascii("%"));
1545 docstring bformat(docstring const & fmt, unsigned int arg1)
1547 LATTEST(contains(fmt, from_ascii("%1$d")));
1548 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1549 return subst(str, from_ascii("%%"), from_ascii("%"));
1553 docstring bformat(docstring const & fmt, docstring const & arg1)
1555 LATTEST(contains(fmt, from_ascii("%1$s")));
1556 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1557 return subst(str, from_ascii("%%"), from_ascii("%"));
1561 docstring bformat(docstring const & fmt, char * arg1)
1563 LATTEST(contains(fmt, from_ascii("%1$s")));
1564 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1565 return subst(str, from_ascii("%%"), from_ascii("%"));
1569 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2)
1571 LATTEST(contains(fmt, from_ascii("%1$s")));
1572 LATTEST(contains(fmt, from_ascii("%2$s")));
1573 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1574 str = subst(str, from_ascii("%2$s"), arg2);
1575 return subst(str, from_ascii("%%"), from_ascii("%"));
1579 docstring bformat(docstring const & fmt, docstring const & arg1, int arg2)
1581 LATTEST(contains(fmt, from_ascii("%1$s")));
1582 LATTEST(contains(fmt, from_ascii("%2$d")));
1583 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1584 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1585 return subst(str, from_ascii("%%"), from_ascii("%"));
1589 docstring bformat(docstring const & fmt, char const * arg1, docstring const & arg2)
1591 return bformat(fmt, from_ascii(arg1), arg2);
1595 docstring bformat(docstring const & fmt, int arg1, int arg2)
1597 LATTEST(contains(fmt, from_ascii("%1$d")));
1598 LATTEST(contains(fmt, from_ascii("%2$d")));
1599 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1600 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1601 return subst(str, from_ascii("%%"), from_ascii("%"));
1605 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2, docstring const & arg3)
1607 LATTEST(contains(fmt, from_ascii("%1$s")));
1608 LATTEST(contains(fmt, from_ascii("%2$s")));
1609 LATTEST(contains(fmt, from_ascii("%3$s")));
1610 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1611 str = subst(str, from_ascii("%2$s"), arg2);
1612 str = subst(str, from_ascii("%3$s"), arg3);
1613 return subst(str, from_ascii("%%"), from_ascii("%"));
1617 docstring bformat(docstring const & fmt,
1618 docstring const & arg1, docstring const & arg2, docstring const & arg3, docstring const & arg4)
1620 LATTEST(contains(fmt, from_ascii("%1$s")));
1621 LATTEST(contains(fmt, from_ascii("%2$s")));
1622 LATTEST(contains(fmt, from_ascii("%3$s")));
1623 LATTEST(contains(fmt, from_ascii("%4$s")));
1624 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1625 str = subst(str, from_ascii("%2$s"), arg2);
1626 str = subst(str, from_ascii("%3$s"), arg3);
1627 str = subst(str, from_ascii("%4$s"), arg4);
1628 return subst(str, from_ascii("%%"), from_ascii("%"));
1631 docstring bformat(docstring const & fmt, docstring const & arg1,
1632 docstring const & arg2, docstring const & arg3,
1633 docstring const & arg4, docstring const & arg5)
1635 LATTEST(contains(fmt, from_ascii("%1$s")));
1636 LATTEST(contains(fmt, from_ascii("%2$s")));
1637 LATTEST(contains(fmt, from_ascii("%3$s")));
1638 LATTEST(contains(fmt, from_ascii("%4$s")));
1639 LATTEST(contains(fmt, from_ascii("%5$s")));
1640 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1641 str = subst(str, from_ascii("%2$s"), arg2);
1642 str = subst(str, from_ascii("%3$s"), arg3);
1643 str = subst(str, from_ascii("%4$s"), arg4);
1644 str = subst(str, from_ascii("%5$s"), arg5);
1645 return subst(str, from_ascii("%%"), from_ascii("%"));
1648 } // namespace support