]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.C
LFUN_UNICODE_INSERT - unicode-insert
[lyx.git] / src / support / lstrings.C
1 /**
2  * \file lstrings.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "support/lstrings.h"
15 #include "support/lyxlib.h"
16 #include "support/convert.h"
17
18 #include "debug.h"
19
20 #include <boost/tokenizer.hpp>
21 #include <boost/assert.hpp>
22
23 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
24 #if USE_BOOST_FORMAT
25 #include <boost/format.hpp>
26 #endif
27 #endif
28
29 #include <cctype>
30 #include <cstdlib>
31
32 #include <algorithm>
33 #include <sstream>
34
35 using lyx::docstring;
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                 int const lc1 = tolower(*p);
80                 int const lc2 = tolower(*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 int ascii_tolower(int c) {
98         if (c >= 'A' && c <= 'Z')
99                 return c - 'A' + 'a';
100         return c;
101 }
102
103
104 template<typename String> inline
105 int do_compare_ascii_no_case(String const & s, String const & s2)
106 {
107         typename String::const_iterator p = s.begin();
108         typename String::const_iterator p2 = s2.begin();
109
110         while (p != s.end() && p2 != s2.end()) {
111                 int const lc1 = ascii_tolower(*p);
112                 int const lc2 = ascii_tolower(*p2);
113                 if (lc1 != lc2)
114                         return (lc1 < lc2) ? -1 : 1;
115                 ++p;
116                 ++p2;
117         }
118
119         if (s.size() == s2.size())
120                 return 0;
121         if (s.size() < s2.size())
122                 return -1;
123         return 1;
124 }
125
126 }
127
128
129 int compare_ascii_no_case(string const & s, string const & s2)
130 {
131         return do_compare_ascii_no_case(s, s2);
132 }
133
134
135 int compare_ascii_no_case(docstring const & s, docstring const & s2)
136 {
137         return do_compare_ascii_no_case(s, s2);
138 }
139
140
141 int compare_no_case(string const & s, string const & s2, unsigned int len)
142 {
143         string::const_iterator p = s.begin();
144         string::const_iterator p2 = s2.begin();
145         unsigned int i = 0;
146         while (i < len && p != s.end() && p2 != s2.end()) {
147                 int const lc1 = tolower(*p);
148                 int const lc2 = tolower(*p2);
149                 if (lc1 != lc2)
150                         return (lc1 < lc2) ? -1 : 1;
151                 ++i;
152                 ++p;
153                 ++p2;
154         }
155
156         if (s.size() >= len && s2.size() >= len)
157                 return 0;
158         if (s.size() < s2.size())
159                 return -1;
160         return 1;
161 }
162
163
164 bool isStrInt(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
172         string::const_iterator cit = tmpstr.begin();
173         if ((*cit) == '-') ++cit;
174         string::const_iterator end = tmpstr.end();
175         for (; cit != end; ++cit) {
176                 if (!isdigit((*cit))) return false;
177         }
178         return true;
179 }
180
181
182 bool isStrUnsignedInt(string const & str)
183 {
184         if (str.empty()) return false;
185
186         // Remove leading and trailing white space chars.
187         string const tmpstr = trim(str);
188         if (tmpstr.empty()) return false;
189
190         string::const_iterator cit = tmpstr.begin();
191         string::const_iterator end = tmpstr.end();
192         for (; cit != end; ++cit) {
193                 if (!isdigit((*cit))) return false;
194         }
195         return true;
196 }
197
198
199 bool isStrDbl(string const & str)
200 {
201         if (str.empty()) return false;
202
203         // Remove leading and trailing white space chars.
204         string const tmpstr = trim(str);
205         if (tmpstr.empty()) return false;
206         //      if (1 < tmpstr.count('.')) return false;
207
208         string::const_iterator cit = tmpstr.begin();
209         bool found_dot(false);
210         if ((*cit) == '-') ++cit;
211         string::const_iterator end = tmpstr.end();
212         for (; cit != end; ++cit) {
213                 if (!isdigit((*cit))
214                     && '.' != (*cit)) {
215                         return false;
216                 }
217                 if ('.' == (*cit)) {
218                         if (found_dot) {
219                                 return false;
220                         } else {
221                                 found_dot = true;
222                         }
223                 }
224         }
225         return true;
226 }
227
228
229 namespace {
230
231 inline
232 bool isHexChar(char_type c)
233 {
234         return c == '0' ||
235                 c == '1' ||
236                 c == '2' ||
237                 c == '3' ||
238                 c == '4' ||
239                 c == '5' ||
240                 c == '6' ||
241                 c == '7' ||
242                 c == '8' ||
243                 c == '9' ||
244                 c == 'a' || c == 'A' ||
245                 c == 'b' || c == 'B' ||
246                 c == 'c' || c == 'C' ||
247                 c == 'd' || c == 'D' ||
248                 c == 'e' || c == 'E' ||
249                 c == 'f' || c == 'F';
250 }
251
252 } // anon namespace
253
254
255 bool isHex(docstring const & str)
256 {
257         int index = 0;
258
259         if (str.length() > 2 and str[0] == '0' &&
260             (str[1] == 'x' || str[1] == 'X'))
261                 index = 2;
262
263         int const len = str.length();
264
265         for (; index < len; ++index) {
266                 if (!isHexChar(str[index]))
267                         return false;
268         }
269         return true;
270 }
271
272
273 int hexToInt(docstring const & str)
274 {
275         string s = to_ascii(str);
276         int h;
277         sscanf(s.c_str(), "%x", &h);
278         return h;
279 }
280
281
282 char lowercase(char c)
283 {
284         return char(tolower(c));
285 }
286
287
288 char uppercase(char c)
289 {
290         return char(toupper(c));
291 }
292
293 // FIXME for lowercase() and uppercase() function below:
294 // 1) std::tolower() and std::toupper() are templates that
295 // compile fine with char_type. With the test (c >= 256) we
296 // do not trust these function to do the right thing with
297 // unicode char.
298 // 2) these functions use the current locale, which is wrong
299 // if it is not latin1 based (latin1 is a subset of UCS4).
300
301 char_type lowercase(char_type c)
302 {
303         if (c >= 256)
304                 return c;
305
306         return tolower(c);
307 }
308
309
310 char_type uppercase(char_type c)
311 {
312         if (c >= 256)
313                 return c;
314
315         return toupper(c);
316 }
317
318
319 namespace {
320
321 // since we cannot use std::tolower and std::toupper directly in the
322 // calls to std::transform yet, we use these helper clases. (Lgb)
323
324 struct local_lowercase {
325         char operator()(char c) const {
326                 return tolower(c);
327         }
328 };
329
330 struct local_uppercase {
331         char operator()(char c) const {
332                 return toupper(c);
333         }
334 };
335
336 struct local_ascii_lowercase {
337         char operator()(char c) const {
338                 return ascii_tolower(c);
339         }
340 };
341
342 } // end of anon namespace
343
344 string const lowercase(string const & a)
345 {
346         string tmp(a);
347         transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
348         return tmp;
349 }
350
351 string const uppercase(string const & a)
352 {
353         string tmp(a);
354         transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
355         return tmp;
356 }
357
358
359 string const ascii_lowercase(string const & a)
360 {
361         string tmp(a);
362         transform(tmp.begin(), tmp.end(), tmp.begin(),
363                   local_ascii_lowercase());
364         return tmp;
365 }
366
367
368 bool prefixIs(string const & a, string const & pre)
369 {
370         string::size_type const prelen = pre.length();
371         string::size_type const alen = a.length();
372
373         if (prelen > alen || a.empty())
374                 return false;
375         else {
376 #if defined(STD_STRING_IS_GOOD)
377                 return a.compare(0, prelen, pre) == 0;
378 #else
379                 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
380 #endif
381         }
382 }
383
384
385 bool prefixIs(docstring const & a, docstring const & pre)
386 {
387         docstring::size_type const prelen = pre.length();
388         docstring::size_type const alen = a.length();
389
390         if (prelen > alen || a.empty())
391                 return false;
392         else
393                 return a.compare(0, prelen, pre) == 0;
394 }
395
396
397 bool suffixIs(string const & a, char c)
398 {
399         if (a.empty()) 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 string const ltrim(string const & a, char const * p)
612 {
613         BOOST_ASSERT(p);
614         if (a.empty() || !*p)
615                 return a;
616         string::size_type l = a.find_first_not_of(p);
617         if (l == string::npos)
618                 return string();
619         return a.substr(l, string::npos);
620 }
621
622
623 docstring const ltrim(docstring const & a, char const * p)
624 {
625         BOOST_ASSERT(p);
626         if (a.empty() || !*p)
627                 return a;
628         size_t l = a.find_first_not_of(from_ascii(p));
629         if (l == docstring::npos)
630                 return docstring();
631         return a.substr(l, docstring::npos);
632 }
633
634 namespace {
635
636 template<typename String, typename Char> inline
637 String const doSplit(String const & a, String & piece, Char delim)
638 {
639         String tmp;
640         typename String::size_type i = a.find(delim);
641         if (i == a.length() - 1) {
642                 piece = a.substr(0, i);
643         } else if (i != String::npos) {
644                 piece = a.substr(0, i);
645                 tmp = a.substr(i + 1);
646         } else if (i == 0) {
647                 piece.erase();
648                 tmp = a.substr(i + 1);
649         } else {
650                 piece = a;
651         }
652         return tmp;
653 }
654
655 }
656
657
658 string const split(string const & a, string & piece, char delim)
659 {
660         return doSplit(a, piece, delim);
661 }
662
663
664 docstring const split(docstring const & a, docstring & piece, char_type delim)
665 {
666         return doSplit(a, piece, delim);
667 }
668
669
670 string const split(string const & a, char delim)
671 {
672         string tmp;
673         string::size_type i = a.find(delim);
674         if (i != string::npos) // found delim
675                 tmp = a.substr(i + 1);
676         return tmp;
677 }
678
679
680 // ale970521
681 string const rsplit(string const & a, string & piece, char delim)
682 {
683         string tmp;
684         string::size_type i = a.rfind(delim);
685         if (i != string::npos) { // delimiter was found
686                 piece = a.substr(0, i);
687                 tmp = a.substr(i + 1);
688         } else { // delimiter was not found
689                 piece.erase();
690         }
691         return tmp;
692 }
693
694
695 // This function escapes 8-bit characters and other problematic
696 // characters that cause problems in latex labels.
697 docstring const escape(docstring const & lab)
698 {
699         lyx::char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
700                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
701         docstring enc;
702         for (docstring::size_type i = 0; i < lab.length(); ++i) {
703                 lyx::char_type c = lab[i];
704                 // FIXME We must change the following algorithm for UCS4
705                 // chars, but that will be a file format change.
706                 if (c >= 128 || c == '=' || c == '%') {
707                         enc += '=';
708                         enc += hexdigit[c>>4];
709                         enc += hexdigit[c & 15];
710                 } else {
711                         enc += c;
712                 }
713         }
714         return enc;
715 }
716
717
718 /// gives a vector of stringparts which have the delimiter delim
719 vector<string> const getVectorFromString(string const & str,
720                                          string const & delim)
721 {
722 // Lars would like this code to go, but for now his replacement (below)
723 // doesn't fullfil the same function. I have, therefore, reactivated the
724 // old code for now. Angus 11 Nov 2002.
725 #if 1
726         vector<string> vec;
727         if (str.empty())
728                 return vec;
729         string keys = rtrim(str);
730         for(;;) {
731                 string::size_type const idx = keys.find(delim);
732                 if (idx == string::npos) {
733                         vec.push_back(ltrim(keys));
734                         break;
735                 }
736                 string const key = trim(keys.substr(0, idx));
737                 if (!key.empty())
738                         vec.push_back(key);
739                 string::size_type const start = idx + delim.size();
740                 keys = keys.substr(start);
741         }
742         return vec;
743 #else
744         boost::char_separator<char> sep(delim.c_str());
745         boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
746         return vector<string>(tokens.begin(), tokens.end());
747 #endif
748 }
749
750
751 // the same vice versa
752 string const getStringFromVector(vector<string> const & vec,
753                                  string const & delim)
754 {
755         string str;
756         int i = 0;
757         for (vector<string>::const_iterator it = vec.begin();
758              it != vec.end(); ++it) {
759                 string item = trim(*it);
760                 if (item.empty())
761                         continue;
762                 if (i++ > 0)
763                         str += delim;
764                 str += item;
765         }
766         return str;
767 }
768
769
770 int findToken(char const * const str[], string const & search_token)
771 {
772         int i = 0;
773
774         while (str[i][0] && str[i] != search_token)
775                 ++i;
776         if (!str[i][0])
777                 i = -1;
778         return i;
779 }
780
781
782 docstring const externalLineEnding(docstring const & str)
783 {
784 #if defined(__APPLE__)
785         // The MAC clipboard uses \r for lineendings, and we use \n
786         return subst(str, '\n', '\r');
787 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
788         // Windows clipboard uses \r\n for lineendings, and we use \n
789         return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
790 #else
791         return str;
792 #endif
793 }
794
795
796 docstring const internalLineEnding(docstring const & str)
797 {
798         docstring const s = subst(str,
799                         lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
800         return subst(s, '\r', '\n');
801 }
802
803
804 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
805 #if USE_BOOST_FORMAT
806
807 template<>
808 docstring bformat(docstring const & fmt, int arg1)
809 {
810         return (boost::basic_format<char_type>(fmt) % arg1).str();
811 }
812
813
814 template<>
815 docstring bformat(docstring const & fmt, long arg1)
816 {
817         return (boost::basic_format<char_type>(fmt) % arg1).str();
818 }
819
820
821 template<>
822 docstring bformat(docstring const & fmt, unsigned int arg1)
823 {
824         return (boost::basic_format<char_type>(fmt) % arg1).str();
825 }
826
827
828 template<>
829 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
830 {
831         return (boost::basic_format<char_type>(fmt) % arg1).str();
832 }
833
834
835 template<>
836 docstring bformat(docstring const & fmt, char * arg1)
837 {
838         return (boost::basic_format<char_type>(fmt) % arg1).str();
839 }
840
841
842 template<>
843 docstring bformat(docstring const & fmt, int arg1, int arg2)
844 {
845         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
846 }
847
848
849 template<>
850 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
851 {
852         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
853 }
854
855
856 template<>
857 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
858 {
859         return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
860 }
861
862
863 template<>
864 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
865 {
866         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
867 }
868
869
870 template<>
871 docstring bformat(docstring const & fmt,
872                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
873 {
874         return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
875 }
876
877 #else
878
879 template<>
880 docstring bformat(docstring const & fmt, int arg1)
881 {
882         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
883         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
884         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
885 }
886
887
888 template<>
889 docstring bformat(docstring const & fmt, long arg1)
890 {
891         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
892         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
893         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
894 }
895
896
897 template<>
898 docstring bformat(docstring const & fmt, unsigned int arg1)
899 {
900         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
901         docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
902         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
903 }
904
905
906 template<>
907 docstring bformat(docstring const & fmt, docstring arg1)
908 {
909         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
910         docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
911         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
912 }
913
914
915 template<>
916 docstring bformat(docstring const & fmt, char * arg1)
917 {
918         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
919         docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
920         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
921 }
922
923
924 template<>
925 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
926 {
927         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
928         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
929         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
930         str = subst(str, lyx::from_ascii("%2$s"), arg2);
931         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
932 }
933
934
935 template<>
936 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
937 {
938         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
939         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
940         docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
941         str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
942         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
943 }
944
945
946 template<>
947 docstring bformat(docstring const & fmt, int arg1, int arg2)
948 {
949         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
950         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
951         docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
952         str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
953         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
954 }
955
956
957 template<>
958 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
959 {
960         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
961         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
962         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
963         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
964         str = subst(str, lyx::from_ascii("%2$s"), arg2);
965         str = subst(str, lyx::from_ascii("%3$s"), arg3);
966         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
967 }
968
969
970 template<>
971 docstring bformat(docstring const & fmt,
972                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
973 {
974         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
975         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
976         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
977         BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
978         docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
979         str = subst(str, lyx::from_ascii("%2$s"), arg2);
980         str = subst(str, lyx::from_ascii("%3$s"), arg3);
981         str = subst(str, lyx::from_ascii("%4$s"), arg4);
982         return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
983 }
984
985 #endif
986 #endif
987
988 } // namespace support
989 } // namespace lyx