]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.cpp
Fix bug #11104. Activate refstyle support for InsetMathRef.
[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
193 bool isAlnumASCII(char_type c)
194 {
195         return isAlphaASCII(c) || isDigitASCII(c);
196 }
197
198
199 bool isASCII(char_type c)
200 {
201         return c < 0x80;
202 }
203
204
205 bool isOpenPunctuation(char_type c)
206 {
207         if (!is_utf16(c)) {
208                 // assume that no non-utf16 character is an op
209                 // c outside the UCS4 range is caught as well
210                 return false;
211         }
212         QChar const qc = ucs4_to_qchar(c);
213         return qc.category() == QChar::Punctuation_Open;
214 }
215
216
217 namespace support {
218
219 int compare_no_case(docstring const & s, docstring const & s2)
220 {
221         docstring::const_iterator p = s.begin();
222         docstring::const_iterator p2 = s2.begin();
223
224         while (p != s.end() && p2 != s2.end()) {
225                 char_type const lc1 = lowercase(*p);
226                 char_type const lc2 = lowercase(*p2);
227                 if (lc1 != lc2)
228                         return (lc1 < lc2) ? -1 : 1;
229                 ++p;
230                 ++p2;
231         }
232
233         if (s.size() == s2.size())
234                 return 0;
235         if (s.size() < s2.size())
236                 return -1;
237         return 1;
238 }
239
240
241 int compare_locale(docstring const & s, docstring const & s2)
242 {
243         return QString::localeAwareCompare(toqstr(s), toqstr(s2));
244 }
245
246
247 namespace {
248
249 template<typename Char>
250 Char ascii_tolower(Char c) {
251         if (c >= 'A' && c <= 'Z')
252                 return c - 'A' + 'a';
253         return c;
254 }
255
256 } // namespace
257
258
259 int compare_ascii_no_case(string const & s, string const & s2)
260 {
261         string::const_iterator p = s.begin();
262         string::const_iterator p2 = s2.begin();
263
264         while (p != s.end() && p2 != s2.end()) {
265                 int const lc1 = ascii_tolower(*p);
266                 int const lc2 = ascii_tolower(*p2);
267                 if (lc1 != lc2)
268                         return (lc1 < lc2) ? -1 : 1;
269                 ++p;
270                 ++p2;
271         }
272
273         if (s.size() == s2.size())
274                 return 0;
275         if (s.size() < s2.size())
276                 return -1;
277         return 1;
278 }
279
280
281 int compare_ascii_no_case(docstring const & s, docstring const & s2)
282 {
283         docstring::const_iterator p = s.begin();
284         docstring::const_iterator p2 = s2.begin();
285
286         while (p != s.end() && p2 != s2.end()) {
287                 char_type const lc1 = ascii_tolower(*p);
288                 char_type const lc2 = ascii_tolower(*p2);
289                 if (lc1 != lc2)
290                         return (lc1 < lc2) ? -1 : 1;
291                 ++p;
292                 ++p2;
293         }
294
295         if (s.size() == s2.size())
296                 return 0;
297         if (s.size() < s2.size())
298                 return -1;
299         return 1;
300 }
301
302
303 bool isStrInt(string const & str)
304 {
305         if (str.empty())
306                 return false;
307
308         // Remove leading and trailing white space chars.
309         string const tmpstr = trim(str);
310         if (tmpstr.empty())
311                 return false;
312
313         string::const_iterator cit = tmpstr.begin();
314         if ((*cit) == '-')
315                 ++cit;
316
317         string::const_iterator end = tmpstr.end();
318         for (; cit != end; ++cit)
319                 if (!isDigitASCII(*cit))
320                         return false;
321
322         return true;
323 }
324
325
326 bool isStrUnsignedInt(string const & str)
327 {
328         if (str.empty())
329                 return false;
330
331         // Remove leading and trailing white space chars.
332         string const tmpstr = trim(str);
333         if (tmpstr.empty())
334                 return false;
335
336         string::const_iterator cit = tmpstr.begin();
337         string::const_iterator end = tmpstr.end();
338         for (; cit != end; ++cit)
339                 if (!isDigitASCII(*cit))
340                         return false;
341
342         return true;
343 }
344
345
346 bool isStrDbl(string const & str)
347 {
348         if (str.empty())
349                 return false;
350
351         // Remove leading and trailing white space chars.
352         string const tmpstr = trim(str);
353         if (tmpstr.empty())
354                 return false;
355         //      if (tmpstr.count('.') > 1) return false;
356
357         string::const_iterator cit = tmpstr.begin();
358         bool found_dot = false;
359         if (*cit == '-')
360                 ++cit;
361         string::const_iterator end = tmpstr.end();
362         for (; cit != end; ++cit) {
363                 if (!isDigitASCII(*cit) && *cit != '.')
364                         return false;
365                 if ('.' == (*cit)) {
366                         if (found_dot)
367                                 return false;
368                         found_dot = true;
369                 }
370         }
371         return true;
372 }
373
374
375 bool hasDigitASCII(docstring const & str)
376 {
377         docstring::const_iterator cit = str.begin();
378         docstring::const_iterator const end = str.end();
379         for (; cit != end; ++cit)
380                 if (isDigitASCII(*cit))
381                         return true;
382         return false;
383 }
384
385
386 bool isHexChar(char_type c)
387 {
388         return c == '0' ||
389                 c == '1' ||
390                 c == '2' ||
391                 c == '3' ||
392                 c == '4' ||
393                 c == '5' ||
394                 c == '6' ||
395                 c == '7' ||
396                 c == '8' ||
397                 c == '9' ||
398                 c == 'a' || c == 'A' ||
399                 c == 'b' || c == 'B' ||
400                 c == 'c' || c == 'C' ||
401                 c == 'd' || c == 'D' ||
402                 c == 'e' || c == 'E' ||
403                 c == 'f' || c == 'F';
404 }
405
406
407 bool isHex(docstring const & str)
408 {
409         size_t index = 0;
410
411         if (str.length() > 2 && str[0] == '0' &&
412             (str[1] == 'x' || str[1] == 'X'))
413                 index = 2;
414
415         size_t const len = str.length();
416
417         for (; index < len; ++index) {
418                 if (!isHexChar(str[index]))
419                         return false;
420         }
421         return true;
422 }
423
424
425 unsigned int hexToInt(docstring const & str)
426 {
427         string s = to_ascii(str);
428         unsigned int h;
429         sscanf(s.c_str(), "%x", &h);
430         return h;
431 }
432
433
434 bool isAscii(docstring const & str)
435 {
436         size_t const len = str.length();
437         for (size_t i = 0; i < len; ++i)
438                 if (str[i] >= 0x80)
439                         return false;
440         return true;
441 }
442
443
444 bool isAscii(string const & str)
445 {
446         size_t const len = str.length();
447         for (size_t i = 0; i < len; ++i)
448                 if (static_cast<unsigned char>(str[i]) >= 0x80)
449                         return false;
450         return true;
451 }
452
453
454 char lowercase(char c)
455 {
456         LASSERT(isASCII(c), return '?');
457         return char(tolower(c));
458 }
459
460
461 char uppercase(char c)
462 {
463         LASSERT(isASCII(c), return '?');
464         return char(toupper(c));
465 }
466
467
468 char_type lowercase(char_type c)
469 {
470         if (!is_utf16(c))
471                 // We don't know how to lowercase a non-utf16 char
472                 return c;
473         return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
474 }
475
476
477 char_type uppercase(char_type c)
478 {
479         if (!is_utf16(c))
480                 // We don't know how to uppercase a non-utf16 char
481                 return c;
482         return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
483 }
484
485
486 bool isLowerCase(char_type ch) {
487         return lowercase(ch) == ch;
488 }
489
490
491 bool isUpperCase(char_type ch) {
492         return uppercase(ch) == ch;
493 }
494
495
496 namespace {
497
498 // since we cannot use tolower and toupper directly in the
499 // calls to transform yet, we use these helper clases. (Lgb)
500
501 struct local_lowercase {
502         char_type operator()(char_type c) const {
503                 return lowercase(c);
504         }
505 };
506
507 struct local_uppercase {
508         char_type operator()(char_type c) const {
509                 return uppercase(c);
510         }
511 };
512
513 template<typename Char> struct local_ascii_lowercase {
514         Char operator()(Char c) const { return ascii_tolower(c); }
515 };
516
517 } // namespace
518
519
520 docstring const lowercase(docstring const & a)
521 {
522         docstring tmp(a);
523         transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
524         return tmp;
525 }
526
527
528 /* Uncomment here and in lstrings.h if you should need this.
529 string const lowercase(string const & a)
530 {
531         string tmp(a);
532         transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
533         return tmp;
534 }
535 */
536
537
538 docstring const uppercase(docstring const & a)
539 {
540         docstring tmp(a);
541         transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
542         return tmp;
543 }
544
545
546 docstring capitalize(docstring const & s) {
547         docstring ret = s;
548         char_type t = uppercase(ret[0]);
549         ret[0] = t;
550         return ret;
551 }
552
553
554 string const ascii_lowercase(string const & a)
555 {
556         string tmp(a);
557         transform(tmp.begin(), tmp.end(), tmp.begin(),
558                   local_ascii_lowercase<char>());
559         return tmp;
560 }
561
562
563 docstring const ascii_lowercase(docstring const & a)
564 {
565         docstring tmp(a);
566         transform(tmp.begin(), tmp.end(), tmp.begin(),
567                   local_ascii_lowercase<char_type>());
568         return tmp;
569 }
570
571
572 char_type superscript(char_type c)
573 {
574         switch (c) {
575                 case    '2': return 0x00b2;
576                 case    '3': return 0x00b3;
577                 case    '1': return 0x00b9;
578                 case    '0': return 0x2070;
579                 case    'i': return 0x2071;
580                 case    '4': return 0x2074;
581                 case    '5': return 0x2075;
582                 case    '6': return 0x2076;
583                 case    '7': return 0x2077;
584                 case    '8': return 0x2078;
585                 case    '9': return 0x2079;
586                 case    '+': return 0x207a;
587                 case    '-': return 0x207b;
588                 case    '=': return 0x207c;
589                 case    '(': return 0x207d;
590                 case    ')': return 0x207e;
591                 case    'n': return 0x207f;
592                 case    'h': return 0x02b0;
593                 case 0x0266: return 0x02b1; // LATIN SMALL LETTER H WITH HOOK
594                 case    'j': return 0x02b2;
595                 case    'r': return 0x02b3;
596                 case 0x0279: return 0x02b4; // LATIN SMALL LETTER TURNED R
597                 case 0x027b: return 0x02b5; // LATIN SMALL LETTER TURNED R WITH HOOK
598                 case 0x0281: return 0x02b6; // LATIN SMALL LETTER CAPITAL INVERTED R
599                 case    'w': return 0x02b7;
600                 case    'y': return 0x02b8;
601 //              case 0x0294: return 0x02c0; // LATIN LETTER GLOTTAL STOP)
602 //              case 0x0295: return 0x02c1; // LATIN LETTER PHARYNGEAL VOICED FRICATIVE
603                                             // (= LATIN LETTER REVERSED GLOTTAL STOP)
604                 case    'l': return 0x02e1;
605                 case    's': return 0x02e2;
606                 case    'x': return 0x02e3;
607 //              case 0x0295: return 0x02e4; // LATIN SMALL LETTER REVERSED GLOTTAL STOP
608                 case    'A': return 0x1d2c;
609                 case 0x00c6: return 0x1d2d; // LATIN CAPITAL LETTER AE
610                 case    'B': return 0x1d2e;
611                 case    'D': return 0x1d30;
612                 case    'E': return 0x1d31;
613                 case    'G': return 0x1d33;
614                 case    'H': return 0x1d34;
615                 case    'I': return 0x1d35;
616                 case    'J': return 0x1d36;
617                 case    'K': return 0x1d37;
618                 case    'L': return 0x1d38;
619                 case    'M': return 0x1d39;
620                 case    'N': return 0x1d3a;
621                 case    'O': return 0x1d3c;
622                 case    'P': return 0x1d3e;
623                 case    'R': return 0x1d3f;
624                 case    'T': return 0x1d40;
625                 case    'U': return 0x1d41;
626                 case    'W': return 0x1d42;
627                 case    'a': return 0x1d43;
628                 case 0x0250: return 0x1d44; // LATIN SMALL LETTER TURNED A
629                 case 0x0251: return 0x1d45; // LATIN SMALL LETTER ALPHA
630                 case    'b': return 0x1d47;
631                 case    'd': return 0x1d48;
632                 case    'e': return 0x1d49;
633                 case 0x0259: return 0x1d4a; // LATIN SMALL LETTER SCHWA
634                 case 0x025b: return 0x1d4b; // LATIN SMALL LETTER OPEN E
635                 case 0x1d08: return 0x1d4c; // LATIN SMALL LETTER TURNED OPEN E
636                 case    'g': return 0x1d4d;
637                 case 0x1d09: return 0x1d4e; // LATIN SMALL LETTER TURNED I
638                 case    'k': return 0x1d4f;
639                 case    'm': return 0x1d50;
640                 case 0x014b: return 0x1d51; // LATIN SMALL LETTER ENG
641                 case    'o': return 0x1d52;
642                 case 0x0254: return 0x1d53; // LATIN SMALL LETTER OPEN O
643                 case 0x1d16: return 0x1d54; // LATIN SMALL LETTER TOP HALF O
644                 case 0x1d17: return 0x1d55; // LATIN SMALL LETTER BOTTOM HALF O
645                 case    'p': return 0x1d56;
646                 case    't': return 0x1d57;
647                 case    'u': return 0x1d58;
648                 case 0x1d1d: return 0x1d59; // LATIN SMALL LETTER SIDEWAYS U
649                 case 0x1d1f: return 0x1d5a; // LATIN SMALL LETTER SIDEWAYS TURNED M
650                 case    'v': return 0x1d5b;
651                 case 0x03b2: return 0x1d5d; // GREEK SMALL LETTER BETA
652                 case 0x03b3: return 0x1d5e; // GREEK SMALL LETTER GAMMA
653                 case 0x03b4: return 0x1d5f; // GREEK SMALL LETTER DELTA
654                 case 0x03c6: return 0x1d60; // GREEK SMALL LETTER PHI
655                 case 0x03c7: return 0x1d61; // GREEK SMALL LETTER CHI
656         }
657         return c;
658 }
659
660
661 char_type subscript(char_type c)
662 {
663         switch (c) {
664                 case    'i': return 0x1d62;
665                 case    'r': return 0x1d63;
666                 case    'u': return 0x1d64;
667                 case    'v': return 0x1d65;
668                 case 0x03b2: return 0x1d66; // GREEK SMALL LETTER BETA
669                 case 0x03b3: return 0x1d67; // GREEK SMALL LETTER GAMMA
670                 case 0x03c1: return 0x1d68; // GREEK SMALL LETTER RHO
671                 case 0x03c6: return 0x1d69; // GREEK SMALL LETTER PHI
672                 case 0x03c7: return 0x1d6a; // GREEK SMALL LETTER CHI
673                 case    '0': return 0x2080;
674                 case    '1': return 0x2081;
675                 case    '2': return 0x2082;
676                 case    '3': return 0x2083;
677                 case    '4': return 0x2084;
678                 case    '5': return 0x2085;
679                 case    '6': return 0x2086;
680                 case    '7': return 0x2087;
681                 case    '8': return 0x2088;
682                 case    '9': return 0x2089;
683                 case    '+': return 0x208a;
684                 case    '-': return 0x208b;
685                 case    '=': return 0x208c;
686                 case    '(': return 0x208d;
687                 case    ')': return 0x208e;
688                 case    'a': return 0x2090;
689                 case    'e': return 0x2091;
690                 case    'o': return 0x2092;
691                 case    'x': return 0x2093;
692                 case 0x0259: return 0x2093; // LATIN SMALL LETTER SCHWA
693         }
694         return c;
695 }
696
697
698 bool prefixIs(docstring const & a, char_type c)
699 {
700         if (a.empty())
701                 return false;
702         return a[0] == c;
703 }
704
705
706 bool prefixIs(string const & a, string const & pre)
707 {
708         size_t const prelen = pre.length();
709         size_t const alen = a.length();
710         return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
711 }
712
713
714 bool prefixIs(docstring const & a, docstring const & pre)
715 {
716         size_t const prelen = pre.length();
717         size_t const alen = a.length();
718         return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
719 }
720
721
722 bool suffixIs(string const & a, char c)
723 {
724         if (a.empty())
725                 return false;
726         return a[a.length() - 1] == c;
727 }
728
729
730 bool suffixIs(docstring const & a, char_type c)
731 {
732         if (a.empty())
733                 return false;
734         return a[a.length() - 1] == c;
735 }
736
737
738 bool suffixIs(string const & a, string const & suf)
739 {
740         size_t const suflen = suf.length();
741         size_t const alen = a.length();
742         return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
743 }
744
745
746 bool suffixIs(docstring const & a, docstring const & suf)
747 {
748         size_t const suflen = suf.length();
749         size_t const alen = a.length();
750         return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
751 }
752
753
754 bool containsOnly(string const & s, string const & cset)
755 {
756         return s.find_first_not_of(cset) == string::npos;
757 }
758
759
760 bool containsOnly(docstring const & s, string const & cset)
761 {
762         return s.find_first_not_of(from_ascii(cset)) == string::npos;
763 }
764
765
766 // ale970405+lasgoutt-970425
767 // rewritten to use new string (Lgb)
768 string const token(string const & a, char delim, int n)
769 {
770         if (a.empty())
771                 return string();
772
773         size_t k = 0;
774         size_t i = 0;
775
776         // Find delimiter or end of string
777         for (; n--;) {
778                 if ((i = a.find(delim, i)) == string::npos)
779                         break;
780                 else
781                         ++i; // step delim
782         }
783
784         // i is now the n'th delim (or string::npos)
785         if (i == string::npos)
786                 return string();
787
788         k = a.find(delim, i);
789         // k is now the n'th + 1 delim (or string::npos)
790
791         return a.substr(i, k - i);
792 }
793
794
795 docstring const token(docstring const & a, char_type delim, int n)
796 {
797         if (a.empty())
798                 return docstring();
799
800         size_t k = 0;
801         size_t i = 0;
802
803         // Find delimiter or end of string
804         for (; n--;) {
805                 if ((i = a.find(delim, i)) == docstring::npos)
806                         break;
807                 else
808                         ++i; // step delim
809         }
810
811         // i is now the n'th delim (or string::npos)
812         if (i == docstring::npos)
813                 return docstring();
814
815         k = a.find(delim, i);
816         // k is now the n'th + 1 delim (or string::npos)
817
818         return a.substr(i, k - i);
819 }
820
821
822 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
823 // rewritten to use new string (Lgb)
824 int tokenPos(string const & a, char delim, string const & tok)
825 {
826         int i = 0;
827         string str = a;
828         string tmptok;
829
830         while (!str.empty()) {
831                 str = split(str, tmptok, delim);
832                 if (tok == tmptok)
833                         return i;
834                 ++i;
835         }
836         return -1;
837 }
838
839
840 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
841 // rewritten to use new string (Lgb)
842 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
843 {
844         int i = 0;
845         docstring str = a;
846         docstring tmptok;
847
848         while (!str.empty()) {
849                 str = split(str, tmptok, delim);
850                 if (tok == tmptok)
851                         return i;
852                 ++i;
853         }
854         return -1;
855 }
856
857
858 namespace {
859
860 /// Substitute all \a oldchar with \a newchar
861 template<typename Ch> inline
862 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
863                 Ch oldchar, Ch newchar)
864 {
865         typedef basic_string<Ch> String;
866         String tmp(a);
867         typename String::iterator lit = tmp.begin();
868         typename String::iterator end = tmp.end();
869         for (; lit != end; ++lit)
870                 if ((*lit) == oldchar)
871                         (*lit) = newchar;
872         return tmp;
873 }
874
875
876 /// Substitute all \a oldchar with \a newchar
877 docstring const subst_char(docstring const & a,
878         docstring::value_type oldchar, docstring::value_type newchar)
879 {
880         docstring tmp(a);
881         docstring::iterator lit = tmp.begin();
882         docstring::iterator end = tmp.end();
883         for (; lit != end; ++lit)
884                 if ((*lit) == oldchar)
885                         (*lit) = newchar;
886         return tmp;
887 }
888
889
890 /// substitutes all instances of \a oldstr with \a newstr
891 template<typename String> inline
892 String const subst_string(String const & a,
893                 String const & oldstr, String const & newstr)
894 {
895         LASSERT(!oldstr.empty(), return a);
896         String lstr = a;
897         size_t i = 0;
898         size_t const olen = oldstr.length();
899         while ((i = lstr.find(oldstr, i)) != string::npos) {
900                 lstr.replace(i, olen, newstr);
901                 i += newstr.length(); // We need to be sure that we don't
902                 // use the same i over and over again.
903         }
904         return lstr;
905 }
906
907
908 docstring const subst_string(docstring const & a,
909                 docstring const & oldstr, docstring const & newstr)
910 {
911         LASSERT(!oldstr.empty(), return a);
912         docstring lstr = a;
913         size_t i = 0;
914         size_t const olen = oldstr.length();
915         while ((i = lstr.find(oldstr, i)) != string::npos) {
916                 lstr.replace(i, olen, newstr);
917                 i += newstr.length(); // We need to be sure that we don't
918                 // use the same i over and over again.
919         }
920         return lstr;
921 }
922
923 } // namespace
924
925
926 string const subst(string const & a, char oldchar, char newchar)
927 {
928         return subst_char(a, oldchar, newchar);
929 }
930
931
932 docstring const subst(docstring const & a,
933                 char_type oldchar, char_type newchar)
934 {
935         return subst_char(a, oldchar, newchar);
936 }
937
938
939 string const subst(string const & a,
940                 string const & oldstr, string const & newstr)
941 {
942         return subst_string(a, oldstr, newstr);
943 }
944
945
946 docstring const subst(docstring const & a,
947                 docstring const & oldstr, docstring const & newstr)
948 {
949         return subst_string(a, oldstr, newstr);
950 }
951
952
953 int count_char(string const & str, char chr)
954 {
955         int count = 0;
956         string::const_iterator lit = str.begin();
957         string::const_iterator end = str.end();
958         for (; lit != end; ++lit)
959                 if ((*lit) == chr)
960                         count++;
961         return count;
962 }
963
964
965 /// Count all occurrences of char \a chr inside \a str
966 int count_char(docstring const & str, docstring::value_type chr)
967 {
968         int count = 0;
969         docstring::const_iterator lit = str.begin();
970         docstring::const_iterator end = str.end();
971         for (; lit != end; ++lit)
972                 if ((*lit) == chr)
973                         count++;
974         return count;
975 }
976
977
978 int count_bin_chars(string const & str)
979 {
980         QString const qstr = toqstr(str).simplified();
981         int count = 0;
982         QString::const_iterator cit = qstr.begin();
983         QString::const_iterator end = qstr.end();
984         for (; cit != end; ++cit)  {
985                 switch (cit->category()) {
986                 case QChar::Separator_Line:
987                 case QChar::Separator_Paragraph:
988                 case QChar::Other_Control:
989                 case QChar::Other_Format:
990                 case QChar::Other_Surrogate:
991                 case QChar::Other_PrivateUse:
992                 case QChar::Other_NotAssigned:
993                         ++count;
994                         break;
995                 default:
996                         break;
997                 }
998         }
999         return count;
1000 }
1001
1002
1003 docstring const trim(docstring const & a, char const * p)
1004 {
1005         LASSERT(p, return a);
1006
1007         if (a.empty() || !*p)
1008                 return a;
1009
1010         docstring s = from_ascii(p);
1011         size_t r = a.find_last_not_of(s);
1012         size_t l = a.find_first_not_of(s);
1013
1014         // Is this the minimal test? (lgb)
1015         if (r == docstring::npos && l == docstring::npos)
1016                 return docstring();
1017
1018         return a.substr(l, r - l + 1);
1019 }
1020
1021
1022 string const trim(string const & a, char const * p)
1023 {
1024         LASSERT(p, return a);
1025
1026         if (a.empty() || !*p)
1027                 return a;
1028
1029         size_t r = a.find_last_not_of(p);
1030         size_t l = a.find_first_not_of(p);
1031
1032         // Is this the minimal test? (lgb)
1033         if (r == string::npos && l == string::npos)
1034                 return string();
1035
1036         return a.substr(l, r - l + 1);
1037 }
1038
1039
1040 string const rtrim(string const & a, char const * p)
1041 {
1042         LASSERT(p, return a);
1043
1044         if (a.empty() || !*p)
1045                 return a;
1046
1047         size_t r = a.find_last_not_of(p);
1048
1049         // Is this test really needed? (Lgb)
1050         if (r == string::npos)
1051                 return string();
1052
1053         return a.substr(0, r + 1);
1054 }
1055
1056
1057 docstring const rtrim(docstring const & a, char const * p)
1058 {
1059         LASSERT(p, return a);
1060
1061         if (a.empty() || !*p)
1062                 return a;
1063
1064         size_t r = a.find_last_not_of(from_ascii(p));
1065
1066         // Is this test really needed? (Lgb)
1067         if (r == docstring::npos)
1068                 return docstring();
1069
1070         return a.substr(0, r + 1);
1071 }
1072
1073
1074 string const ltrim(string const & a, char const * p)
1075 {
1076         LASSERT(p, return a);
1077         if (a.empty() || !*p)
1078                 return a;
1079         size_t l = a.find_first_not_of(p);
1080         if (l == string::npos)
1081                 return string();
1082         return a.substr(l, string::npos);
1083 }
1084
1085
1086 docstring const ltrim(docstring const & a, char const * p)
1087 {
1088         LASSERT(p, return a);
1089         if (a.empty() || !*p)
1090                 return a;
1091         size_t l = a.find_first_not_of(from_ascii(p));
1092         if (l == docstring::npos)
1093                 return docstring();
1094         return a.substr(l, docstring::npos);
1095 }
1096
1097 namespace {
1098
1099 template<typename String, typename Char> inline
1100 String const doSplit(String const & a, String & piece, Char delim)
1101 {
1102         String tmp;
1103         size_t i = a.find(delim);
1104         if (i == a.length() - 1) {
1105                 piece = a.substr(0, i);
1106         } else if (i == 0) {
1107                 piece.erase();
1108                 tmp = a.substr(i + 1);
1109         } else if (i != String::npos) {
1110                 piece = a.substr(0, i);
1111                 tmp = a.substr(i + 1);
1112         } else {
1113                 piece = a;
1114         }
1115         return tmp;
1116 }
1117
1118
1119 // FIXME: why is this specialization needed?
1120 template<typename Char> inline
1121 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
1122 {
1123         docstring tmp;
1124         size_t i = a.find(delim);
1125         if (i == a.length() - 1) {
1126                 piece = a.substr(0, i);
1127         } else if (i == 0) {
1128                 piece.erase();
1129                 tmp = a.substr(i + 1);
1130         } else if (i != docstring::npos) {
1131                 piece = a.substr(0, i);
1132                 tmp = a.substr(i + 1);
1133         } else {
1134                 piece = a;
1135         }
1136         return tmp;
1137 }
1138
1139 } // namespace
1140
1141
1142 string const split(string const & a, string & piece, char delim)
1143 {
1144         return doSplit(a, piece, delim);
1145 }
1146
1147
1148 docstring const split(docstring const & a, docstring & piece, char_type delim)
1149 {
1150         return doSplit(a, piece, delim);
1151 }
1152
1153
1154 string const split(string const & a, char delim)
1155 {
1156         string tmp;
1157         size_t i = a.find(delim);
1158         if (i != string::npos) // found delim
1159                 tmp = a.substr(i + 1);
1160         return tmp;
1161 }
1162
1163
1164 // ale970521
1165 string const rsplit(string const & a, string & piece, char delim)
1166 {
1167         string tmp;
1168         size_t i = a.rfind(delim);
1169         if (i != string::npos) { // delimiter was found
1170                 piece = a.substr(0, i);
1171                 tmp = a.substr(i + 1);
1172         } else { // delimiter was not found
1173                 piece.erase();
1174         }
1175         return tmp;
1176 }
1177
1178
1179 docstring const rsplit(docstring const & a, docstring & piece, char_type delim)
1180 {
1181         docstring tmp;
1182         size_t i = a.rfind(delim);
1183         if (i != string::npos) { // delimiter was found
1184                 piece = a.substr(0, i);
1185                 tmp = a.substr(i + 1);
1186         } else { // delimiter was not found
1187                 piece.erase();
1188         }
1189         return tmp;
1190 }
1191
1192
1193 docstring const rsplit(docstring const & a, char_type delim)
1194 {
1195         docstring tmp;
1196         size_t i = a.rfind(delim);
1197         if (i != string::npos)
1198                 tmp = a.substr(i + 1);
1199         return tmp;
1200 }
1201
1202
1203 docstring const escape(docstring const & lab)
1204 {
1205         char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
1206                                    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1207         docstring enc;
1208         for (char_type const c : lab) {
1209                 if (c >= 128 || c == '=' || c == '%' || c == '#' || c == '$'
1210                     || c == '}' || c == '{' || c == ']' || c == '[' || c == '&'
1211                     || c == '\\') {
1212                         // Although char_type is a 32 bit type we know that
1213                         // UCS4 occupies only 21 bits, so we don't need to
1214                         // encode bigger values. Test for 2^24 because we
1215                         // can encode that with the 6 hex digits that are
1216                         // needed for 21 bits anyway.
1217                         LASSERT(c < (1 << 24), continue);
1218                         enc += '=';
1219                         enc += hexdigit[(c>>20) & 15];
1220                         enc += hexdigit[(c>>16) & 15];
1221                         enc += hexdigit[(c>>12) & 15];
1222                         enc += hexdigit[(c>> 8) & 15];
1223                         enc += hexdigit[(c>> 4) & 15];
1224                         enc += hexdigit[ c      & 15];
1225                 } else {
1226                         enc += c;
1227                 }
1228         }
1229         return enc;
1230 }
1231
1232
1233 docstring const protectArgument(docstring & arg, char const l,
1234                           char const r)
1235 {
1236         if (contains(arg, l) || contains(arg, r))
1237                 // protect brackets
1238                 arg = '{' + arg + '}';
1239         return arg;
1240 }
1241
1242
1243 bool truncateWithEllipsis(docstring & str, size_t const len, bool const mid)
1244 {
1245         if (str.size() <= len)
1246                 return false;
1247         if (mid && len > 0) {
1248                 size_t const hlen = len / 2;
1249                 docstring suffix = str.substr(str.size() - hlen);
1250                 str.resize(hlen);
1251                 str[hlen - 1] = 0x2026;// HORIZONTAL ELLIPSIS
1252                 str += suffix;
1253         } else {
1254                 str.resize(len);
1255                 if (len > 0)
1256                         str[len - 1] = 0x2026;// HORIZONTAL ELLIPSIS
1257         }
1258         return true;
1259 }
1260
1261
1262 namespace {
1263
1264 // this doesn't check whether str is empty, so do that first.
1265 vector<docstring> wrapToVec(docstring const & str, int ind,
1266                             size_t const width)
1267 {
1268         docstring s = trim(str);
1269         if (s.empty())
1270                 return vector<docstring>();
1271
1272         docstring indent;
1273         if (ind < 0) {
1274                 indent.insert(0, -ind, ' ');
1275                 ind = 0;
1276         } else if (ind > 0)
1277                 s.insert(0, ind, ' ');
1278
1279         vector<docstring> retval;
1280         while (s.size() > width) {
1281                 // find the last space within the first 'width' chars
1282                 size_t const i = s.find_last_of(' ', width - 1);
1283                 if (i == docstring::npos || i <= size_t(ind)) {
1284                         // no space found
1285                         truncateWithEllipsis(s, width);
1286                         break;
1287                 }
1288                 retval.push_back(s.substr(0, i));
1289                 s = indent + s.substr(i);
1290                 ind = indent.size();
1291         }
1292         if (!s.empty())
1293                 retval.push_back(s);
1294         return retval;
1295 }
1296
1297 } // namespace
1298
1299
1300 docstring wrap(docstring const & str, int const ind, size_t const width)
1301 {
1302         docstring s = trim(str);
1303         if (s.empty())
1304                 return docstring();
1305
1306         vector<docstring> const svec = wrapToVec(str, ind, width);
1307         return getStringFromVector(svec, from_ascii("\n"));
1308 }
1309
1310
1311 docstring wrapParas(docstring const & str, int const indent,
1312                     size_t const width, size_t const maxlines)
1313 {
1314         if (str.empty())
1315                 return docstring();
1316
1317         vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1318         vector<docstring> retval;
1319
1320         vector<docstring>::const_iterator it = pars.begin();
1321         vector<docstring>::const_iterator const en = pars.end();
1322         for (; it != en; ++it) {
1323                 vector<docstring> tmp = wrapToVec(*it, indent, width);
1324                 size_t const nlines = tmp.size();
1325                 if (nlines == 0)
1326                         continue;
1327                 size_t const curlines = retval.size();
1328                 if (maxlines > 0 && curlines + nlines > maxlines) {
1329                         tmp.resize(maxlines - curlines);
1330                         docstring last = tmp.back();
1331                         size_t const lsize = last.size();
1332                         if (lsize > width - 1) {
1333                                 size_t const i = last.find_last_of(' ', width - 1);
1334                                 if (i == docstring::npos || i <= size_t(indent))
1335                                         // no space found
1336                                         truncateWithEllipsis(last, lsize);
1337                                 else
1338                                         truncateWithEllipsis(last, i);
1339                         } else
1340                                 last.push_back(0x2026);//HORIZONTAL ELLIPSIS
1341                         tmp.pop_back();
1342                         tmp.push_back(last);
1343                 }
1344                 retval.insert(retval.end(), tmp.begin(), tmp.end());
1345                 if (maxlines > 0 && retval.size() >= maxlines)
1346                         break;
1347         }
1348         return getStringFromVector(retval, from_ascii("\n"));
1349 }
1350
1351
1352 namespace {
1353
1354 template<typename String> vector<String> const
1355 getVectorFromStringT(String const & str, String const & delim,
1356                      bool keepempty, bool trimit)
1357 {
1358 // Lars would like this code to go, but for now his replacement (below)
1359 // doesn't fullfil the same function. I have, therefore, reactivated the
1360 // old code for now. Angus 11 Nov 2002.
1361 #if 1
1362         vector<String> vec;
1363         if (str.empty())
1364                 return vec;
1365         String keys = trimit ? rtrim(str) : str;
1366         while (true) {
1367                 size_t const idx = keys.find(delim);
1368                 if (idx == String::npos) {
1369                         vec.push_back(trimit ? ltrim(keys) : keys);
1370                         break;
1371                 }
1372                 String const key = trimit ?
1373                         trim(keys.substr(0, idx)) : keys.substr(0, idx);
1374                 if (!key.empty() || keepempty)
1375                         vec.push_back(key);
1376                 size_t const start = idx + delim.size();
1377                 keys = keys.substr(start);
1378         }
1379         return vec;
1380 #else
1381         typedef boost::char_separator<typename String::value_type> Separator;
1382         typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1383         Separator sep(delim.c_str());
1384         Tokenizer tokens(str, sep);
1385         return vector<String>(tokens.begin(), tokens.end());
1386 #endif
1387 }
1388
1389
1390 template<typename String> const String
1391         getStringFromVector(vector<String> const & vec, String const & delim)
1392 {
1393         String str;
1394         typename vector<String>::const_iterator it = vec.begin();
1395         typename vector<String>::const_iterator en = vec.end();
1396         for (; it != en; ++it) {
1397                 String item = trim(*it);
1398                 if (item.empty())
1399                         continue;
1400                 if (!str.empty())
1401                         str += delim;
1402                 str += item;
1403         }
1404         return str;
1405 }
1406
1407 } // namespace
1408
1409
1410 vector<string> const getVectorFromString(string const & str,
1411         string const & delim, bool keepempty, bool trimit)
1412 {
1413         return getVectorFromStringT<string>(str, delim, keepempty, trimit);
1414 }
1415
1416
1417 vector<docstring> const getVectorFromString(docstring const & str,
1418         docstring const & delim, bool keepempty, bool trimit)
1419 {
1420         return getVectorFromStringT<docstring>(str, delim, keepempty, trimit);
1421 }
1422
1423
1424 string const getStringFromVector(vector<string> const & vec,
1425                                  string const & delim)
1426 {
1427         return getStringFromVector<string>(vec, delim);
1428 }
1429
1430
1431 docstring const getStringFromVector(vector<docstring> const & vec,
1432                                     docstring const & delim)
1433 {
1434         return getStringFromVector<docstring>(vec, delim);
1435 }
1436
1437
1438 int findToken(char const * const str[], string const & search_token)
1439 {
1440         int i = 0;
1441
1442         while (str[i][0] && str[i] != search_token)
1443                 ++i;
1444         if (!str[i][0])
1445                 i = -1;
1446         return i;
1447 }
1448
1449
1450 std::string formatFPNumber(double x)
1451 {
1452         // Need manual tweaking, QString::number(x, 'f', 16) does not work either
1453         ostringstream os;
1454         os << std::fixed;
1455         // Prevent outputs of 23.4200000000000017 but output small numbers
1456         // with at least 6 significant digits.
1457         int const precision = (x == 0.0) ? 0 : max(6 - iround(log10(fabs(x))), 0);
1458         os << std::setprecision(precision) << x;
1459         string result = os.str();
1460         if (result.find('.') != string::npos) {
1461                 result = rtrim(result, "0");
1462                 if (result[result.length()-1] == '.')
1463                         result = rtrim(result, ".");
1464         }
1465         return result;
1466 }
1467
1468
1469 docstring to_percent_encoding(docstring const & in, docstring const & ex)
1470 {
1471         QByteArray input = to_utf8(in).c_str();
1472         QByteArray excludes = to_utf8(ex).c_str();
1473         return from_utf8(string(input.toPercentEncoding(excludes).data()));
1474 }
1475
1476
1477 string from_percent_encoding(string const & in)
1478 {
1479         return QByteArray::fromPercentEncoding(in.c_str()).data();
1480 }
1481
1482
1483 docstring bformat(docstring const & fmt, int arg1)
1484 {
1485         LATTEST(contains(fmt, from_ascii("%1$d")));
1486         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1487         return subst(str, from_ascii("%%"), from_ascii("%"));
1488 }
1489
1490
1491 docstring bformat(docstring const & fmt, long arg1)
1492 {
1493         LATTEST(contains(fmt, from_ascii("%1$d")));
1494         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1495         return subst(str, from_ascii("%%"), from_ascii("%"));
1496 }
1497
1498
1499 #ifdef HAVE_LONG_LONG_INT
1500 docstring bformat(docstring const & fmt, long long arg1)
1501 {
1502         LATTEST(contains(fmt, from_ascii("%1$d")));
1503         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1504         return subst(str, from_ascii("%%"), from_ascii("%"));
1505 }
1506 #endif
1507
1508
1509 docstring bformat(docstring const & fmt, unsigned int arg1)
1510 {
1511         LATTEST(contains(fmt, from_ascii("%1$d")));
1512         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1513         return subst(str, from_ascii("%%"), from_ascii("%"));
1514 }
1515
1516
1517 docstring bformat(docstring const & fmt, docstring const & arg1)
1518 {
1519         LATTEST(contains(fmt, from_ascii("%1$s")));
1520         docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1521         return subst(str, from_ascii("%%"), from_ascii("%"));
1522 }
1523
1524
1525 docstring bformat(docstring const & fmt, char * arg1)
1526 {
1527         LATTEST(contains(fmt, from_ascii("%1$s")));
1528         docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1529         return subst(str, from_ascii("%%"), from_ascii("%"));
1530 }
1531
1532
1533 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2)
1534 {
1535         LATTEST(contains(fmt, from_ascii("%1$s")));
1536         LATTEST(contains(fmt, from_ascii("%2$s")));
1537         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1538         str = subst(str, from_ascii("%2$s"), arg2);
1539         return subst(str, from_ascii("%%"), from_ascii("%"));
1540 }
1541
1542
1543 docstring bformat(docstring const & fmt, docstring const & arg1, int arg2)
1544 {
1545         LATTEST(contains(fmt, from_ascii("%1$s")));
1546         LATTEST(contains(fmt, from_ascii("%2$d")));
1547         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1548         str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1549         return subst(str, from_ascii("%%"), from_ascii("%"));
1550 }
1551
1552
1553 docstring bformat(docstring const & fmt, char const * arg1, docstring const & arg2)
1554 {
1555         return bformat(fmt, from_ascii(arg1), arg2);
1556 }
1557
1558
1559 docstring bformat(docstring const & fmt, int arg1, int arg2)
1560 {
1561         LATTEST(contains(fmt, from_ascii("%1$d")));
1562         LATTEST(contains(fmt, from_ascii("%2$d")));
1563         docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1564         str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1565         return subst(str, from_ascii("%%"), from_ascii("%"));
1566 }
1567
1568
1569 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2, docstring const & arg3)
1570 {
1571         LATTEST(contains(fmt, from_ascii("%1$s")));
1572         LATTEST(contains(fmt, from_ascii("%2$s")));
1573         LATTEST(contains(fmt, from_ascii("%3$s")));
1574         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1575         str = subst(str, from_ascii("%2$s"), arg2);
1576         str = subst(str, from_ascii("%3$s"), arg3);
1577         return subst(str, from_ascii("%%"), from_ascii("%"));
1578 }
1579
1580
1581 docstring bformat(docstring const & fmt,
1582                docstring const & arg1, docstring const & arg2, docstring const & arg3, docstring const & arg4)
1583 {
1584         LATTEST(contains(fmt, from_ascii("%1$s")));
1585         LATTEST(contains(fmt, from_ascii("%2$s")));
1586         LATTEST(contains(fmt, from_ascii("%3$s")));
1587         LATTEST(contains(fmt, from_ascii("%4$s")));
1588         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1589         str = subst(str, from_ascii("%2$s"), arg2);
1590         str = subst(str, from_ascii("%3$s"), arg3);
1591         str = subst(str, from_ascii("%4$s"), arg4);
1592         return subst(str, from_ascii("%%"), from_ascii("%"));
1593 }
1594
1595 docstring bformat(docstring const & fmt, docstring const & arg1,
1596                                   docstring const & arg2, docstring const & arg3,
1597                                   docstring const & arg4, docstring const & arg5)
1598 {
1599         LATTEST(contains(fmt, from_ascii("%1$s")));
1600         LATTEST(contains(fmt, from_ascii("%2$s")));
1601         LATTEST(contains(fmt, from_ascii("%3$s")));
1602         LATTEST(contains(fmt, from_ascii("%4$s")));
1603         LATTEST(contains(fmt, from_ascii("%5$s")));
1604         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1605         str = subst(str, from_ascii("%2$s"), arg2);
1606         str = subst(str, from_ascii("%3$s"), arg3);
1607         str = subst(str, from_ascii("%4$s"), arg4);
1608         str = subst(str, from_ascii("%5$s"), arg5);
1609         return subst(str, from_ascii("%%"), from_ascii("%"));
1610 }
1611
1612 } // namespace support
1613 } // namespace lyx