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