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