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