]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.C
Fix output of labels and references that contain characters with an UCS4
[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 docstring const escape(docstring const & lab)
706 {
707         char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
708                                    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
709         docstring enc;
710         for (docstring::size_type i = 0; i < lab.length(); ++i) {
711                 char_type c = lab[i];
712                 if (c >= 128 || c == '=' || c == '%') {
713                         // Although char_type is a 32 bit type we know that
714                         // UCS4 occupies only 21 bits, so we don't need to
715                         // encode bigger values. Test for 2^24 because we
716                         // can encode that with the 6 hex digits that are
717                         // needed for 21 bits anyway.
718                         BOOST_ASSERT(c < (1 << 24));
719                         enc += '=';
720                         enc += hexdigit[(c>>20) & 15];
721                         enc += hexdigit[(c>>16) & 15];
722                         enc += hexdigit[(c>>12) & 15];
723                         enc += hexdigit[(c>> 8) & 15];
724                         enc += hexdigit[(c>> 4) & 15];
725                         enc += hexdigit[ c      & 15];
726                 } else {
727                         enc += c;
728                 }
729         }
730         return enc;
731 }
732
733
734 /// gives a vector of stringparts which have the delimiter delim
735 vector<string> const getVectorFromString(string const & str,
736                                          string const & delim)
737 {
738 // Lars would like this code to go, but for now his replacement (below)
739 // doesn't fullfil the same function. I have, therefore, reactivated the
740 // old code for now. Angus 11 Nov 2002.
741 #if 1
742         vector<string> vec;
743         if (str.empty())
744                 return vec;
745         string keys = rtrim(str);
746         for(;;) {
747                 string::size_type const idx = keys.find(delim);
748                 if (idx == string::npos) {
749                         vec.push_back(ltrim(keys));
750                         break;
751                 }
752                 string const key = trim(keys.substr(0, idx));
753                 if (!key.empty())
754                         vec.push_back(key);
755                 string::size_type const start = idx + delim.size();
756                 keys = keys.substr(start);
757         }
758         return vec;
759 #else
760         boost::char_separator<char> sep(delim.c_str());
761         boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
762         return vector<string>(tokens.begin(), tokens.end());
763 #endif
764 }
765
766
767 // the same vice versa
768 string const getStringFromVector(vector<string> const & vec,
769                                  string const & delim)
770 {
771         string str;
772         int i = 0;
773         for (vector<string>::const_iterator it = vec.begin();
774              it != vec.end(); ++it) {
775                 string item = trim(*it);
776                 if (item.empty())
777                         continue;
778                 if (i++ > 0)
779                         str += delim;
780                 str += item;
781         }
782         return str;
783 }
784
785
786 int findToken(char const * const str[], string const & search_token)
787 {
788         int i = 0;
789
790         while (str[i][0] && str[i] != search_token)
791                 ++i;
792         if (!str[i][0])
793                 i = -1;
794         return i;
795 }
796
797
798 docstring const externalLineEnding(docstring const & str)
799 {
800 #if defined(__APPLE__)
801         // The MAC clipboard uses \r for lineendings, and we use \n
802         return subst(str, '\n', '\r');
803 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
804         // Windows clipboard uses \r\n for lineendings, and we use \n
805         return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
806 #else
807         return str;
808 #endif
809 }
810
811
812 docstring const internalLineEnding(docstring const & str)
813 {
814         docstring const s = subst(str,
815                         lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
816         return subst(s, '\r', '\n');
817 }
818
819
820 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
821 #if USE_BOOST_FORMAT
822
823 template<>
824 docstring bformat(docstring const & fmt, int arg1)
825 {
826         return (boost::basic_format<char_type>(fmt) % arg1).str();
827 }
828
829
830 template<>
831 docstring bformat(docstring const & fmt, long arg1)
832 {
833         return (boost::basic_format<char_type>(fmt) % arg1).str();
834 }
835
836
837 template<>
838 docstring bformat(docstring const & fmt, unsigned int arg1)
839 {
840         return (boost::basic_format<char_type>(fmt) % arg1).str();
841 }
842
843
844 template<>
845 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
846 {
847         return (boost::basic_format<char_type>(fmt) % arg1).str();
848 }
849
850
851 template<>
852 docstring bformat(docstring const & fmt, char * arg1)
853 {
854         return (boost::basic_format<char_type>(fmt) % arg1).str();
855 }
856
857
858 template<>
859 docstring bformat(docstring const & fmt, int arg1, int arg2)
860 {
861         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
862 }
863
864
865 template<>
866 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
867 {
868         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
869 }
870
871
872 template<>
873 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
874 {
875         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
876 }
877
878
879 template<>
880 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
881 {
882         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
883 }
884
885
886 template<>
887 docstring bformat(docstring const & fmt,
888                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
889 {
890         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
891 }
892
893 #else
894
895 template<>
896 docstring bformat(docstring const & fmt, int arg1)
897 {
898         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
899         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
900         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
901 }
902
903
904 template<>
905 docstring bformat(docstring const & fmt, long arg1)
906 {
907         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
908         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
909         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
910 }
911
912
913 template<>
914 docstring bformat(docstring const & fmt, unsigned int arg1)
915 {
916         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
917         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
918         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
919 }
920
921
922 template<>
923 docstring bformat(docstring const & fmt, docstring arg1)
924 {
925         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
926         docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
927         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
928 }
929
930
931 template<>
932 docstring bformat(docstring const & fmt, char * arg1)
933 {
934         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
935         docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
936         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
937 }
938
939
940 template<>
941 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
942 {
943         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
944         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
945         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
946         str = subst(str, lyx::from_ascii("%2$s"), arg2);
947         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
948 }
949
950
951 template<>
952 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
953 {
954         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
955         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
956         docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
957         str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
958         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
959 }
960
961
962 template<>
963 docstring bformat(docstring const & fmt, int arg1, int arg2)
964 {
965         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
966         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
967         docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
968         str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
969         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
970 }
971
972
973 template<>
974 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
975 {
976         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
977         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
978         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
979         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
980         str = subst(str, lyx::from_ascii("%2$s"), arg2);
981         str = subst(str, lyx::from_ascii("%3$s"), arg3);
982         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
983 }
984
985
986 template<>
987 docstring bformat(docstring const & fmt,
988                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
989 {
990         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
991         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
992         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
993         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
994         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
995         str = subst(str, lyx::from_ascii("%2$s"), arg2);
996         str = subst(str, lyx::from_ascii("%3$s"), arg3);
997         str = subst(str, lyx::from_ascii("%4$s"), arg4);
998         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
999 }
1000
1001 #endif
1002 #endif
1003
1004 } // namespace support
1005 } // namespace lyx