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,
917 bool const case_sens)
919 LASSERT(!oldstr.empty(), return a);
922 size_t const olen = oldstr.length();
923 // string to be searched in
924 docstring se_str = case_sens ? res : lowercase(res);
925 // token to be searched within the above
926 docstring const se_tok = case_sens ? oldstr : lowercase(oldstr);
927 while ((i = se_str.find(se_tok, i)) != string::npos) {
928 res.replace(i, olen, newstr);
929 i += newstr.length(); // We need to be sure that we don't
930 // use the same i over and over again.
931 se_str = case_sens ? res : lowercase(res);
939 string const subst(string const & a, char oldchar, char newchar)
941 return subst_char(a, oldchar, newchar);
945 docstring const subst(docstring const & a,
946 char_type oldchar, char_type newchar)
948 return subst_char(a, oldchar, newchar);
952 string const subst(string const & a,
953 string const & oldstr, string const & newstr)
955 return subst_string(a, oldstr, newstr);
959 docstring const subst(docstring const & a,
960 docstring const & oldstr, docstring const & newstr,
963 return subst_string(a, oldstr, newstr, case_sens);
967 int count_char(string const & str, char chr)
970 string::const_iterator lit = str.begin();
971 string::const_iterator end = str.end();
972 for (; lit != end; ++lit)
979 /// Count all occurrences of char \a chr inside \a str
980 int count_char(docstring const & str, docstring::value_type chr)
983 docstring::const_iterator lit = str.begin();
984 docstring::const_iterator end = str.end();
985 for (; lit != end; ++lit)
992 int wordCount(docstring const & d)
994 docstring dt = trim(d);
998 for (auto const & c : dt) {
1007 int count_bin_chars(string const & str)
1009 QString const qstr = toqstr(str).simplified();
1011 QString::const_iterator cit = qstr.begin();
1012 QString::const_iterator end = qstr.end();
1013 for (; cit != end; ++cit) {
1014 switch (cit->category()) {
1015 case QChar::Separator_Line:
1016 case QChar::Separator_Paragraph:
1017 case QChar::Other_Control:
1018 case QChar::Other_Format:
1019 case QChar::Other_Surrogate:
1020 case QChar::Other_PrivateUse:
1021 case QChar::Other_NotAssigned:
1032 docstring const trim(docstring const & a, char const * p)
1034 LASSERT(p, return a);
1036 if (a.empty() || !*p)
1039 docstring s = from_ascii(p);
1040 size_t r = a.find_last_not_of(s);
1041 size_t l = a.find_first_not_of(s);
1043 // Is this the minimal test? (lgb)
1044 if (r == docstring::npos && l == docstring::npos)
1047 return a.substr(l, r - l + 1);
1051 string const trim(string const & a, char const * p)
1053 LASSERT(p, return a);
1055 if (a.empty() || !*p)
1058 size_t r = a.find_last_not_of(p);
1059 size_t l = a.find_first_not_of(p);
1061 // Is this the minimal test? (lgb)
1062 if (r == string::npos && l == string::npos)
1065 return a.substr(l, r - l + 1);
1069 string const rtrim(string const & a, char const * p)
1071 LASSERT(p, return a);
1073 if (a.empty() || !*p)
1076 size_t r = a.find_last_not_of(p);
1078 // Is this test really needed? (Lgb)
1079 if (r == string::npos)
1082 return a.substr(0, r + 1);
1086 docstring const rtrim(docstring const & a, char const * p)
1088 LASSERT(p, return a);
1090 if (a.empty() || !*p)
1093 size_t r = a.find_last_not_of(from_ascii(p));
1095 // Is this test really needed? (Lgb)
1096 if (r == docstring::npos)
1099 return a.substr(0, r + 1);
1103 string const ltrim(string const & a, char const * p)
1105 LASSERT(p, return a);
1106 if (a.empty() || !*p)
1108 size_t l = a.find_first_not_of(p);
1109 if (l == string::npos)
1111 return a.substr(l, string::npos);
1115 docstring const ltrim(docstring const & a, char const * p)
1117 LASSERT(p, return a);
1118 if (a.empty() || !*p)
1120 size_t l = a.find_first_not_of(from_ascii(p));
1121 if (l == docstring::npos)
1123 return a.substr(l, docstring::npos);
1128 template<typename String, typename Char> inline
1129 String const doSplit(String const & a, String & piece, Char delim)
1132 size_t i = a.find(delim);
1133 if (i == a.length() - 1) {
1134 piece = a.substr(0, i);
1135 } else if (i == 0) {
1137 tmp = a.substr(i + 1);
1138 } else if (i != String::npos) {
1139 piece = a.substr(0, i);
1140 tmp = a.substr(i + 1);
1148 // FIXME: why is this specialization needed?
1149 template<typename Char> inline
1150 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
1153 size_t i = a.find(delim);
1154 if (i == a.length() - 1) {
1155 piece = a.substr(0, i);
1156 } else if (i == 0) {
1158 tmp = a.substr(i + 1);
1159 } else if (i != docstring::npos) {
1160 piece = a.substr(0, i);
1161 tmp = a.substr(i + 1);
1171 string const split(string const & a, string & piece, char delim)
1173 return doSplit(a, piece, delim);
1177 docstring const split(docstring const & a, docstring & piece, char_type delim)
1179 return doSplit(a, piece, delim);
1183 string const split(string const & a, char delim)
1186 size_t i = a.find(delim);
1187 if (i != string::npos) // found delim
1188 tmp = a.substr(i + 1);
1194 string const rsplit(string const & a, string & piece, char delim)
1197 size_t i = a.rfind(delim);
1198 if (i != string::npos) { // delimiter was found
1199 piece = a.substr(0, i);
1200 tmp = a.substr(i + 1);
1201 } else { // delimiter was not found
1208 docstring const rsplit(docstring const & a, docstring & piece, char_type delim)
1211 size_t i = a.rfind(delim);
1212 if (i != string::npos) { // delimiter was found
1213 piece = a.substr(0, i);
1214 tmp = a.substr(i + 1);
1215 } else { // delimiter was not found
1222 docstring const rsplit(docstring const & a, char_type delim)
1225 size_t i = a.rfind(delim);
1226 if (i != string::npos)
1227 tmp = a.substr(i + 1);
1232 docstring const escape(docstring const & lab)
1234 char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
1235 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1237 for (char_type const c : lab) {
1238 if (c >= 128 || c == '=' || c == '%' || c == '#' || c == '$'
1239 || c == '}' || c == '{' || c == ']' || c == '[' || c == '&'
1242 // Although char_type is a 32 bit type we know that
1243 // UCS4 occupies only 21 bits, so we don't need to
1244 // encode bigger values. Test for 2^24 because we
1245 // can encode that with the 6 hex digits that are
1246 // needed for 21 bits anyway.
1247 LASSERT(c < (1 << 24), continue);
1249 enc += hexdigit[(c>>20) & 15];
1250 enc += hexdigit[(c>>16) & 15];
1251 enc += hexdigit[(c>>12) & 15];
1252 enc += hexdigit[(c>> 8) & 15];
1253 enc += hexdigit[(c>> 4) & 15];
1254 enc += hexdigit[ c & 15];
1263 docstring const protectArgument(docstring & arg, char const l,
1266 if (contains(arg, l) || contains(arg, r))
1268 arg = '{' + arg + '}';
1273 bool truncateWithEllipsis(docstring & str, size_t const len, bool const mid)
1275 if (str.size() <= len)
1277 if (mid && len > 0) {
1278 size_t const hlen = len / 2;
1279 docstring suffix = str.substr(str.size() - hlen);
1281 str[hlen - 1] = 0x2026;// HORIZONTAL ELLIPSIS
1286 str[len - 1] = 0x2026;// HORIZONTAL ELLIPSIS
1294 // this doesn't check whether str is empty, so do that first.
1295 vector<docstring> wrapToVec(docstring const & str, int ind,
1298 docstring s = trim(str);
1300 return vector<docstring>();
1304 indent.insert(0, -ind, ' ');
1307 s.insert(0, ind, ' ');
1309 vector<docstring> retval;
1310 while (s.size() > width) {
1311 // find the last space within the first 'width' chars
1312 size_t const i = s.find_last_of(' ', width - 1);
1313 if (i == docstring::npos || i <= size_t(ind)) {
1315 truncateWithEllipsis(s, width);
1318 retval.push_back(s.substr(0, i));
1319 s = indent + s.substr(i);
1320 ind = indent.size();
1323 retval.push_back(s);
1330 docstring wrap(docstring const & str, int const ind, size_t const width)
1332 docstring s = trim(str);
1336 vector<docstring> const svec = wrapToVec(str, ind, width);
1337 return getStringFromVector(svec, from_ascii("\n"));
1341 docstring wrapParas(docstring const & str, int const indent,
1342 size_t const width, size_t const maxlines)
1347 vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1348 vector<docstring> retval;
1350 vector<docstring>::const_iterator it = pars.begin();
1351 vector<docstring>::const_iterator const en = pars.end();
1352 for (; it != en; ++it) {
1353 vector<docstring> tmp = wrapToVec(*it, indent, width);
1354 size_t const nlines = tmp.size();
1357 size_t const curlines = retval.size();
1358 if (maxlines > 0 && curlines + nlines > maxlines) {
1359 tmp.resize(maxlines - curlines);
1360 docstring last = tmp.back();
1361 size_t const lsize = last.size();
1362 if (lsize > width - 1) {
1363 size_t const i = last.find_last_of(' ', width - 1);
1364 if (i == docstring::npos || i <= size_t(indent))
1366 truncateWithEllipsis(last, lsize);
1368 truncateWithEllipsis(last, i);
1370 last.push_back(0x2026);//HORIZONTAL ELLIPSIS
1372 tmp.push_back(last);
1374 retval.insert(retval.end(), tmp.begin(), tmp.end());
1375 if (maxlines > 0 && retval.size() >= maxlines)
1378 return getStringFromVector(retval, from_ascii("\n"));
1384 template<typename String> vector<String> const
1385 getVectorFromStringT(String const & str, String const & delim,
1386 bool keepempty, bool trimit)
1388 // Lars would like this code to go, but for now his replacement (below)
1389 // doesn't fullfil the same function. I have, therefore, reactivated the
1390 // old code for now. Angus 11 Nov 2002.
1395 String keys = trimit ? rtrim(str) : str;
1397 size_t const idx = keys.find(delim);
1398 if (idx == String::npos) {
1399 vec.push_back(trimit ? ltrim(keys) : keys);
1402 String const key = trimit ?
1403 trim(keys.substr(0, idx)) : keys.substr(0, idx);
1404 if (!key.empty() || keepempty)
1406 size_t const start = idx + delim.size();
1407 keys = keys.substr(start);
1411 typedef boost::char_separator<typename String::value_type> Separator;
1412 typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1413 Separator sep(delim.c_str());
1414 Tokenizer tokens(str, sep);
1415 return vector<String>(tokens.begin(), tokens.end());
1420 template<typename String> const String
1421 getStringFromVector(vector<String> const & vec, String const & delim)
1424 typename vector<String>::const_iterator it = vec.begin();
1425 typename vector<String>::const_iterator en = vec.end();
1426 for (; it != en; ++it) {
1427 String item = trim(*it);
1440 vector<string> const getVectorFromString(string const & str,
1441 string const & delim, bool keepempty, bool trimit)
1443 return getVectorFromStringT<string>(str, delim, keepempty, trimit);
1447 vector<docstring> const getVectorFromString(docstring const & str,
1448 docstring const & delim, bool keepempty, bool trimit)
1450 return getVectorFromStringT<docstring>(str, delim, keepempty, trimit);
1454 string const getStringFromVector(vector<string> const & vec,
1455 string const & delim)
1457 return getStringFromVector<string>(vec, delim);
1461 docstring const getStringFromVector(vector<docstring> const & vec,
1462 docstring const & delim)
1464 return getStringFromVector<docstring>(vec, delim);
1468 int findToken(char const * const str[], string const & search_token)
1472 while (str[i][0] && str[i] != search_token)
1480 std::string formatFPNumber(double x)
1482 // Need manual tweaking, QString::number(x, 'f', 16) does not work either
1485 // Prevent outputs of 23.4200000000000017 but output small numbers
1486 // with at least 6 significant digits.
1487 int const precision = (x == 0.0) ? 0 : max(6 - iround(log10(fabs(x))), 0);
1488 os << std::setprecision(precision) << x;
1489 string result = os.str();
1490 if (result.find('.') != string::npos) {
1491 result = rtrim(result, "0");
1492 if (result[result.length()-1] == '.')
1493 result = rtrim(result, ".");
1499 docstring to_percent_encoding(docstring const & in, docstring const & ex)
1501 QByteArray input = to_utf8(in).c_str();
1502 QByteArray excludes = to_utf8(ex).c_str();
1503 return from_utf8(string(input.toPercentEncoding(excludes).data()));
1507 string from_percent_encoding(string const & in)
1509 return QByteArray::fromPercentEncoding(in.c_str()).data();
1513 int countExpanders(docstring const & str)
1515 // Numbers of characters that are expanded by inter-word spacing. These
1516 // characters are spaces, except for characters 09-0D which are treated
1517 // specially. (From a combination of testing with the notepad found in qt's
1518 // examples, and reading the source code.)
1520 for (char_type c : str)
1521 if (c > 0x0d && isSpace(c))
1527 docstring bformat(docstring const & fmt, int 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 docstring bformat(docstring const & fmt, long arg1)
1537 LATTEST(contains(fmt, from_ascii("%1$d")));
1538 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1539 return subst(str, from_ascii("%%"), from_ascii("%"));
1543 #ifdef HAVE_LONG_LONG_INT
1544 docstring bformat(docstring const & fmt, long long arg1)
1546 LATTEST(contains(fmt, from_ascii("%1$d")));
1547 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1548 return subst(str, from_ascii("%%"), from_ascii("%"));
1553 docstring bformat(docstring const & fmt, unsigned int arg1)
1555 LATTEST(contains(fmt, from_ascii("%1$d")));
1556 docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1557 return subst(str, from_ascii("%%"), from_ascii("%"));
1561 docstring bformat(docstring const & fmt, docstring const & arg1)
1563 LATTEST(contains(fmt, from_ascii("%1$s")));
1564 docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1565 return subst(str, from_ascii("%%"), from_ascii("%"));
1569 docstring bformat(docstring const & fmt, char * arg1)
1571 LATTEST(contains(fmt, from_ascii("%1$s")));
1572 docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1573 return subst(str, from_ascii("%%"), from_ascii("%"));
1577 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2)
1579 LATTEST(contains(fmt, from_ascii("%1$s")));
1580 LATTEST(contains(fmt, from_ascii("%2$s")));
1581 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1582 str = subst(str, from_ascii("%2$s"), arg2);
1583 return subst(str, from_ascii("%%"), from_ascii("%"));
1587 docstring bformat(docstring const & fmt, docstring const & arg1, int arg2)
1589 LATTEST(contains(fmt, from_ascii("%1$s")));
1590 LATTEST(contains(fmt, from_ascii("%2$d")));
1591 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1592 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1593 return subst(str, from_ascii("%%"), from_ascii("%"));
1597 docstring bformat(docstring const & fmt, char const * arg1, docstring const & arg2)
1599 return bformat(fmt, from_ascii(arg1), arg2);
1603 docstring bformat(docstring const & fmt, int arg1, int arg2)
1605 LATTEST(contains(fmt, from_ascii("%1$d")));
1606 LATTEST(contains(fmt, from_ascii("%2$d")));
1607 docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1608 str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1609 return subst(str, from_ascii("%%"), from_ascii("%"));
1613 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2, docstring const & arg3)
1615 LATTEST(contains(fmt, from_ascii("%1$s")));
1616 LATTEST(contains(fmt, from_ascii("%2$s")));
1617 LATTEST(contains(fmt, from_ascii("%3$s")));
1618 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1619 str = subst(str, from_ascii("%2$s"), arg2);
1620 str = subst(str, from_ascii("%3$s"), arg3);
1621 return subst(str, from_ascii("%%"), from_ascii("%"));
1625 docstring bformat(docstring const & fmt,
1626 docstring const & arg1, docstring const & arg2, docstring const & arg3, docstring const & arg4)
1628 LATTEST(contains(fmt, from_ascii("%1$s")));
1629 LATTEST(contains(fmt, from_ascii("%2$s")));
1630 LATTEST(contains(fmt, from_ascii("%3$s")));
1631 LATTEST(contains(fmt, from_ascii("%4$s")));
1632 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1633 str = subst(str, from_ascii("%2$s"), arg2);
1634 str = subst(str, from_ascii("%3$s"), arg3);
1635 str = subst(str, from_ascii("%4$s"), arg4);
1636 return subst(str, from_ascii("%%"), from_ascii("%"));
1639 docstring bformat(docstring const & fmt, docstring const & arg1,
1640 docstring const & arg2, docstring const & arg3,
1641 docstring const & arg4, docstring const & arg5)
1643 LATTEST(contains(fmt, from_ascii("%1$s")));
1644 LATTEST(contains(fmt, from_ascii("%2$s")));
1645 LATTEST(contains(fmt, from_ascii("%3$s")));
1646 LATTEST(contains(fmt, from_ascii("%4$s")));
1647 LATTEST(contains(fmt, from_ascii("%5$s")));
1648 docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1649 str = subst(str, from_ascii("%2$s"), arg2);
1650 str = subst(str, from_ascii("%3$s"), arg3);
1651 str = subst(str, from_ascii("%4$s"), arg4);
1652 str = subst(str, from_ascii("%5$s"), arg5);
1653 return subst(str, from_ascii("%%"), from_ascii("%"));
1656 } // namespace support