]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.C
MacOSX compile fix.
[lyx.git] / src / support / lstrings.C
1 /**
2  * \file lstrings.C
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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "support/lstrings.h"
15 #include "support/lyxlib.h"
16 #include "support/convert.h"
17
18 #include "debug.h"
19
20 #include <boost/tokenizer.hpp>
21 #include <boost/assert.hpp>
22
23 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
24 #if USE_BOOST_FORMAT
25 #include <boost/format.hpp>
26 #endif
27 #endif
28
29 #include <cctype>
30 #include <cstdlib>
31
32 #include <algorithm>
33 #include <sstream>
34
35 using lyx::docstring;
36
37 using std::transform;
38 using std::string;
39 using std::vector;
40
41 #ifndef CXX_GLOBAL_CSTD
42 using std::isdigit;
43 using std::tolower;
44 using std::toupper;
45 #endif
46
47
48 namespace lyx {
49 namespace support {
50
51 int compare_no_case(string const & s, string const & s2)
52 {
53         string::const_iterator p = s.begin();
54         string::const_iterator p2 = s2.begin();
55
56         while (p != s.end() && p2 != s2.end()) {
57                 int const lc1 = tolower(*p);
58                 int const lc2 = tolower(*p2);
59                 if (lc1 != lc2)
60                         return (lc1 < lc2) ? -1 : 1;
61                 ++p;
62                 ++p2;
63         }
64
65         if (s.size() == s2.size())
66                 return 0;
67         if (s.size() < s2.size())
68                 return -1;
69         return 1;
70 }
71
72
73 int compare_no_case(docstring const & s, docstring const & s2)
74 {
75         docstring::const_iterator p = s.begin();
76         docstring::const_iterator p2 = s2.begin();
77
78         while (p != s.end() && p2 != s2.end()) {
79                 int const lc1 = tolower(*p);
80                 int const lc2 = tolower(*p2);
81                 if (lc1 != lc2)
82                         return (lc1 < lc2) ? -1 : 1;
83                 ++p;
84                 ++p2;
85         }
86
87         if (s.size() == s2.size())
88                 return 0;
89         if (s.size() < s2.size())
90                 return -1;
91         return 1;
92 }
93
94
95 namespace {
96
97 int ascii_tolower(int c) {
98         if (c >= 'A' && c <= 'Z')
99                 return c - 'A' + 'a';
100         return c;
101 }
102
103
104 template<typename String> inline
105 int do_compare_ascii_no_case(String const & s, String const & s2)
106 {
107         typename String::const_iterator p = s.begin();
108         typename String::const_iterator p2 = s2.begin();
109
110         while (p != s.end() && p2 != s2.end()) {
111                 int const lc1 = ascii_tolower(*p);
112                 int const lc2 = ascii_tolower(*p2);
113                 if (lc1 != lc2)
114                         return (lc1 < lc2) ? -1 : 1;
115                 ++p;
116                 ++p2;
117         }
118
119         if (s.size() == s2.size())
120                 return 0;
121         if (s.size() < s2.size())
122                 return -1;
123         return 1;
124 }
125
126 }
127
128
129 int compare_ascii_no_case(string const & s, string const & s2)
130 {
131         return do_compare_ascii_no_case(s, s2);
132 }
133
134
135 int compare_ascii_no_case(docstring const & s, docstring const & s2)
136 {
137         return do_compare_ascii_no_case(s, s2);
138 }
139
140
141 int compare_no_case(string const & s, string const & s2, unsigned int len)
142 {
143         string::const_iterator p = s.begin();
144         string::const_iterator p2 = s2.begin();
145         unsigned int i = 0;
146         while (i < len && p != s.end() && p2 != s2.end()) {
147                 int const lc1 = tolower(*p);
148                 int const lc2 = tolower(*p2);
149                 if (lc1 != lc2)
150                         return (lc1 < lc2) ? -1 : 1;
151                 ++i;
152                 ++p;
153                 ++p2;
154         }
155
156         if (s.size() >= len && s2.size() >= len)
157                 return 0;
158         if (s.size() < s2.size())
159                 return -1;
160         return 1;
161 }
162
163
164 bool isStrInt(string const & str)
165 {
166         if (str.empty()) return false;
167
168         // Remove leading and trailing white space chars.
169         string const tmpstr = trim(str);
170         if (tmpstr.empty()) return false;
171
172         string::const_iterator cit = tmpstr.begin();
173         if ((*cit) == '-') ++cit;
174         string::const_iterator end = tmpstr.end();
175         for (; cit != end; ++cit) {
176                 if (!isdigit((*cit))) return false;
177         }
178         return true;
179 }
180
181
182 bool isStrUnsignedInt(string const & str)
183 {
184         if (str.empty()) return false;
185
186         // Remove leading and trailing white space chars.
187         string const tmpstr = trim(str);
188         if (tmpstr.empty()) return false;
189
190         string::const_iterator cit = tmpstr.begin();
191         string::const_iterator end = tmpstr.end();
192         for (; cit != end; ++cit) {
193                 if (!isdigit((*cit))) return false;
194         }
195         return true;
196 }
197
198
199 bool isStrDbl(string const & str)
200 {
201         if (str.empty()) return false;
202
203         // Remove leading and trailing white space chars.
204         string const tmpstr = trim(str);
205         if (tmpstr.empty()) return false;
206         //      if (1 < tmpstr.count('.')) return false;
207
208         string::const_iterator cit = tmpstr.begin();
209         bool found_dot(false);
210         if ((*cit) == '-') ++cit;
211         string::const_iterator end = tmpstr.end();
212         for (; cit != end; ++cit) {
213                 if (!isdigit((*cit))
214                     && '.' != (*cit)) {
215                         return false;
216                 }
217                 if ('.' == (*cit)) {
218                         if (found_dot) {
219                                 return false;
220                         } else {
221                                 found_dot = true;
222                         }
223                 }
224         }
225         return true;
226 }
227
228
229 char lowercase(char c)
230 {
231         return char(tolower(c));
232 }
233
234
235 char uppercase(char c)
236 {
237         return char(toupper(c));
238 }
239
240 // FIXME for lowercase() and uppercase() function below:
241 // 1) std::tolower() and std::toupper() are templates that
242 // compile fine with char_type. With the test (c >= 256) we
243 // do not trust these function to do the right thing with
244 // unicode char.
245 // 2) these functions use the current locale, which is wrong
246 // if it is not latin1 based (latin1 is a subset of UCS4).
247
248 char_type lowercase(char_type c)
249 {
250         if (c >= 256)
251                 return c;
252
253         return tolower(c);
254 }
255
256
257 char_type uppercase(char_type c)
258 {
259         if (c >= 256)
260                 return c;
261
262         return toupper(c);
263 }
264
265
266 namespace {
267
268 // since we cannot use std::tolower and std::toupper directly in the
269 // calls to std::transform yet, we use these helper clases. (Lgb)
270
271 struct local_lowercase {
272         char operator()(char c) const {
273                 return tolower(c);
274         }
275 };
276
277 struct local_uppercase {
278         char operator()(char c) const {
279                 return toupper(c);
280         }
281 };
282
283 struct local_ascii_lowercase {
284         char operator()(char c) const {
285                 return ascii_tolower(c);
286         }
287 };
288
289 } // end of anon namespace
290
291 string const lowercase(string const & a)
292 {
293         string tmp(a);
294         transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
295         return tmp;
296 }
297
298 string const uppercase(string const & a)
299 {
300         string tmp(a);
301         transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
302         return tmp;
303 }
304
305
306 string const ascii_lowercase(string const & a)
307 {
308         string tmp(a);
309         transform(tmp.begin(), tmp.end(), tmp.begin(),
310                   local_ascii_lowercase());
311         return tmp;
312 }
313
314
315 bool prefixIs(string const & a, string const & pre)
316 {
317         string::size_type const prelen = pre.length();
318         string::size_type const alen = a.length();
319
320         if (prelen > alen || a.empty())
321                 return false;
322         else {
323 #if defined(STD_STRING_IS_GOOD)
324                 return a.compare(0, prelen, pre) == 0;
325 #else
326                 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
327 #endif
328         }
329 }
330
331
332 bool suffixIs(string const & a, char c)
333 {
334         if (a.empty()) return false;
335         return a[a.length() - 1] == c;
336 }
337
338
339 bool suffixIs(string const & a, string const & suf)
340 {
341         string::size_type const suflen = suf.length();
342         string::size_type const alen = a.length();
343
344         if (suflen > alen) {
345                 return false;
346         } else {
347 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
348                 string tmp(a, alen - suflen);
349                 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
350 #else
351                 return a.compare(alen - suflen, suflen, suf) == 0;
352 #endif
353         }
354 }
355
356
357 bool containsOnly(string const & s, string const & cset)
358 {
359         return s.find_first_not_of(cset) == string::npos;
360 }
361
362
363 // ale970405+lasgoutt-970425
364 // rewritten to use new string (Lgb)
365 string const token(string const & a, char delim, int n)
366 {
367         if (a.empty()) return string();
368
369         string::size_type k = 0;
370         string::size_type i = 0;
371
372         // Find delimiter or end of string
373         for (; n--;)
374                 if ((i = a.find(delim, i)) == string::npos)
375                         break;
376                 else
377                         ++i; // step delim
378         // i is now the n'th delim (or string::npos)
379         if (i == string::npos) return string();
380         k = a.find(delim, i);
381         // k is now the n'th + 1 delim (or string::npos)
382
383         return a.substr(i, k - i);
384 }
385
386
387 docstring const token(docstring const & a, char_type delim, int n)
388 {
389         if (a.empty()) return docstring();
390
391         string::size_type k = 0;
392         string::size_type i = 0;
393
394         // Find delimiter or end of string
395         for (; n--;)
396                 if ((i = a.find(delim, i)) == docstring::npos)
397                         break;
398                 else
399                         ++i; // step delim
400         // i is now the n'th delim (or string::npos)
401         if (i == docstring::npos) return docstring();
402         k = a.find(delim, i);
403         // k is now the n'th + 1 delim (or string::npos)
404
405         return a.substr(i, k - i);
406 }
407
408
409 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
410 // rewritten to use new string (Lgb)
411 int tokenPos(string const & a, char delim, string const & tok)
412 {
413         int i = 0;
414         string str(a);
415         string tmptok;
416
417         while (!str.empty()) {
418                 str = split(str, tmptok, delim);
419                 if (tok == tmptok)
420                         return i;
421                 ++i;
422         }
423         return -1;
424 }
425
426
427 namespace {
428
429 /// Substitute all \a oldchar with \a newchar
430 template<typename Ch> inline
431 std::basic_string<Ch> const subst_char(std::basic_string<Ch> const & a,
432                 Ch oldchar, Ch newchar)
433 {
434         typedef std::basic_string<Ch> String;
435         String tmp(a);
436         typename String::iterator lit = tmp.begin();
437         typename String::iterator end = tmp.end();
438         for (; lit != end; ++lit)
439                 if ((*lit) == oldchar)
440                         (*lit) = newchar;
441         return tmp;
442 }
443
444
445 /// substitutes all instances of \a oldstr with \a newstr
446 template<typename String> inline
447 String const subst_string(String const & a,
448                 String const & oldstr, String const & newstr)
449 {
450         BOOST_ASSERT(!oldstr.empty());
451         String lstr = a;
452         typename String::size_type i = 0;
453         typename String::size_type const olen = oldstr.length();
454         while ((i = lstr.find(oldstr, i)) != string::npos) {
455                 lstr.replace(i, olen, newstr);
456                 i += newstr.length(); // We need to be sure that we dont
457                 // use the same i over and over again.
458         }
459         return lstr;
460 }
461
462 }
463
464
465 string const subst(string const & a, char oldchar, char newchar)
466 {
467         return subst_char(a, oldchar, newchar);
468 }
469
470
471 docstring const subst(docstring const & a,
472                 char_type oldchar, char_type newchar)
473 {
474         return subst_char(a, oldchar, newchar);
475 }
476
477
478 string const subst(string const & a,
479                 string const & oldstr, string const & newstr)
480 {
481         return subst_string(a, oldstr, newstr);
482 }
483
484
485 docstring const subst(docstring const & a,
486                 docstring const & oldstr, docstring const & newstr)
487 {
488         return subst_string(a, oldstr, newstr);
489 }
490
491
492 docstring const trim(docstring const & a, char const * p)
493 {
494         BOOST_ASSERT(p);
495
496         if (a.empty() || !*p)
497                 return a;
498
499         docstring s = lyx::from_ascii(p);
500         docstring::size_type r = a.find_last_not_of(s);
501         docstring::size_type l = a.find_first_not_of(s);
502
503         // Is this the minimal test? (lgb)
504         if (r == docstring::npos && l == docstring::npos)
505                 return docstring();
506
507         return a.substr(l, r - l + 1);
508 }
509
510
511 string const trim(string const & a, char const * p)
512 {
513         BOOST_ASSERT(p);
514
515         if (a.empty() || !*p)
516                 return a;
517
518         string::size_type r = a.find_last_not_of(p);
519         string::size_type l = a.find_first_not_of(p);
520
521         // Is this the minimal test? (lgb)
522         if (r == string::npos && l == string::npos)
523                 return string();
524
525         return a.substr(l, r - l + 1);
526 }
527
528
529 string const rtrim(string const & a, char const * p)
530 {
531         BOOST_ASSERT(p);
532
533         if (a.empty() || !*p)
534                 return a;
535
536         string::size_type r = a.find_last_not_of(p);
537
538         // Is this test really needed? (Lgb)
539         if (r == string::npos)
540                 return string();
541
542         return a.substr(0, r + 1);
543 }
544
545
546 string const ltrim(string const & a, char const * p)
547 {
548         BOOST_ASSERT(p);
549
550         if (a.empty() || !*p)
551                 return a;
552
553         string::size_type l = a.find_first_not_of(p);
554
555         if (l == string::npos)
556                 return string();
557
558         return a.substr(l, string::npos);
559 }
560
561
562 namespace {
563
564 template<typename String, typename Char> inline
565 String const doSplit(String const & a, String & piece, Char delim)
566 {
567         String tmp;
568         typename String::size_type i = a.find(delim);
569         if (i == a.length() - 1) {
570                 piece = a.substr(0, i);
571         } else if (i != String::npos) {
572                 piece = a.substr(0, i);
573                 tmp = a.substr(i + 1);
574         } else if (i == 0) {
575                 piece.erase();
576                 tmp = a.substr(i + 1);
577         } else {
578                 piece = a;
579         }
580         return tmp;
581 }
582
583 }
584
585
586 string const split(string const & a, string & piece, char delim)
587 {
588         return doSplit(a, piece, delim);
589 }
590
591
592 docstring const split(docstring const & a, docstring & piece, char_type delim)
593 {
594         return doSplit(a, piece, delim);
595 }
596
597
598 string const split(string const & a, char delim)
599 {
600         string tmp;
601         string::size_type i = a.find(delim);
602         if (i != string::npos) // found delim
603                 tmp = a.substr(i + 1);
604         return tmp;
605 }
606
607
608 // ale970521
609 string const rsplit(string const & a, string & piece, char delim)
610 {
611         string tmp;
612         string::size_type i = a.rfind(delim);
613         if (i != string::npos) { // delimiter was found
614                 piece = a.substr(0, i);
615                 tmp = a.substr(i + 1);
616         } else { // delimiter was not found
617                 piece.erase();
618         }
619         return tmp;
620 }
621
622
623 // This function escapes 8-bit characters and other problematic
624 // characters that cause problems in latex labels.
625 string const escape(string const & lab)
626 {
627         char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
628                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
629         string enc;
630         for (string::size_type i = 0; i < lab.length(); ++i) {
631                 unsigned char c= lab[i];
632                 if (c >= 128 || c == '=' || c == '%') {
633                         enc += '=';
634                         enc += hexdigit[c>>4];
635                         enc += hexdigit[c & 15];
636                 } else {
637                         enc += c;
638                 }
639         }
640         return enc;
641 }
642
643
644 /// gives a vector of stringparts which have the delimiter delim
645 vector<string> const getVectorFromString(string const & str,
646                                          string const & delim)
647 {
648 // Lars would like this code to go, but for now his replacement (below)
649 // doesn't fullfil the same function. I have, therefore, reactivated the
650 // old code for now. Angus 11 Nov 2002.
651 #if 1
652         vector<string> vec;
653         if (str.empty())
654                 return vec;
655         string keys = rtrim(str);
656         for(;;) {
657                 string::size_type const idx = keys.find(delim);
658                 if (idx == string::npos) {
659                         vec.push_back(ltrim(keys));
660                         break;
661                 }
662                 string const key = trim(keys.substr(0, idx));
663                 if (!key.empty())
664                         vec.push_back(key);
665                 string::size_type const start = idx + delim.size();
666                 keys = keys.substr(start);
667         }
668         return vec;
669 #else
670         boost::char_separator<char> sep(delim.c_str());
671         boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
672         return vector<string>(tokens.begin(), tokens.end());
673 #endif
674 }
675
676
677 // the same vice versa
678 string const getStringFromVector(vector<string> const & vec,
679                                  string const & delim)
680 {
681         string str;
682         int i = 0;
683         for (vector<string>::const_iterator it = vec.begin();
684              it != vec.end(); ++it) {
685                 string item = trim(*it);
686                 if (item.empty())
687                         continue;
688                 if (i++ > 0)
689                         str += delim;
690                 str += item;
691         }
692         return str;
693 }
694
695
696 int findToken(char const * const str[], string const & search_token)
697 {
698         int i = 0;
699
700         while (str[i][0] && str[i] != search_token)
701                 ++i;
702         if (!str[i][0])
703                 i = -1;
704         return i;
705 }
706
707
708 docstring const externalLineEnding(docstring const & str)
709 {
710 #if defined(__APPLE__)
711         // The MAC clipboard uses \r for lineendings, and we use \n
712         return subst(str, '\n', '\r');
713 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
714         // Windows clipboard uses \r\n for lineendings, and we use \n
715         return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
716 #else
717         return str;
718 #endif
719 }
720
721
722 docstring const internalLineEnding(docstring const & str)
723 {
724         docstring const s = subst(str,
725                         lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
726         return subst(s, '\r', '\n');
727 }
728
729
730 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
731 #if USE_BOOST_FORMAT
732
733 template<>
734 docstring bformat(docstring const & fmt, int arg1)
735 {
736         return (boost::basic_format<char_type>(fmt) % arg1).str();
737 }
738
739
740 template<>
741 docstring bformat(docstring const & fmt, long arg1)
742 {
743         return (boost::basic_format<char_type>(fmt) % arg1).str();
744 }
745
746
747 template<>
748 docstring bformat(docstring const & fmt, unsigned int arg1)
749 {
750         return (boost::basic_format<char_type>(fmt) % arg1).str();
751 }
752
753
754 template<>
755 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
756 {
757         return (boost::basic_format<char_type>(fmt) % arg1).str();
758 }
759
760
761 template<>
762 docstring bformat(docstring const & fmt, char * arg1)
763 {
764         return (boost::basic_format<char_type>(fmt) % arg1).str();
765 }
766
767
768 template<>
769 docstring bformat(docstring const & fmt, int arg1, int arg2)
770 {
771         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
772 }
773
774
775 template<>
776 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
777 {
778         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
779 }
780
781
782 template<>
783 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
784 {
785         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
786 }
787
788
789 template<>
790 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
791 {
792         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
793 }
794
795
796 template<>
797 docstring bformat(docstring const & fmt,
798                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
799 {
800         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
801 }
802
803 #else
804
805 template<>
806 docstring bformat(docstring const & fmt, int arg1)
807 {
808         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
809         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
810         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
811 }
812
813
814 template<>
815 docstring bformat(docstring const & fmt, long arg1)
816 {
817         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
818         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
819         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
820 }
821
822
823 template<>
824 docstring bformat(docstring const & fmt, unsigned int arg1)
825 {
826         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
827         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
828         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
829 }
830
831
832 template<>
833 docstring bformat(docstring const & fmt, docstring arg1)
834 {
835         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
836         docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
837         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
838 }
839
840
841 template<>
842 docstring bformat(docstring const & fmt, char * arg1)
843 {
844         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
845         docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
846         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
847 }
848
849
850 template<>
851 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
852 {
853         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
854         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
855         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
856         str = subst(str, lyx::from_ascii("%2$s"), arg2);
857         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
858 }
859
860
861 template<>
862 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
863 {
864         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
865         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
866         docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
867         str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
868         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
869 }
870
871
872 template<>
873 docstring bformat(docstring const & fmt, int arg1, int arg2)
874 {
875         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
876         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
877         docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
878         str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
879         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
880 }
881
882
883 template<>
884 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
885 {
886         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
887         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
888         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
889         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
890         str = subst(str, lyx::from_ascii("%2$s"), arg2);
891         str = subst(str, lyx::from_ascii("%3$s"), arg3);
892         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
893 }
894
895
896 template<>
897 docstring bformat(docstring const & fmt,
898                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
899 {
900         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
901         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
902         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
903         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
904         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
905         str = subst(str, lyx::from_ascii("%2$s"), arg2);
906         str = subst(str, lyx::from_ascii("%3$s"), arg3);
907         str = subst(str, lyx::from_ascii("%4$s"), arg4);
908         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
909 }
910
911 #endif
912 #endif
913
914 } // namespace support
915 } // namespace lyx