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