]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.C
Make it possible to uses non-ascii labelstring, endlabelstring and
[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 namespace {
230
231 inline
232 bool isHexChar(char_type c)
233 {
234         return c == '0' ||
235                 c == '1' ||
236                 c == '2' ||
237                 c == '3' ||
238                 c == '4' ||
239                 c == '5' ||
240                 c == '6' ||
241                 c == '7' ||
242                 c == '8' ||
243                 c == '9' ||
244                 c == 'a' || c == 'A' ||
245                 c == 'b' || c == 'B' ||
246                 c == 'c' || c == 'C' ||
247                 c == 'd' || c == 'D' ||
248                 c == 'e' || c == 'E' ||
249                 c == 'f' || c == 'F';
250 }
251
252 } // anon namespace
253
254
255 bool isHex(docstring const & str)
256 {
257         int index = 0;
258
259         if (str.length() > 2 && str[0] == '0' &&
260             (str[1] == 'x' || str[1] == 'X'))
261                 index = 2;
262
263         int const len = str.length();
264
265         for (; index < len; ++index) {
266                 if (!isHexChar(str[index]))
267                         return false;
268         }
269         return true;
270 }
271
272
273 int hexToInt(docstring const & str)
274 {
275         string s = to_ascii(str);
276         int h;
277         sscanf(s.c_str(), "%x", &h);
278         return h;
279 }
280
281
282 bool isAscii(docstring const & str)
283 {
284         int const len = str.length();
285         for (int i = 0; i < len; ++i)
286                 if (str[i] >= 0x80)
287                         return false;
288         return true;
289 }
290
291
292 char lowercase(char c)
293 {
294         return char(tolower(c));
295 }
296
297
298 char uppercase(char c)
299 {
300         return char(toupper(c));
301 }
302
303 // FIXME for lowercase() and uppercase() function below:
304 // 1) std::tolower() and std::toupper() are templates that
305 // compile fine with char_type. With the test (c >= 256) we
306 // do not trust these function to do the right thing with
307 // unicode char.
308 // 2) these functions use the current locale, which is wrong
309 // if it is not latin1 based (latin1 is a subset of UCS4).
310
311 char_type lowercase(char_type c)
312 {
313         if (c >= 256)
314                 return c;
315
316         return tolower(c);
317 }
318
319
320 char_type uppercase(char_type c)
321 {
322         if (c >= 256)
323                 return c;
324
325         return toupper(c);
326 }
327
328
329 namespace {
330
331 // since we cannot use std::tolower and std::toupper directly in the
332 // calls to std::transform yet, we use these helper clases. (Lgb)
333
334 struct local_lowercase {
335         char operator()(char c) const {
336                 return tolower(c);
337         }
338 };
339
340 struct local_uppercase {
341         char operator()(char c) const {
342                 return toupper(c);
343         }
344 };
345
346 struct local_ascii_lowercase {
347         char operator()(char c) const {
348                 return ascii_tolower(c);
349         }
350 };
351
352 } // end of anon namespace
353
354 string const lowercase(string const & a)
355 {
356         string tmp(a);
357         transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
358         return tmp;
359 }
360
361 string const uppercase(string const & a)
362 {
363         string tmp(a);
364         transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
365         return tmp;
366 }
367
368
369 string const ascii_lowercase(string const & a)
370 {
371         string tmp(a);
372         transform(tmp.begin(), tmp.end(), tmp.begin(),
373                   local_ascii_lowercase());
374         return tmp;
375 }
376
377
378 bool prefixIs(string const & a, string const & pre)
379 {
380         string::size_type const prelen = pre.length();
381         string::size_type const alen = a.length();
382
383         if (prelen > alen || a.empty())
384                 return false;
385         else {
386 #if defined(STD_STRING_IS_GOOD)
387                 return a.compare(0, prelen, pre) == 0;
388 #else
389                 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
390 #endif
391         }
392 }
393
394
395 bool prefixIs(docstring const & a, docstring const & pre)
396 {
397         docstring::size_type const prelen = pre.length();
398         docstring::size_type const alen = a.length();
399
400         if (prelen > alen || a.empty())
401                 return false;
402         else
403                 return a.compare(0, prelen, pre) == 0;
404 }
405
406
407 bool suffixIs(string const & a, char c)
408 {
409         if (a.empty()) return false;
410         return a[a.length() - 1] == c;
411 }
412
413
414 bool suffixIs(string const & a, string const & suf)
415 {
416         string::size_type const suflen = suf.length();
417         string::size_type const alen = a.length();
418
419         if (suflen > alen) {
420                 return false;
421         } else {
422 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
423                 string tmp(a, alen - suflen);
424                 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
425 #else
426                 return a.compare(alen - suflen, suflen, suf) == 0;
427 #endif
428         }
429 }
430
431
432 bool containsOnly(string const & s, string const & cset)
433 {
434         return s.find_first_not_of(cset) == string::npos;
435 }
436
437
438 // ale970405+lasgoutt-970425
439 // rewritten to use new string (Lgb)
440 string const token(string const & a, char delim, int n)
441 {
442         if (a.empty()) return string();
443
444         string::size_type k = 0;
445         string::size_type i = 0;
446
447         // Find delimiter or end of string
448         for (; n--;)
449                 if ((i = a.find(delim, i)) == string::npos)
450                         break;
451                 else
452                         ++i; // step delim
453         // i is now the n'th delim (or string::npos)
454         if (i == string::npos) return string();
455         k = a.find(delim, i);
456         // k is now the n'th + 1 delim (or string::npos)
457
458         return a.substr(i, k - i);
459 }
460
461
462 docstring const token(docstring const & a, char_type delim, int n)
463 {
464         if (a.empty()) return docstring();
465
466         string::size_type k = 0;
467         string::size_type i = 0;
468
469         // Find delimiter or end of string
470         for (; n--;)
471                 if ((i = a.find(delim, i)) == docstring::npos)
472                         break;
473                 else
474                         ++i; // step delim
475         // i is now the n'th delim (or string::npos)
476         if (i == docstring::npos) return docstring();
477         k = a.find(delim, i);
478         // k is now the n'th + 1 delim (or string::npos)
479
480         return a.substr(i, k - i);
481 }
482
483
484 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
485 // rewritten to use new string (Lgb)
486 int tokenPos(string const & a, char delim, string const & tok)
487 {
488         int i = 0;
489         string str(a);
490         string tmptok;
491
492         while (!str.empty()) {
493                 str = split(str, tmptok, delim);
494                 if (tok == tmptok)
495                         return i;
496                 ++i;
497         }
498         return -1;
499 }
500
501
502 namespace {
503
504 /// Substitute all \a oldchar with \a newchar
505 template<typename Ch> inline
506 std::basic_string<Ch> const subst_char(std::basic_string<Ch> const & a,
507                 Ch oldchar, Ch newchar)
508 {
509         typedef std::basic_string<Ch> String;
510         String tmp(a);
511         typename String::iterator lit = tmp.begin();
512         typename String::iterator end = tmp.end();
513         for (; lit != end; ++lit)
514                 if ((*lit) == oldchar)
515                         (*lit) = newchar;
516         return tmp;
517 }
518
519
520 /// substitutes all instances of \a oldstr with \a newstr
521 template<typename String> inline
522 String const subst_string(String const & a,
523                 String const & oldstr, String const & newstr)
524 {
525         BOOST_ASSERT(!oldstr.empty());
526         String lstr = a;
527         typename String::size_type i = 0;
528         typename String::size_type const olen = oldstr.length();
529         while ((i = lstr.find(oldstr, i)) != string::npos) {
530                 lstr.replace(i, olen, newstr);
531                 i += newstr.length(); // We need to be sure that we dont
532                 // use the same i over and over again.
533         }
534         return lstr;
535 }
536
537 }
538
539
540 string const subst(string const & a, char oldchar, char newchar)
541 {
542         return subst_char(a, oldchar, newchar);
543 }
544
545
546 docstring const subst(docstring const & a,
547                 char_type oldchar, char_type newchar)
548 {
549         return subst_char(a, oldchar, newchar);
550 }
551
552
553 string const subst(string const & a,
554                 string const & oldstr, string const & newstr)
555 {
556         return subst_string(a, oldstr, newstr);
557 }
558
559
560 docstring const subst(docstring const & a,
561                 docstring const & oldstr, docstring const & newstr)
562 {
563         return subst_string(a, oldstr, newstr);
564 }
565
566
567 docstring const trim(docstring const & a, char const * p)
568 {
569         BOOST_ASSERT(p);
570
571         if (a.empty() || !*p)
572                 return a;
573
574         docstring s = lyx::from_ascii(p);
575         docstring::size_type r = a.find_last_not_of(s);
576         docstring::size_type l = a.find_first_not_of(s);
577
578         // Is this the minimal test? (lgb)
579         if (r == docstring::npos && l == docstring::npos)
580                 return docstring();
581
582         return a.substr(l, r - l + 1);
583 }
584
585
586 string const trim(string const & a, char const * p)
587 {
588         BOOST_ASSERT(p);
589
590         if (a.empty() || !*p)
591                 return a;
592
593         string::size_type r = a.find_last_not_of(p);
594         string::size_type l = a.find_first_not_of(p);
595
596         // Is this the minimal test? (lgb)
597         if (r == string::npos && l == string::npos)
598                 return string();
599
600         return a.substr(l, r - l + 1);
601 }
602
603
604 string const rtrim(string const & a, char const * p)
605 {
606         BOOST_ASSERT(p);
607
608         if (a.empty() || !*p)
609                 return a;
610
611         string::size_type r = a.find_last_not_of(p);
612
613         // Is this test really needed? (Lgb)
614         if (r == string::npos)
615                 return string();
616
617         return a.substr(0, r + 1);
618 }
619
620
621 string const ltrim(string const & a, char const * p)
622 {
623         BOOST_ASSERT(p);
624         if (a.empty() || !*p)
625                 return a;
626         string::size_type l = a.find_first_not_of(p);
627         if (l == string::npos)
628                 return string();
629         return a.substr(l, string::npos);
630 }
631
632
633 docstring const ltrim(docstring const & a, char const * p)
634 {
635         BOOST_ASSERT(p);
636         if (a.empty() || !*p)
637                 return a;
638         size_t l = a.find_first_not_of(from_ascii(p));
639         if (l == docstring::npos)
640                 return docstring();
641         return a.substr(l, docstring::npos);
642 }
643
644 namespace {
645
646 template<typename String, typename Char> inline
647 String const doSplit(String const & a, String & piece, Char delim)
648 {
649         String tmp;
650         typename String::size_type i = a.find(delim);
651         if (i == a.length() - 1) {
652                 piece = a.substr(0, i);
653         } else if (i != String::npos) {
654                 piece = a.substr(0, i);
655                 tmp = a.substr(i + 1);
656         } else if (i == 0) {
657                 piece.erase();
658                 tmp = a.substr(i + 1);
659         } else {
660                 piece = a;
661         }
662         return tmp;
663 }
664
665 }
666
667
668 string const split(string const & a, string & piece, char delim)
669 {
670         return doSplit(a, piece, delim);
671 }
672
673
674 docstring const split(docstring const & a, docstring & piece, char_type delim)
675 {
676         return doSplit(a, piece, delim);
677 }
678
679
680 string const split(string const & a, char delim)
681 {
682         string tmp;
683         string::size_type i = a.find(delim);
684         if (i != string::npos) // found delim
685                 tmp = a.substr(i + 1);
686         return tmp;
687 }
688
689
690 // ale970521
691 string const rsplit(string const & a, string & piece, char delim)
692 {
693         string tmp;
694         string::size_type i = a.rfind(delim);
695         if (i != string::npos) { // delimiter was found
696                 piece = a.substr(0, i);
697                 tmp = a.substr(i + 1);
698         } else { // delimiter was not found
699                 piece.erase();
700         }
701         return tmp;
702 }
703
704
705 // This function escapes 8-bit characters and other problematic
706 // characters that cause problems in latex labels.
707 docstring const escape(docstring const & lab)
708 {
709         lyx::char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
710                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
711         docstring enc;
712         for (docstring::size_type i = 0; i < lab.length(); ++i) {
713                 lyx::char_type c = lab[i];
714                 // FIXME We must change the following algorithm for UCS4
715                 // chars, but that will be a file format change.
716                 if (c >= 128 || c == '=' || c == '%') {
717                         enc += '=';
718                         enc += hexdigit[c>>4];
719                         enc += hexdigit[c & 15];
720                 } else {
721                         enc += c;
722                 }
723         }
724         return enc;
725 }
726
727
728 /// gives a vector of stringparts which have the delimiter delim
729 vector<string> const getVectorFromString(string const & str,
730                                          string const & delim)
731 {
732 // Lars would like this code to go, but for now his replacement (below)
733 // doesn't fullfil the same function. I have, therefore, reactivated the
734 // old code for now. Angus 11 Nov 2002.
735 #if 1
736         vector<string> vec;
737         if (str.empty())
738                 return vec;
739         string keys = rtrim(str);
740         for(;;) {
741                 string::size_type const idx = keys.find(delim);
742                 if (idx == string::npos) {
743                         vec.push_back(ltrim(keys));
744                         break;
745                 }
746                 string const key = trim(keys.substr(0, idx));
747                 if (!key.empty())
748                         vec.push_back(key);
749                 string::size_type const start = idx + delim.size();
750                 keys = keys.substr(start);
751         }
752         return vec;
753 #else
754         boost::char_separator<char> sep(delim.c_str());
755         boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
756         return vector<string>(tokens.begin(), tokens.end());
757 #endif
758 }
759
760
761 // the same vice versa
762 string const getStringFromVector(vector<string> const & vec,
763                                  string const & delim)
764 {
765         string str;
766         int i = 0;
767         for (vector<string>::const_iterator it = vec.begin();
768              it != vec.end(); ++it) {
769                 string item = trim(*it);
770                 if (item.empty())
771                         continue;
772                 if (i++ > 0)
773                         str += delim;
774                 str += item;
775         }
776         return str;
777 }
778
779
780 int findToken(char const * const str[], string const & search_token)
781 {
782         int i = 0;
783
784         while (str[i][0] && str[i] != search_token)
785                 ++i;
786         if (!str[i][0])
787                 i = -1;
788         return i;
789 }
790
791
792 docstring const externalLineEnding(docstring const & str)
793 {
794 #if defined(__APPLE__)
795         // The MAC clipboard uses \r for lineendings, and we use \n
796         return subst(str, '\n', '\r');
797 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
798         // Windows clipboard uses \r\n for lineendings, and we use \n
799         return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
800 #else
801         return str;
802 #endif
803 }
804
805
806 docstring const internalLineEnding(docstring const & str)
807 {
808         docstring const s = subst(str,
809                         lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
810         return subst(s, '\r', '\n');
811 }
812
813
814 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
815 #if USE_BOOST_FORMAT
816
817 template<>
818 docstring bformat(docstring const & fmt, int arg1)
819 {
820         return (boost::basic_format<char_type>(fmt) % arg1).str();
821 }
822
823
824 template<>
825 docstring bformat(docstring const & fmt, long arg1)
826 {
827         return (boost::basic_format<char_type>(fmt) % arg1).str();
828 }
829
830
831 template<>
832 docstring bformat(docstring const & fmt, unsigned int arg1)
833 {
834         return (boost::basic_format<char_type>(fmt) % arg1).str();
835 }
836
837
838 template<>
839 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
840 {
841         return (boost::basic_format<char_type>(fmt) % arg1).str();
842 }
843
844
845 template<>
846 docstring bformat(docstring const & fmt, char * arg1)
847 {
848         return (boost::basic_format<char_type>(fmt) % arg1).str();
849 }
850
851
852 template<>
853 docstring bformat(docstring const & fmt, int arg1, int arg2)
854 {
855         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
856 }
857
858
859 template<>
860 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
861 {
862         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
863 }
864
865
866 template<>
867 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
868 {
869         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
870 }
871
872
873 template<>
874 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
875 {
876         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
877 }
878
879
880 template<>
881 docstring bformat(docstring const & fmt,
882                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
883 {
884         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
885 }
886
887 #else
888
889 template<>
890 docstring bformat(docstring const & fmt, int arg1)
891 {
892         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
893         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
894         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
895 }
896
897
898 template<>
899 docstring bformat(docstring const & fmt, long arg1)
900 {
901         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
902         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
903         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
904 }
905
906
907 template<>
908 docstring bformat(docstring const & fmt, unsigned int arg1)
909 {
910         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
911         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
912         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
913 }
914
915
916 template<>
917 docstring bformat(docstring const & fmt, docstring arg1)
918 {
919         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
920         docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
921         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
922 }
923
924
925 template<>
926 docstring bformat(docstring const & fmt, char * arg1)
927 {
928         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
929         docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
930         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
931 }
932
933
934 template<>
935 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
936 {
937         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
938         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
939         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
940         str = subst(str, lyx::from_ascii("%2$s"), arg2);
941         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
942 }
943
944
945 template<>
946 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
947 {
948         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
949         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
950         docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
951         str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
952         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
953 }
954
955
956 template<>
957 docstring bformat(docstring const & fmt, int arg1, int arg2)
958 {
959         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
960         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
961         docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
962         str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
963         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
964 }
965
966
967 template<>
968 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
969 {
970         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
971         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
972         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
973         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
974         str = subst(str, lyx::from_ascii("%2$s"), arg2);
975         str = subst(str, lyx::from_ascii("%3$s"), arg3);
976         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
977 }
978
979
980 template<>
981 docstring bformat(docstring const & fmt,
982                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
983 {
984         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
985         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
986         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
987         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
988         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
989         str = subst(str, lyx::from_ascii("%2$s"), arg2);
990         str = subst(str, lyx::from_ascii("%3$s"), arg3);
991         str = subst(str, lyx::from_ascii("%4$s"), arg4);
992         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
993 }
994
995 #endif
996 #endif
997
998 } // namespace support
999 } // namespace lyx