]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.cpp
f5f3b1c4760ac6a86b8cb99b7cf4d7f4852aed56
[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 bool hasDigit(docstring const & str)
319 {
320         if (str.empty())
321                 return false;
322
323         // Remove leading and trailing white space chars.
324         docstring const tmpstr = trim(str);
325         if (tmpstr.empty())
326                 return false;
327
328         docstring::const_iterator cit = tmpstr.begin();
329         docstring::const_iterator end = tmpstr.end();
330         for (; cit != end; ++cit)
331                 if (isdigit((*cit)))
332                         return true;
333
334         return false;
335 }
336
337
338 static bool isHexChar(char_type c)
339 {
340         return c == '0' ||
341                 c == '1' ||
342                 c == '2' ||
343                 c == '3' ||
344                 c == '4' ||
345                 c == '5' ||
346                 c == '6' ||
347                 c == '7' ||
348                 c == '8' ||
349                 c == '9' ||
350                 c == 'a' || c == 'A' ||
351                 c == 'b' || c == 'B' ||
352                 c == 'c' || c == 'C' ||
353                 c == 'd' || c == 'D' ||
354                 c == 'e' || c == 'E' ||
355                 c == 'f' || c == 'F';
356 }
357
358
359 bool isHex(docstring const & str)
360 {
361         int index = 0;
362
363         if (str.length() > 2 && str[0] == '0' &&
364             (str[1] == 'x' || str[1] == 'X'))
365                 index = 2;
366
367         int const len = str.length();
368
369         for (; index < len; ++index) {
370                 if (!isHexChar(str[index]))
371                         return false;
372         }
373         return true;
374 }
375
376
377 int hexToInt(docstring const & str)
378 {
379         string s = to_ascii(str);
380         int h;
381         sscanf(s.c_str(), "%x", &h);
382         return h;
383 }
384
385
386 bool isAscii(docstring const & str)
387 {
388         int const len = str.length();
389         for (int i = 0; i < len; ++i)
390                 if (str[i] >= 0x80)
391                         return false;
392         return true;
393 }
394
395
396 bool isAscii(string const & str)
397 {
398         int const len = str.length();
399         for (int i = 0; i < len; ++i)
400                 if (static_cast<unsigned char>(str[i]) >= 0x80)
401                         return false;
402         return true;
403 }
404
405
406 char lowercase(char c)
407 {
408         LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
409         return char(tolower(c));
410 }
411
412
413 char uppercase(char c)
414 {
415         LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
416         return char(toupper(c));
417 }
418
419
420 char_type lowercase(char_type c)
421 {
422         if (!is_utf16(c))
423                 // We don't know how to lowercase a non-utf16 char
424                 return c;
425         return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
426 }
427
428
429 char_type uppercase(char_type c)
430 {
431         if (!is_utf16(c))
432                 // We don't know how to uppercase a non-utf16 char
433                 return c;
434         return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
435 }
436
437
438 namespace {
439
440 // since we cannot use tolower and toupper directly in the
441 // calls to transform yet, we use these helper clases. (Lgb)
442
443 struct local_lowercase {
444         char_type operator()(char_type c) const {
445                 if (!is_utf16(c))
446                         // We don't know how to lowercase a non-utf16 char
447                         return c;
448                 return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
449         }
450 };
451
452 struct local_uppercase {
453         char_type operator()(char_type c) const {
454                 if (!is_utf16(c))
455                         // We don't know how to uppercase a non-utf16 char
456                         return c;
457                 return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
458         }
459 };
460
461 template<typename Char> struct local_ascii_lowercase {
462         Char operator()(Char c) const { return ascii_tolower(c); }
463 };
464
465 } // end of anon namespace
466
467 docstring const lowercase(docstring const & a)
468 {
469         docstring tmp(a);
470         transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
471         return tmp;
472 }
473
474
475 docstring const uppercase(docstring const & a)
476 {
477         docstring tmp(a);
478         transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
479         return tmp;
480 }
481
482
483 string const ascii_lowercase(string const & a)
484 {
485         string tmp(a);
486         transform(tmp.begin(), tmp.end(), tmp.begin(),
487                   local_ascii_lowercase<char>());
488         return tmp;
489 }
490
491
492 docstring const ascii_lowercase(docstring const & a)
493 {
494         docstring tmp(a);
495         transform(tmp.begin(), tmp.end(), tmp.begin(),
496                   local_ascii_lowercase<char_type>());
497         return tmp;
498 }
499
500
501 bool prefixIs(docstring const & a, char_type c)
502 {
503         if (a.empty())
504                 return false;
505         return a[0] == c;
506 }
507
508
509 bool prefixIs(string const & a, string const & pre)
510 {
511         size_t const prelen = pre.length();
512         size_t const alen = a.length();
513         return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
514 }
515
516
517 bool prefixIs(docstring const & a, docstring const & pre)
518 {
519         size_t const prelen = pre.length();
520         size_t const alen = a.length();
521         return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
522 }
523
524
525 bool suffixIs(string const & a, char c)
526 {
527         if (a.empty())
528                 return false;
529         return a[a.length() - 1] == c;
530 }
531
532
533 bool suffixIs(docstring const & a, char_type c)
534 {
535         if (a.empty())
536                 return false;
537         return a[a.length() - 1] == c;
538 }
539
540
541 bool suffixIs(string const & a, string const & suf)
542 {
543         size_t const suflen = suf.length();
544         size_t const alen = a.length();
545         return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
546 }
547
548
549 bool suffixIs(docstring const & a, docstring const & suf)
550 {
551         size_t const suflen = suf.length();
552         size_t const alen = a.length();
553         return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
554 }
555
556
557 bool containsOnly(string const & s, string const & cset)
558 {
559         return s.find_first_not_of(cset) == string::npos;
560 }
561
562
563 // ale970405+lasgoutt-970425
564 // rewritten to use new string (Lgb)
565 string const token(string const & a, char delim, int n)
566 {
567         if (a.empty())
568                 return string();
569
570         size_t k = 0;
571         size_t i = 0;
572
573         // Find delimiter or end of string
574         for (; n--;) {
575                 if ((i = a.find(delim, i)) == string::npos)
576                         break;
577                 else
578                         ++i; // step delim
579         }
580
581         // i is now the n'th delim (or string::npos)
582         if (i == string::npos)
583                 return string();
584
585         k = a.find(delim, i);
586         // k is now the n'th + 1 delim (or string::npos)
587
588         return a.substr(i, k - i);
589 }
590
591
592 docstring const token(docstring const & a, char_type delim, int n)
593 {
594         if (a.empty())
595                 return docstring();
596
597         size_t k = 0;
598         size_t i = 0;
599
600         // Find delimiter or end of string
601         for (; n--;) {
602                 if ((i = a.find(delim, i)) == docstring::npos)
603                         break;
604                 else
605                         ++i; // step delim
606         }
607
608         // i is now the n'th delim (or string::npos)
609         if (i == docstring::npos)
610                 return docstring();
611
612         k = a.find(delim, i);
613         // k is now the n'th + 1 delim (or string::npos)
614
615         return a.substr(i, k - i);
616 }
617
618
619 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
620 // rewritten to use new string (Lgb)
621 int tokenPos(string const & a, char delim, string const & tok)
622 {
623         int i = 0;
624         string str = a;
625         string tmptok;
626
627         while (!str.empty()) {
628                 str = split(str, tmptok, delim);
629                 if (tok == tmptok)
630                         return i;
631                 ++i;
632         }
633         return -1;
634 }
635
636
637 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
638 // rewritten to use new string (Lgb)
639 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
640 {
641         int i = 0;
642         docstring str = a;
643         docstring tmptok;
644
645         while (!str.empty()) {
646                 str = split(str, tmptok, delim);
647                 if (tok == tmptok)
648                         return i;
649                 ++i;
650         }
651         return -1;
652 }
653
654
655 namespace {
656
657 /// Substitute all \a oldchar with \a newchar
658 template<typename Ch> inline
659 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
660                 Ch oldchar, Ch newchar)
661 {
662         typedef basic_string<Ch> String;
663         String tmp(a);
664         typename String::iterator lit = tmp.begin();
665         typename String::iterator end = tmp.end();
666         for (; lit != end; ++lit)
667                 if ((*lit) == oldchar)
668                         (*lit) = newchar;
669         return tmp;
670 }
671
672 /// Substitute all \a oldchar with \a newchar
673 docstring const subst_char(docstring const & a,
674         docstring::value_type oldchar, docstring::value_type newchar)
675 {
676         docstring tmp(a);
677         docstring::iterator lit = tmp.begin();
678         docstring::iterator end = tmp.end();
679         for (; lit != end; ++lit)
680                 if ((*lit) == oldchar)
681                         (*lit) = newchar;
682         return tmp;
683 }
684
685
686 /// substitutes all instances of \a oldstr with \a newstr
687 template<typename String> inline
688 String const subst_string(String const & a,
689                 String const & oldstr, String const & newstr)
690 {
691         LASSERT(!oldstr.empty(), /**/);
692         String lstr = a;
693         size_t i = 0;
694         size_t const olen = oldstr.length();
695         while ((i = lstr.find(oldstr, i)) != string::npos) {
696                 lstr.replace(i, olen, newstr);
697                 i += newstr.length(); // We need to be sure that we dont
698                 // use the same i over and over again.
699         }
700         return lstr;
701 }
702
703 docstring const subst_string(docstring const & a,
704                 docstring const & oldstr, docstring const & newstr)
705 {
706         LASSERT(!oldstr.empty(), /**/);
707         docstring lstr = a;
708         size_t i = 0;
709         size_t const olen = oldstr.length();
710         while ((i = lstr.find(oldstr, i)) != string::npos) {
711                 lstr.replace(i, olen, newstr);
712                 i += newstr.length(); // We need to be sure that we dont
713                 // use the same i over and over again.
714         }
715         return lstr;
716 }
717
718 }
719
720
721 string const subst(string const & a, char oldchar, char newchar)
722 {
723         return subst_char(a, oldchar, newchar);
724 }
725
726
727 docstring const subst(docstring const & a,
728                 char_type oldchar, char_type newchar)
729 {
730         return subst_char(a, oldchar, newchar);
731 }
732
733
734 string const subst(string const & a,
735                 string const & oldstr, string const & newstr)
736 {
737         return subst_string(a, oldstr, newstr);
738 }
739
740
741 docstring const subst(docstring const & a,
742                 docstring const & oldstr, docstring const & newstr)
743 {
744         return subst_string(a, oldstr, newstr);
745 }
746
747
748 docstring const trim(docstring const & a, char const * p)
749 {
750         LASSERT(p, /**/);
751
752         if (a.empty() || !*p)
753                 return a;
754
755         docstring s = from_ascii(p);
756         size_t r = a.find_last_not_of(s);
757         size_t l = a.find_first_not_of(s);
758
759         // Is this the minimal test? (lgb)
760         if (r == docstring::npos && l == docstring::npos)
761                 return docstring();
762
763         return a.substr(l, r - l + 1);
764 }
765
766
767 string const trim(string const & a, char const * p)
768 {
769         LASSERT(p, /**/);
770
771         if (a.empty() || !*p)
772                 return a;
773
774         size_t r = a.find_last_not_of(p);
775         size_t l = a.find_first_not_of(p);
776
777         // Is this the minimal test? (lgb)
778         if (r == string::npos && l == string::npos)
779                 return string();
780
781         return a.substr(l, r - l + 1);
782 }
783
784
785 string const rtrim(string const & a, char const * p)
786 {
787         LASSERT(p, /**/);
788
789         if (a.empty() || !*p)
790                 return a;
791
792         size_t r = a.find_last_not_of(p);
793
794         // Is this test really needed? (Lgb)
795         if (r == string::npos)
796                 return string();
797
798         return a.substr(0, r + 1);
799 }
800
801
802 docstring const rtrim(docstring const & a, char const * p)
803 {
804         LASSERT(p, /**/);
805
806         if (a.empty() || !*p)
807                 return a;
808
809         size_t r = a.find_last_not_of(from_ascii(p));
810
811         // Is this test really needed? (Lgb)
812         if (r == docstring::npos)
813                 return docstring();
814
815         return a.substr(0, r + 1);
816 }
817
818
819 string const ltrim(string const & a, char const * p)
820 {
821         LASSERT(p, /**/);
822         if (a.empty() || !*p)
823                 return a;
824         size_t l = a.find_first_not_of(p);
825         if (l == string::npos)
826                 return string();
827         return a.substr(l, string::npos);
828 }
829
830
831 docstring const ltrim(docstring const & a, char const * p)
832 {
833         LASSERT(p, /**/);
834         if (a.empty() || !*p)
835                 return a;
836         size_t l = a.find_first_not_of(from_ascii(p));
837         if (l == docstring::npos)
838                 return docstring();
839         return a.substr(l, docstring::npos);
840 }
841
842 namespace {
843
844 template<typename String, typename Char> inline
845 String const doSplit(String const & a, String & piece, Char delim)
846 {
847         String tmp;
848         size_t i = a.find(delim);
849         if (i == a.length() - 1) {
850                 piece = a.substr(0, i);
851         } else if (i != String::npos) {
852                 piece = a.substr(0, i);
853                 tmp = a.substr(i + 1);
854         } else if (i == 0) {
855                 piece.erase();
856                 tmp = a.substr(i + 1);
857         } else {
858                 piece = a;
859         }
860         return tmp;
861 }
862
863 template<typename Char> inline
864 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
865 {
866         docstring tmp;
867         size_t i = a.find(delim);
868         if (i == a.length() - 1) {
869                 piece = a.substr(0, i);
870         } else if (i != docstring::npos) {
871                 piece = a.substr(0, i);
872                 tmp = a.substr(i + 1);
873         } else if (i == 0) {
874                 piece.erase();
875                 tmp = a.substr(i + 1);
876         } else {
877                 piece = a;
878         }
879         return tmp;
880 }
881
882 } // anon
883
884
885 string const split(string const & a, string & piece, char delim)
886 {
887         return doSplit(a, piece, delim);
888 }
889
890
891 docstring const split(docstring const & a, docstring & piece, char_type delim)
892 {
893         return doSplit(a, piece, delim);
894 }
895
896
897 string const split(string const & a, char delim)
898 {
899         string tmp;
900         size_t i = a.find(delim);
901         if (i != string::npos) // found delim
902                 tmp = a.substr(i + 1);
903         return tmp;
904 }
905
906
907 // ale970521
908 string const rsplit(string const & a, string & piece, char delim)
909 {
910         string tmp;
911         size_t i = a.rfind(delim);
912         if (i != string::npos) { // delimiter was found
913                 piece = a.substr(0, i);
914                 tmp = a.substr(i + 1);
915         } else { // delimiter was not found
916                 piece.erase();
917         }
918         return tmp;
919 }
920
921
922 docstring const rsplit(docstring const & a, char_type delim)
923 {
924         docstring tmp;
925         size_t i = a.rfind(delim);
926         if (i != string::npos)
927                 tmp = a.substr(i + 1);
928         return tmp;
929 }
930
931
932 docstring const escape(docstring const & lab)
933 {
934         char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
935                                    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
936         docstring enc;
937         for (size_t i = 0; i < lab.length(); ++i) {
938                 char_type c = lab[i];
939                 if (c >= 128 || c == '=' || c == '%') {
940                         // Although char_type is a 32 bit type we know that
941                         // UCS4 occupies only 21 bits, so we don't need to
942                         // encode bigger values. Test for 2^24 because we
943                         // can encode that with the 6 hex digits that are
944                         // needed for 21 bits anyway.
945                         LASSERT(c < (1 << 24), /**/);
946                         enc += '=';
947                         enc += hexdigit[(c>>20) & 15];
948                         enc += hexdigit[(c>>16) & 15];
949                         enc += hexdigit[(c>>12) & 15];
950                         enc += hexdigit[(c>> 8) & 15];
951                         enc += hexdigit[(c>> 4) & 15];
952                         enc += hexdigit[ c      & 15];
953                 } else {
954                         enc += c;
955                 }
956         }
957         return enc;
958 }
959
960
961 namespace {
962
963 // this doesn't check whether str is empty, so do that first.
964 vector<docstring> wrapToVec(docstring const & str, int ind,
965                             size_t const width)
966 {
967         docstring s = trim(str);
968         if (s.empty())
969                 return vector<docstring>();
970
971         docstring indent;
972         if (ind < 0) {
973                 indent.insert(0, -ind, ' ');
974                 ind = 0;
975         } else if (ind > 0)
976                 s.insert(0, ind, ' ');
977
978         vector<docstring> retval;
979         while (s.size() > width) {
980                 // find the last space within the first 'width' chars
981                 size_t const i = s.find_last_of(' ', width - 1);
982                 if (i == docstring::npos || i <= size_t(ind)) {
983                         // no space found
984                         s = s.substr(0, width - 3) + "...";
985                         break;
986                 }
987                 retval.push_back(s.substr(0, i));
988                 s = indent + s.substr(i);
989                 ind = indent.size();
990         }
991         if (!s.empty())
992                 retval.push_back(s);
993         return retval;
994 }
995
996 }
997
998
999 docstring wrap(docstring const & str, int const ind, size_t const width)
1000 {
1001         docstring s = trim(str);
1002         if (s.empty())
1003                 return docstring();
1004
1005         vector<docstring> const svec = wrapToVec(str, ind, width);
1006         return getStringFromVector(svec, from_ascii("\n"));
1007 }
1008
1009
1010 docstring wrapParas(docstring const & str, int const indent,
1011                     size_t const width, size_t const maxlines)
1012 {
1013         if (str.empty())
1014                 return docstring();
1015
1016         vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1017         vector<docstring> retval;
1018
1019         vector<docstring>::const_iterator it = pars.begin();
1020         vector<docstring>::const_iterator const en = pars.end();
1021         for (; it != en; ++it) {
1022                 vector<docstring> tmp = wrapToVec(*it, indent, width);
1023                 size_t const nlines = tmp.size();
1024                 if (nlines == 0)
1025                         continue;
1026                 size_t const curlines = retval.size();
1027                 if (maxlines > 0 && curlines + nlines >= maxlines) {
1028                         tmp.resize(maxlines - curlines - 1);
1029                         tmp.push_back(from_ascii("..."));
1030                 }
1031                 retval.insert(retval.end(), tmp.begin(), tmp.end());
1032                 if (maxlines > 0 && retval.size() >= maxlines)
1033                         break;
1034         }
1035         return getStringFromVector(retval, from_ascii("\n"));
1036 }
1037
1038
1039 namespace {
1040
1041 template<typename String> vector<String> const
1042 getVectorFromStringT(String const & str, String const & delim, bool keepempty)
1043 {
1044 // Lars would like this code to go, but for now his replacement (below)
1045 // doesn't fullfil the same function. I have, therefore, reactivated the
1046 // old code for now. Angus 11 Nov 2002.
1047 #if 1
1048         vector<String> vec;
1049         if (str.empty())
1050                 return vec;
1051         String keys = rtrim(str);
1052         while (true) {
1053                 size_t const idx = keys.find(delim);
1054                 if (idx == String::npos) {
1055                         vec.push_back(ltrim(keys));
1056                         break;
1057                 }
1058                 String const key = trim(keys.substr(0, idx));
1059                 if (!key.empty() || keepempty)
1060                         vec.push_back(key);
1061                 size_t const start = idx + delim.size();
1062                 keys = keys.substr(start);
1063         }
1064         return vec;
1065 #else
1066         typedef boost::char_separator<typename String::value_type> Separator;
1067         typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1068         Separator sep(delim.c_str());
1069         Tokenizer tokens(str, sep);
1070         return vector<String>(tokens.begin(), tokens.end());
1071 #endif
1072 }
1073
1074
1075 template<typename String> const String
1076         getStringFromVector(vector<String> const & vec, String const & delim)
1077 {
1078         String str;
1079         typename vector<String>::const_iterator it = vec.begin();
1080         typename vector<String>::const_iterator en = vec.end();
1081         for (; it != en; ++it) {
1082                 String item = trim(*it);
1083                 if (item.empty())
1084                         continue;
1085                 if (!str.empty())
1086                         str += delim;
1087                 str += item;
1088         }
1089         return str;
1090 }
1091
1092 } // namespace anon
1093
1094
1095 vector<string> const getVectorFromString(string const & str,
1096                                          string const & delim,
1097                                          bool keepempty)
1098 {
1099         return getVectorFromStringT<string>(str, delim, keepempty);
1100 }
1101
1102
1103 vector<docstring> const getVectorFromString(docstring const & str,
1104                                             docstring const & delim,
1105                                             bool keepempty)
1106 {
1107         return getVectorFromStringT<docstring>(str, delim, keepempty);
1108 }
1109
1110
1111 string const getStringFromVector(vector<string> const & vec,
1112                                  string const & delim)
1113 {
1114         return getStringFromVector<string>(vec, delim);
1115 }
1116
1117
1118 docstring const getStringFromVector(vector<docstring> const & vec,
1119                                     docstring const & delim)
1120 {
1121         return getStringFromVector<docstring>(vec, delim);
1122 }
1123
1124
1125 int findToken(char const * const str[], string const & search_token)
1126 {
1127         int i = 0;
1128
1129         while (str[i][0] && str[i] != search_token)
1130                 ++i;
1131         if (!str[i][0])
1132                 i = -1;
1133         return i;
1134 }
1135
1136
1137 template<>
1138 docstring bformat(docstring const & fmt, int arg1)
1139 {
1140         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1141         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1142         return subst(str, from_ascii("%%"), from_ascii("%"));
1143 }
1144
1145
1146 template<>
1147 docstring bformat(docstring const & fmt, long arg1)
1148 {
1149         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1150         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1151         return subst(str, from_ascii("%%"), from_ascii("%"));
1152 }
1153
1154
1155 template<>
1156 docstring bformat(docstring const & fmt, unsigned int arg1)
1157 {
1158         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1159         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1160         return subst(str, from_ascii("%%"), from_ascii("%"));
1161 }
1162
1163
1164 template<>
1165 docstring bformat(docstring const & fmt, docstring arg1)
1166 {
1167         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1168         docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1169         return subst(str, from_ascii("%%"), from_ascii("%"));
1170 }
1171
1172
1173 template<>
1174 docstring bformat(docstring const & fmt, char * arg1)
1175 {
1176         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1177         docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1178         return subst(str, from_ascii("%%"), from_ascii("%"));
1179 }
1180
1181
1182 template<>
1183 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1184 {
1185         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1186         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1187         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1188         str = subst(str, from_ascii("%2$s"), arg2);
1189         return subst(str, from_ascii("%%"), from_ascii("%"));
1190 }
1191
1192
1193 template<>
1194 docstring bformat(docstring const & fmt, docstring arg1, int arg2)
1195 {
1196         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1197         LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1198         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1199         str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1200         return subst(str, from_ascii("%%"), from_ascii("%"));
1201 }
1202
1203
1204 template<>
1205 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1206 {
1207         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1208         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1209         docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1210         str = subst(fmt, from_ascii("%2$s"), arg2);
1211         return subst(str, from_ascii("%%"), from_ascii("%"));
1212 }
1213
1214
1215 template<>
1216 docstring bformat(docstring const & fmt, int arg1, int arg2)
1217 {
1218         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1219         LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1220         docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1221         str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1222         return subst(str, from_ascii("%%"), from_ascii("%"));
1223 }
1224
1225
1226 template<>
1227 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1228 {
1229         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1230         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1231         LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1232         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1233         str = subst(str, from_ascii("%2$s"), arg2);
1234         str = subst(str, from_ascii("%3$s"), arg3);
1235         return subst(str, from_ascii("%%"), from_ascii("%"));
1236 }
1237
1238
1239 template<>
1240 docstring bformat(docstring const & fmt,
1241                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1242 {
1243         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1244         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1245         LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1246         LASSERT(contains(fmt, from_ascii("%4$s")), /**/);
1247         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1248         str = subst(str, from_ascii("%2$s"), arg2);
1249         str = subst(str, from_ascii("%3$s"), arg3);
1250         str = subst(str, from_ascii("%4$s"), arg4);
1251         return subst(str, from_ascii("%%"), from_ascii("%"));
1252 }
1253
1254 } // namespace support
1255 } // namespace lyx