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