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