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