]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.cpp
BufferView.cpp: typo
[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 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 /// Substitute all \a oldchar with \a newchar
670 docstring const subst_char(docstring const & a,
671         docstring::value_type oldchar, docstring::value_type newchar)
672 {
673         docstring tmp(a);
674         docstring::iterator lit = tmp.begin();
675         docstring::iterator end = tmp.end();
676         for (; lit != end; ++lit)
677                 if ((*lit) == oldchar)
678                         (*lit) = newchar;
679         return tmp;
680 }
681
682
683 /// substitutes all instances of \a oldstr with \a newstr
684 template<typename String> inline
685 String const subst_string(String const & a,
686                 String const & oldstr, String const & newstr)
687 {
688         LASSERT(!oldstr.empty(), /**/);
689         String lstr = a;
690         size_t i = 0;
691         size_t const olen = oldstr.length();
692         while ((i = lstr.find(oldstr, i)) != string::npos) {
693                 lstr.replace(i, olen, newstr);
694                 i += newstr.length(); // We need to be sure that we dont
695                 // use the same i over and over again.
696         }
697         return lstr;
698 }
699
700 docstring const subst_string(docstring const & a,
701                 docstring const & oldstr, docstring const & newstr)
702 {
703         LASSERT(!oldstr.empty(), /**/);
704         docstring lstr = a;
705         size_t i = 0;
706         size_t const olen = oldstr.length();
707         while ((i = lstr.find(oldstr, i)) != string::npos) {
708                 lstr.replace(i, olen, newstr);
709                 i += newstr.length(); // We need to be sure that we dont
710                 // use the same i over and over again.
711         }
712         return lstr;
713 }
714
715 }
716
717
718 string const subst(string const & a, char oldchar, char newchar)
719 {
720         return subst_char(a, oldchar, newchar);
721 }
722
723
724 docstring const subst(docstring const & a,
725                 char_type oldchar, char_type newchar)
726 {
727         return subst_char(a, oldchar, newchar);
728 }
729
730
731 string const subst(string const & a,
732                 string const & oldstr, string const & newstr)
733 {
734         return subst_string(a, oldstr, newstr);
735 }
736
737
738 docstring const subst(docstring const & a,
739                 docstring const & oldstr, docstring const & newstr)
740 {
741         return subst_string(a, oldstr, newstr);
742 }
743
744
745 docstring const trim(docstring const & a, char const * p)
746 {
747         LASSERT(p, /**/);
748
749         if (a.empty() || !*p)
750                 return a;
751
752         docstring s = from_ascii(p);
753         size_t r = a.find_last_not_of(s);
754         size_t l = a.find_first_not_of(s);
755
756         // Is this the minimal test? (lgb)
757         if (r == docstring::npos && l == docstring::npos)
758                 return docstring();
759
760         return a.substr(l, r - l + 1);
761 }
762
763
764 string const trim(string const & a, char const * p)
765 {
766         LASSERT(p, /**/);
767
768         if (a.empty() || !*p)
769                 return a;
770
771         size_t r = a.find_last_not_of(p);
772         size_t l = a.find_first_not_of(p);
773
774         // Is this the minimal test? (lgb)
775         if (r == string::npos && l == string::npos)
776                 return string();
777
778         return a.substr(l, r - l + 1);
779 }
780
781
782 string const rtrim(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
791         // Is this test really needed? (Lgb)
792         if (r == string::npos)
793                 return string();
794
795         return a.substr(0, r + 1);
796 }
797
798
799 docstring const rtrim(docstring const & a, char const * p)
800 {
801         LASSERT(p, /**/);
802
803         if (a.empty() || !*p)
804                 return a;
805
806         size_t r = a.find_last_not_of(from_ascii(p));
807
808         // Is this test really needed? (Lgb)
809         if (r == docstring::npos)
810                 return docstring();
811
812         return a.substr(0, r + 1);
813 }
814
815
816 string const ltrim(string const & a, char const * p)
817 {
818         LASSERT(p, /**/);
819         if (a.empty() || !*p)
820                 return a;
821         size_t l = a.find_first_not_of(p);
822         if (l == string::npos)
823                 return string();
824         return a.substr(l, string::npos);
825 }
826
827
828 docstring const ltrim(docstring const & a, char const * p)
829 {
830         LASSERT(p, /**/);
831         if (a.empty() || !*p)
832                 return a;
833         size_t l = a.find_first_not_of(from_ascii(p));
834         if (l == docstring::npos)
835                 return docstring();
836         return a.substr(l, docstring::npos);
837 }
838
839 namespace {
840
841 template<typename String, typename Char> inline
842 String const doSplit(String const & a, String & piece, Char delim)
843 {
844         String tmp;
845         size_t i = a.find(delim);
846         if (i == a.length() - 1) {
847                 piece = a.substr(0, i);
848         } else if (i != String::npos) {
849                 piece = a.substr(0, i);
850                 tmp = a.substr(i + 1);
851         } else if (i == 0) {
852                 piece.erase();
853                 tmp = a.substr(i + 1);
854         } else {
855                 piece = a;
856         }
857         return tmp;
858 }
859
860 template<typename Char> inline
861 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
862 {
863         docstring tmp;
864         size_t i = a.find(delim);
865         if (i == a.length() - 1) {
866                 piece = a.substr(0, i);
867         } else if (i != docstring::npos) {
868                 piece = a.substr(0, i);
869                 tmp = a.substr(i + 1);
870         } else if (i == 0) {
871                 piece.erase();
872                 tmp = a.substr(i + 1);
873         } else {
874                 piece = a;
875         }
876         return tmp;
877 }
878
879 } // anon
880
881
882 string const split(string const & a, string & piece, char delim)
883 {
884         return doSplit(a, piece, delim);
885 }
886
887
888 docstring const split(docstring const & a, docstring & piece, char_type delim)
889 {
890         return doSplit(a, piece, delim);
891 }
892
893
894 string const split(string const & a, char delim)
895 {
896         string tmp;
897         size_t i = a.find(delim);
898         if (i != string::npos) // found delim
899                 tmp = a.substr(i + 1);
900         return tmp;
901 }
902
903
904 // ale970521
905 string const rsplit(string const & a, string & piece, char delim)
906 {
907         string tmp;
908         size_t i = a.rfind(delim);
909         if (i != string::npos) { // delimiter was found
910                 piece = a.substr(0, i);
911                 tmp = a.substr(i + 1);
912         } else { // delimiter was not found
913                 piece.erase();
914         }
915         return tmp;
916 }
917
918
919 docstring const rsplit(docstring const & a, char_type delim)
920 {
921         docstring tmp;
922         size_t i = a.rfind(delim);
923         if (i != string::npos)
924                 tmp = a.substr(i + 1);
925         return tmp;
926 }
927
928
929 docstring const escape(docstring const & lab)
930 {
931         char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
932                                    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
933         docstring enc;
934         for (size_t i = 0; i < lab.length(); ++i) {
935                 char_type c = lab[i];
936                 if (c >= 128 || c == '=' || c == '%') {
937                         // Although char_type is a 32 bit type we know that
938                         // UCS4 occupies only 21 bits, so we don't need to
939                         // encode bigger values. Test for 2^24 because we
940                         // can encode that with the 6 hex digits that are
941                         // needed for 21 bits anyway.
942                         LASSERT(c < (1 << 24), /**/);
943                         enc += '=';
944                         enc += hexdigit[(c>>20) & 15];
945                         enc += hexdigit[(c>>16) & 15];
946                         enc += hexdigit[(c>>12) & 15];
947                         enc += hexdigit[(c>> 8) & 15];
948                         enc += hexdigit[(c>> 4) & 15];
949                         enc += hexdigit[ c      & 15];
950                 } else {
951                         enc += c;
952                 }
953         }
954         return enc;
955 }
956
957
958 namespace {
959
960 // this doesn't check whether str is empty, so do that first.
961 vector<docstring> wrapToVec(docstring const & str, int ind,
962                             size_t const width)
963 {
964         docstring s = trim(str);
965         if (s.empty())
966                 return vector<docstring>();
967
968         docstring indent;
969         if (ind < 0) {
970                 indent.insert(0, -ind, ' ');
971                 ind = 0;
972         } else if (ind > 0)
973                 s.insert(0, ind, ' ');
974
975         vector<docstring> retval;
976         while (s.size() > width) {
977                 // find the last space within the first 'width' chars
978                 size_t const i = s.find_last_of(' ', width - 1);
979                 if (i == docstring::npos || i <= size_t(ind)) {
980                         // no space found
981                         s = s.substr(0, width - 3) + "...";
982                         break;
983                 }
984                 retval.push_back(s.substr(0, i));
985                 s = indent + s.substr(i);
986                 ind = indent.size();
987         }
988         if (!s.empty())
989                 retval.push_back(s);
990         return retval;
991 }
992
993 }
994
995
996 docstring wrap(docstring const & str, int const ind, size_t const width)
997 {
998         docstring s = trim(str);
999         if (s.empty())
1000                 return docstring();
1001
1002         vector<docstring> const svec = wrapToVec(str, ind, width);
1003         return getStringFromVector(svec, from_ascii("\n"));
1004 }
1005
1006
1007 docstring wrapParas(docstring const & str, int const indent,
1008                     size_t const width, size_t const maxlines)
1009 {
1010         if (str.empty())
1011                 return docstring();
1012
1013         vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1014         vector<docstring> retval;
1015
1016         vector<docstring>::const_iterator it = pars.begin();
1017         vector<docstring>::const_iterator const en = pars.end();
1018         for (; it != en; ++it) {
1019                 vector<docstring> tmp = wrapToVec(*it, indent, width);
1020                 size_t const nlines = tmp.size();
1021                 if (nlines == 0)
1022                         continue;
1023                 size_t const curlines = retval.size();
1024                 if (maxlines > 0 && curlines + nlines >= maxlines) {
1025                         tmp.resize(maxlines - curlines - 1);
1026                         tmp.push_back(from_ascii("..."));
1027                 }
1028                 retval.insert(retval.end(), tmp.begin(), tmp.end());
1029                 if (maxlines > 0 && retval.size() >= maxlines)
1030                         break;
1031         }
1032         return getStringFromVector(retval, from_ascii("\n"));
1033 }
1034
1035
1036 namespace {
1037
1038 template<typename String> vector<String> const
1039 getVectorFromStringT(String const & str, String const & delim, bool keepempty)
1040 {
1041 // Lars would like this code to go, but for now his replacement (below)
1042 // doesn't fullfil the same function. I have, therefore, reactivated the
1043 // old code for now. Angus 11 Nov 2002.
1044 #if 1
1045         vector<String> vec;
1046         if (str.empty())
1047                 return vec;
1048         String keys = rtrim(str);
1049         while (true) {
1050                 size_t const idx = keys.find(delim);
1051                 if (idx == String::npos) {
1052                         vec.push_back(ltrim(keys));
1053                         break;
1054                 }
1055                 String const key = trim(keys.substr(0, idx));
1056                 if (!key.empty() || keepempty)
1057                         vec.push_back(key);
1058                 size_t const start = idx + delim.size();
1059                 keys = keys.substr(start);
1060         }
1061         return vec;
1062 #else
1063         typedef boost::char_separator<typename String::value_type> Separator;
1064         typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1065         Separator sep(delim.c_str());
1066         Tokenizer tokens(str, sep);
1067         return vector<String>(tokens.begin(), tokens.end());
1068 #endif
1069 }
1070
1071
1072 template<typename String> const String
1073         getStringFromVector(vector<String> const & vec, String const & delim)
1074 {
1075         String str;
1076         typename vector<String>::const_iterator it = vec.begin();
1077         typename vector<String>::const_iterator en = vec.end();
1078         for (; it != en; ++it) {
1079                 String item = trim(*it);
1080                 if (item.empty())
1081                         continue;
1082                 if (!str.empty())
1083                         str += delim;
1084                 str += item;
1085         }
1086         return str;
1087 }
1088
1089 } // namespace anon
1090
1091
1092 vector<string> const getVectorFromString(string const & str,
1093                                          string const & delim,
1094                                          bool keepempty)
1095 {
1096         return getVectorFromStringT<string>(str, delim, keepempty);
1097 }
1098
1099
1100 vector<docstring> const getVectorFromString(docstring const & str,
1101                                             docstring const & delim,
1102                                             bool keepempty)
1103 {
1104         return getVectorFromStringT<docstring>(str, delim, keepempty);
1105 }
1106
1107
1108 string const getStringFromVector(vector<string> const & vec,
1109                                  string const & delim)
1110 {
1111         return getStringFromVector<string>(vec, delim);
1112 }
1113
1114
1115 docstring const getStringFromVector(vector<docstring> const & vec,
1116                                     docstring const & delim)
1117 {
1118         return getStringFromVector<docstring>(vec, delim);
1119 }
1120
1121
1122 int findToken(char const * const str[], string const & search_token)
1123 {
1124         int i = 0;
1125
1126         while (str[i][0] && str[i] != search_token)
1127                 ++i;
1128         if (!str[i][0])
1129                 i = -1;
1130         return i;
1131 }
1132
1133
1134 template<>
1135 docstring bformat(docstring const & fmt, int arg1)
1136 {
1137         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1138         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1139         return subst(str, from_ascii("%%"), from_ascii("%"));
1140 }
1141
1142
1143 template<>
1144 docstring bformat(docstring const & fmt, long arg1)
1145 {
1146         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1147         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1148         return subst(str, from_ascii("%%"), from_ascii("%"));
1149 }
1150
1151
1152 template<>
1153 docstring bformat(docstring const & fmt, unsigned 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, docstring arg1)
1163 {
1164         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1165         docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1166         return subst(str, from_ascii("%%"), from_ascii("%"));
1167 }
1168
1169
1170 template<>
1171 docstring bformat(docstring const & fmt, char * arg1)
1172 {
1173         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1174         docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1175         return subst(str, from_ascii("%%"), from_ascii("%"));
1176 }
1177
1178
1179 template<>
1180 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1181 {
1182         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1183         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1184         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1185         str = subst(str, from_ascii("%2$s"), arg2);
1186         return subst(str, from_ascii("%%"), from_ascii("%"));
1187 }
1188
1189
1190 template<>
1191 docstring bformat(docstring const & fmt, docstring arg1, int arg2)
1192 {
1193         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1194         LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1195         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1196         str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1197         return subst(str, from_ascii("%%"), from_ascii("%"));
1198 }
1199
1200
1201 template<>
1202 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1203 {
1204         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1205         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1206         docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1207         str = subst(fmt, from_ascii("%2$s"), arg2);
1208         return subst(str, from_ascii("%%"), from_ascii("%"));
1209 }
1210
1211
1212 template<>
1213 docstring bformat(docstring const & fmt, int arg1, int arg2)
1214 {
1215         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1216         LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1217         docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1218         str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1219         return subst(str, from_ascii("%%"), from_ascii("%"));
1220 }
1221
1222
1223 template<>
1224 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1225 {
1226         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1227         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1228         LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1229         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1230         str = subst(str, from_ascii("%2$s"), arg2);
1231         str = subst(str, from_ascii("%3$s"), arg3);
1232         return subst(str, from_ascii("%%"), from_ascii("%"));
1233 }
1234
1235
1236 template<>
1237 docstring bformat(docstring const & fmt,
1238                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1239 {
1240         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1241         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1242         LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1243         LASSERT(contains(fmt, from_ascii("%4$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         str = subst(str, from_ascii("%4$s"), arg4);
1248         return subst(str, from_ascii("%%"), from_ascii("%"));
1249 }
1250
1251 } // namespace support
1252 } // namespace lyx