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