]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.cpp
daa862747c16ea97f72499764f12ec704852b7d1
[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 namespace {
51 /**
52  * Convert a QChar into a UCS4 character.
53  * This is a hack (it does only make sense for the common part of the UCS4
54  * and UTF16 encodings) and should not be used.
55  * This does only exist because of performance reasons (a real conversion
56  * using iconv is too slow on windows).
57  */
58 inline char_type qchar_to_ucs4(QChar const & qchar)
59 {
60         LASSERT(is_utf16(static_cast<char_type>(qchar.unicode())), /**/);
61         return static_cast<char_type>(qchar.unicode());
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 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 /// Maximum valid UCS4 code point
78 char_type const ucs4_max = 0x10ffff;
79 } // anon namespace
80
81
82 bool isLetterChar(char_type c)
83 {
84         if (!is_utf16(c)) {
85                 if (c > ucs4_max)
86                         // outside the UCS4 range
87                         return false;
88                 // assume that all non-utf16 characters are letters
89                 return true;
90         }
91         return ucs4_to_qchar(c).isLetter();
92 }
93
94
95 bool isLower(char_type c)
96 {
97         if (!is_utf16(c))
98                 return false;
99         return ucs4_to_qchar(c).isLower();
100 }
101
102
103 bool isAlphaASCII(char_type c)
104 {
105         return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
106 }
107
108
109 bool isPrintable(char_type c)
110 {
111         if (!is_utf16(c)) {
112                 if (c > ucs4_max)
113                         // outside the UCS4 range
114                         return false;
115                 // assume that all non-utf16 characters are printable
116                 return true;
117         }
118         return ucs4_to_qchar(c).isPrint();
119 }
120
121
122 bool isPrintableNonspace(char_type c)
123 {
124         if (!is_utf16(c)) {
125                 if (c > ucs4_max)
126                         // outside the UCS4 range
127                         return false;
128                 // assume that all non-utf16 characters are printable and
129                 // no space
130                 return true;
131         }
132         QChar const qc = ucs4_to_qchar(c);
133         return qc.isPrint() && !qc.isSpace();
134 }
135
136
137 bool isSpace(char_type c)
138 {
139         if (!is_utf16(c)) {
140                 // assume that no non-utf16 character is a space
141                 // c outside the UCS4 range is catched as well
142                 return false;
143         }
144         QChar const qc = ucs4_to_qchar(c);
145         return qc.isSpace();
146 }
147
148
149 bool isDigit(char_type c)
150 {
151         if (!is_utf16(c))
152                 // assume that no non-utf16 character is a digit
153                 // c outside the UCS4 range is catched as well
154                 return false;
155         return ucs4_to_qchar(c).isDigit();
156 }
157
158
159 bool isDigitASCII(char_type c)
160 {
161         return '0' <= c && c <= '9';
162 }
163
164 namespace support {
165
166 int compare_no_case(docstring const & s, docstring const & s2)
167 {
168         docstring::const_iterator p = s.begin();
169         docstring::const_iterator p2 = s2.begin();
170
171         while (p != s.end() && p2 != s2.end()) {
172                 char_type const lc1 = lowercase(*p);
173                 char_type const lc2 = lowercase(*p2);
174                 if (lc1 != lc2)
175                         return (lc1 < lc2) ? -1 : 1;
176                 ++p;
177                 ++p2;
178         }
179
180         if (s.size() == s2.size())
181                 return 0;
182         if (s.size() < s2.size())
183                 return -1;
184         return 1;
185 }
186
187
188 namespace {
189
190 template<typename Char>
191 Char ascii_tolower(Char c) {
192         if (c >= 'A' && c <= 'Z')
193                 return c - 'A' + 'a';
194         return c;
195 }
196
197 }
198
199
200 int compare_ascii_no_case(string const & s, string const & s2)
201 {
202         string::const_iterator p = s.begin();
203         string::const_iterator p2 = s2.begin();
204
205         while (p != s.end() && p2 != s2.end()) {
206                 int const lc1 = ascii_tolower(*p);
207                 int const lc2 = ascii_tolower(*p2);
208                 if (lc1 != lc2)
209                         return (lc1 < lc2) ? -1 : 1;
210                 ++p;
211                 ++p2;
212         }
213
214         if (s.size() == s2.size())
215                 return 0;
216         if (s.size() < s2.size())
217                 return -1;
218         return 1;
219 }
220
221
222 int compare_ascii_no_case(docstring const & s, docstring const & s2)
223 {
224         docstring::const_iterator p = s.begin();
225         docstring::const_iterator p2 = s2.begin();
226
227         while (p != s.end() && p2 != s2.end()) {
228                 char_type const lc1 = ascii_tolower(*p);
229                 char_type const lc2 = ascii_tolower(*p2);
230                 if (lc1 != lc2)
231                         return (lc1 < lc2) ? -1 : 1;
232                 ++p;
233                 ++p2;
234         }
235
236         if (s.size() == s2.size())
237                 return 0;
238         if (s.size() < s2.size())
239                 return -1;
240         return 1;
241 }
242
243
244 bool isStrInt(string const & str)
245 {
246         if (str.empty())
247                 return false;
248
249         // Remove leading and trailing white space chars.
250         string const tmpstr = trim(str);
251         if (tmpstr.empty())
252                 return false;
253
254         string::const_iterator cit = tmpstr.begin();
255         if ((*cit) == '-')
256                 ++cit;
257
258         string::const_iterator end = tmpstr.end();
259         for (; cit != end; ++cit)
260                 if (!isdigit((*cit)))
261                         return false;
262
263         return true;
264 }
265
266
267 bool isStrUnsignedInt(string const & str)
268 {
269         if (str.empty())
270                 return false;
271
272         // Remove leading and trailing white space chars.
273         string const tmpstr = trim(str);
274         if (tmpstr.empty())
275                 return false;
276
277         string::const_iterator cit = tmpstr.begin();
278         string::const_iterator end = tmpstr.end();
279         for (; cit != end; ++cit)
280                 if (!isdigit((*cit)))
281                         return false;
282
283         return true;
284 }
285
286
287 bool isStrDbl(string const & str)
288 {
289         if (str.empty())
290                 return false;
291
292         // Remove leading and trailing white space chars.
293         string const tmpstr = trim(str);
294         if (tmpstr.empty())
295                 return false;
296         //      if (tmpstr.count('.') > 1) return false;
297
298         string::const_iterator cit = tmpstr.begin();
299         bool found_dot = false;
300         if (*cit == '-')
301                 ++cit;
302         string::const_iterator end = tmpstr.end();
303         for (; cit != end; ++cit) {
304                 if (!isdigit(*cit) && *cit != '.')
305                         return false;
306                 if ('.' == (*cit)) {
307                         if (found_dot)
308                                 return false;
309                         found_dot = true;
310                 }
311         }
312         return true;
313 }
314
315
316 bool hasDigit(docstring const & str)
317 {
318         if (str.empty())
319                 return false;
320
321         docstring::const_iterator cit = str.begin();
322         docstring::const_iterator const end = str.end();
323         for (; cit != end; ++cit) {
324                 if (*cit == ' ')
325                         continue;
326                 if (isdigit((*cit)))
327                         return true;
328         }
329         return false;
330 }
331
332
333 static bool isHexChar(char_type c)
334 {
335         return c == '0' ||
336                 c == '1' ||
337                 c == '2' ||
338                 c == '3' ||
339                 c == '4' ||
340                 c == '5' ||
341                 c == '6' ||
342                 c == '7' ||
343                 c == '8' ||
344                 c == '9' ||
345                 c == 'a' || c == 'A' ||
346                 c == 'b' || c == 'B' ||
347                 c == 'c' || c == 'C' ||
348                 c == 'd' || c == 'D' ||
349                 c == 'e' || c == 'E' ||
350                 c == 'f' || c == 'F';
351 }
352
353
354 bool isHex(docstring const & str)
355 {
356         int index = 0;
357
358         if (str.length() > 2 && str[0] == '0' &&
359             (str[1] == 'x' || str[1] == 'X'))
360                 index = 2;
361
362         int const len = str.length();
363
364         for (; index < len; ++index) {
365                 if (!isHexChar(str[index]))
366                         return false;
367         }
368         return true;
369 }
370
371
372 int hexToInt(docstring const & str)
373 {
374         string s = to_ascii(str);
375         int h;
376         sscanf(s.c_str(), "%x", &h);
377         return h;
378 }
379
380
381 bool isAscii(docstring const & str)
382 {
383         int const len = str.length();
384         for (int i = 0; i < len; ++i)
385                 if (str[i] >= 0x80)
386                         return false;
387         return true;
388 }
389
390
391 bool isAscii(string const & str)
392 {
393         int const len = str.length();
394         for (int i = 0; i < len; ++i)
395                 if (static_cast<unsigned char>(str[i]) >= 0x80)
396                         return false;
397         return true;
398 }
399
400
401 char lowercase(char c)
402 {
403         LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
404         return char(tolower(c));
405 }
406
407
408 char uppercase(char c)
409 {
410         LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
411         return char(toupper(c));
412 }
413
414
415 char_type lowercase(char_type c)
416 {
417         if (!is_utf16(c))
418                 // We don't know how to lowercase a non-utf16 char
419                 return c;
420         return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
421 }
422
423
424 char_type uppercase(char_type c)
425 {
426         if (!is_utf16(c))
427                 // We don't know how to uppercase a non-utf16 char
428                 return c;
429         return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
430 }
431
432
433 bool isLowerCase(char_type ch) {
434         return lowercase(ch) == ch;
435 }
436
437
438 bool isUpperCase(char_type ch) {
439         return uppercase(ch) == ch;
440 }
441
442
443 namespace {
444
445 // since we cannot use tolower and toupper directly in the
446 // calls to transform yet, we use these helper clases. (Lgb)
447
448 struct local_lowercase {
449         char_type operator()(char_type c) const {
450                 return lowercase(c);
451         }
452 };
453
454 struct local_uppercase {
455         char_type operator()(char_type c) const {
456                 return uppercase(c);
457         }
458 };
459
460 template<typename Char> struct local_ascii_lowercase {
461         Char operator()(Char c) const { return ascii_tolower(c); }
462 };
463
464 } // end of anon namespace
465
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
673 /// Substitute all \a oldchar with \a newchar
674 docstring const subst_char(docstring const & a,
675         docstring::value_type oldchar, docstring::value_type newchar)
676 {
677         docstring tmp(a);
678         docstring::iterator lit = tmp.begin();
679         docstring::iterator end = tmp.end();
680         for (; lit != end; ++lit)
681                 if ((*lit) == oldchar)
682                         (*lit) = newchar;
683         return tmp;
684 }
685
686
687 /// substitutes all instances of \a oldstr with \a newstr
688 template<typename String> inline
689 String const subst_string(String const & a,
690                 String const & oldstr, String const & newstr)
691 {
692         LASSERT(!oldstr.empty(), /**/);
693         String lstr = a;
694         size_t i = 0;
695         size_t const olen = oldstr.length();
696         while ((i = lstr.find(oldstr, i)) != string::npos) {
697                 lstr.replace(i, olen, newstr);
698                 i += newstr.length(); // We need to be sure that we dont
699                 // use the same i over and over again.
700         }
701         return lstr;
702 }
703
704
705 docstring const subst_string(docstring const & a,
706                 docstring const & oldstr, docstring const & newstr)
707 {
708         LASSERT(!oldstr.empty(), /**/);
709         docstring lstr = a;
710         size_t i = 0;
711         size_t const olen = oldstr.length();
712         while ((i = lstr.find(oldstr, i)) != string::npos) {
713                 lstr.replace(i, olen, newstr);
714                 i += newstr.length(); // We need to be sure that we dont
715                 // use the same i over and over again.
716         }
717         return lstr;
718 }
719
720 }
721
722
723 string const subst(string const & a, char oldchar, char newchar)
724 {
725         return subst_char(a, oldchar, newchar);
726 }
727
728
729 docstring const subst(docstring const & a,
730                 char_type oldchar, char_type newchar)
731 {
732         return subst_char(a, oldchar, newchar);
733 }
734
735
736 string const subst(string const & a,
737                 string const & oldstr, string const & newstr)
738 {
739         return subst_string(a, oldstr, newstr);
740 }
741
742
743 docstring const subst(docstring const & a,
744                 docstring const & oldstr, docstring const & newstr)
745 {
746         return subst_string(a, oldstr, newstr);
747 }
748
749
750 /// Count all occurences of char \a chr inside \a str
751 int count_char(docstring const & str, docstring::value_type chr)
752 {
753         int count = 0;
754         docstring::const_iterator lit = str.begin();
755         docstring::const_iterator end = str.end();
756         for (; lit != end; ++lit)
757                 if ((*lit) == chr)
758                         count++;
759         return count;
760 }
761
762
763 docstring const trim(docstring const & a, char const * p)
764 {
765         LASSERT(p, /**/);
766
767         if (a.empty() || !*p)
768                 return a;
769
770         docstring s = from_ascii(p);
771         size_t r = a.find_last_not_of(s);
772         size_t l = a.find_first_not_of(s);
773
774         // Is this the minimal test? (lgb)
775         if (r == docstring::npos && l == docstring::npos)
776                 return docstring();
777
778         return a.substr(l, r - l + 1);
779 }
780
781
782 string const trim(string const & a, char const * p)
783 {
784         LASSERT(p, /**/);
785
786         if (a.empty() || !*p)
787                 return a;
788
789         size_t r = a.find_last_not_of(p);
790         size_t l = a.find_first_not_of(p);
791
792         // Is this the minimal test? (lgb)
793         if (r == string::npos && l == string::npos)
794                 return string();
795
796         return a.substr(l, r - l + 1);
797 }
798
799
800 string const rtrim(string const & a, char const * p)
801 {
802         LASSERT(p, /**/);
803
804         if (a.empty() || !*p)
805                 return a;
806
807         size_t r = a.find_last_not_of(p);
808
809         // Is this test really needed? (Lgb)
810         if (r == string::npos)
811                 return string();
812
813         return a.substr(0, r + 1);
814 }
815
816
817 docstring const rtrim(docstring const & a, char const * p)
818 {
819         LASSERT(p, /**/);
820
821         if (a.empty() || !*p)
822                 return a;
823
824         size_t r = a.find_last_not_of(from_ascii(p));
825
826         // Is this test really needed? (Lgb)
827         if (r == docstring::npos)
828                 return docstring();
829
830         return a.substr(0, r + 1);
831 }
832
833
834 string const ltrim(string const & a, char const * p)
835 {
836         LASSERT(p, /**/);
837         if (a.empty() || !*p)
838                 return a;
839         size_t l = a.find_first_not_of(p);
840         if (l == string::npos)
841                 return string();
842         return a.substr(l, string::npos);
843 }
844
845
846 docstring const ltrim(docstring const & a, char const * p)
847 {
848         LASSERT(p, /**/);
849         if (a.empty() || !*p)
850                 return a;
851         size_t l = a.find_first_not_of(from_ascii(p));
852         if (l == docstring::npos)
853                 return docstring();
854         return a.substr(l, docstring::npos);
855 }
856
857 namespace {
858
859 template<typename String, typename Char> inline
860 String const doSplit(String const & a, String & piece, Char delim)
861 {
862         String tmp;
863         size_t i = a.find(delim);
864         if (i == a.length() - 1) {
865                 piece = a.substr(0, i);
866         } else if (i != String::npos) {
867                 piece = a.substr(0, i);
868                 tmp = a.substr(i + 1);
869         } else if (i == 0) {
870                 piece.erase();
871                 tmp = a.substr(i + 1);
872         } else {
873                 piece = a;
874         }
875         return tmp;
876 }
877
878 template<typename Char> inline
879 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
880 {
881         docstring tmp;
882         size_t i = a.find(delim);
883         if (i == a.length() - 1) {
884                 piece = a.substr(0, i);
885         } else if (i != docstring::npos) {
886                 piece = a.substr(0, i);
887                 tmp = a.substr(i + 1);
888         } else if (i == 0) {
889                 piece.erase();
890                 tmp = a.substr(i + 1);
891         } else {
892                 piece = a;
893         }
894         return tmp;
895 }
896
897 } // anon
898
899
900 string const split(string const & a, string & piece, char delim)
901 {
902         return doSplit(a, piece, delim);
903 }
904
905
906 docstring const split(docstring const & a, docstring & piece, char_type delim)
907 {
908         return doSplit(a, piece, delim);
909 }
910
911
912 string const split(string const & a, char delim)
913 {
914         string tmp;
915         size_t i = a.find(delim);
916         if (i != string::npos) // found delim
917                 tmp = a.substr(i + 1);
918         return tmp;
919 }
920
921
922 // ale970521
923 string const rsplit(string const & a, string & piece, char delim)
924 {
925         string tmp;
926         size_t i = a.rfind(delim);
927         if (i != string::npos) { // delimiter was found
928                 piece = a.substr(0, i);
929                 tmp = a.substr(i + 1);
930         } else { // delimiter was not found
931                 piece.erase();
932         }
933         return tmp;
934 }
935
936
937 docstring const rsplit(docstring const & a, char_type delim)
938 {
939         docstring tmp;
940         size_t i = a.rfind(delim);
941         if (i != string::npos)
942                 tmp = a.substr(i + 1);
943         return tmp;
944 }
945
946
947 docstring const escape(docstring const & lab)
948 {
949         char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
950                                    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
951         docstring enc;
952         for (size_t i = 0; i < lab.length(); ++i) {
953                 char_type c = lab[i];
954                 if (c >= 128 || c == '=' || c == '%') {
955                         // Although char_type is a 32 bit type we know that
956                         // UCS4 occupies only 21 bits, so we don't need to
957                         // encode bigger values. Test for 2^24 because we
958                         // can encode that with the 6 hex digits that are
959                         // needed for 21 bits anyway.
960                         LASSERT(c < (1 << 24), /**/);
961                         enc += '=';
962                         enc += hexdigit[(c>>20) & 15];
963                         enc += hexdigit[(c>>16) & 15];
964                         enc += hexdigit[(c>>12) & 15];
965                         enc += hexdigit[(c>> 8) & 15];
966                         enc += hexdigit[(c>> 4) & 15];
967                         enc += hexdigit[ c      & 15];
968                 } else {
969                         enc += c;
970                 }
971         }
972         return enc;
973 }
974
975
976 namespace {
977
978 // this doesn't check whether str is empty, so do that first.
979 vector<docstring> wrapToVec(docstring const & str, int ind,
980                             size_t const width)
981 {
982         docstring s = trim(str);
983         if (s.empty())
984                 return vector<docstring>();
985
986         docstring indent;
987         if (ind < 0) {
988                 indent.insert(0, -ind, ' ');
989                 ind = 0;
990         } else if (ind > 0)
991                 s.insert(0, ind, ' ');
992
993         vector<docstring> retval;
994         while (s.size() > width) {
995                 // find the last space within the first 'width' chars
996                 size_t const i = s.find_last_of(' ', width - 1);
997                 if (i == docstring::npos || i <= size_t(ind)) {
998                         // no space found
999                         s = s.substr(0, width - 3) + "...";
1000                         break;
1001                 }
1002                 retval.push_back(s.substr(0, i));
1003                 s = indent + s.substr(i);
1004                 ind = indent.size();
1005         }
1006         if (!s.empty())
1007                 retval.push_back(s);
1008         return retval;
1009 }
1010
1011 }
1012
1013
1014 docstring wrap(docstring const & str, int const ind, size_t const width)
1015 {
1016         docstring s = trim(str);
1017         if (s.empty())
1018                 return docstring();
1019
1020         vector<docstring> const svec = wrapToVec(str, ind, width);
1021         return getStringFromVector(svec, from_ascii("\n"));
1022 }
1023
1024
1025 docstring wrapParas(docstring const & str, int const indent,
1026                     size_t const width, size_t const maxlines)
1027 {
1028         if (str.empty())
1029                 return docstring();
1030
1031         vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1032         vector<docstring> retval;
1033
1034         vector<docstring>::const_iterator it = pars.begin();
1035         vector<docstring>::const_iterator const en = pars.end();
1036         for (; it != en; ++it) {
1037                 vector<docstring> tmp = wrapToVec(*it, indent, width);
1038                 size_t const nlines = tmp.size();
1039                 if (nlines == 0)
1040                         continue;
1041                 size_t const curlines = retval.size();
1042                 if (maxlines > 0 && curlines + nlines >= maxlines) {
1043                         tmp.resize(maxlines - curlines - 1);
1044                         tmp.push_back(from_ascii("..."));
1045                 }
1046                 retval.insert(retval.end(), tmp.begin(), tmp.end());
1047                 if (maxlines > 0 && retval.size() >= maxlines)
1048                         break;
1049         }
1050         return getStringFromVector(retval, from_ascii("\n"));
1051 }
1052
1053
1054 namespace {
1055
1056 template<typename String> vector<String> const
1057 getVectorFromStringT(String const & str, String const & delim, bool keepempty)
1058 {
1059 // Lars would like this code to go, but for now his replacement (below)
1060 // doesn't fullfil the same function. I have, therefore, reactivated the
1061 // old code for now. Angus 11 Nov 2002.
1062 #if 1
1063         vector<String> vec;
1064         if (str.empty())
1065                 return vec;
1066         String keys = rtrim(str);
1067         while (true) {
1068                 size_t const idx = keys.find(delim);
1069                 if (idx == String::npos) {
1070                         vec.push_back(ltrim(keys));
1071                         break;
1072                 }
1073                 String const key = trim(keys.substr(0, idx));
1074                 if (!key.empty() || keepempty)
1075                         vec.push_back(key);
1076                 size_t const start = idx + delim.size();
1077                 keys = keys.substr(start);
1078         }
1079         return vec;
1080 #else
1081         typedef boost::char_separator<typename String::value_type> Separator;
1082         typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1083         Separator sep(delim.c_str());
1084         Tokenizer tokens(str, sep);
1085         return vector<String>(tokens.begin(), tokens.end());
1086 #endif
1087 }
1088
1089
1090 template<typename String> const String
1091         getStringFromVector(vector<String> const & vec, String const & delim)
1092 {
1093         String str;
1094         typename vector<String>::const_iterator it = vec.begin();
1095         typename vector<String>::const_iterator en = vec.end();
1096         for (; it != en; ++it) {
1097                 String item = trim(*it);
1098                 if (item.empty())
1099                         continue;
1100                 if (!str.empty())
1101                         str += delim;
1102                 str += item;
1103         }
1104         return str;
1105 }
1106
1107 } // namespace anon
1108
1109
1110 vector<string> const getVectorFromString(string const & str,
1111                                          string const & delim,
1112                                          bool keepempty)
1113 {
1114         return getVectorFromStringT<string>(str, delim, keepempty);
1115 }
1116
1117
1118 vector<docstring> const getVectorFromString(docstring const & str,
1119                                             docstring const & delim,
1120                                             bool keepempty)
1121 {
1122         return getVectorFromStringT<docstring>(str, delim, keepempty);
1123 }
1124
1125
1126 string const getStringFromVector(vector<string> const & vec,
1127                                  string const & delim)
1128 {
1129         return getStringFromVector<string>(vec, delim);
1130 }
1131
1132
1133 docstring const getStringFromVector(vector<docstring> const & vec,
1134                                     docstring const & delim)
1135 {
1136         return getStringFromVector<docstring>(vec, delim);
1137 }
1138
1139
1140 int findToken(char const * const str[], string const & search_token)
1141 {
1142         int i = 0;
1143
1144         while (str[i][0] && str[i] != search_token)
1145                 ++i;
1146         if (!str[i][0])
1147                 i = -1;
1148         return i;
1149 }
1150
1151
1152 template<>
1153 docstring bformat(docstring const & fmt, int arg1)
1154 {
1155         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1156         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1157         return subst(str, from_ascii("%%"), from_ascii("%"));
1158 }
1159
1160
1161 template<>
1162 docstring bformat(docstring const & fmt, long arg1)
1163 {
1164         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1165         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1166         return subst(str, from_ascii("%%"), from_ascii("%"));
1167 }
1168
1169
1170 template<>
1171 docstring bformat(docstring const & fmt, unsigned int arg1)
1172 {
1173         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1174         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1175         return subst(str, from_ascii("%%"), from_ascii("%"));
1176 }
1177
1178
1179 template<>
1180 docstring bformat(docstring const & fmt, docstring arg1)
1181 {
1182         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1183         docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1184         return subst(str, from_ascii("%%"), from_ascii("%"));
1185 }
1186
1187
1188 template<>
1189 docstring bformat(docstring const & fmt, char * arg1)
1190 {
1191         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1192         docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1193         return subst(str, from_ascii("%%"), from_ascii("%"));
1194 }
1195
1196
1197 template<>
1198 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1199 {
1200         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1201         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1202         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1203         str = subst(str, from_ascii("%2$s"), arg2);
1204         return subst(str, from_ascii("%%"), from_ascii("%"));
1205 }
1206
1207
1208 template<>
1209 docstring bformat(docstring const & fmt, docstring arg1, int arg2)
1210 {
1211         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1212         LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1213         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1214         str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1215         return subst(str, from_ascii("%%"), from_ascii("%"));
1216 }
1217
1218
1219 template<>
1220 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1221 {
1222         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1223         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1224         docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1225         str = subst(fmt, from_ascii("%2$s"), arg2);
1226         return subst(str, from_ascii("%%"), from_ascii("%"));
1227 }
1228
1229
1230 template<>
1231 docstring bformat(docstring const & fmt, int arg1, int arg2)
1232 {
1233         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1234         LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1235         docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1236         str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1237         return subst(str, from_ascii("%%"), from_ascii("%"));
1238 }
1239
1240
1241 template<>
1242 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1243 {
1244         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1245         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1246         LASSERT(contains(fmt, from_ascii("%3$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         return subst(str, from_ascii("%%"), from_ascii("%"));
1251 }
1252
1253
1254 template<>
1255 docstring bformat(docstring const & fmt,
1256                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1257 {
1258         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1259         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1260         LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1261         LASSERT(contains(fmt, from_ascii("%4$s")), /**/);
1262         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1263         str = subst(str, from_ascii("%2$s"), arg2);
1264         str = subst(str, from_ascii("%3$s"), arg3);
1265         str = subst(str, from_ascii("%4$s"), arg4);
1266         return subst(str, from_ascii("%%"), from_ascii("%"));
1267 }
1268
1269 } // namespace support
1270 } // namespace lyx