]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.C
Use UTF8 for LaTeX export.
[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 docstring const escape(docstring const & lab)
626 {
627         lyx::char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
628                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
629         docstring enc;
630         for (docstring::size_type i = 0; i < lab.length(); ++i) {
631                 lyx::char_type c= lab[i];
632                 // FIXME We must change the following algorithm for UCS4
633                 // chars, but that will be a file format change.
634                 if (c >= 128 || c == '=' || c == '%') {
635                         enc += '=';
636                         enc += hexdigit[c>>4];
637                         enc += hexdigit[c & 15];
638                 } else {
639                         enc += c;
640                 }
641         }
642         return enc;
643 }
644
645
646 /// gives a vector of stringparts which have the delimiter delim
647 vector<string> const getVectorFromString(string const & str,
648                                          string const & delim)
649 {
650 // Lars would like this code to go, but for now his replacement (below)
651 // doesn't fullfil the same function. I have, therefore, reactivated the
652 // old code for now. Angus 11 Nov 2002.
653 #if 1
654         vector<string> vec;
655         if (str.empty())
656                 return vec;
657         string keys = rtrim(str);
658         for(;;) {
659                 string::size_type const idx = keys.find(delim);
660                 if (idx == string::npos) {
661                         vec.push_back(ltrim(keys));
662                         break;
663                 }
664                 string const key = trim(keys.substr(0, idx));
665                 if (!key.empty())
666                         vec.push_back(key);
667                 string::size_type const start = idx + delim.size();
668                 keys = keys.substr(start);
669         }
670         return vec;
671 #else
672         boost::char_separator<char> sep(delim.c_str());
673         boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
674         return vector<string>(tokens.begin(), tokens.end());
675 #endif
676 }
677
678
679 // the same vice versa
680 string const getStringFromVector(vector<string> const & vec,
681                                  string const & delim)
682 {
683         string str;
684         int i = 0;
685         for (vector<string>::const_iterator it = vec.begin();
686              it != vec.end(); ++it) {
687                 string item = trim(*it);
688                 if (item.empty())
689                         continue;
690                 if (i++ > 0)
691                         str += delim;
692                 str += item;
693         }
694         return str;
695 }
696
697
698 int findToken(char const * const str[], string const & search_token)
699 {
700         int i = 0;
701
702         while (str[i][0] && str[i] != search_token)
703                 ++i;
704         if (!str[i][0])
705                 i = -1;
706         return i;
707 }
708
709
710 docstring const externalLineEnding(docstring const & str)
711 {
712 #if defined(__APPLE__)
713         // The MAC clipboard uses \r for lineendings, and we use \n
714         return subst(str, '\n', '\r');
715 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
716         // Windows clipboard uses \r\n for lineendings, and we use \n
717         return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
718 #else
719         return str;
720 #endif
721 }
722
723
724 docstring const internalLineEnding(docstring const & str)
725 {
726         docstring const s = subst(str,
727                         lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
728         return subst(s, '\r', '\n');
729 }
730
731
732 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
733 #if USE_BOOST_FORMAT
734
735 template<>
736 docstring bformat(docstring const & fmt, int arg1)
737 {
738         return (boost::basic_format<char_type>(fmt) % arg1).str();
739 }
740
741
742 template<>
743 docstring bformat(docstring const & fmt, long arg1)
744 {
745         return (boost::basic_format<char_type>(fmt) % arg1).str();
746 }
747
748
749 template<>
750 docstring bformat(docstring const & fmt, unsigned int arg1)
751 {
752         return (boost::basic_format<char_type>(fmt) % arg1).str();
753 }
754
755
756 template<>
757 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
758 {
759         return (boost::basic_format<char_type>(fmt) % arg1).str();
760 }
761
762
763 template<>
764 docstring bformat(docstring const & fmt, char * arg1)
765 {
766         return (boost::basic_format<char_type>(fmt) % arg1).str();
767 }
768
769
770 template<>
771 docstring bformat(docstring const & fmt, int arg1, int arg2)
772 {
773         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
774 }
775
776
777 template<>
778 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
779 {
780         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
781 }
782
783
784 template<>
785 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
786 {
787         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
788 }
789
790
791 template<>
792 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
793 {
794         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
795 }
796
797
798 template<>
799 docstring bformat(docstring const & fmt,
800                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
801 {
802         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
803 }
804
805 #else
806
807 template<>
808 docstring bformat(docstring const & fmt, int arg1)
809 {
810         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
811         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
812         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
813 }
814
815
816 template<>
817 docstring bformat(docstring const & fmt, long arg1)
818 {
819         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
820         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
821         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
822 }
823
824
825 template<>
826 docstring bformat(docstring const & fmt, unsigned int arg1)
827 {
828         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
829         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
830         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
831 }
832
833
834 template<>
835 docstring bformat(docstring const & fmt, docstring arg1)
836 {
837         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
838         docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
839         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
840 }
841
842
843 template<>
844 docstring bformat(docstring const & fmt, char * arg1)
845 {
846         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
847         docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
848         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
849 }
850
851
852 template<>
853 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
854 {
855         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
856         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
857         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
858         str = subst(str, lyx::from_ascii("%2$s"), arg2);
859         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
860 }
861
862
863 template<>
864 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
865 {
866         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
867         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
868         docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
869         str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
870         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
871 }
872
873
874 template<>
875 docstring bformat(docstring const & fmt, int arg1, int arg2)
876 {
877         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
878         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
879         docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
880         str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
881         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
882 }
883
884
885 template<>
886 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
887 {
888         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
889         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
890         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
891         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
892         str = subst(str, lyx::from_ascii("%2$s"), arg2);
893         str = subst(str, lyx::from_ascii("%3$s"), arg3);
894         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
895 }
896
897
898 template<>
899 docstring bformat(docstring const & fmt,
900                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
901 {
902         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
903         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
904         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
905         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
906         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
907         str = subst(str, lyx::from_ascii("%2$s"), arg2);
908         str = subst(str, lyx::from_ascii("%3$s"), arg3);
909         str = subst(str, lyx::from_ascii("%4$s"), arg4);
910         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
911 }
912
913 #endif
914 #endif
915
916 } // namespace support
917 } // namespace lyx