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