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