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