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