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