]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.C
Use the new InsetCommandParams interface (inset part), from Ugras and me
[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 prefixIs(docstring const & a, docstring const & pre)
333 {
334         docstring::size_type const prelen = pre.length();
335         docstring::size_type const alen = a.length();
336
337         if (prelen > alen || a.empty())
338                 return false;
339         else
340                 return a.compare(0, prelen, pre) == 0;
341 }
342
343
344 bool suffixIs(string const & a, char c)
345 {
346         if (a.empty()) return false;
347         return a[a.length() - 1] == c;
348 }
349
350
351 bool suffixIs(string const & a, string const & suf)
352 {
353         string::size_type const suflen = suf.length();
354         string::size_type const alen = a.length();
355
356         if (suflen > alen) {
357                 return false;
358         } else {
359 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
360                 string tmp(a, alen - suflen);
361                 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
362 #else
363                 return a.compare(alen - suflen, suflen, suf) == 0;
364 #endif
365         }
366 }
367
368
369 bool containsOnly(string const & s, string const & cset)
370 {
371         return s.find_first_not_of(cset) == string::npos;
372 }
373
374
375 // ale970405+lasgoutt-970425
376 // rewritten to use new string (Lgb)
377 string const token(string const & a, char delim, int n)
378 {
379         if (a.empty()) return string();
380
381         string::size_type k = 0;
382         string::size_type i = 0;
383
384         // Find delimiter or end of string
385         for (; n--;)
386                 if ((i = a.find(delim, i)) == string::npos)
387                         break;
388                 else
389                         ++i; // step delim
390         // i is now the n'th delim (or string::npos)
391         if (i == string::npos) return string();
392         k = a.find(delim, i);
393         // k is now the n'th + 1 delim (or string::npos)
394
395         return a.substr(i, k - i);
396 }
397
398
399 docstring const token(docstring const & a, char_type delim, int n)
400 {
401         if (a.empty()) return docstring();
402
403         string::size_type k = 0;
404         string::size_type i = 0;
405
406         // Find delimiter or end of string
407         for (; n--;)
408                 if ((i = a.find(delim, i)) == docstring::npos)
409                         break;
410                 else
411                         ++i; // step delim
412         // i is now the n'th delim (or string::npos)
413         if (i == docstring::npos) return docstring();
414         k = a.find(delim, i);
415         // k is now the n'th + 1 delim (or string::npos)
416
417         return a.substr(i, k - i);
418 }
419
420
421 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
422 // rewritten to use new string (Lgb)
423 int tokenPos(string const & a, char delim, string const & tok)
424 {
425         int i = 0;
426         string str(a);
427         string tmptok;
428
429         while (!str.empty()) {
430                 str = split(str, tmptok, delim);
431                 if (tok == tmptok)
432                         return i;
433                 ++i;
434         }
435         return -1;
436 }
437
438
439 namespace {
440
441 /// Substitute all \a oldchar with \a newchar
442 template<typename Ch> inline
443 std::basic_string<Ch> const subst_char(std::basic_string<Ch> const & a,
444                 Ch oldchar, Ch newchar)
445 {
446         typedef std::basic_string<Ch> String;
447         String tmp(a);
448         typename String::iterator lit = tmp.begin();
449         typename String::iterator end = tmp.end();
450         for (; lit != end; ++lit)
451                 if ((*lit) == oldchar)
452                         (*lit) = newchar;
453         return tmp;
454 }
455
456
457 /// substitutes all instances of \a oldstr with \a newstr
458 template<typename String> inline
459 String const subst_string(String const & a,
460                 String const & oldstr, String const & newstr)
461 {
462         BOOST_ASSERT(!oldstr.empty());
463         String lstr = a;
464         typename String::size_type i = 0;
465         typename String::size_type const olen = oldstr.length();
466         while ((i = lstr.find(oldstr, i)) != string::npos) {
467                 lstr.replace(i, olen, newstr);
468                 i += newstr.length(); // We need to be sure that we dont
469                 // use the same i over and over again.
470         }
471         return lstr;
472 }
473
474 }
475
476
477 string const subst(string const & a, char oldchar, char newchar)
478 {
479         return subst_char(a, oldchar, newchar);
480 }
481
482
483 docstring const subst(docstring const & a,
484                 char_type oldchar, char_type newchar)
485 {
486         return subst_char(a, oldchar, newchar);
487 }
488
489
490 string const subst(string const & a,
491                 string const & oldstr, string const & newstr)
492 {
493         return subst_string(a, oldstr, newstr);
494 }
495
496
497 docstring const subst(docstring const & a,
498                 docstring const & oldstr, docstring const & newstr)
499 {
500         return subst_string(a, oldstr, newstr);
501 }
502
503
504 docstring const trim(docstring const & a, char const * p)
505 {
506         BOOST_ASSERT(p);
507
508         if (a.empty() || !*p)
509                 return a;
510
511         docstring s = lyx::from_ascii(p);
512         docstring::size_type r = a.find_last_not_of(s);
513         docstring::size_type l = a.find_first_not_of(s);
514
515         // Is this the minimal test? (lgb)
516         if (r == docstring::npos && l == docstring::npos)
517                 return docstring();
518
519         return a.substr(l, r - l + 1);
520 }
521
522
523 string const trim(string const & a, char const * p)
524 {
525         BOOST_ASSERT(p);
526
527         if (a.empty() || !*p)
528                 return a;
529
530         string::size_type r = a.find_last_not_of(p);
531         string::size_type l = a.find_first_not_of(p);
532
533         // Is this the minimal test? (lgb)
534         if (r == string::npos && l == string::npos)
535                 return string();
536
537         return a.substr(l, r - l + 1);
538 }
539
540
541 string const rtrim(string const & a, char const * p)
542 {
543         BOOST_ASSERT(p);
544
545         if (a.empty() || !*p)
546                 return a;
547
548         string::size_type r = a.find_last_not_of(p);
549
550         // Is this test really needed? (Lgb)
551         if (r == string::npos)
552                 return string();
553
554         return a.substr(0, r + 1);
555 }
556
557
558 string const ltrim(string const & a, char const * p)
559 {
560         BOOST_ASSERT(p);
561
562         if (a.empty() || !*p)
563                 return a;
564
565         string::size_type l = a.find_first_not_of(p);
566
567         if (l == string::npos)
568                 return string();
569
570         return a.substr(l, string::npos);
571 }
572
573
574 namespace {
575
576 template<typename String, typename Char> inline
577 String const doSplit(String const & a, String & piece, Char delim)
578 {
579         String tmp;
580         typename String::size_type i = a.find(delim);
581         if (i == a.length() - 1) {
582                 piece = a.substr(0, i);
583         } else if (i != String::npos) {
584                 piece = a.substr(0, i);
585                 tmp = a.substr(i + 1);
586         } else if (i == 0) {
587                 piece.erase();
588                 tmp = a.substr(i + 1);
589         } else {
590                 piece = a;
591         }
592         return tmp;
593 }
594
595 }
596
597
598 string const split(string const & a, string & piece, char delim)
599 {
600         return doSplit(a, piece, delim);
601 }
602
603
604 docstring const split(docstring const & a, docstring & piece, char_type delim)
605 {
606         return doSplit(a, piece, delim);
607 }
608
609
610 string const split(string const & a, char delim)
611 {
612         string tmp;
613         string::size_type i = a.find(delim);
614         if (i != string::npos) // found delim
615                 tmp = a.substr(i + 1);
616         return tmp;
617 }
618
619
620 // ale970521
621 string const rsplit(string const & a, string & piece, char delim)
622 {
623         string tmp;
624         string::size_type i = a.rfind(delim);
625         if (i != string::npos) { // delimiter was found
626                 piece = a.substr(0, i);
627                 tmp = a.substr(i + 1);
628         } else { // delimiter was not found
629                 piece.erase();
630         }
631         return tmp;
632 }
633
634
635 // This function escapes 8-bit characters and other problematic
636 // characters that cause problems in latex labels.
637 docstring const escape(docstring const & lab)
638 {
639         lyx::char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
640                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
641         docstring enc;
642         for (docstring::size_type i = 0; i < lab.length(); ++i) {
643                 lyx::char_type c= lab[i];
644                 // FIXME We must change the following algorithm for UCS4
645                 // chars, but that will be a file format change.
646                 if (c >= 128 || c == '=' || c == '%') {
647                         enc += '=';
648                         enc += hexdigit[c>>4];
649                         enc += hexdigit[c & 15];
650                 } else {
651                         enc += c;
652                 }
653         }
654         return enc;
655 }
656
657
658 /// gives a vector of stringparts which have the delimiter delim
659 vector<string> const getVectorFromString(string const & str,
660                                          string const & delim)
661 {
662 // Lars would like this code to go, but for now his replacement (below)
663 // doesn't fullfil the same function. I have, therefore, reactivated the
664 // old code for now. Angus 11 Nov 2002.
665 #if 1
666         vector<string> vec;
667         if (str.empty())
668                 return vec;
669         string keys = rtrim(str);
670         for(;;) {
671                 string::size_type const idx = keys.find(delim);
672                 if (idx == string::npos) {
673                         vec.push_back(ltrim(keys));
674                         break;
675                 }
676                 string const key = trim(keys.substr(0, idx));
677                 if (!key.empty())
678                         vec.push_back(key);
679                 string::size_type const start = idx + delim.size();
680                 keys = keys.substr(start);
681         }
682         return vec;
683 #else
684         boost::char_separator<char> sep(delim.c_str());
685         boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
686         return vector<string>(tokens.begin(), tokens.end());
687 #endif
688 }
689
690
691 // the same vice versa
692 string const getStringFromVector(vector<string> const & vec,
693                                  string const & delim)
694 {
695         string str;
696         int i = 0;
697         for (vector<string>::const_iterator it = vec.begin();
698              it != vec.end(); ++it) {
699                 string item = trim(*it);
700                 if (item.empty())
701                         continue;
702                 if (i++ > 0)
703                         str += delim;
704                 str += item;
705         }
706         return str;
707 }
708
709
710 int findToken(char const * const str[], string const & search_token)
711 {
712         int i = 0;
713
714         while (str[i][0] && str[i] != search_token)
715                 ++i;
716         if (!str[i][0])
717                 i = -1;
718         return i;
719 }
720
721
722 docstring const externalLineEnding(docstring const & str)
723 {
724 #if defined(__APPLE__)
725         // The MAC clipboard uses \r for lineendings, and we use \n
726         return subst(str, '\n', '\r');
727 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
728         // Windows clipboard uses \r\n for lineendings, and we use \n
729         return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
730 #else
731         return str;
732 #endif
733 }
734
735
736 docstring const internalLineEnding(docstring const & str)
737 {
738         docstring const s = subst(str,
739                         lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
740         return subst(s, '\r', '\n');
741 }
742
743
744 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
745 #if USE_BOOST_FORMAT
746
747 template<>
748 docstring bformat(docstring const & fmt, int arg1)
749 {
750         return (boost::basic_format<char_type>(fmt) % arg1).str();
751 }
752
753
754 template<>
755 docstring bformat(docstring const & fmt, long arg1)
756 {
757         return (boost::basic_format<char_type>(fmt) % arg1).str();
758 }
759
760
761 template<>
762 docstring bformat(docstring const & fmt, unsigned int arg1)
763 {
764         return (boost::basic_format<char_type>(fmt) % arg1).str();
765 }
766
767
768 template<>
769 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
770 {
771         return (boost::basic_format<char_type>(fmt) % arg1).str();
772 }
773
774
775 template<>
776 docstring bformat(docstring const & fmt, char * arg1)
777 {
778         return (boost::basic_format<char_type>(fmt) % arg1).str();
779 }
780
781
782 template<>
783 docstring bformat(docstring const & fmt, int arg1, int 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)
791 {
792         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
793 }
794
795
796 template<>
797 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
798 {
799         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
800 }
801
802
803 template<>
804 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
805 {
806         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
807 }
808
809
810 template<>
811 docstring bformat(docstring const & fmt,
812                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
813 {
814         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
815 }
816
817 #else
818
819 template<>
820 docstring bformat(docstring const & fmt, int arg1)
821 {
822         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
823         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
824         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
825 }
826
827
828 template<>
829 docstring bformat(docstring const & fmt, long arg1)
830 {
831         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
832         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
833         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
834 }
835
836
837 template<>
838 docstring bformat(docstring const & fmt, unsigned int arg1)
839 {
840         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
841         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
842         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
843 }
844
845
846 template<>
847 docstring bformat(docstring const & fmt, docstring arg1)
848 {
849         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
850         docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
851         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
852 }
853
854
855 template<>
856 docstring bformat(docstring const & fmt, char * arg1)
857 {
858         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
859         docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
860         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
861 }
862
863
864 template<>
865 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
866 {
867         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
868         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
869         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
870         str = subst(str, lyx::from_ascii("%2$s"), arg2);
871         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
872 }
873
874
875 template<>
876 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
877 {
878         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
879         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
880         docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
881         str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
882         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
883 }
884
885
886 template<>
887 docstring bformat(docstring const & fmt, int arg1, int arg2)
888 {
889         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
890         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
891         docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
892         str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
893         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
894 }
895
896
897 template<>
898 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
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         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
904         str = subst(str, lyx::from_ascii("%2$s"), arg2);
905         str = subst(str, lyx::from_ascii("%3$s"), arg3);
906         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
907 }
908
909
910 template<>
911 docstring bformat(docstring const & fmt,
912                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
913 {
914         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
915         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
916         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
917         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
918         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
919         str = subst(str, lyx::from_ascii("%2$s"), arg2);
920         str = subst(str, lyx::from_ascii("%3$s"), arg3);
921         str = subst(str, lyx::from_ascii("%4$s"), arg4);
922         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
923 }
924
925 #endif
926 #endif
927
928 } // namespace support
929 } // namespace lyx