]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[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 "support/assert.h"
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         LASSERT(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         LASSERT(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         LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
376         return char(tolower(c));
377 }
378
379
380 char uppercase(char c)
381 {
382         LASSERT(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         return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
481 }
482
483
484 bool prefixIs(docstring const & a, docstring const & pre)
485 {
486         size_t const prelen = pre.length();
487         size_t const alen = a.length();
488         return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
489 }
490
491
492 bool suffixIs(string const & a, char c)
493 {
494         if (a.empty()) return false;
495         return a[a.length() - 1] == c;
496 }
497
498
499 bool suffixIs(docstring const & a, char_type c)
500 {
501         if (a.empty())
502                 return false;
503         return a[a.length() - 1] == c;
504 }
505
506
507 bool suffixIs(string const & a, string const & suf)
508 {
509         size_t const suflen = suf.length();
510         size_t const alen = a.length();
511         return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
512 }
513
514
515 bool containsOnly(string const & s, string const & cset)
516 {
517         return s.find_first_not_of(cset) == string::npos;
518 }
519
520
521 // ale970405+lasgoutt-970425
522 // rewritten to use new string (Lgb)
523 string const token(string const & a, char delim, int n)
524 {
525         if (a.empty())
526                 return string();
527
528         size_t k = 0;
529         size_t i = 0;
530
531         // Find delimiter or end of string
532         for (; n--;) {
533                 if ((i = a.find(delim, i)) == string::npos)
534                         break;
535                 else
536                         ++i; // step delim
537         }
538
539         // i is now the n'th delim (or string::npos)
540         if (i == string::npos)
541                 return string();
542
543         k = a.find(delim, i);
544         // k is now the n'th + 1 delim (or string::npos)
545
546         return a.substr(i, k - i);
547 }
548
549
550 docstring const token(docstring const & a, char_type delim, int n)
551 {
552         if (a.empty())
553                 return docstring();
554
555         size_t k = 0;
556         size_t i = 0;
557
558         // Find delimiter or end of string
559         for (; n--;) {
560                 if ((i = a.find(delim, i)) == docstring::npos)
561                         break;
562                 else
563                         ++i; // step delim
564         }
565
566         // i is now the n'th delim (or string::npos)
567         if (i == docstring::npos)
568                 return docstring();
569
570         k = a.find(delim, i);
571         // k is now the n'th + 1 delim (or string::npos)
572
573         return a.substr(i, k - i);
574 }
575
576
577 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
578 // rewritten to use new string (Lgb)
579 int tokenPos(string const & a, char delim, string const & tok)
580 {
581         int i = 0;
582         string str = a;
583         string tmptok;
584
585         while (!str.empty()) {
586                 str = split(str, tmptok, delim);
587                 if (tok == tmptok)
588                         return i;
589                 ++i;
590         }
591         return -1;
592 }
593
594
595 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
596 // rewritten to use new string (Lgb)
597 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
598 {
599         int i = 0;
600         docstring str = a;
601         docstring tmptok;
602
603         while (!str.empty()) {
604                 str = split(str, tmptok, delim);
605                 if (tok == tmptok)
606                         return i;
607                 ++i;
608         }
609         return -1;
610 }
611
612
613 namespace {
614
615 /// Substitute all \a oldchar with \a newchar
616 template<typename Ch> inline
617 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
618                 Ch oldchar, Ch newchar)
619 {
620         typedef basic_string<Ch> String;
621         String tmp(a);
622         typename String::iterator lit = tmp.begin();
623         typename String::iterator end = tmp.end();
624         for (; lit != end; ++lit)
625                 if ((*lit) == oldchar)
626                         (*lit) = newchar;
627         return tmp;
628 }
629
630 /// Substitute all \a oldchar with \a newchar
631 docstring const subst_char(docstring const & a,
632         docstring::value_type oldchar, docstring::value_type newchar)
633 {
634         docstring tmp(a);
635         docstring::iterator lit = tmp.begin();
636         docstring::iterator end = tmp.end();
637         for (; lit != end; ++lit)
638                 if ((*lit) == oldchar)
639                         (*lit) = newchar;
640         return tmp;
641 }
642
643
644 /// substitutes all instances of \a oldstr with \a newstr
645 template<typename String> inline
646 String const subst_string(String const & a,
647                 String const & oldstr, String const & newstr)
648 {
649         LASSERT(!oldstr.empty(), /**/);
650         String lstr = a;
651         size_t i = 0;
652         size_t const olen = oldstr.length();
653         while ((i = lstr.find(oldstr, i)) != string::npos) {
654                 lstr.replace(i, olen, newstr);
655                 i += newstr.length(); // We need to be sure that we dont
656                 // use the same i over and over again.
657         }
658         return lstr;
659 }
660
661 docstring const subst_string(docstring const & a,
662                 docstring const & oldstr, docstring const & newstr)
663 {
664         LASSERT(!oldstr.empty(), /**/);
665         docstring lstr = a;
666         size_t i = 0;
667         size_t const olen = oldstr.length();
668         while ((i = lstr.find(oldstr, i)) != string::npos) {
669                 lstr.replace(i, olen, newstr);
670                 i += newstr.length(); // We need to be sure that we dont
671                 // use the same i over and over again.
672         }
673         return lstr;
674 }
675
676 }
677
678
679 string const subst(string const & a, char oldchar, char newchar)
680 {
681         return subst_char(a, oldchar, newchar);
682 }
683
684
685 docstring const subst(docstring const & a,
686                 char_type oldchar, char_type newchar)
687 {
688         return subst_char(a, oldchar, newchar);
689 }
690
691
692 string const subst(string const & a,
693                 string const & oldstr, string const & newstr)
694 {
695         return subst_string(a, oldstr, newstr);
696 }
697
698
699 docstring const subst(docstring const & a,
700                 docstring const & oldstr, docstring const & newstr)
701 {
702         return subst_string(a, oldstr, newstr);
703 }
704
705
706 docstring const trim(docstring const & a, char const * p)
707 {
708         LASSERT(p, /**/);
709
710         if (a.empty() || !*p)
711                 return a;
712
713         docstring s = from_ascii(p);
714         size_t r = a.find_last_not_of(s);
715         size_t l = a.find_first_not_of(s);
716
717         // Is this the minimal test? (lgb)
718         if (r == docstring::npos && l == docstring::npos)
719                 return docstring();
720
721         return a.substr(l, r - l + 1);
722 }
723
724
725 string const trim(string const & a, char const * p)
726 {
727         LASSERT(p, /**/);
728
729         if (a.empty() || !*p)
730                 return a;
731
732         size_t r = a.find_last_not_of(p);
733         size_t l = a.find_first_not_of(p);
734
735         // Is this the minimal test? (lgb)
736         if (r == string::npos && l == string::npos)
737                 return string();
738
739         return a.substr(l, r - l + 1);
740 }
741
742
743 string const rtrim(string const & a, char const * p)
744 {
745         LASSERT(p, /**/);
746
747         if (a.empty() || !*p)
748                 return a;
749
750         size_t r = a.find_last_not_of(p);
751
752         // Is this test really needed? (Lgb)
753         if (r == string::npos)
754                 return string();
755
756         return a.substr(0, r + 1);
757 }
758
759
760 docstring const rtrim(docstring const & a, char const * p)
761 {
762         LASSERT(p, /**/);
763
764         if (a.empty() || !*p)
765                 return a;
766
767         size_t r = a.find_last_not_of(from_ascii(p));
768
769         // Is this test really needed? (Lgb)
770         if (r == docstring::npos)
771                 return docstring();
772
773         return a.substr(0, r + 1);
774 }
775
776
777 string const ltrim(string const & a, char const * p)
778 {
779         LASSERT(p, /**/);
780         if (a.empty() || !*p)
781                 return a;
782         size_t l = a.find_first_not_of(p);
783         if (l == string::npos)
784                 return string();
785         return a.substr(l, string::npos);
786 }
787
788
789 docstring const ltrim(docstring const & a, char const * p)
790 {
791         LASSERT(p, /**/);
792         if (a.empty() || !*p)
793                 return a;
794         size_t l = a.find_first_not_of(from_ascii(p));
795         if (l == docstring::npos)
796                 return docstring();
797         return a.substr(l, docstring::npos);
798 }
799
800 namespace {
801
802 template<typename String, typename Char> inline
803 String const doSplit(String const & a, String & piece, Char delim)
804 {
805         String tmp;
806         size_t i = a.find(delim);
807         if (i == a.length() - 1) {
808                 piece = a.substr(0, i);
809         } else if (i != String::npos) {
810                 piece = a.substr(0, i);
811                 tmp = a.substr(i + 1);
812         } else if (i == 0) {
813                 piece.erase();
814                 tmp = a.substr(i + 1);
815         } else {
816                 piece = a;
817         }
818         return tmp;
819 }
820
821 template<typename Char> inline
822 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
823 {
824         docstring tmp;
825         size_t i = a.find(delim);
826         if (i == a.length() - 1) {
827                 piece = a.substr(0, i);
828         } else if (i != docstring::npos) {
829                 piece = a.substr(0, i);
830                 tmp = a.substr(i + 1);
831         } else if (i == 0) {
832                 piece.erase();
833                 tmp = a.substr(i + 1);
834         } else {
835                 piece = a;
836         }
837         return tmp;
838 }
839
840 } // anon
841
842
843 string const split(string const & a, string & piece, char delim)
844 {
845         return doSplit(a, piece, delim);
846 }
847
848
849 docstring const split(docstring const & a, docstring & piece, char_type delim)
850 {
851         return doSplit(a, piece, delim);
852 }
853
854
855 string const split(string const & a, char delim)
856 {
857         string tmp;
858         size_t i = a.find(delim);
859         if (i != string::npos) // found delim
860                 tmp = a.substr(i + 1);
861         return tmp;
862 }
863
864
865 // ale970521
866 string const rsplit(string const & a, string & piece, char delim)
867 {
868         string tmp;
869         size_t i = a.rfind(delim);
870         if (i != string::npos) { // delimiter was found
871                 piece = a.substr(0, i);
872                 tmp = a.substr(i + 1);
873         } else { // delimiter was not found
874                 piece.erase();
875         }
876         return tmp;
877 }
878
879
880 docstring const escape(docstring const & lab)
881 {
882         char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
883                                    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
884         docstring enc;
885         for (size_t i = 0; i < lab.length(); ++i) {
886                 char_type c = lab[i];
887                 if (c >= 128 || c == '=' || c == '%') {
888                         // Although char_type is a 32 bit type we know that
889                         // UCS4 occupies only 21 bits, so we don't need to
890                         // encode bigger values. Test for 2^24 because we
891                         // can encode that with the 6 hex digits that are
892                         // needed for 21 bits anyway.
893                         LASSERT(c < (1 << 24), /**/);
894                         enc += '=';
895                         enc += hexdigit[(c>>20) & 15];
896                         enc += hexdigit[(c>>16) & 15];
897                         enc += hexdigit[(c>>12) & 15];
898                         enc += hexdigit[(c>> 8) & 15];
899                         enc += hexdigit[(c>> 4) & 15];
900                         enc += hexdigit[ c      & 15];
901                 } else {
902                         enc += c;
903                 }
904         }
905         return enc;
906 }
907
908
909 namespace {
910
911 template<typename String> vector<String> const
912 getVectorFromStringT(String const & str, String const & delim)
913 {
914 // Lars would like this code to go, but for now his replacement (below)
915 // doesn't fullfil the same function. I have, therefore, reactivated the
916 // old code for now. Angus 11 Nov 2002.
917 #if 1
918         vector<String> vec;
919         if (str.empty())
920                 return vec;
921         String keys = rtrim(str);
922         while (true) {
923                 size_t const idx = keys.find(delim);
924                 if (idx == String::npos) {
925                         vec.push_back(ltrim(keys));
926                         break;
927                 }
928                 String const key = trim(keys.substr(0, idx));
929                 if (!key.empty())
930                         vec.push_back(key);
931                 size_t const start = idx + delim.size();
932                 keys = keys.substr(start);
933         }
934         return vec;
935 #else
936         typedef boost::char_separator<typename String::value_type> Separator;
937         typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
938         Separator sep(delim.c_str());
939         Tokenizer tokens(str, sep);
940         return vector<String>(tokens.begin(), tokens.end());
941 #endif
942 }
943
944 } // namespace anon
945
946
947 vector<string> const getVectorFromString(string const & str,
948                                          string const & delim)
949 {
950         return getVectorFromStringT<string>(str, delim);
951 }
952
953
954 vector<docstring> const getVectorFromString(docstring const & str,
955                                             docstring const & delim)
956 {
957         return getVectorFromStringT<docstring>(str, delim);
958 }
959
960
961 // the same vice versa
962 string const getStringFromVector(vector<string> const & vec,
963                                  string const & delim)
964 {
965         string str;
966         int i = 0;
967         for (vector<string>::const_iterator it = vec.begin();
968              it != vec.end(); ++it) {
969                 string item = trim(*it);
970                 if (item.empty())
971                         continue;
972                 if (i++ > 0)
973                         str += delim;
974                 str += item;
975         }
976         return str;
977 }
978
979
980 int findToken(char const * const str[], string const & search_token)
981 {
982         int i = 0;
983
984         while (str[i][0] && str[i] != search_token)
985                 ++i;
986         if (!str[i][0])
987                 i = -1;
988         return i;
989 }
990
991
992 docstring const externalLineEnding(docstring const & str)
993 {
994 #if defined(__APPLE__)
995         // The MAC clipboard uses \r for lineendings, and we use \n
996         return subst(str, '\n', '\r');
997 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
998         // Windows clipboard uses \r\n for lineendings, and we use \n
999         return subst(str, from_ascii("\n"), from_ascii("\r\n"));
1000 #else
1001         return str;
1002 #endif
1003 }
1004
1005
1006 docstring const internalLineEnding(docstring const & str)
1007 {
1008         docstring const s = subst(str, from_ascii("\r\n"), from_ascii("\n"));
1009         return subst(s, '\r', '\n');
1010 }
1011
1012
1013 template<>
1014 docstring bformat(docstring const & fmt, int arg1)
1015 {
1016         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1017         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1018         return subst(str, from_ascii("%%"), from_ascii("%"));
1019 }
1020
1021
1022 template<>
1023 docstring bformat(docstring const & fmt, long arg1)
1024 {
1025         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1026         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1027         return subst(str, from_ascii("%%"), from_ascii("%"));
1028 }
1029
1030
1031 template<>
1032 docstring bformat(docstring const & fmt, unsigned int arg1)
1033 {
1034         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1035         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1036         return subst(str, from_ascii("%%"), from_ascii("%"));
1037 }
1038
1039
1040 template<>
1041 docstring bformat(docstring const & fmt, docstring arg1)
1042 {
1043         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1044         docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1045         return subst(str, from_ascii("%%"), from_ascii("%"));
1046 }
1047
1048
1049 template<>
1050 docstring bformat(docstring const & fmt, char * arg1)
1051 {
1052         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1053         docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1054         return subst(str, from_ascii("%%"), from_ascii("%"));
1055 }
1056
1057
1058 template<>
1059 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1060 {
1061         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1062         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1063         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1064         str = subst(str, from_ascii("%2$s"), arg2);
1065         return subst(str, from_ascii("%%"), from_ascii("%"));
1066 }
1067
1068
1069 template<>
1070 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1071 {
1072         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1073         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1074         docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1075         str = subst(fmt, from_ascii("%2$s"), arg2);
1076         return subst(str, from_ascii("%%"), from_ascii("%"));
1077 }
1078
1079
1080 template<>
1081 docstring bformat(docstring const & fmt, int arg1, int arg2)
1082 {
1083         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1084         LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1085         docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1086         str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1087         return subst(str, from_ascii("%%"), from_ascii("%"));
1088 }
1089
1090
1091 template<>
1092 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1093 {
1094         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1095         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1096         LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1097         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1098         str = subst(str, from_ascii("%2$s"), arg2);
1099         str = subst(str, from_ascii("%3$s"), arg3);
1100         return subst(str, from_ascii("%%"), from_ascii("%"));
1101 }
1102
1103
1104 template<>
1105 docstring bformat(docstring const & fmt,
1106                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1107 {
1108         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1109         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1110         LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1111         LASSERT(contains(fmt, from_ascii("%4$s")), /**/);
1112         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1113         str = subst(str, from_ascii("%2$s"), arg2);
1114         str = subst(str, from_ascii("%3$s"), arg3);
1115         str = subst(str, from_ascii("%4$s"), arg4);
1116         return subst(str, from_ascii("%%"), from_ascii("%"));
1117 }
1118
1119 } // namespace support
1120 } // namespace lyx