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