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