]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.cpp
Fix IM window position for a new window (#13094)
[lyx.git] / src / support / lstrings.cpp
1 /**
2  * \file lstrings.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Dekel Tsur
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "support/lstrings.h"
16
17 #include "support/convert.h"
18 #include "support/debug.h"
19 #include "support/lyxlib.h"
20 #include "support/qstring_helpers.h"
21
22 #include "support/lassert.h"
23
24 #include <QString>
25
26 #include <cstdio>
27 #include <cstring>
28 #include <algorithm>
29 #include <iomanip>
30 #include <sstream>
31 #include <typeinfo>
32
33 using namespace std;
34
35 namespace lyx {
36
37 // Using this allows us to have docstring default arguments in headers
38 // without #include "support/docstring" there.
39 docstring const & empty_docstring()
40 {
41         static const docstring s;
42         return s;
43 }
44
45 // Using this allows us to have string default arguments in headers
46 // without #include <string>
47 string const & empty_string()
48 {
49         static const string s;
50         return s;
51 }
52
53 namespace {
54 /**
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).
60  */
61 inline char_type qchar_to_ucs4(QChar const & qchar)
62 {
63         LASSERT(is_utf16(static_cast<char_type>(qchar.unicode())), return '?');
64         return static_cast<char_type>(qchar.unicode());
65 }
66
67 /**
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).
73  */
74 inline QChar const ucs4_to_qchar(char_type const ucs4)
75 {
76         LASSERT(is_utf16(ucs4), return QChar('?'));
77         return QChar(static_cast<unsigned short>(ucs4));
78 }
79
80 /// Maximum valid UCS4 code point
81 char_type const ucs4_max = 0x10ffff;
82 } // namespace
83
84
85 bool isLetterChar(char_type c)
86 {
87         if (!is_utf16(c)) {
88                 if (c > ucs4_max)
89                         // outside the UCS4 range
90                         return false;
91                 // assume that all non-utf16 characters are letters
92                 return true;
93         }
94         return ucs4_to_qchar(c).isLetter();
95 }
96
97
98 bool isLower(char_type c)
99 {
100         if (!is_utf16(c))
101                 return false;
102         return ucs4_to_qchar(c).isLower();
103 }
104
105
106 bool isAlphaASCII(char_type c)
107 {
108         return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
109 }
110
111
112 bool isPrintable(char_type c)
113 {
114         if (!is_utf16(c)) {
115                 if (c > ucs4_max)
116                         // outside the UCS4 range
117                         return false;
118                 // assume that all non-utf16 characters are printable
119                 return true;
120         }
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)
125                 return true;
126         return ucs4_to_qchar(c).isPrint();
127 }
128
129
130 bool isPrintableNonspace(char_type c)
131 {
132         if (!is_utf16(c)) {
133                 if (c > ucs4_max)
134                         // outside the UCS4 range
135                         return false;
136                 // assume that all non-utf16 characters are printable and
137                 // no space
138                 return true;
139         }
140         QChar const qc = ucs4_to_qchar(c);
141         return qc.isPrint() && !qc.isSpace();
142 }
143
144
145 bool isSpace(char_type c)
146 {
147         if (!is_utf16(c)) {
148                 // assume that no non-utf16 character is a space
149                 // c outside the UCS4 range is caught as well
150                 return false;
151         }
152         QChar const qc = ucs4_to_qchar(c);
153         return qc.isSpace();
154 }
155
156
157 bool isNumber(char_type c)
158 {
159         if (!is_utf16(c))
160                 // assume that no non-utf16 character is a numeral
161                 // c outside the UCS4 range is caught as well
162                 return false;
163         return ucs4_to_qchar(c).isNumber();
164 }
165
166
167 bool isCommonNumberSeparator(char_type c)
168 {
169         if (!is_utf16(c))
170                 // assume that no non-utf16 character is a numeral
171                 // c outside the UCS4 range is caught as well
172                 return false;
173         return ucs4_to_qchar(c).direction() == QChar::DirCS;
174 }
175
176
177 bool isEuropeanNumberTerminator(char_type c)
178 {
179         if (!is_utf16(c))
180                 // assume that no non-utf16 character is a numeral
181                 // c outside the UCS4 range is caught as well
182                 return false;
183         return ucs4_to_qchar(c).direction() == QChar::DirET;
184 }
185
186
187 bool isDigitASCII(char_type c)
188 {
189         return '0' <= c && c <= '9';
190 }
191
192 bool isNumberChar(char_type c)
193 {
194         if (c > ucs4_max)
195                 // outside the UCS4 range
196                 return false;
197         return ucs4_to_qchar(c).isNumber();
198 }
199
200 bool isAlnumASCII(char_type c)
201 {
202         return isAlphaASCII(c) || isDigitASCII(c);
203 }
204
205
206 bool isASCII(char_type c)
207 {
208         return c < 0x80;
209 }
210
211
212 bool isOpenPunctuation(char_type c)
213 {
214         if (!is_utf16(c)) {
215                 // assume that no non-utf16 character is an op
216                 // c outside the UCS4 range is caught as well
217                 return false;
218         }
219         QChar const qc = ucs4_to_qchar(c);
220         return qc.category() == QChar::Punctuation_Open;
221 }
222
223
224 namespace support {
225
226 int compare_no_case(docstring const & s, docstring const & s2)
227 {
228         docstring::const_iterator p = s.begin();
229         docstring::const_iterator p2 = s2.begin();
230
231         while (p != s.end() && p2 != s2.end()) {
232                 char_type const lc1 = lowercase(*p);
233                 char_type const lc2 = lowercase(*p2);
234                 if (lc1 != lc2)
235                         return (lc1 < lc2) ? -1 : 1;
236                 ++p;
237                 ++p2;
238         }
239
240         if (s.size() == s2.size())
241                 return 0;
242         if (s.size() < s2.size())
243                 return -1;
244         return 1;
245 }
246
247
248 int compare_locale(docstring const & s, docstring const & s2)
249 {
250         return QString::localeAwareCompare(toqstr(s), toqstr(s2));
251 }
252
253
254 namespace {
255
256 template<typename Char>
257 Char ascii_tolower(Char c) {
258         if (c >= 'A' && c <= 'Z')
259                 return c - 'A' + 'a';
260         return c;
261 }
262
263 } // namespace
264
265
266 int compare_ascii_no_case(string const & s, string const & s2)
267 {
268         string::const_iterator p = s.begin();
269         string::const_iterator p2 = s2.begin();
270
271         while (p != s.end() && p2 != s2.end()) {
272                 int const lc1 = ascii_tolower(*p);
273                 int const lc2 = ascii_tolower(*p2);
274                 if (lc1 != lc2)
275                         return (lc1 < lc2) ? -1 : 1;
276                 ++p;
277                 ++p2;
278         }
279
280         if (s.size() == s2.size())
281                 return 0;
282         if (s.size() < s2.size())
283                 return -1;
284         return 1;
285 }
286
287
288 int compare_ascii_no_case(docstring const & s, docstring const & s2)
289 {
290         docstring::const_iterator p = s.begin();
291         docstring::const_iterator p2 = s2.begin();
292
293         while (p != s.end() && p2 != s2.end()) {
294                 char_type const lc1 = ascii_tolower(*p);
295                 char_type const lc2 = ascii_tolower(*p2);
296                 if (lc1 != lc2)
297                         return (lc1 < lc2) ? -1 : 1;
298                 ++p;
299                 ++p2;
300         }
301
302         if (s.size() == s2.size())
303                 return 0;
304         if (s.size() < s2.size())
305                 return -1;
306         return 1;
307 }
308
309
310 bool isStrInt(string const & str)
311 {
312         if (str.empty())
313                 return false;
314
315         // Remove leading and trailing white space chars.
316         string const tmpstr = trim(str);
317         if (tmpstr.empty())
318                 return false;
319
320         string::const_iterator cit = tmpstr.begin();
321         if ((*cit) == '-')
322                 ++cit;
323
324         string::const_iterator end = tmpstr.end();
325         for (; cit != end; ++cit)
326                 if (!isDigitASCII(*cit))
327                         return false;
328
329         return true;
330 }
331
332
333 bool isStrUnsignedInt(string const & str)
334 {
335         if (str.empty())
336                 return false;
337
338         // Remove leading and trailing white space chars.
339         string const tmpstr = trim(str);
340         if (tmpstr.empty())
341                 return false;
342
343         string::const_iterator cit = tmpstr.begin();
344         string::const_iterator end = tmpstr.end();
345         for (; cit != end; ++cit)
346                 if (!isDigitASCII(*cit))
347                         return false;
348
349         return true;
350 }
351
352
353 bool isStrDbl(string const & str)
354 {
355         if (str.empty())
356                 return false;
357
358         // Remove leading and trailing white space chars.
359         string const tmpstr = trim(str);
360         if (tmpstr.empty())
361                 return false;
362         //      if (tmpstr.count('.') > 1) return false;
363
364         string::const_iterator cit = tmpstr.begin();
365         bool found_dot = false;
366         if (*cit == '-')
367                 ++cit;
368         string::const_iterator end = tmpstr.end();
369         for (; cit != end; ++cit) {
370                 if (!isDigitASCII(*cit) && *cit != '.')
371                         return false;
372                 if ('.' == (*cit)) {
373                         if (found_dot)
374                                 return false;
375                         found_dot = true;
376                 }
377         }
378         return true;
379 }
380
381
382 bool hasDigitASCII(docstring const & str)
383 {
384         docstring::const_iterator cit = str.begin();
385         docstring::const_iterator const end = str.end();
386         for (; cit != end; ++cit)
387                 if (isDigitASCII(*cit))
388                         return true;
389         return false;
390 }
391
392
393 bool isHexChar(char_type c)
394 {
395         return c == '0' ||
396                 c == '1' ||
397                 c == '2' ||
398                 c == '3' ||
399                 c == '4' ||
400                 c == '5' ||
401                 c == '6' ||
402                 c == '7' ||
403                 c == '8' ||
404                 c == '9' ||
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';
411 }
412
413
414 bool isHex(docstring const & str)
415 {
416         size_t index = 0;
417
418         if (str.length() > 2 && str[0] == '0' &&
419             (str[1] == 'x' || str[1] == 'X'))
420                 index = 2;
421
422         size_t const len = str.length();
423
424         for (; index < len; ++index) {
425                 if (!isHexChar(str[index]))
426                         return false;
427         }
428         return true;
429 }
430
431
432 unsigned int hexToInt(docstring const & str)
433 {
434         string s = to_ascii(str);
435         unsigned int h;
436         sscanf(s.c_str(), "%x", &h);
437         return h;
438 }
439
440
441 bool isAscii(docstring const & str)
442 {
443         size_t const len = str.length();
444         for (size_t i = 0; i < len; ++i)
445                 if (str[i] >= 0x80)
446                         return false;
447         return true;
448 }
449
450
451 bool isAscii(string const & str)
452 {
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)
456                         return false;
457         return true;
458 }
459
460
461 char lowercase(char c)
462 {
463         LASSERT(isASCII(c), return '?');
464         return char(tolower(c));
465 }
466
467
468 char uppercase(char c)
469 {
470         LASSERT(isASCII(c), return '?');
471         return char(toupper(c));
472 }
473
474
475 char_type lowercase(char_type c)
476 {
477         if (!is_utf16(c))
478                 // We don't know how to lowercase a non-utf16 char
479                 return c;
480         return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
481 }
482
483
484 char_type uppercase(char_type c)
485 {
486         if (!is_utf16(c))
487                 // We don't know how to uppercase a non-utf16 char
488                 return c;
489         return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
490 }
491
492
493 bool isLowerCase(char_type ch) {
494         return lowercase(ch) == ch;
495 }
496
497
498 bool isUpperCase(char_type ch) {
499         return uppercase(ch) == ch;
500 }
501
502
503 namespace {
504
505 // since we cannot use tolower and toupper directly in the
506 // calls to transform yet, we use these helper clases. (Lgb)
507
508 struct local_lowercase {
509         char_type operator()(char_type c) const {
510                 return lowercase(c);
511         }
512 };
513
514 struct local_uppercase {
515         char_type operator()(char_type c) const {
516                 return uppercase(c);
517         }
518 };
519
520 template<typename Char> struct local_ascii_lowercase {
521         Char operator()(Char c) const { return ascii_tolower(c); }
522 };
523
524 } // namespace
525
526
527 docstring const lowercase(docstring const & a)
528 {
529         docstring tmp(a);
530         transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
531         return tmp;
532 }
533
534
535 /* Uncomment here and in lstrings.h if you should need this.
536 string const lowercase(string const & a)
537 {
538         string tmp(a);
539         transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
540         return tmp;
541 }
542 */
543
544
545 docstring const uppercase(docstring const & a)
546 {
547         docstring tmp(a);
548         transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
549         return tmp;
550 }
551
552
553 docstring capitalize(docstring const & s) {
554         docstring ret = s;
555         char_type t = uppercase(ret[0]);
556         ret[0] = t;
557         return ret;
558 }
559
560
561 string const ascii_lowercase(string const & a)
562 {
563         string tmp(a);
564         transform(tmp.begin(), tmp.end(), tmp.begin(),
565                   local_ascii_lowercase<char>());
566         return tmp;
567 }
568
569
570 docstring const ascii_lowercase(docstring const & a)
571 {
572         docstring tmp(a);
573         transform(tmp.begin(), tmp.end(), tmp.begin(),
574                   local_ascii_lowercase<char_type>());
575         return tmp;
576 }
577
578
579 char_type superscript(char_type c)
580 {
581         switch (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
663         }
664         return c;
665 }
666
667
668 char_type subscript(char_type c)
669 {
670         switch (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
700         }
701         return c;
702 }
703
704
705 bool prefixIs(docstring const & a, char_type c)
706 {
707         if (a.empty())
708                 return false;
709         return a[0] == c;
710 }
711
712
713 bool prefixIs(string const & a, string const & pre)
714 {
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;
718 }
719
720
721 bool prefixIs(docstring const & a, docstring const & pre)
722 {
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;
726 }
727
728
729 bool suffixIs(string const & a, char c)
730 {
731         if (a.empty())
732                 return false;
733         return a[a.length() - 1] == c;
734 }
735
736
737 bool suffixIs(docstring const & a, char_type c)
738 {
739         if (a.empty())
740                 return false;
741         return a[a.length() - 1] == c;
742 }
743
744
745 bool suffixIs(string const & a, string const & suf)
746 {
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;
750 }
751
752
753 bool suffixIs(docstring const & a, docstring const & suf)
754 {
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;
758 }
759
760
761 bool containsOnly(string const & s, string const & cset)
762 {
763         return s.find_first_not_of(cset) == string::npos;
764 }
765
766
767 bool containsOnly(docstring const & s, string const & cset)
768 {
769         return s.find_first_not_of(from_ascii(cset)) == string::npos;
770 }
771
772
773 // ale970405+lasgoutt-970425
774 // rewritten to use new string (Lgb)
775 string const token(string const & a, char delim, int n)
776 {
777         if (a.empty())
778                 return string();
779
780         size_t k = 0;
781         size_t i = 0;
782
783         // Find delimiter or end of string
784         for (; n--;) {
785                 if ((i = a.find(delim, i)) == string::npos)
786                         break;
787                 else
788                         ++i; // step delim
789         }
790
791         // i is now the n'th delim (or string::npos)
792         if (i == string::npos)
793                 return string();
794
795         k = a.find(delim, i);
796         // k is now the n'th + 1 delim (or string::npos)
797
798         return a.substr(i, k - i);
799 }
800
801
802 docstring const token(docstring const & a, char_type delim, int n)
803 {
804         if (a.empty())
805                 return docstring();
806
807         size_t k = 0;
808         size_t i = 0;
809
810         // Find delimiter or end of string
811         for (; n--;) {
812                 if ((i = a.find(delim, i)) == docstring::npos)
813                         break;
814                 else
815                         ++i; // step delim
816         }
817
818         // i is now the n'th delim (or string::npos)
819         if (i == docstring::npos)
820                 return docstring();
821
822         k = a.find(delim, i);
823         // k is now the n'th + 1 delim (or string::npos)
824
825         return a.substr(i, k - i);
826 }
827
828
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)
832 {
833         int i = 0;
834         string str = a;
835         string tmptok;
836
837         while (!str.empty()) {
838                 str = split(str, tmptok, delim);
839                 if (tok == tmptok)
840                         return i;
841                 ++i;
842         }
843         return -1;
844 }
845
846
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)
850 {
851         int i = 0;
852         docstring str = a;
853         docstring tmptok;
854
855         while (!str.empty()) {
856                 str = split(str, tmptok, delim);
857                 if (tok == tmptok)
858                         return i;
859                 ++i;
860         }
861         return -1;
862 }
863
864
865 namespace {
866
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)
871 {
872         typedef basic_string<Ch> String;
873         String tmp(a);
874         typename String::iterator lit = tmp.begin();
875         typename String::iterator end = tmp.end();
876         for (; lit != end; ++lit)
877                 if ((*lit) == oldchar)
878                         (*lit) = newchar;
879         return tmp;
880 }
881
882
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)
886 {
887         docstring tmp(a);
888         docstring::iterator lit = tmp.begin();
889         docstring::iterator end = tmp.end();
890         for (; lit != end; ++lit)
891                 if ((*lit) == oldchar)
892                         (*lit) = newchar;
893         return tmp;
894 }
895
896
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)
901 {
902         LASSERT(!oldstr.empty(), return a);
903         String lstr = a;
904         size_t i = 0;
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.
910         }
911         return lstr;
912 }
913
914
915 docstring const subst_string(docstring const & a,
916                 docstring const & oldstr, docstring const & newstr,
917                 bool const case_sens)
918 {
919         LASSERT(!oldstr.empty(), return a);
920         docstring res = a;
921         size_t i = 0;
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);
932         }
933         return res;
934 }
935
936 } // namespace
937
938
939 string const subst(string const & a, char oldchar, char newchar)
940 {
941         return subst_char(a, oldchar, newchar);
942 }
943
944
945 docstring const subst(docstring const & a,
946                 char_type oldchar, char_type newchar)
947 {
948         return subst_char(a, oldchar, newchar);
949 }
950
951
952 string const subst(string const & a,
953                 string const & oldstr, string const & newstr)
954 {
955         return subst_string(a, oldstr, newstr);
956 }
957
958
959 docstring const subst(docstring const & a,
960                 docstring const & oldstr, docstring const & newstr,
961                 bool case_sens)
962 {
963         return subst_string(a, oldstr, newstr, case_sens);
964 }
965
966
967 int count_char(string const & str, char chr)
968 {
969         int count = 0;
970         string::const_iterator lit = str.begin();
971         string::const_iterator end = str.end();
972         for (; lit != end; ++lit)
973                 if ((*lit) == chr)
974                         count++;
975         return count;
976 }
977
978
979 /// Count all occurrences of char \a chr inside \a str
980 int count_char(docstring const & str, docstring::value_type chr)
981 {
982         int count = 0;
983         docstring::const_iterator lit = str.begin();
984         docstring::const_iterator end = str.end();
985         for (; lit != end; ++lit)
986                 if ((*lit) == chr)
987                         count++;
988         return count;
989 }
990
991
992 int wordCount(docstring const & d)
993 {
994         docstring dt = trim(d);
995         if (dt.empty())
996                 return 0;
997         int words = 1;
998         for (auto const & c : dt) {
999                 if (isSpace(c))
1000                         words++;
1001         }
1002         return words;
1003 }
1004
1005
1006
1007 int count_bin_chars(string const & str)
1008 {
1009         QString const qstr = toqstr(str).simplified();
1010         int count = 0;
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:
1022                         ++count;
1023                         break;
1024                 default:
1025                         break;
1026                 }
1027         }
1028         return count;
1029 }
1030
1031
1032 docstring const trim(docstring const & a, char const * p)
1033 {
1034         LASSERT(p, return a);
1035
1036         if (a.empty() || !*p)
1037                 return a;
1038
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);
1042
1043         // Is this the minimal test? (lgb)
1044         if (r == docstring::npos && l == docstring::npos)
1045                 return docstring();
1046
1047         return a.substr(l, r - l + 1);
1048 }
1049
1050
1051 string const trim(string const & a, char const * p)
1052 {
1053         LASSERT(p, return a);
1054
1055         if (a.empty() || !*p)
1056                 return a;
1057
1058         size_t r = a.find_last_not_of(p);
1059         size_t l = a.find_first_not_of(p);
1060
1061         // Is this the minimal test? (lgb)
1062         if (r == string::npos && l == string::npos)
1063                 return string();
1064
1065         return a.substr(l, r - l + 1);
1066 }
1067
1068
1069 string const rtrim(string const & a, char const * p)
1070 {
1071         LASSERT(p, return a);
1072
1073         if (a.empty() || !*p)
1074                 return a;
1075
1076         size_t r = a.find_last_not_of(p);
1077
1078         // Is this test really needed? (Lgb)
1079         if (r == string::npos)
1080                 return string();
1081
1082         return a.substr(0, r + 1);
1083 }
1084
1085
1086 docstring const rtrim(docstring const & a, char const * p)
1087 {
1088         LASSERT(p, return a);
1089
1090         if (a.empty() || !*p)
1091                 return a;
1092
1093         size_t r = a.find_last_not_of(from_ascii(p));
1094
1095         // Is this test really needed? (Lgb)
1096         if (r == docstring::npos)
1097                 return docstring();
1098
1099         return a.substr(0, r + 1);
1100 }
1101
1102
1103 string const ltrim(string const & a, char const * p)
1104 {
1105         LASSERT(p, return a);
1106         if (a.empty() || !*p)
1107                 return a;
1108         size_t l = a.find_first_not_of(p);
1109         if (l == string::npos)
1110                 return string();
1111         return a.substr(l, string::npos);
1112 }
1113
1114
1115 docstring const ltrim(docstring const & a, char const * p)
1116 {
1117         LASSERT(p, return a);
1118         if (a.empty() || !*p)
1119                 return a;
1120         size_t l = a.find_first_not_of(from_ascii(p));
1121         if (l == docstring::npos)
1122                 return docstring();
1123         return a.substr(l, docstring::npos);
1124 }
1125
1126 namespace {
1127
1128 template<typename String, typename Char> inline
1129 String const doSplit(String const & a, String & piece, Char delim)
1130 {
1131         String tmp;
1132         size_t i = a.find(delim);
1133         if (i == a.length() - 1) {
1134                 piece = a.substr(0, i);
1135         } else if (i == 0) {
1136                 piece.erase();
1137                 tmp = a.substr(i + 1);
1138         } else if (i != String::npos) {
1139                 piece = a.substr(0, i);
1140                 tmp = a.substr(i + 1);
1141         } else {
1142                 piece = a;
1143         }
1144         return tmp;
1145 }
1146
1147
1148 // FIXME: why is this specialization needed?
1149 template<typename Char> inline
1150 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
1151 {
1152         docstring tmp;
1153         size_t i = a.find(delim);
1154         if (i == a.length() - 1) {
1155                 piece = a.substr(0, i);
1156         } else if (i == 0) {
1157                 piece.erase();
1158                 tmp = a.substr(i + 1);
1159         } else if (i != docstring::npos) {
1160                 piece = a.substr(0, i);
1161                 tmp = a.substr(i + 1);
1162         } else {
1163                 piece = a;
1164         }
1165         return tmp;
1166 }
1167
1168 } // namespace
1169
1170
1171 string const split(string const & a, string & piece, char delim)
1172 {
1173         return doSplit(a, piece, delim);
1174 }
1175
1176
1177 docstring const split(docstring const & a, docstring & piece, char_type delim)
1178 {
1179         return doSplit(a, piece, delim);
1180 }
1181
1182
1183 string const split(string const & a, char delim)
1184 {
1185         string tmp;
1186         size_t i = a.find(delim);
1187         if (i != string::npos) // found delim
1188                 tmp = a.substr(i + 1);
1189         return tmp;
1190 }
1191
1192
1193 // ale970521
1194 string const rsplit(string const & a, string & piece, char delim)
1195 {
1196         string tmp;
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
1202                 piece.erase();
1203         }
1204         return tmp;
1205 }
1206
1207
1208 docstring const rsplit(docstring const & a, docstring & piece, char_type delim)
1209 {
1210         docstring tmp;
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
1216                 piece.erase();
1217         }
1218         return tmp;
1219 }
1220
1221
1222 docstring const rsplit(docstring const & a, char_type delim)
1223 {
1224         docstring tmp;
1225         size_t i = a.rfind(delim);
1226         if (i != string::npos)
1227                 tmp = a.substr(i + 1);
1228         return tmp;
1229 }
1230
1231
1232 docstring const escape(docstring const & lab)
1233 {
1234         char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
1235                                    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1236         docstring enc;
1237         for (char_type const c : lab) {
1238                 if (c >= 128 || c == '=' || c == '%' || c == '#' || c == '$'
1239                     || c == '}' || c == '{' || c == ']' || c == '[' || c == '&'
1240                     || c == '\\')
1241                 {
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);
1248                         enc += '=';
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];
1255                 } else {
1256                         enc += c;
1257                 }
1258         }
1259         return enc;
1260 }
1261
1262
1263 docstring const protectArgument(docstring & arg, char const l,
1264                           char const r)
1265 {
1266         if (contains(arg, l) || contains(arg, r))
1267                 // protect brackets
1268                 arg = '{' + arg + '}';
1269         return arg;
1270 }
1271
1272
1273 bool truncateWithEllipsis(docstring & str, size_t const len, bool const mid)
1274 {
1275         if (str.size() <= len)
1276                 return false;
1277         if (mid && len > 0) {
1278                 size_t const hlen = len / 2;
1279                 docstring suffix = str.substr(str.size() - hlen);
1280                 str.resize(hlen);
1281                 str[hlen - 1] = 0x2026;// HORIZONTAL ELLIPSIS
1282                 str += suffix;
1283         } else {
1284                 str.resize(len);
1285                 if (len > 0)
1286                         str[len - 1] = 0x2026;// HORIZONTAL ELLIPSIS
1287         }
1288         return true;
1289 }
1290
1291
1292 namespace {
1293
1294 // this doesn't check whether str is empty, so do that first.
1295 vector<docstring> wrapToVec(docstring const & str, int ind,
1296                             size_t const width)
1297 {
1298         docstring s = trim(str);
1299         if (s.empty())
1300                 return vector<docstring>();
1301
1302         docstring indent;
1303         if (ind < 0) {
1304                 indent.insert(0, -ind, ' ');
1305                 ind = 0;
1306         } else if (ind > 0)
1307                 s.insert(0, ind, ' ');
1308
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)) {
1314                         // no space found
1315                         truncateWithEllipsis(s, width);
1316                         break;
1317                 }
1318                 retval.push_back(s.substr(0, i));
1319                 s = indent + s.substr(i);
1320                 ind = indent.size();
1321         }
1322         if (!s.empty())
1323                 retval.push_back(s);
1324         return retval;
1325 }
1326
1327 } // namespace
1328
1329
1330 docstring wrap(docstring const & str, int const ind, size_t const width)
1331 {
1332         docstring s = trim(str);
1333         if (s.empty())
1334                 return docstring();
1335
1336         vector<docstring> const svec = wrapToVec(str, ind, width);
1337         return getStringFromVector(svec, from_ascii("\n"));
1338 }
1339
1340
1341 docstring wrapParas(docstring const & str, int const indent,
1342                     size_t const width, size_t const maxlines)
1343 {
1344         if (str.empty())
1345                 return docstring();
1346
1347         vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1348         vector<docstring> retval;
1349
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();
1355                 if (nlines == 0)
1356                         continue;
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))
1365                                         // no space found
1366                                         truncateWithEllipsis(last, lsize);
1367                                 else
1368                                         truncateWithEllipsis(last, i);
1369                         } else
1370                                 last.push_back(0x2026);//HORIZONTAL ELLIPSIS
1371                         tmp.pop_back();
1372                         tmp.push_back(last);
1373                 }
1374                 retval.insert(retval.end(), tmp.begin(), tmp.end());
1375                 if (maxlines > 0 && retval.size() >= maxlines)
1376                         break;
1377         }
1378         return getStringFromVector(retval, from_ascii("\n"));
1379 }
1380
1381
1382 namespace {
1383
1384 template<typename String> vector<String> const
1385 getVectorFromStringT(String const & str, String const & delim,
1386                      bool keepempty, bool trimit)
1387 {
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.
1391 #if 1
1392         vector<String> vec;
1393         if (str.empty())
1394                 return vec;
1395         String keys = trimit ? rtrim(str) : str;
1396         while (true) {
1397                 size_t const idx = keys.find(delim);
1398                 if (idx == String::npos) {
1399                         vec.push_back(trimit ? ltrim(keys) : keys);
1400                         break;
1401                 }
1402                 String const key = trimit ?
1403                         trim(keys.substr(0, idx)) : keys.substr(0, idx);
1404                 if (!key.empty() || keepempty)
1405                         vec.push_back(key);
1406                 size_t const start = idx + delim.size();
1407                 keys = keys.substr(start);
1408         }
1409         return vec;
1410 #else
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());
1416 #endif
1417 }
1418
1419
1420 template<typename String> const String
1421         getStringFromVector(vector<String> const & vec, String const & delim)
1422 {
1423         String str;
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);
1428                 if (item.empty())
1429                         continue;
1430                 if (!str.empty())
1431                         str += delim;
1432                 str += item;
1433         }
1434         return str;
1435 }
1436
1437 } // namespace
1438
1439
1440 vector<string> const getVectorFromString(string const & str,
1441         string const & delim, bool keepempty, bool trimit)
1442 {
1443         return getVectorFromStringT<string>(str, delim, keepempty, trimit);
1444 }
1445
1446
1447 vector<docstring> const getVectorFromString(docstring const & str,
1448         docstring const & delim, bool keepempty, bool trimit)
1449 {
1450         return getVectorFromStringT<docstring>(str, delim, keepempty, trimit);
1451 }
1452
1453
1454 string const getStringFromVector(vector<string> const & vec,
1455                                  string const & delim)
1456 {
1457         return getStringFromVector<string>(vec, delim);
1458 }
1459
1460
1461 docstring const getStringFromVector(vector<docstring> const & vec,
1462                                     docstring const & delim)
1463 {
1464         return getStringFromVector<docstring>(vec, delim);
1465 }
1466
1467
1468 int findToken(char const * const str[], string const & search_token)
1469 {
1470         int i = 0;
1471
1472         while (str[i][0] && str[i] != search_token)
1473                 ++i;
1474         if (!str[i][0])
1475                 i = -1;
1476         return i;
1477 }
1478
1479
1480 std::string formatFPNumber(double x)
1481 {
1482         // Need manual tweaking, QString::number(x, 'f', 16) does not work either
1483         ostringstream os;
1484         os << std::fixed;
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, ".");
1494         }
1495         return result;
1496 }
1497
1498
1499 docstring to_percent_encoding(docstring const & in, docstring const & ex)
1500 {
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()));
1504 }
1505
1506
1507 string from_percent_encoding(string const & in)
1508 {
1509         return QByteArray::fromPercentEncoding(in.c_str()).data();
1510 }
1511
1512
1513 int countExpanders(docstring const & str)
1514 {
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.)
1519         int nexp = 0;
1520         for (char_type c : str)
1521                 if (c > 0x0d && isSpace(c))
1522                         ++nexp;
1523         return nexp;
1524 }
1525
1526
1527 docstring bformat(docstring const & fmt, int arg1)
1528 {
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("%"));
1532 }
1533
1534
1535 docstring bformat(docstring const & fmt, long arg1)
1536 {
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("%"));
1540 }
1541
1542
1543 #ifdef HAVE_LONG_LONG_INT
1544 docstring bformat(docstring const & fmt, long long arg1)
1545 {
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("%"));
1549 }
1550 #endif
1551
1552
1553 docstring bformat(docstring const & fmt, unsigned int arg1)
1554 {
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("%"));
1558 }
1559
1560
1561 docstring bformat(docstring const & fmt, docstring const & arg1)
1562 {
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("%"));
1566 }
1567
1568
1569 docstring bformat(docstring const & fmt, char * arg1)
1570 {
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("%"));
1574 }
1575
1576
1577 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2)
1578 {
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("%"));
1584 }
1585
1586
1587 docstring bformat(docstring const & fmt, docstring const & arg1, int arg2)
1588 {
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("%"));
1594 }
1595
1596
1597 docstring bformat(docstring const & fmt, char const * arg1, docstring const & arg2)
1598 {
1599         return bformat(fmt, from_ascii(arg1), arg2);
1600 }
1601
1602
1603 docstring bformat(docstring const & fmt, int arg1, int arg2)
1604 {
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("%"));
1610 }
1611
1612
1613 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2, docstring const & arg3)
1614 {
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("%"));
1622 }
1623
1624
1625 docstring bformat(docstring const & fmt,
1626                docstring const & arg1, docstring const & arg2, docstring const & arg3, docstring const & arg4)
1627 {
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("%"));
1637 }
1638
1639 docstring bformat(docstring const & fmt, docstring const & arg1,
1640                                   docstring const & arg2, docstring const & arg3,
1641                                   docstring const & arg4, docstring const & arg5)
1642 {
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("%"));
1654 }
1655
1656 } // namespace support
1657 } // namespace lyx