]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.cpp
Let paragraph::requestSpellcheck() consider contained insets
[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 wordCount(docstring const & d)
979 {
980         docstring dt = trim(d);
981         if (dt.empty())
982                 return 0;
983         int words = 1;
984         for (auto const & c : dt) {
985                 if (isSpace(c))
986                         words++;
987         }
988         return words;
989 }
990
991
992
993 int count_bin_chars(string const & str)
994 {
995         QString const qstr = toqstr(str).simplified();
996         int count = 0;
997         QString::const_iterator cit = qstr.begin();
998         QString::const_iterator end = qstr.end();
999         for (; cit != end; ++cit)  {
1000                 switch (cit->category()) {
1001                 case QChar::Separator_Line:
1002                 case QChar::Separator_Paragraph:
1003                 case QChar::Other_Control:
1004                 case QChar::Other_Format:
1005                 case QChar::Other_Surrogate:
1006                 case QChar::Other_PrivateUse:
1007                 case QChar::Other_NotAssigned:
1008                         ++count;
1009                         break;
1010                 default:
1011                         break;
1012                 }
1013         }
1014         return count;
1015 }
1016
1017
1018 docstring const trim(docstring const & a, char const * p)
1019 {
1020         LASSERT(p, return a);
1021
1022         if (a.empty() || !*p)
1023                 return a;
1024
1025         docstring s = from_ascii(p);
1026         size_t r = a.find_last_not_of(s);
1027         size_t l = a.find_first_not_of(s);
1028
1029         // Is this the minimal test? (lgb)
1030         if (r == docstring::npos && l == docstring::npos)
1031                 return docstring();
1032
1033         return a.substr(l, r - l + 1);
1034 }
1035
1036
1037 string const trim(string const & a, char const * p)
1038 {
1039         LASSERT(p, return a);
1040
1041         if (a.empty() || !*p)
1042                 return a;
1043
1044         size_t r = a.find_last_not_of(p);
1045         size_t l = a.find_first_not_of(p);
1046
1047         // Is this the minimal test? (lgb)
1048         if (r == string::npos && l == string::npos)
1049                 return string();
1050
1051         return a.substr(l, r - l + 1);
1052 }
1053
1054
1055 string const rtrim(string const & a, char const * p)
1056 {
1057         LASSERT(p, return a);
1058
1059         if (a.empty() || !*p)
1060                 return a;
1061
1062         size_t r = a.find_last_not_of(p);
1063
1064         // Is this test really needed? (Lgb)
1065         if (r == string::npos)
1066                 return string();
1067
1068         return a.substr(0, r + 1);
1069 }
1070
1071
1072 docstring const rtrim(docstring const & a, char const * p)
1073 {
1074         LASSERT(p, return a);
1075
1076         if (a.empty() || !*p)
1077                 return a;
1078
1079         size_t r = a.find_last_not_of(from_ascii(p));
1080
1081         // Is this test really needed? (Lgb)
1082         if (r == docstring::npos)
1083                 return docstring();
1084
1085         return a.substr(0, r + 1);
1086 }
1087
1088
1089 string const ltrim(string const & a, char const * p)
1090 {
1091         LASSERT(p, return a);
1092         if (a.empty() || !*p)
1093                 return a;
1094         size_t l = a.find_first_not_of(p);
1095         if (l == string::npos)
1096                 return string();
1097         return a.substr(l, string::npos);
1098 }
1099
1100
1101 docstring const ltrim(docstring const & a, char const * p)
1102 {
1103         LASSERT(p, return a);
1104         if (a.empty() || !*p)
1105                 return a;
1106         size_t l = a.find_first_not_of(from_ascii(p));
1107         if (l == docstring::npos)
1108                 return docstring();
1109         return a.substr(l, docstring::npos);
1110 }
1111
1112 namespace {
1113
1114 template<typename String, typename Char> inline
1115 String const doSplit(String const & a, String & piece, Char delim)
1116 {
1117         String tmp;
1118         size_t i = a.find(delim);
1119         if (i == a.length() - 1) {
1120                 piece = a.substr(0, i);
1121         } else if (i == 0) {
1122                 piece.erase();
1123                 tmp = a.substr(i + 1);
1124         } else if (i != String::npos) {
1125                 piece = a.substr(0, i);
1126                 tmp = a.substr(i + 1);
1127         } else {
1128                 piece = a;
1129         }
1130         return tmp;
1131 }
1132
1133
1134 // FIXME: why is this specialization needed?
1135 template<typename Char> inline
1136 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
1137 {
1138         docstring tmp;
1139         size_t i = a.find(delim);
1140         if (i == a.length() - 1) {
1141                 piece = a.substr(0, i);
1142         } else if (i == 0) {
1143                 piece.erase();
1144                 tmp = a.substr(i + 1);
1145         } else if (i != docstring::npos) {
1146                 piece = a.substr(0, i);
1147                 tmp = a.substr(i + 1);
1148         } else {
1149                 piece = a;
1150         }
1151         return tmp;
1152 }
1153
1154 } // namespace
1155
1156
1157 string const split(string const & a, string & piece, char delim)
1158 {
1159         return doSplit(a, piece, delim);
1160 }
1161
1162
1163 docstring const split(docstring const & a, docstring & piece, char_type delim)
1164 {
1165         return doSplit(a, piece, delim);
1166 }
1167
1168
1169 string const split(string const & a, char delim)
1170 {
1171         string tmp;
1172         size_t i = a.find(delim);
1173         if (i != string::npos) // found delim
1174                 tmp = a.substr(i + 1);
1175         return tmp;
1176 }
1177
1178
1179 // ale970521
1180 string const rsplit(string const & a, string & piece, char delim)
1181 {
1182         string tmp;
1183         size_t i = a.rfind(delim);
1184         if (i != string::npos) { // delimiter was found
1185                 piece = a.substr(0, i);
1186                 tmp = a.substr(i + 1);
1187         } else { // delimiter was not found
1188                 piece.erase();
1189         }
1190         return tmp;
1191 }
1192
1193
1194 docstring const rsplit(docstring const & a, docstring & piece, char_type delim)
1195 {
1196         docstring 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, char_type delim)
1209 {
1210         docstring tmp;
1211         size_t i = a.rfind(delim);
1212         if (i != string::npos)
1213                 tmp = a.substr(i + 1);
1214         return tmp;
1215 }
1216
1217
1218 docstring const escape(docstring const & lab)
1219 {
1220         char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
1221                                    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1222         docstring enc;
1223         for (char_type const c : lab) {
1224                 if (c >= 128 || c == '=' || c == '%' || c == '#' || c == '$'
1225                     || c == '}' || c == '{' || c == ']' || c == '[' || c == '&'
1226                     || c == '\\') {
1227                         // Although char_type is a 32 bit type we know that
1228                         // UCS4 occupies only 21 bits, so we don't need to
1229                         // encode bigger values. Test for 2^24 because we
1230                         // can encode that with the 6 hex digits that are
1231                         // needed for 21 bits anyway.
1232                         LASSERT(c < (1 << 24), continue);
1233                         enc += '=';
1234                         enc += hexdigit[(c>>20) & 15];
1235                         enc += hexdigit[(c>>16) & 15];
1236                         enc += hexdigit[(c>>12) & 15];
1237                         enc += hexdigit[(c>> 8) & 15];
1238                         enc += hexdigit[(c>> 4) & 15];
1239                         enc += hexdigit[ c      & 15];
1240                 } else {
1241                         enc += c;
1242                 }
1243         }
1244         return enc;
1245 }
1246
1247
1248 docstring const protectArgument(docstring & arg, char const l,
1249                           char const r)
1250 {
1251         if (contains(arg, l) || contains(arg, r))
1252                 // protect brackets
1253                 arg = '{' + arg + '}';
1254         return arg;
1255 }
1256
1257
1258 bool truncateWithEllipsis(docstring & str, size_t const len, bool const mid)
1259 {
1260         if (str.size() <= len)
1261                 return false;
1262         if (mid && len > 0) {
1263                 size_t const hlen = len / 2;
1264                 docstring suffix = str.substr(str.size() - hlen);
1265                 str.resize(hlen);
1266                 str[hlen - 1] = 0x2026;// HORIZONTAL ELLIPSIS
1267                 str += suffix;
1268         } else {
1269                 str.resize(len);
1270                 if (len > 0)
1271                         str[len - 1] = 0x2026;// HORIZONTAL ELLIPSIS
1272         }
1273         return true;
1274 }
1275
1276
1277 namespace {
1278
1279 // this doesn't check whether str is empty, so do that first.
1280 vector<docstring> wrapToVec(docstring const & str, int ind,
1281                             size_t const width)
1282 {
1283         docstring s = trim(str);
1284         if (s.empty())
1285                 return vector<docstring>();
1286
1287         docstring indent;
1288         if (ind < 0) {
1289                 indent.insert(0, -ind, ' ');
1290                 ind = 0;
1291         } else if (ind > 0)
1292                 s.insert(0, ind, ' ');
1293
1294         vector<docstring> retval;
1295         while (s.size() > width) {
1296                 // find the last space within the first 'width' chars
1297                 size_t const i = s.find_last_of(' ', width - 1);
1298                 if (i == docstring::npos || i <= size_t(ind)) {
1299                         // no space found
1300                         truncateWithEllipsis(s, width);
1301                         break;
1302                 }
1303                 retval.push_back(s.substr(0, i));
1304                 s = indent + s.substr(i);
1305                 ind = indent.size();
1306         }
1307         if (!s.empty())
1308                 retval.push_back(s);
1309         return retval;
1310 }
1311
1312 } // namespace
1313
1314
1315 docstring wrap(docstring const & str, int const ind, size_t const width)
1316 {
1317         docstring s = trim(str);
1318         if (s.empty())
1319                 return docstring();
1320
1321         vector<docstring> const svec = wrapToVec(str, ind, width);
1322         return getStringFromVector(svec, from_ascii("\n"));
1323 }
1324
1325
1326 docstring wrapParas(docstring const & str, int const indent,
1327                     size_t const width, size_t const maxlines)
1328 {
1329         if (str.empty())
1330                 return docstring();
1331
1332         vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1333         vector<docstring> retval;
1334
1335         vector<docstring>::const_iterator it = pars.begin();
1336         vector<docstring>::const_iterator const en = pars.end();
1337         for (; it != en; ++it) {
1338                 vector<docstring> tmp = wrapToVec(*it, indent, width);
1339                 size_t const nlines = tmp.size();
1340                 if (nlines == 0)
1341                         continue;
1342                 size_t const curlines = retval.size();
1343                 if (maxlines > 0 && curlines + nlines > maxlines) {
1344                         tmp.resize(maxlines - curlines);
1345                         docstring last = tmp.back();
1346                         size_t const lsize = last.size();
1347                         if (lsize > width - 1) {
1348                                 size_t const i = last.find_last_of(' ', width - 1);
1349                                 if (i == docstring::npos || i <= size_t(indent))
1350                                         // no space found
1351                                         truncateWithEllipsis(last, lsize);
1352                                 else
1353                                         truncateWithEllipsis(last, i);
1354                         } else
1355                                 last.push_back(0x2026);//HORIZONTAL ELLIPSIS
1356                         tmp.pop_back();
1357                         tmp.push_back(last);
1358                 }
1359                 retval.insert(retval.end(), tmp.begin(), tmp.end());
1360                 if (maxlines > 0 && retval.size() >= maxlines)
1361                         break;
1362         }
1363         return getStringFromVector(retval, from_ascii("\n"));
1364 }
1365
1366
1367 namespace {
1368
1369 template<typename String> vector<String> const
1370 getVectorFromStringT(String const & str, String const & delim,
1371                      bool keepempty, bool trimit)
1372 {
1373 // Lars would like this code to go, but for now his replacement (below)
1374 // doesn't fullfil the same function. I have, therefore, reactivated the
1375 // old code for now. Angus 11 Nov 2002.
1376 #if 1
1377         vector<String> vec;
1378         if (str.empty())
1379                 return vec;
1380         String keys = trimit ? rtrim(str) : str;
1381         while (true) {
1382                 size_t const idx = keys.find(delim);
1383                 if (idx == String::npos) {
1384                         vec.push_back(trimit ? ltrim(keys) : keys);
1385                         break;
1386                 }
1387                 String const key = trimit ?
1388                         trim(keys.substr(0, idx)) : keys.substr(0, idx);
1389                 if (!key.empty() || keepempty)
1390                         vec.push_back(key);
1391                 size_t const start = idx + delim.size();
1392                 keys = keys.substr(start);
1393         }
1394         return vec;
1395 #else
1396         typedef boost::char_separator<typename String::value_type> Separator;
1397         typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1398         Separator sep(delim.c_str());
1399         Tokenizer tokens(str, sep);
1400         return vector<String>(tokens.begin(), tokens.end());
1401 #endif
1402 }
1403
1404
1405 template<typename String> const String
1406         getStringFromVector(vector<String> const & vec, String const & delim)
1407 {
1408         String str;
1409         typename vector<String>::const_iterator it = vec.begin();
1410         typename vector<String>::const_iterator en = vec.end();
1411         for (; it != en; ++it) {
1412                 String item = trim(*it);
1413                 if (item.empty())
1414                         continue;
1415                 if (!str.empty())
1416                         str += delim;
1417                 str += item;
1418         }
1419         return str;
1420 }
1421
1422 } // namespace
1423
1424
1425 vector<string> const getVectorFromString(string const & str,
1426         string const & delim, bool keepempty, bool trimit)
1427 {
1428         return getVectorFromStringT<string>(str, delim, keepempty, trimit);
1429 }
1430
1431
1432 vector<docstring> const getVectorFromString(docstring const & str,
1433         docstring const & delim, bool keepempty, bool trimit)
1434 {
1435         return getVectorFromStringT<docstring>(str, delim, keepempty, trimit);
1436 }
1437
1438
1439 string const getStringFromVector(vector<string> const & vec,
1440                                  string const & delim)
1441 {
1442         return getStringFromVector<string>(vec, delim);
1443 }
1444
1445
1446 docstring const getStringFromVector(vector<docstring> const & vec,
1447                                     docstring const & delim)
1448 {
1449         return getStringFromVector<docstring>(vec, delim);
1450 }
1451
1452
1453 int findToken(char const * const str[], string const & search_token)
1454 {
1455         int i = 0;
1456
1457         while (str[i][0] && str[i] != search_token)
1458                 ++i;
1459         if (!str[i][0])
1460                 i = -1;
1461         return i;
1462 }
1463
1464
1465 std::string formatFPNumber(double x)
1466 {
1467         // Need manual tweaking, QString::number(x, 'f', 16) does not work either
1468         ostringstream os;
1469         os << std::fixed;
1470         // Prevent outputs of 23.4200000000000017 but output small numbers
1471         // with at least 6 significant digits.
1472         int const precision = (x == 0.0) ? 0 : max(6 - iround(log10(fabs(x))), 0);
1473         os << std::setprecision(precision) << x;
1474         string result = os.str();
1475         if (result.find('.') != string::npos) {
1476                 result = rtrim(result, "0");
1477                 if (result[result.length()-1] == '.')
1478                         result = rtrim(result, ".");
1479         }
1480         return result;
1481 }
1482
1483
1484 docstring to_percent_encoding(docstring const & in, docstring const & ex)
1485 {
1486         QByteArray input = to_utf8(in).c_str();
1487         QByteArray excludes = to_utf8(ex).c_str();
1488         return from_utf8(string(input.toPercentEncoding(excludes).data()));
1489 }
1490
1491
1492 string from_percent_encoding(string const & in)
1493 {
1494         return QByteArray::fromPercentEncoding(in.c_str()).data();
1495 }
1496
1497
1498 docstring bformat(docstring const & fmt, int arg1)
1499 {
1500         LATTEST(contains(fmt, from_ascii("%1$d")));
1501         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1502         return subst(str, from_ascii("%%"), from_ascii("%"));
1503 }
1504
1505
1506 docstring bformat(docstring const & fmt, long arg1)
1507 {
1508         LATTEST(contains(fmt, from_ascii("%1$d")));
1509         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1510         return subst(str, from_ascii("%%"), from_ascii("%"));
1511 }
1512
1513
1514 #ifdef HAVE_LONG_LONG_INT
1515 docstring bformat(docstring const & fmt, long long arg1)
1516 {
1517         LATTEST(contains(fmt, from_ascii("%1$d")));
1518         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1519         return subst(str, from_ascii("%%"), from_ascii("%"));
1520 }
1521 #endif
1522
1523
1524 docstring bformat(docstring const & fmt, unsigned int arg1)
1525 {
1526         LATTEST(contains(fmt, from_ascii("%1$d")));
1527         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1528         return subst(str, from_ascii("%%"), from_ascii("%"));
1529 }
1530
1531
1532 docstring bformat(docstring const & fmt, docstring const & arg1)
1533 {
1534         LATTEST(contains(fmt, from_ascii("%1$s")));
1535         docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1536         return subst(str, from_ascii("%%"), from_ascii("%"));
1537 }
1538
1539
1540 docstring bformat(docstring const & fmt, char * arg1)
1541 {
1542         LATTEST(contains(fmt, from_ascii("%1$s")));
1543         docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1544         return subst(str, from_ascii("%%"), from_ascii("%"));
1545 }
1546
1547
1548 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2)
1549 {
1550         LATTEST(contains(fmt, from_ascii("%1$s")));
1551         LATTEST(contains(fmt, from_ascii("%2$s")));
1552         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1553         str = subst(str, from_ascii("%2$s"), arg2);
1554         return subst(str, from_ascii("%%"), from_ascii("%"));
1555 }
1556
1557
1558 docstring bformat(docstring const & fmt, docstring const & arg1, int arg2)
1559 {
1560         LATTEST(contains(fmt, from_ascii("%1$s")));
1561         LATTEST(contains(fmt, from_ascii("%2$d")));
1562         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1563         str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1564         return subst(str, from_ascii("%%"), from_ascii("%"));
1565 }
1566
1567
1568 docstring bformat(docstring const & fmt, char const * arg1, docstring const & arg2)
1569 {
1570         return bformat(fmt, from_ascii(arg1), arg2);
1571 }
1572
1573
1574 docstring bformat(docstring const & fmt, int arg1, int arg2)
1575 {
1576         LATTEST(contains(fmt, from_ascii("%1$d")));
1577         LATTEST(contains(fmt, from_ascii("%2$d")));
1578         docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1579         str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1580         return subst(str, from_ascii("%%"), from_ascii("%"));
1581 }
1582
1583
1584 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2, docstring const & arg3)
1585 {
1586         LATTEST(contains(fmt, from_ascii("%1$s")));
1587         LATTEST(contains(fmt, from_ascii("%2$s")));
1588         LATTEST(contains(fmt, from_ascii("%3$s")));
1589         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1590         str = subst(str, from_ascii("%2$s"), arg2);
1591         str = subst(str, from_ascii("%3$s"), arg3);
1592         return subst(str, from_ascii("%%"), from_ascii("%"));
1593 }
1594
1595
1596 docstring bformat(docstring const & fmt,
1597                docstring const & arg1, docstring const & arg2, docstring const & arg3, docstring const & arg4)
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         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1604         str = subst(str, from_ascii("%2$s"), arg2);
1605         str = subst(str, from_ascii("%3$s"), arg3);
1606         str = subst(str, from_ascii("%4$s"), arg4);
1607         return subst(str, from_ascii("%%"), from_ascii("%"));
1608 }
1609
1610 docstring bformat(docstring const & fmt, docstring const & arg1,
1611                                   docstring const & arg2, docstring const & arg3,
1612                                   docstring const & arg4, docstring const & arg5)
1613 {
1614         LATTEST(contains(fmt, from_ascii("%1$s")));
1615         LATTEST(contains(fmt, from_ascii("%2$s")));
1616         LATTEST(contains(fmt, from_ascii("%3$s")));
1617         LATTEST(contains(fmt, from_ascii("%4$s")));
1618         LATTEST(contains(fmt, from_ascii("%5$s")));
1619         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1620         str = subst(str, from_ascii("%2$s"), arg2);
1621         str = subst(str, from_ascii("%3$s"), arg3);
1622         str = subst(str, from_ascii("%4$s"), arg4);
1623         str = subst(str, from_ascii("%5$s"), arg5);
1624         return subst(str, from_ascii("%%"), from_ascii("%"));
1625 }
1626
1627 } // namespace support
1628 } // namespace lyx