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