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