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