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