]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.cpp
9e5301fc004991e406b365e7ff4cce69f62e1943
[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/gettext.h"
19 #include "support/qstring_helpers.h"
20 #include "support/textutils.h"
21
22 #include <boost/tokenizer.hpp>
23 #include "support/lassert.h"
24
25 #include <QString>
26 #include <QVector>
27
28 #include <cstdio>
29 #include <algorithm>
30
31 using namespace std;
32
33 namespace lyx {
34
35 // Using this allows us to have docstring default arguments in headers
36 // without #include "support/docstring" there.
37 docstring const & empty_docstring()
38 {
39         static docstring s;
40         return s;
41 }
42
43 // Using this allows us to have string default arguments in headers
44 // without #include <string>
45 string const & empty_string()
46 {
47         static string s;
48         return s;
49 }
50
51 namespace {
52 /**
53  * Convert a QChar into a UCS4 character.
54  * This is a hack (it does only make sense for the common part of the UCS4
55  * and UTF16 encodings) and should not be used.
56  * This does only exist because of performance reasons (a real conversion
57  * using iconv is too slow on windows).
58  */
59 inline char_type qchar_to_ucs4(QChar const & qchar)
60 {
61         LASSERT(is_utf16(static_cast<char_type>(qchar.unicode())), /**/);
62         return static_cast<char_type>(qchar.unicode());
63 }
64
65 /**
66  * Convert a UCS4 character into a QChar.
67  * This is a hack (it does only make sense for the common part of the UCS4
68  * and UTF16 encodings) and should not be used.
69  * This does only exist because of performance reasons (a real conversion
70  * using iconv is too slow on windows).
71  */
72 inline QChar const ucs4_to_qchar(char_type const ucs4)
73 {
74         LASSERT(is_utf16(ucs4), /**/);
75         return QChar(static_cast<unsigned short>(ucs4));
76 }
77
78 /// Maximum valid UCS4 code point
79 char_type const ucs4_max = 0x10ffff;
80 } // anon namespace
81
82
83 bool isLetterChar(char_type c)
84 {
85         if (!is_utf16(c)) {
86                 if (c > ucs4_max)
87                         // outside the UCS4 range
88                         return false;
89                 // assume that all non-utf16 characters are letters
90                 return true;
91         }
92         return ucs4_to_qchar(c).isLetter();
93 }
94
95
96 bool isLower(char_type c)
97 {
98         if (!is_utf16(c))
99                 return false;
100         return ucs4_to_qchar(c).isLower();
101 }
102
103
104 bool isAlphaASCII(char_type c)
105 {
106         return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
107 }
108
109
110 bool isPrintable(char_type c)
111 {
112         if (!is_utf16(c)) {
113                 if (c > ucs4_max)
114                         // outside the UCS4 range
115                         return false;
116                 // assume that all non-utf16 characters are printable
117                 return true;
118         }
119         return ucs4_to_qchar(c).isPrint();
120 }
121
122
123 bool isPrintableNonspace(char_type c)
124 {
125         if (!is_utf16(c)) {
126                 if (c > ucs4_max)
127                         // outside the UCS4 range
128                         return false;
129                 // assume that all non-utf16 characters are printable and
130                 // no space
131                 return true;
132         }
133         QChar const qc = ucs4_to_qchar(c);
134         return qc.isPrint() && !qc.isSpace();
135 }
136
137
138 bool isSpace(char_type c)
139 {
140         if (!is_utf16(c)) {
141                 // assume that no non-utf16 character is a space
142                 // c outside the UCS4 range is catched as well
143                 return false;
144         }
145         QChar const qc = ucs4_to_qchar(c);
146         return qc.isSpace();
147 }
148
149
150 bool isDigit(char_type c)
151 {
152         if (!is_utf16(c))
153                 // assume that no non-utf16 character is a digit
154                 // c outside the UCS4 range is catched as well
155                 return false;
156         return ucs4_to_qchar(c).isDigit();
157 }
158
159
160 bool isDigitASCII(char_type c)
161 {
162         return '0' <= c && c <= '9';
163 }
164
165
166 bool isAlnumASCII(char_type c)
167 {
168         return ('0' <= c && c <= '9')
169                 || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
170 }
171
172
173 namespace support {
174
175 int compare_no_case(docstring const & s, docstring const & s2)
176 {
177         docstring::const_iterator p = s.begin();
178         docstring::const_iterator p2 = s2.begin();
179
180         while (p != s.end() && p2 != s2.end()) {
181                 char_type const lc1 = lowercase(*p);
182                 char_type const lc2 = lowercase(*p2);
183                 if (lc1 != lc2)
184                         return (lc1 < lc2) ? -1 : 1;
185                 ++p;
186                 ++p2;
187         }
188
189         if (s.size() == s2.size())
190                 return 0;
191         if (s.size() < s2.size())
192                 return -1;
193         return 1;
194 }
195
196
197 namespace {
198
199 template<typename Char>
200 Char ascii_tolower(Char c) {
201         if (c >= 'A' && c <= 'Z')
202                 return c - 'A' + 'a';
203         return c;
204 }
205
206 }
207
208
209 int compare_ascii_no_case(string const & s, string const & s2)
210 {
211         string::const_iterator p = s.begin();
212         string::const_iterator p2 = s2.begin();
213
214         while (p != s.end() && p2 != s2.end()) {
215                 int const lc1 = ascii_tolower(*p);
216                 int const lc2 = ascii_tolower(*p2);
217                 if (lc1 != lc2)
218                         return (lc1 < lc2) ? -1 : 1;
219                 ++p;
220                 ++p2;
221         }
222
223         if (s.size() == s2.size())
224                 return 0;
225         if (s.size() < s2.size())
226                 return -1;
227         return 1;
228 }
229
230
231 int compare_ascii_no_case(docstring const & s, docstring const & s2)
232 {
233         docstring::const_iterator p = s.begin();
234         docstring::const_iterator p2 = s2.begin();
235
236         while (p != s.end() && p2 != s2.end()) {
237                 char_type const lc1 = ascii_tolower(*p);
238                 char_type const lc2 = ascii_tolower(*p2);
239                 if (lc1 != lc2)
240                         return (lc1 < lc2) ? -1 : 1;
241                 ++p;
242                 ++p2;
243         }
244
245         if (s.size() == s2.size())
246                 return 0;
247         if (s.size() < s2.size())
248                 return -1;
249         return 1;
250 }
251
252
253 bool isStrInt(string const & str)
254 {
255         if (str.empty())
256                 return false;
257
258         // Remove leading and trailing white space chars.
259         string const tmpstr = trim(str);
260         if (tmpstr.empty())
261                 return false;
262
263         string::const_iterator cit = tmpstr.begin();
264         if ((*cit) == '-')
265                 ++cit;
266
267         string::const_iterator end = tmpstr.end();
268         for (; cit != end; ++cit)
269                 if (!isdigit((*cit)))
270                         return false;
271
272         return true;
273 }
274
275
276 bool isStrUnsignedInt(string const & str)
277 {
278         if (str.empty())
279                 return false;
280
281         // Remove leading and trailing white space chars.
282         string const tmpstr = trim(str);
283         if (tmpstr.empty())
284                 return false;
285
286         string::const_iterator cit = tmpstr.begin();
287         string::const_iterator end = tmpstr.end();
288         for (; cit != end; ++cit)
289                 if (!isdigit((*cit)))
290                         return false;
291
292         return true;
293 }
294
295
296 bool isStrDbl(string const & str)
297 {
298         if (str.empty())
299                 return false;
300
301         // Remove leading and trailing white space chars.
302         string const tmpstr = trim(str);
303         if (tmpstr.empty())
304                 return false;
305         //      if (tmpstr.count('.') > 1) return false;
306
307         string::const_iterator cit = tmpstr.begin();
308         bool found_dot = false;
309         if (*cit == '-')
310                 ++cit;
311         string::const_iterator end = tmpstr.end();
312         for (; cit != end; ++cit) {
313                 if (!isdigit(*cit) && *cit != '.')
314                         return false;
315                 if ('.' == (*cit)) {
316                         if (found_dot)
317                                 return false;
318                         found_dot = true;
319                 }
320         }
321         return true;
322 }
323
324
325 bool hasDigit(docstring const & str)
326 {
327         if (str.empty())
328                 return false;
329
330         docstring::const_iterator cit = str.begin();
331         docstring::const_iterator const end = str.end();
332         for (; cit != end; ++cit) {
333                 if (*cit == ' ')
334                         continue;
335                 if (isdigit((*cit)))
336                         return true;
337         }
338         return false;
339 }
340
341
342 static bool isHexChar(char_type c)
343 {
344         return c == '0' ||
345                 c == '1' ||
346                 c == '2' ||
347                 c == '3' ||
348                 c == '4' ||
349                 c == '5' ||
350                 c == '6' ||
351                 c == '7' ||
352                 c == '8' ||
353                 c == '9' ||
354                 c == 'a' || c == 'A' ||
355                 c == 'b' || c == 'B' ||
356                 c == 'c' || c == 'C' ||
357                 c == 'd' || c == 'D' ||
358                 c == 'e' || c == 'E' ||
359                 c == 'f' || c == 'F';
360 }
361
362
363 bool isHex(docstring const & str)
364 {
365         int index = 0;
366
367         if (str.length() > 2 && str[0] == '0' &&
368             (str[1] == 'x' || str[1] == 'X'))
369                 index = 2;
370
371         int const len = str.length();
372
373         for (; index < len; ++index) {
374                 if (!isHexChar(str[index]))
375                         return false;
376         }
377         return true;
378 }
379
380
381 int hexToInt(docstring const & str)
382 {
383         string s = to_ascii(str);
384         int h;
385         sscanf(s.c_str(), "%x", &h);
386         return h;
387 }
388
389
390 bool isAscii(docstring const & str)
391 {
392         int const len = str.length();
393         for (int i = 0; i < len; ++i)
394                 if (str[i] >= 0x80)
395                         return false;
396         return true;
397 }
398
399
400 bool isAscii(string const & str)
401 {
402         int const len = str.length();
403         for (int i = 0; i < len; ++i)
404                 if (static_cast<unsigned char>(str[i]) >= 0x80)
405                         return false;
406         return true;
407 }
408
409
410 char lowercase(char c)
411 {
412         LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
413         return char(tolower(c));
414 }
415
416
417 char uppercase(char c)
418 {
419         LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
420         return char(toupper(c));
421 }
422
423
424 char_type lowercase(char_type c)
425 {
426         if (!is_utf16(c))
427                 // We don't know how to lowercase a non-utf16 char
428                 return c;
429         return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
430 }
431
432
433 char_type uppercase(char_type c)
434 {
435         if (!is_utf16(c))
436                 // We don't know how to uppercase a non-utf16 char
437                 return c;
438         return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
439 }
440
441
442 bool isLowerCase(char_type ch) {
443         return lowercase(ch) == ch;
444 }
445
446
447 bool isUpperCase(char_type ch) {
448         return uppercase(ch) == ch;
449 }
450
451
452 namespace {
453
454 // since we cannot use tolower and toupper directly in the
455 // calls to transform yet, we use these helper clases. (Lgb)
456
457 struct local_lowercase {
458         char_type operator()(char_type c) const {
459                 return lowercase(c);
460         }
461 };
462
463 struct local_uppercase {
464         char_type operator()(char_type c) const {
465                 return uppercase(c);
466         }
467 };
468
469 template<typename Char> struct local_ascii_lowercase {
470         Char operator()(Char c) const { return ascii_tolower(c); }
471 };
472
473 } // end of anon namespace
474
475
476 docstring const lowercase(docstring const & a)
477 {
478         docstring tmp(a);
479         transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
480         return tmp;
481 }
482
483
484 docstring const uppercase(docstring const & a)
485 {
486         docstring tmp(a);
487         transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
488         return tmp;
489 }
490
491
492 string const ascii_lowercase(string const & a)
493 {
494         string tmp(a);
495         transform(tmp.begin(), tmp.end(), tmp.begin(),
496                   local_ascii_lowercase<char>());
497         return tmp;
498 }
499
500
501 docstring const ascii_lowercase(docstring const & a)
502 {
503         docstring tmp(a);
504         transform(tmp.begin(), tmp.end(), tmp.begin(),
505                   local_ascii_lowercase<char_type>());
506         return tmp;
507 }
508
509
510 char_type superscript(char_type c)
511 {
512         switch (c) {
513                 case    '2': return 0x00b2;
514                 case    '3': return 0x00b3;
515                 case    '1': return 0x00b9;
516                 case    '0': return 0x2070;
517                 case    'i': return 0x2071;
518                 case    '4': return 0x2074;
519                 case    '5': return 0x2075;
520                 case    '6': return 0x2076;
521                 case    '7': return 0x2077;
522                 case    '8': return 0x2078;
523                 case    '9': return 0x2079;
524                 case    '+': return 0x207a;
525                 case    '-': return 0x207b;
526                 case    '=': return 0x207c;
527                 case    '(': return 0x207d;
528                 case    ')': return 0x207e;
529                 case    'n': return 0x207f;
530                 case    'h': return 0x02b0;
531                 case 0x0266: return 0x02b1; // LATIN SMALL LETTER H WITH HOOK
532                 case    'j': return 0x02b2;
533                 case    'r': return 0x02b3;
534                 case 0x0279: return 0x02b4; // LATIN SMALL LETTER TURNED R
535                 case 0x027b: return 0x02b5; // LATIN SMALL LETTER TURNED R WITH HOOK
536                 case 0x0281: return 0x02b6; // LATIN SMALL LETTER CAPITAL INVERTED R
537                 case    'w': return 0x02b7;
538                 case    'y': return 0x02b8;
539 //              case 0x0294: return 0x02c0; // LATIN LETTER GLOTTAL STOP)
540 //              case 0x0295: return 0x02c1; // LATIN LETTER PHARYNGEAL VOICED FRICATIVE
541                                             // (= LATIN LETTER REVERSED GLOTTAL STOP)
542                 case    'l': return 0x02e1;
543                 case    's': return 0x02e2;
544                 case    'x': return 0x02e3;
545 //              case 0x0295: return 0x02e4; // LATIN SMALL LETTER REVERSED GLOTTAL STOP
546                 case    'A': return 0x1d2c;
547                 case 0x00c6: return 0x1d2d; // LATIN CAPITAL LETTER AE
548                 case    'B': return 0x1d2e;
549                 case    'D': return 0x1d30;
550                 case    'E': return 0x1d31;
551                 case    'G': return 0x1d33;
552                 case    'H': return 0x1d34;
553                 case    'I': return 0x1d35;
554                 case    'J': return 0x1d36;
555                 case    'K': return 0x1d37;
556                 case    'L': return 0x1d38;
557                 case    'M': return 0x1d39;
558                 case    'N': return 0x1d3a;
559                 case    'O': return 0x1d3c;
560                 case    'P': return 0x1d3e;
561                 case    'R': return 0x1d3f;
562                 case    'T': return 0x1d40;
563                 case    'U': return 0x1d41;
564                 case    'W': return 0x1d42;
565                 case    'a': return 0x1d43;
566                 case 0x0250: return 0x1d44; // LATIN SMALL LETTER TURNED A
567                 case 0x0251: return 0x1d45; // LATIN SMALL LETTER ALPHA
568                 case    'b': return 0x1d47;
569                 case    'd': return 0x1d48;
570                 case    'e': return 0x1d49;
571                 case 0x0259: return 0x1d4a; // LATIN SMALL LETTER SCHWA
572                 case 0x025b: return 0x1d4b; // LATIN SMALL LETTER OPEN E
573                 case 0x1d08: return 0x1d4c; // LATIN SMALL LETTER TURNED OPEN E
574                 case    'g': return 0x1d4d;
575                 case 0x1d09: return 0x1d4e; // LATIN SMALL LETTER TURNED I
576                 case    'k': return 0x1d4f;
577                 case    'm': return 0x1d50;
578                 case 0x014b: return 0x1d51; // LATIN SMALL LETTER ENG
579                 case    'o': return 0x1d52;
580                 case 0x0254: return 0x1d53; // LATIN SMALL LETTER OPEN O
581                 case 0x1d16: return 0x1d54; // LATIN SMALL LETTER TOP HALF O
582                 case 0x1d17: return 0x1d55; // LATIN SMALL LETTER BOTTOM HALF O
583                 case    'p': return 0x1d56;
584                 case    't': return 0x1d57;
585                 case    'u': return 0x1d58;
586                 case 0x1d1d: return 0x1d59; // LATIN SMALL LETTER SIDEWAYS U
587                 case 0x1d1f: return 0x1d5a; // LATIN SMALL LETTER SIDEWAYS TURNED M
588                 case    'v': return 0x1d5b;
589                 case 0x03b2: return 0x1d5d; // GREEK SMALL LETTER BETA
590                 case 0x03b3: return 0x1d5e; // GREEK SMALL LETTER GAMMA
591                 case 0x03b4: return 0x1d5f; // GREEK SMALL LETTER DELTA
592                 case 0x03c6: return 0x1d60; // GREEK SMALL LETTER PHI
593                 case 0x03c7: return 0x1d61; // GREEK SMALL LETTER CHI
594         }
595         return c;
596 }
597
598
599 char_type subscript(char_type c)
600 {
601         switch (c) {
602                 case    'i': return 0x1d62;
603                 case    'r': return 0x1d63;
604                 case    'u': return 0x1d64;
605                 case    'v': return 0x1d65;
606                 case 0x03b2: return 0x1d66; // GREEK SMALL LETTER BETA
607                 case 0x03b3: return 0x1d67; // GREEK SMALL LETTER GAMMA
608                 case 0x03c1: return 0x1d68; // GREEK SMALL LETTER RHO
609                 case 0x03c6: return 0x1d69; // GREEK SMALL LETTER PHI
610                 case 0x03c7: return 0x1d6a; // GREEK SMALL LETTER CHI
611                 case    '0': return 0x2080;
612                 case    '1': return 0x2081;
613                 case    '2': return 0x2082;
614                 case    '3': return 0x2083;
615                 case    '4': return 0x2084;
616                 case    '5': return 0x2085;
617                 case    '6': return 0x2086;
618                 case    '7': return 0x2087;
619                 case    '8': return 0x2088;
620                 case    '9': return 0x2089;
621                 case    '+': return 0x208a;
622                 case    '-': return 0x208b;
623                 case    '=': return 0x208c;
624                 case    '(': return 0x208d;
625                 case    ')': return 0x208e;
626                 case    'a': return 0x2090;
627                 case    'e': return 0x2091;
628                 case    'o': return 0x2092;
629                 case    'x': return 0x2093;
630                 case 0x0259: return 0x2093; // LATIN SMALL LETTER SCHWA
631         }
632         return c;
633 }
634
635
636 bool prefixIs(docstring const & a, char_type c)
637 {
638         if (a.empty())
639                 return false;
640         return a[0] == c;
641 }
642
643
644 bool prefixIs(string const & a, string const & pre)
645 {
646         size_t const prelen = pre.length();
647         size_t const alen = a.length();
648         return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
649 }
650
651
652 bool prefixIs(docstring const & a, docstring const & pre)
653 {
654         size_t const prelen = pre.length();
655         size_t const alen = a.length();
656         return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
657 }
658
659
660 bool suffixIs(string const & a, char c)
661 {
662         if (a.empty())
663                 return false;
664         return a[a.length() - 1] == c;
665 }
666
667
668 bool suffixIs(docstring const & a, char_type c)
669 {
670         if (a.empty())
671                 return false;
672         return a[a.length() - 1] == c;
673 }
674
675
676 bool suffixIs(string const & a, string const & suf)
677 {
678         size_t const suflen = suf.length();
679         size_t const alen = a.length();
680         return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
681 }
682
683
684 bool suffixIs(docstring const & a, docstring const & suf)
685 {
686         size_t const suflen = suf.length();
687         size_t const alen = a.length();
688         return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
689 }
690
691
692 bool containsOnly(string const & s, string const & cset)
693 {
694         return s.find_first_not_of(cset) == string::npos;
695 }
696
697
698 // ale970405+lasgoutt-970425
699 // rewritten to use new string (Lgb)
700 string const token(string const & a, char delim, int n)
701 {
702         if (a.empty())
703                 return string();
704
705         size_t k = 0;
706         size_t i = 0;
707
708         // Find delimiter or end of string
709         for (; n--;) {
710                 if ((i = a.find(delim, i)) == string::npos)
711                         break;
712                 else
713                         ++i; // step delim
714         }
715
716         // i is now the n'th delim (or string::npos)
717         if (i == string::npos)
718                 return string();
719
720         k = a.find(delim, i);
721         // k is now the n'th + 1 delim (or string::npos)
722
723         return a.substr(i, k - i);
724 }
725
726
727 docstring const token(docstring const & a, char_type delim, int n)
728 {
729         if (a.empty())
730                 return docstring();
731
732         size_t k = 0;
733         size_t i = 0;
734
735         // Find delimiter or end of string
736         for (; n--;) {
737                 if ((i = a.find(delim, i)) == docstring::npos)
738                         break;
739                 else
740                         ++i; // step delim
741         }
742
743         // i is now the n'th delim (or string::npos)
744         if (i == docstring::npos)
745                 return docstring();
746
747         k = a.find(delim, i);
748         // k is now the n'th + 1 delim (or string::npos)
749
750         return a.substr(i, k - i);
751 }
752
753
754 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
755 // rewritten to use new string (Lgb)
756 int tokenPos(string const & a, char delim, string const & tok)
757 {
758         int i = 0;
759         string str = a;
760         string tmptok;
761
762         while (!str.empty()) {
763                 str = split(str, tmptok, delim);
764                 if (tok == tmptok)
765                         return i;
766                 ++i;
767         }
768         return -1;
769 }
770
771
772 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
773 // rewritten to use new string (Lgb)
774 int tokenPos(docstring const & a, char_type delim, docstring const & tok)
775 {
776         int i = 0;
777         docstring str = a;
778         docstring tmptok;
779
780         while (!str.empty()) {
781                 str = split(str, tmptok, delim);
782                 if (tok == tmptok)
783                         return i;
784                 ++i;
785         }
786         return -1;
787 }
788
789
790 namespace {
791
792 /// Substitute all \a oldchar with \a newchar
793 template<typename Ch> inline
794 basic_string<Ch> const subst_char(basic_string<Ch> const & a,
795                 Ch oldchar, Ch newchar)
796 {
797         typedef basic_string<Ch> String;
798         String tmp(a);
799         typename String::iterator lit = tmp.begin();
800         typename String::iterator end = tmp.end();
801         for (; lit != end; ++lit)
802                 if ((*lit) == oldchar)
803                         (*lit) = newchar;
804         return tmp;
805 }
806
807
808 /// Substitute all \a oldchar with \a newchar
809 docstring const subst_char(docstring const & a,
810         docstring::value_type oldchar, docstring::value_type newchar)
811 {
812         docstring tmp(a);
813         docstring::iterator lit = tmp.begin();
814         docstring::iterator end = tmp.end();
815         for (; lit != end; ++lit)
816                 if ((*lit) == oldchar)
817                         (*lit) = newchar;
818         return tmp;
819 }
820
821
822 /// substitutes all instances of \a oldstr with \a newstr
823 template<typename String> inline
824 String const subst_string(String const & a,
825                 String const & oldstr, String const & newstr)
826 {
827         LASSERT(!oldstr.empty(), /**/);
828         String lstr = a;
829         size_t i = 0;
830         size_t const olen = oldstr.length();
831         while ((i = lstr.find(oldstr, i)) != string::npos) {
832                 lstr.replace(i, olen, newstr);
833                 i += newstr.length(); // We need to be sure that we dont
834                 // use the same i over and over again.
835         }
836         return lstr;
837 }
838
839
840 docstring const subst_string(docstring const & a,
841                 docstring const & oldstr, docstring const & newstr)
842 {
843         LASSERT(!oldstr.empty(), /**/);
844         docstring lstr = a;
845         size_t i = 0;
846         size_t const olen = oldstr.length();
847         while ((i = lstr.find(oldstr, i)) != string::npos) {
848                 lstr.replace(i, olen, newstr);
849                 i += newstr.length(); // We need to be sure that we dont
850                 // use the same i over and over again.
851         }
852         return lstr;
853 }
854
855 }
856
857
858 string const subst(string const & a, char oldchar, char newchar)
859 {
860         return subst_char(a, oldchar, newchar);
861 }
862
863
864 docstring const subst(docstring const & a,
865                 char_type oldchar, char_type newchar)
866 {
867         return subst_char(a, oldchar, newchar);
868 }
869
870
871 string const subst(string const & a,
872                 string const & oldstr, string const & newstr)
873 {
874         return subst_string(a, oldstr, newstr);
875 }
876
877
878 docstring const subst(docstring const & a,
879                 docstring const & oldstr, docstring const & newstr)
880 {
881         return subst_string(a, oldstr, newstr);
882 }
883
884
885 /// Count all occurences of char \a chr inside \a str
886 int count_char(docstring const & str, docstring::value_type chr)
887 {
888         int count = 0;
889         docstring::const_iterator lit = str.begin();
890         docstring::const_iterator end = str.end();
891         for (; lit != end; ++lit)
892                 if ((*lit) == chr)
893                         count++;
894         return count;
895 }
896
897
898 docstring const trim(docstring const & a, char const * p)
899 {
900         LASSERT(p, /**/);
901
902         if (a.empty() || !*p)
903                 return a;
904
905         docstring s = from_ascii(p);
906         size_t r = a.find_last_not_of(s);
907         size_t l = a.find_first_not_of(s);
908
909         // Is this the minimal test? (lgb)
910         if (r == docstring::npos && l == docstring::npos)
911                 return docstring();
912
913         return a.substr(l, r - l + 1);
914 }
915
916
917 string const trim(string const & a, char const * p)
918 {
919         LASSERT(p, /**/);
920
921         if (a.empty() || !*p)
922                 return a;
923
924         size_t r = a.find_last_not_of(p);
925         size_t l = a.find_first_not_of(p);
926
927         // Is this the minimal test? (lgb)
928         if (r == string::npos && l == string::npos)
929                 return string();
930
931         return a.substr(l, r - l + 1);
932 }
933
934
935 string const rtrim(string const & a, char const * p)
936 {
937         LASSERT(p, /**/);
938
939         if (a.empty() || !*p)
940                 return a;
941
942         size_t r = a.find_last_not_of(p);
943
944         // Is this test really needed? (Lgb)
945         if (r == string::npos)
946                 return string();
947
948         return a.substr(0, r + 1);
949 }
950
951
952 docstring const rtrim(docstring const & a, char const * p)
953 {
954         LASSERT(p, /**/);
955
956         if (a.empty() || !*p)
957                 return a;
958
959         size_t r = a.find_last_not_of(from_ascii(p));
960
961         // Is this test really needed? (Lgb)
962         if (r == docstring::npos)
963                 return docstring();
964
965         return a.substr(0, r + 1);
966 }
967
968
969 string const ltrim(string const & a, char const * p)
970 {
971         LASSERT(p, /**/);
972         if (a.empty() || !*p)
973                 return a;
974         size_t l = a.find_first_not_of(p);
975         if (l == string::npos)
976                 return string();
977         return a.substr(l, string::npos);
978 }
979
980
981 docstring const ltrim(docstring const & a, char const * p)
982 {
983         LASSERT(p, /**/);
984         if (a.empty() || !*p)
985                 return a;
986         size_t l = a.find_first_not_of(from_ascii(p));
987         if (l == docstring::npos)
988                 return docstring();
989         return a.substr(l, docstring::npos);
990 }
991
992 namespace {
993
994 template<typename String, typename Char> inline
995 String const doSplit(String const & a, String & piece, Char delim)
996 {
997         String tmp;
998         size_t i = a.find(delim);
999         if (i == a.length() - 1) {
1000                 piece = a.substr(0, i);
1001         } else if (i != String::npos) {
1002                 piece = a.substr(0, i);
1003                 tmp = a.substr(i + 1);
1004         } else if (i == 0) {
1005                 piece.erase();
1006                 tmp = a.substr(i + 1);
1007         } else {
1008                 piece = a;
1009         }
1010         return tmp;
1011 }
1012
1013 template<typename Char> inline
1014 docstring const doSplit(docstring const & a, docstring & piece, Char delim)
1015 {
1016         docstring tmp;
1017         size_t i = a.find(delim);
1018         if (i == a.length() - 1) {
1019                 piece = a.substr(0, i);
1020         } else if (i != docstring::npos) {
1021                 piece = a.substr(0, i);
1022                 tmp = a.substr(i + 1);
1023         } else if (i == 0) {
1024                 piece.erase();
1025                 tmp = a.substr(i + 1);
1026         } else {
1027                 piece = a;
1028         }
1029         return tmp;
1030 }
1031
1032 } // anon
1033
1034
1035 string const split(string const & a, string & piece, char delim)
1036 {
1037         return doSplit(a, piece, delim);
1038 }
1039
1040
1041 docstring const split(docstring const & a, docstring & piece, char_type delim)
1042 {
1043         return doSplit(a, piece, delim);
1044 }
1045
1046
1047 string const split(string const & a, char delim)
1048 {
1049         string tmp;
1050         size_t i = a.find(delim);
1051         if (i != string::npos) // found delim
1052                 tmp = a.substr(i + 1);
1053         return tmp;
1054 }
1055
1056
1057 // ale970521
1058 string const rsplit(string const & a, string & piece, char delim)
1059 {
1060         string tmp;
1061         size_t i = a.rfind(delim);
1062         if (i != string::npos) { // delimiter was found
1063                 piece = a.substr(0, i);
1064                 tmp = a.substr(i + 1);
1065         } else { // delimiter was not found
1066                 piece.erase();
1067         }
1068         return tmp;
1069 }
1070
1071
1072 docstring const rsplit(docstring const & a, char_type delim)
1073 {
1074         docstring tmp;
1075         size_t i = a.rfind(delim);
1076         if (i != string::npos)
1077                 tmp = a.substr(i + 1);
1078         return tmp;
1079 }
1080
1081
1082 docstring const escape(docstring const & lab)
1083 {
1084         char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
1085                                    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1086         docstring enc;
1087         for (size_t i = 0; i < lab.length(); ++i) {
1088                 char_type c = lab[i];
1089                 if (c >= 128 || c == '=' || c == '%') {
1090                         // Although char_type is a 32 bit type we know that
1091                         // UCS4 occupies only 21 bits, so we don't need to
1092                         // encode bigger values. Test for 2^24 because we
1093                         // can encode that with the 6 hex digits that are
1094                         // needed for 21 bits anyway.
1095                         LASSERT(c < (1 << 24), /**/);
1096                         enc += '=';
1097                         enc += hexdigit[(c>>20) & 15];
1098                         enc += hexdigit[(c>>16) & 15];
1099                         enc += hexdigit[(c>>12) & 15];
1100                         enc += hexdigit[(c>> 8) & 15];
1101                         enc += hexdigit[(c>> 4) & 15];
1102                         enc += hexdigit[ c      & 15];
1103                 } else {
1104                         enc += c;
1105                 }
1106         }
1107         return enc;
1108 }
1109
1110
1111 namespace {
1112
1113 // this doesn't check whether str is empty, so do that first.
1114 vector<docstring> wrapToVec(docstring const & str, int ind,
1115                             size_t const width)
1116 {
1117         docstring s = trim(str);
1118         if (s.empty())
1119                 return vector<docstring>();
1120
1121         docstring indent;
1122         if (ind < 0) {
1123                 indent.insert(0, -ind, ' ');
1124                 ind = 0;
1125         } else if (ind > 0)
1126                 s.insert(0, ind, ' ');
1127
1128         vector<docstring> retval;
1129         while (s.size() > width) {
1130                 // find the last space within the first 'width' chars
1131                 size_t const i = s.find_last_of(' ', width - 1);
1132                 if (i == docstring::npos || i <= size_t(ind)) {
1133                         // no space found
1134                         s = s.substr(0, width - 3) + "...";
1135                         break;
1136                 }
1137                 retval.push_back(s.substr(0, i));
1138                 s = indent + s.substr(i);
1139                 ind = indent.size();
1140         }
1141         if (!s.empty())
1142                 retval.push_back(s);
1143         return retval;
1144 }
1145
1146 }
1147
1148
1149 docstring wrap(docstring const & str, int const ind, size_t const width)
1150 {
1151         docstring s = trim(str);
1152         if (s.empty())
1153                 return docstring();
1154
1155         vector<docstring> const svec = wrapToVec(str, ind, width);
1156         return getStringFromVector(svec, from_ascii("\n"));
1157 }
1158
1159
1160 docstring wrapParas(docstring const & str, int const indent,
1161                     size_t const width, size_t const maxlines)
1162 {
1163         if (str.empty())
1164                 return docstring();
1165
1166         vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
1167         vector<docstring> retval;
1168
1169         vector<docstring>::const_iterator it = pars.begin();
1170         vector<docstring>::const_iterator const en = pars.end();
1171         for (; it != en; ++it) {
1172                 vector<docstring> tmp = wrapToVec(*it, indent, width);
1173                 size_t const nlines = tmp.size();
1174                 if (nlines == 0)
1175                         continue;
1176                 size_t const curlines = retval.size();
1177                 if (maxlines > 0 && curlines + nlines >= maxlines) {
1178                         tmp.resize(maxlines - curlines - 1);
1179                         tmp.push_back(from_ascii("..."));
1180                 }
1181                 retval.insert(retval.end(), tmp.begin(), tmp.end());
1182                 if (maxlines > 0 && retval.size() >= maxlines)
1183                         break;
1184         }
1185         return getStringFromVector(retval, from_ascii("\n"));
1186 }
1187
1188
1189 namespace {
1190
1191 template<typename String> vector<String> const
1192 getVectorFromStringT(String const & str, String const & delim, bool keepempty)
1193 {
1194 // Lars would like this code to go, but for now his replacement (below)
1195 // doesn't fullfil the same function. I have, therefore, reactivated the
1196 // old code for now. Angus 11 Nov 2002.
1197 #if 1
1198         vector<String> vec;
1199         if (str.empty())
1200                 return vec;
1201         String keys = rtrim(str);
1202         while (true) {
1203                 size_t const idx = keys.find(delim);
1204                 if (idx == String::npos) {
1205                         vec.push_back(ltrim(keys));
1206                         break;
1207                 }
1208                 String const key = trim(keys.substr(0, idx));
1209                 if (!key.empty() || keepempty)
1210                         vec.push_back(key);
1211                 size_t const start = idx + delim.size();
1212                 keys = keys.substr(start);
1213         }
1214         return vec;
1215 #else
1216         typedef boost::char_separator<typename String::value_type> Separator;
1217         typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
1218         Separator sep(delim.c_str());
1219         Tokenizer tokens(str, sep);
1220         return vector<String>(tokens.begin(), tokens.end());
1221 #endif
1222 }
1223
1224
1225 template<typename String> const String
1226         getStringFromVector(vector<String> const & vec, String const & delim)
1227 {
1228         String str;
1229         typename vector<String>::const_iterator it = vec.begin();
1230         typename vector<String>::const_iterator en = vec.end();
1231         for (; it != en; ++it) {
1232                 String item = trim(*it);
1233                 if (item.empty())
1234                         continue;
1235                 if (!str.empty())
1236                         str += delim;
1237                 str += item;
1238         }
1239         return str;
1240 }
1241
1242 } // namespace anon
1243
1244
1245 vector<string> const getVectorFromString(string const & str,
1246                                          string const & delim,
1247                                          bool keepempty)
1248 {
1249         return getVectorFromStringT<string>(str, delim, keepempty);
1250 }
1251
1252
1253 vector<docstring> const getVectorFromString(docstring const & str,
1254                                             docstring const & delim,
1255                                             bool keepempty)
1256 {
1257         return getVectorFromStringT<docstring>(str, delim, keepempty);
1258 }
1259
1260
1261 string const getStringFromVector(vector<string> const & vec,
1262                                  string const & delim)
1263 {
1264         return getStringFromVector<string>(vec, delim);
1265 }
1266
1267
1268 docstring const getStringFromVector(vector<docstring> const & vec,
1269                                     docstring const & delim)
1270 {
1271         return getStringFromVector<docstring>(vec, delim);
1272 }
1273
1274
1275 int findToken(char const * const str[], string const & search_token)
1276 {
1277         int i = 0;
1278
1279         while (str[i][0] && str[i] != search_token)
1280                 ++i;
1281         if (!str[i][0])
1282                 i = -1;
1283         return i;
1284 }
1285
1286
1287 string const languageTestString()
1288 {
1289         return N_("[[Replace with the code of your language]]");
1290 }
1291
1292
1293 template<>
1294 docstring bformat(docstring const & fmt, int arg1)
1295 {
1296         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1297         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1298         return subst(str, from_ascii("%%"), from_ascii("%"));
1299 }
1300
1301
1302 template<>
1303 docstring bformat(docstring const & fmt, long arg1)
1304 {
1305         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1306         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1307         return subst(str, from_ascii("%%"), from_ascii("%"));
1308 }
1309
1310
1311 template<>
1312 docstring bformat(docstring const & fmt, unsigned int arg1)
1313 {
1314         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1315         docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1316         return subst(str, from_ascii("%%"), from_ascii("%"));
1317 }
1318
1319
1320 template<>
1321 docstring bformat(docstring const & fmt, docstring arg1)
1322 {
1323         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1324         docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
1325         return subst(str, from_ascii("%%"), from_ascii("%"));
1326 }
1327
1328
1329 template<>
1330 docstring bformat(docstring const & fmt, char * arg1)
1331 {
1332         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1333         docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1334         return subst(str, from_ascii("%%"), from_ascii("%"));
1335 }
1336
1337
1338 template<>
1339 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
1340 {
1341         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1342         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1343         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1344         str = subst(str, from_ascii("%2$s"), arg2);
1345         return subst(str, from_ascii("%%"), from_ascii("%"));
1346 }
1347
1348
1349 template<>
1350 docstring bformat(docstring const & fmt, docstring arg1, int arg2)
1351 {
1352         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1353         LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1354         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1355         str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1356         return subst(str, from_ascii("%%"), from_ascii("%"));
1357 }
1358
1359
1360 template<>
1361 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
1362 {
1363         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1364         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1365         docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
1366         str = subst(fmt, from_ascii("%2$s"), arg2);
1367         return subst(str, from_ascii("%%"), from_ascii("%"));
1368 }
1369
1370
1371 template<>
1372 docstring bformat(docstring const & fmt, int arg1, int arg2)
1373 {
1374         LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
1375         LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
1376         docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
1377         str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
1378         return subst(str, from_ascii("%%"), from_ascii("%"));
1379 }
1380
1381
1382 template<>
1383 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
1384 {
1385         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1386         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1387         LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1388         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1389         str = subst(str, from_ascii("%2$s"), arg2);
1390         str = subst(str, from_ascii("%3$s"), arg3);
1391         return subst(str, from_ascii("%%"), from_ascii("%"));
1392 }
1393
1394
1395 template<>
1396 docstring bformat(docstring const & fmt,
1397                docstring arg1, docstring arg2, docstring arg3, docstring arg4)
1398 {
1399         LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
1400         LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
1401         LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
1402         LASSERT(contains(fmt, from_ascii("%4$s")), /**/);
1403         docstring str = subst(fmt, from_ascii("%1$s"), arg1);
1404         str = subst(str, from_ascii("%2$s"), arg2);
1405         str = subst(str, from_ascii("%3$s"), arg3);
1406         str = subst(str, from_ascii("%4$s"), arg4);
1407         return subst(str, from_ascii("%%"), from_ascii("%"));
1408 }
1409
1410 } // namespace support
1411 } // namespace lyx