]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.C
hopefully fix tex2lyx linking.
[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 #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 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 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());
388         return tmp;
389 }
390
391 string const uppercase(string const & a)
392 {
393         string tmp(a);
394         transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
395         return tmp;
396 }
397
398
399 string const ascii_lowercase(string const & a)
400 {
401         string tmp(a);
402         transform(tmp.begin(), tmp.end(), tmp.begin(),
403                   local_ascii_lowercase());
404         return tmp;
405 }
406
407
408 bool prefixIs(string const & a, string const & pre)
409 {
410         string::size_type const prelen = pre.length();
411         string::size_type const alen = a.length();
412
413         if (prelen > alen || a.empty())
414                 return false;
415         else {
416 #if defined(STD_STRING_IS_GOOD)
417                 return a.compare(0, prelen, pre) == 0;
418 #else
419                 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
420 #endif
421         }
422 }
423
424
425 bool prefixIs(docstring const & a, docstring const & pre)
426 {
427         docstring::size_type const prelen = pre.length();
428         docstring::size_type const alen = a.length();
429
430         if (prelen > alen || a.empty())
431                 return false;
432         else
433                 return a.compare(0, prelen, pre) == 0;
434 }
435
436
437 bool suffixIs(string const & a, char c)
438 {
439         if (a.empty()) return false;
440         return a[a.length() - 1] == c;
441 }
442
443
444 bool suffixIs(string const & a, string const & suf)
445 {
446         string::size_type const suflen = suf.length();
447         string::size_type const alen = a.length();
448
449         if (suflen > alen) {
450                 return false;
451         } else {
452 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
453                 string tmp(a, alen - suflen);
454                 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
455 #else
456                 return a.compare(alen - suflen, suflen, suf) == 0;
457 #endif
458         }
459 }
460
461
462 bool containsOnly(string const & s, string const & cset)
463 {
464         return s.find_first_not_of(cset) == string::npos;
465 }
466
467
468 // ale970405+lasgoutt-970425
469 // rewritten to use new string (Lgb)
470 string const token(string const & a, char delim, int n)
471 {
472         if (a.empty()) return string();
473
474         string::size_type k = 0;
475         string::size_type i = 0;
476
477         // Find delimiter or end of string
478         for (; n--;)
479                 if ((i = a.find(delim, i)) == string::npos)
480                         break;
481                 else
482                         ++i; // step delim
483         // i is now the n'th delim (or string::npos)
484         if (i == string::npos) return string();
485         k = a.find(delim, i);
486         // k is now the n'th + 1 delim (or string::npos)
487
488         return a.substr(i, k - i);
489 }
490
491
492 docstring const token(docstring const & a, char_type delim, int n)
493 {
494         if (a.empty()) return docstring();
495
496         string::size_type k = 0;
497         string::size_type i = 0;
498
499         // Find delimiter or end of string
500         for (; n--;)
501                 if ((i = a.find(delim, i)) == docstring::npos)
502                         break;
503                 else
504                         ++i; // step delim
505         // i is now the n'th delim (or string::npos)
506         if (i == docstring::npos) return docstring();
507         k = a.find(delim, i);
508         // k is now the n'th + 1 delim (or string::npos)
509
510         return a.substr(i, k - i);
511 }
512
513
514 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
515 // rewritten to use new string (Lgb)
516 int tokenPos(string const & a, char delim, string const & tok)
517 {
518         int i = 0;
519         string str(a);
520         string tmptok;
521
522         while (!str.empty()) {
523                 str = split(str, tmptok, delim);
524                 if (tok == tmptok)
525                         return i;
526                 ++i;
527         }
528         return -1;
529 }
530
531
532 namespace {
533
534 /// Substitute all \a oldchar with \a newchar
535 template<typename Ch> inline
536 std::basic_string<Ch> const subst_char(std::basic_string<Ch> const & a,
537                 Ch oldchar, Ch newchar)
538 {
539         typedef std::basic_string<Ch> String;
540         String tmp(a);
541         typename String::iterator lit = tmp.begin();
542         typename String::iterator end = tmp.end();
543         for (; lit != end; ++lit)
544                 if ((*lit) == oldchar)
545                         (*lit) = newchar;
546         return tmp;
547 }
548
549
550 /// substitutes all instances of \a oldstr with \a newstr
551 template<typename String> inline
552 String const subst_string(String const & a,
553                 String const & oldstr, String const & newstr)
554 {
555         BOOST_ASSERT(!oldstr.empty());
556         String lstr = a;
557         typename String::size_type i = 0;
558         typename String::size_type const olen = oldstr.length();
559         while ((i = lstr.find(oldstr, i)) != string::npos) {
560                 lstr.replace(i, olen, newstr);
561                 i += newstr.length(); // We need to be sure that we dont
562                 // use the same i over and over again.
563         }
564         return lstr;
565 }
566
567 }
568
569
570 string const subst(string const & a, char oldchar, char newchar)
571 {
572         return subst_char(a, oldchar, newchar);
573 }
574
575
576 docstring const subst(docstring const & a,
577                 char_type oldchar, char_type newchar)
578 {
579         return subst_char(a, oldchar, newchar);
580 }
581
582
583 string const subst(string const & a,
584                 string const & oldstr, string const & newstr)
585 {
586         return subst_string(a, oldstr, newstr);
587 }
588
589
590 docstring const subst(docstring const & a,
591                 docstring const & oldstr, docstring const & newstr)
592 {
593         return subst_string(a, oldstr, newstr);
594 }
595
596
597 docstring const trim(docstring const & a, char const * p)
598 {
599         BOOST_ASSERT(p);
600
601         if (a.empty() || !*p)
602                 return a;
603
604         docstring s = lyx::from_ascii(p);
605         docstring::size_type r = a.find_last_not_of(s);
606         docstring::size_type l = a.find_first_not_of(s);
607
608         // Is this the minimal test? (lgb)
609         if (r == docstring::npos && l == docstring::npos)
610                 return docstring();
611
612         return a.substr(l, r - l + 1);
613 }
614
615
616 string const trim(string const & a, char const * p)
617 {
618         BOOST_ASSERT(p);
619
620         if (a.empty() || !*p)
621                 return a;
622
623         string::size_type r = a.find_last_not_of(p);
624         string::size_type l = a.find_first_not_of(p);
625
626         // Is this the minimal test? (lgb)
627         if (r == string::npos && l == string::npos)
628                 return string();
629
630         return a.substr(l, r - l + 1);
631 }
632
633
634 string const rtrim(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
643         // Is this test really needed? (Lgb)
644         if (r == string::npos)
645                 return string();
646
647         return a.substr(0, r + 1);
648 }
649
650
651 string const ltrim(string const & a, char const * p)
652 {
653         BOOST_ASSERT(p);
654         if (a.empty() || !*p)
655                 return a;
656         string::size_type l = a.find_first_not_of(p);
657         if (l == string::npos)
658                 return string();
659         return a.substr(l, string::npos);
660 }
661
662
663 docstring const ltrim(docstring const & a, char const * p)
664 {
665         BOOST_ASSERT(p);
666         if (a.empty() || !*p)
667                 return a;
668         size_t l = a.find_first_not_of(from_ascii(p));
669         if (l == docstring::npos)
670                 return docstring();
671         return a.substr(l, docstring::npos);
672 }
673
674 namespace {
675
676 template<typename String, typename Char> inline
677 String const doSplit(String const & a, String & piece, Char delim)
678 {
679         String tmp;
680         typename String::size_type i = a.find(delim);
681         if (i == a.length() - 1) {
682                 piece = a.substr(0, i);
683         } else if (i != String::npos) {
684                 piece = a.substr(0, i);
685                 tmp = a.substr(i + 1);
686         } else if (i == 0) {
687                 piece.erase();
688                 tmp = a.substr(i + 1);
689         } else {
690                 piece = a;
691         }
692         return tmp;
693 }
694
695 }
696
697
698 string const split(string const & a, string & piece, char delim)
699 {
700         return doSplit(a, piece, delim);
701 }
702
703
704 docstring const split(docstring const & a, docstring & piece, char_type delim)
705 {
706         return doSplit(a, piece, delim);
707 }
708
709
710 string const split(string const & a, char delim)
711 {
712         string tmp;
713         string::size_type i = a.find(delim);
714         if (i != string::npos) // found delim
715                 tmp = a.substr(i + 1);
716         return tmp;
717 }
718
719
720 // ale970521
721 string const rsplit(string const & a, string & piece, char delim)
722 {
723         string tmp;
724         string::size_type i = a.rfind(delim);
725         if (i != string::npos) { // delimiter was found
726                 piece = a.substr(0, i);
727                 tmp = a.substr(i + 1);
728         } else { // delimiter was not found
729                 piece.erase();
730         }
731         return tmp;
732 }
733
734
735 docstring const escape(docstring const & lab)
736 {
737         char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
738                                    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
739         docstring enc;
740         for (docstring::size_type i = 0; i < lab.length(); ++i) {
741                 char_type c = lab[i];
742                 if (c >= 128 || c == '=' || c == '%') {
743                         // Although char_type is a 32 bit type we know that
744                         // UCS4 occupies only 21 bits, so we don't need to
745                         // encode bigger values. Test for 2^24 because we
746                         // can encode that with the 6 hex digits that are
747                         // needed for 21 bits anyway.
748                         BOOST_ASSERT(c < (1 << 24));
749                         enc += '=';
750                         enc += hexdigit[(c>>20) & 15];
751                         enc += hexdigit[(c>>16) & 15];
752                         enc += hexdigit[(c>>12) & 15];
753                         enc += hexdigit[(c>> 8) & 15];
754                         enc += hexdigit[(c>> 4) & 15];
755                         enc += hexdigit[ c      & 15];
756                 } else {
757                         enc += c;
758                 }
759         }
760         return enc;
761 }
762
763
764 /// gives a vector of stringparts which have the delimiter delim
765 vector<string> const getVectorFromString(string const & str,
766                                          string const & delim)
767 {
768 // Lars would like this code to go, but for now his replacement (below)
769 // doesn't fullfil the same function. I have, therefore, reactivated the
770 // old code for now. Angus 11 Nov 2002.
771 #if 1
772         vector<string> vec;
773         if (str.empty())
774                 return vec;
775         string keys = rtrim(str);
776         for(;;) {
777                 string::size_type const idx = keys.find(delim);
778                 if (idx == string::npos) {
779                         vec.push_back(ltrim(keys));
780                         break;
781                 }
782                 string const key = trim(keys.substr(0, idx));
783                 if (!key.empty())
784                         vec.push_back(key);
785                 string::size_type const start = idx + delim.size();
786                 keys = keys.substr(start);
787         }
788         return vec;
789 #else
790         boost::char_separator<char> sep(delim.c_str());
791         boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
792         return vector<string>(tokens.begin(), tokens.end());
793 #endif
794 }
795
796
797 // the same vice versa
798 string const getStringFromVector(vector<string> const & vec,
799                                  string const & delim)
800 {
801         string str;
802         int i = 0;
803         for (vector<string>::const_iterator it = vec.begin();
804              it != vec.end(); ++it) {
805                 string item = trim(*it);
806                 if (item.empty())
807                         continue;
808                 if (i++ > 0)
809                         str += delim;
810                 str += item;
811         }
812         return str;
813 }
814
815
816 int findToken(char const * const str[], string const & search_token)
817 {
818         int i = 0;
819
820         while (str[i][0] && str[i] != search_token)
821                 ++i;
822         if (!str[i][0])
823                 i = -1;
824         return i;
825 }
826
827
828 docstring const externalLineEnding(docstring const & str)
829 {
830 #if defined(__APPLE__)
831         // The MAC clipboard uses \r for lineendings, and we use \n
832         return subst(str, '\n', '\r');
833 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
834         // Windows clipboard uses \r\n for lineendings, and we use \n
835         return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
836 #else
837         return str;
838 #endif
839 }
840
841
842 docstring const internalLineEnding(docstring const & str)
843 {
844         docstring const s = subst(str,
845                         lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
846         return subst(s, '\r', '\n');
847 }
848
849
850 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
851 #if USE_BOOST_FORMAT
852
853 template<>
854 docstring bformat(docstring const & fmt, int arg1)
855 {
856         return (boost::basic_format<char_type>(fmt) % arg1).str();
857 }
858
859
860 template<>
861 docstring bformat(docstring const & fmt, long arg1)
862 {
863         return (boost::basic_format<char_type>(fmt) % arg1).str();
864 }
865
866
867 template<>
868 docstring bformat(docstring const & fmt, unsigned int arg1)
869 {
870         return (boost::basic_format<char_type>(fmt) % arg1).str();
871 }
872
873
874 template<>
875 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
876 {
877         return (boost::basic_format<char_type>(fmt) % arg1).str();
878 }
879
880
881 template<>
882 docstring bformat(docstring const & fmt, char * arg1)
883 {
884         return (boost::basic_format<char_type>(fmt) % arg1).str();
885 }
886
887
888 template<>
889 docstring bformat(docstring const & fmt, int arg1, int arg2)
890 {
891         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
892 }
893
894
895 template<>
896 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
897 {
898         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
899 }
900
901
902 template<>
903 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
904 {
905         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
906 }
907
908
909 template<>
910 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
911 {
912         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
913 }
914
915
916 template<>
917 docstring bformat(docstring const & fmt,
918                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
919 {
920         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
921 }
922
923 #else
924
925 template<>
926 docstring bformat(docstring const & fmt, int arg1)
927 {
928         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
929         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
930         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
931 }
932
933
934 template<>
935 docstring bformat(docstring const & fmt, long arg1)
936 {
937         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
938         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
939         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
940 }
941
942
943 template<>
944 docstring bformat(docstring const & fmt, unsigned int arg1)
945 {
946         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
947         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
948         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
949 }
950
951
952 template<>
953 docstring bformat(docstring const & fmt, docstring arg1)
954 {
955         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
956         docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
957         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
958 }
959
960
961 template<>
962 docstring bformat(docstring const & fmt, char * arg1)
963 {
964         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
965         docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
966         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
967 }
968
969
970 template<>
971 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
972 {
973         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
974         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
975         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
976         str = subst(str, lyx::from_ascii("%2$s"), arg2);
977         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
978 }
979
980
981 template<>
982 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
983 {
984         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
985         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
986         docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
987         str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
988         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
989 }
990
991
992 template<>
993 docstring bformat(docstring const & fmt, int arg1, int arg2)
994 {
995         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
996         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
997         docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
998         str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
999         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1000 }
1001
1002
1003 template<>
1004 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1005 {
1006         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1007         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
1008         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
1009         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
1010         str = subst(str, lyx::from_ascii("%2$s"), arg2);
1011         str = subst(str, lyx::from_ascii("%3$s"), arg3);
1012         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1013 }
1014
1015
1016 template<>
1017 docstring bformat(docstring const & fmt,
1018                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1019 {
1020         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
1021         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
1022         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
1023         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
1024         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
1025         str = subst(str, lyx::from_ascii("%2$s"), arg2);
1026         str = subst(str, lyx::from_ascii("%3$s"), arg3);
1027         str = subst(str, lyx::from_ascii("%4$s"), arg4);
1028         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
1029 }
1030
1031 #endif
1032 #endif
1033
1034 } // namespace support
1035 } // namespace lyx