]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.C
convert lfun arguments to docstring
[lyx.git] / src / support / lstrings.C
1 /**
2  * \file lstrings.C
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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "support/lstrings.h"
15 #include "support/lyxlib.h"
16 #include "support/convert.h"
17
18 #include "debug.h"
19
20 #include <boost/tokenizer.hpp>
21 #include <boost/assert.hpp>
22
23 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
24 #if USE_BOOST_FORMAT
25 #include <boost/format.hpp>
26 #endif
27 #endif
28
29 #include <cctype>
30 #include <cstdlib>
31
32 #include <algorithm>
33 #include <sstream>
34
35 using std::transform;
36 using std::string;
37 using std::vector;
38
39 #ifndef CXX_GLOBAL_CSTD
40 using std::isdigit;
41 using std::tolower;
42 using std::toupper;
43 #endif
44
45
46 namespace lyx {
47 namespace support {
48
49 int compare_no_case(string const & s, string const & s2)
50 {
51         string::const_iterator p = s.begin();
52         string::const_iterator p2 = s2.begin();
53
54         while (p != s.end() && p2 != s2.end()) {
55                 int const lc1 = tolower(*p);
56                 int const lc2 = tolower(*p2);
57                 if (lc1 != lc2)
58                         return (lc1 < lc2) ? -1 : 1;
59                 ++p;
60                 ++p2;
61         }
62
63         if (s.size() == s2.size())
64                 return 0;
65         if (s.size() < s2.size())
66                 return -1;
67         return 1;
68 }
69
70
71 namespace {
72         int ascii_tolower(int c) {
73                 if (c >= 'A' && c <= 'Z')
74                         return c - 'A' + 'a';
75                 return c;
76         }
77 }
78
79
80 int compare_ascii_no_case(string const & s, string const & s2)
81 {
82         string::const_iterator p = s.begin();
83         string::const_iterator p2 = s2.begin();
84
85         while (p != s.end() && p2 != s2.end()) {
86                 int const lc1 = ascii_tolower(*p);
87                 int const lc2 = ascii_tolower(*p2);
88                 if (lc1 != lc2)
89                         return (lc1 < lc2) ? -1 : 1;
90                 ++p;
91                 ++p2;
92         }
93
94         if (s.size() == s2.size())
95                 return 0;
96         if (s.size() < s2.size())
97                 return -1;
98         return 1;
99 }
100
101
102 int compare_no_case(string const & s, string const & s2, unsigned int len)
103 {
104         string::const_iterator p = s.begin();
105         string::const_iterator p2 = s2.begin();
106         unsigned int i = 0;
107         while (i < len && p != s.end() && p2 != s2.end()) {
108                 int const lc1 = tolower(*p);
109                 int const lc2 = tolower(*p2);
110                 if (lc1 != lc2)
111                         return (lc1 < lc2) ? -1 : 1;
112                 ++i;
113                 ++p;
114                 ++p2;
115         }
116
117         if (s.size() >= len && s2.size() >= len)
118                 return 0;
119         if (s.size() < s2.size())
120                 return -1;
121         return 1;
122 }
123
124
125 bool isStrInt(string const & str)
126 {
127         if (str.empty()) return false;
128
129         // Remove leading and trailing white space chars.
130         string const tmpstr = trim(str);
131         if (tmpstr.empty()) return false;
132
133         string::const_iterator cit = tmpstr.begin();
134         if ((*cit) == '-') ++cit;
135         string::const_iterator end = tmpstr.end();
136         for (; cit != end; ++cit) {
137                 if (!isdigit((*cit))) return false;
138         }
139         return true;
140 }
141
142
143 bool isStrUnsignedInt(string const & str)
144 {
145         if (str.empty()) return false;
146
147         // Remove leading and trailing white space chars.
148         string const tmpstr = trim(str);
149         if (tmpstr.empty()) return false;
150
151         string::const_iterator cit = tmpstr.begin();
152         string::const_iterator end = tmpstr.end();
153         for (; cit != end; ++cit) {
154                 if (!isdigit((*cit))) return false;
155         }
156         return true;
157 }
158
159
160 bool isStrDbl(string const & str)
161 {
162         if (str.empty()) return false;
163
164         // Remove leading and trailing white space chars.
165         string const tmpstr = trim(str);
166         if (tmpstr.empty()) return false;
167         //      if (1 < tmpstr.count('.')) return false;
168
169         string::const_iterator cit = tmpstr.begin();
170         bool found_dot(false);
171         if ((*cit) == '-') ++cit;
172         string::const_iterator end = tmpstr.end();
173         for (; cit != end; ++cit) {
174                 if (!isdigit((*cit))
175                     && '.' != (*cit)) {
176                         return false;
177                 }
178                 if ('.' == (*cit)) {
179                         if (found_dot) {
180                                 return false;
181                         } else {
182                                 found_dot = true;
183                         }
184                 }
185         }
186         return true;
187 }
188
189
190 char lowercase(char c)
191 {
192         return char(tolower(c));
193 }
194
195
196 char uppercase(char c)
197 {
198         return char(toupper(c));
199 }
200
201
202 namespace {
203
204 // since we cannot use std::tolower and std::toupper directly in the
205 // calls to std::transform yet, we use these helper clases. (Lgb)
206
207 struct local_lowercase {
208         char operator()(char c) const {
209                 return tolower(c);
210         }
211 };
212
213 struct local_uppercase {
214         char operator()(char c) const {
215                 return toupper(c);
216         }
217 };
218
219 struct local_ascii_lowercase {
220         char operator()(char c) const {
221                 return ascii_tolower(c);
222         }
223 };
224
225 } // end of anon namespace
226
227 string const lowercase(string const & a)
228 {
229         string tmp(a);
230         transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
231         return tmp;
232 }
233
234 string const uppercase(string const & a)
235 {
236         string tmp(a);
237         transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
238         return tmp;
239 }
240
241
242 string const ascii_lowercase(string const & a)
243 {
244         string tmp(a);
245         transform(tmp.begin(), tmp.end(), tmp.begin(),
246                   local_ascii_lowercase());
247         return tmp;
248 }
249
250
251 bool prefixIs(string const & a, string const & pre)
252 {
253         string::size_type const prelen = pre.length();
254         string::size_type const alen = a.length();
255
256         if (prelen > alen || a.empty())
257                 return false;
258         else {
259 #if defined(STD_STRING_IS_GOOD)
260                 return a.compare(0, prelen, pre) == 0;
261 #else
262                 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
263 #endif
264         }
265 }
266
267
268 bool suffixIs(string const & a, char c)
269 {
270         if (a.empty()) return false;
271         return a[a.length() - 1] == c;
272 }
273
274
275 bool suffixIs(string const & a, string const & suf)
276 {
277         string::size_type const suflen = suf.length();
278         string::size_type const alen = a.length();
279
280         if (suflen > alen) {
281                 return false;
282         } else {
283 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
284                 string tmp(a, alen - suflen);
285                 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
286 #else
287                 return a.compare(alen - suflen, suflen, suf) == 0;
288 #endif
289         }
290 }
291
292
293 bool containsOnly(string const & s, string const & cset)
294 {
295         return s.find_first_not_of(cset) == string::npos;
296 }
297
298
299 // ale970405+lasgoutt-970425
300 // rewritten to use new string (Lgb)
301 string const token(string const & a, char delim, int n)
302 {
303         if (a.empty()) return string();
304
305         string::size_type k = 0;
306         string::size_type i = 0;
307
308         // Find delimiter or end of string
309         for (; n--;)
310                 if ((i = a.find(delim, i)) == string::npos)
311                         break;
312                 else
313                         ++i; // step delim
314         // i is now the n'th delim (or string::npos)
315         if (i == string::npos) return string();
316         k = a.find(delim, i);
317         // k is now the n'th + 1 delim (or string::npos)
318
319         return a.substr(i, k - i);
320 }
321
322
323 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
324 // rewritten to use new string (Lgb)
325 int tokenPos(string const & a, char delim, string const & tok)
326 {
327         int i = 0;
328         string str(a);
329         string tmptok;
330
331         while (!str.empty()) {
332                 str = split(str, tmptok, delim);
333                 if (tok == tmptok)
334                         return i;
335                 ++i;
336         }
337         return -1;
338 }
339
340
341 string const subst(string const & a, char oldchar, char newchar)
342 {
343         string tmp(a);
344         string::iterator lit = tmp.begin();
345         string::iterator end = tmp.end();
346         for (; lit != end; ++lit)
347                 if ((*lit) == oldchar)
348                         (*lit) = newchar;
349         return tmp;
350 }
351
352
353 string const subst(string const & a,
354                    string const & oldstr, string const & newstr)
355 {
356         BOOST_ASSERT(!oldstr.empty());
357         string lstr = a;
358         string::size_type i = 0;
359         string::size_type const olen = oldstr.length();
360         while ((i = lstr.find(oldstr, i)) != string::npos) {
361                 lstr.replace(i, olen, newstr);
362                 i += newstr.length(); // We need to be sure that we dont
363                 // use the same i over and over again.
364         }
365         return lstr;
366 }
367
368
369 string const trim(string const & a, char const * p)
370 {
371         BOOST_ASSERT(p);
372
373         if (a.empty() || !*p)
374                 return a;
375
376         string::size_type r = a.find_last_not_of(p);
377         string::size_type l = a.find_first_not_of(p);
378
379         // Is this the minimal test? (lgb)
380         if (r == string::npos && l == string::npos)
381                 return string();
382
383         return a.substr(l, r - l + 1);
384 }
385
386
387 string const rtrim(string const & a, char const * p)
388 {
389         BOOST_ASSERT(p);
390
391         if (a.empty() || !*p)
392                 return a;
393
394         string::size_type r = a.find_last_not_of(p);
395
396         // Is this test really needed? (Lgb)
397         if (r == string::npos)
398                 return string();
399
400         return a.substr(0, r + 1);
401 }
402
403
404 string const ltrim(string const & a, char const * p)
405 {
406         BOOST_ASSERT(p);
407
408         if (a.empty() || !*p)
409                 return a;
410
411         string::size_type l = a.find_first_not_of(p);
412
413         if (l == string::npos)
414                 return string();
415
416         return a.substr(l, string::npos);
417 }
418
419
420 string const split(string const & a, string & piece, char delim)
421 {
422         string tmp;
423         string::size_type i = a.find(delim);
424         if (i == a.length() - 1) {
425                 piece = a.substr(0, i);
426         } else if (i != string::npos) {
427                 piece = a.substr(0, i);
428                 tmp = a.substr(i + 1);
429         } else if (i == 0) {
430                 piece.erase();
431                 tmp = a.substr(i + 1);
432         } else {
433                 piece = a;
434         }
435         return tmp;
436 }
437
438
439 string const split(string const & a, char delim)
440 {
441         string tmp;
442         string::size_type i = a.find(delim);
443         if (i != string::npos) // found delim
444                 tmp = a.substr(i + 1);
445         return tmp;
446 }
447
448
449 // ale970521
450 string const rsplit(string const & a, string & piece, char delim)
451 {
452         string tmp;
453         string::size_type i = a.rfind(delim);
454         if (i != string::npos) { // delimiter was found
455                 piece = a.substr(0, i);
456                 tmp = a.substr(i + 1);
457         } else { // delimiter was not found
458                 piece.erase();
459         }
460         return tmp;
461 }
462
463
464 // This function escapes 8-bit characters and other problematic
465 // characters that cause problems in latex labels.
466 string const escape(string const & lab)
467 {
468         char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
469                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
470         string enc;
471         for (string::size_type i = 0; i < lab.length(); ++i) {
472                 unsigned char c= lab[i];
473                 if (c >= 128 || c == '=' || c == '%') {
474                         enc += '=';
475                         enc += hexdigit[c>>4];
476                         enc += hexdigit[c & 15];
477                 } else {
478                         enc += c;
479                 }
480         }
481         return enc;
482 }
483
484
485 /// gives a vector of stringparts which have the delimiter delim
486 vector<string> const getVectorFromString(string const & str,
487                                          string const & delim)
488 {
489 // Lars would like this code to go, but for now his replacement (below)
490 // doesn't fullfil the same function. I have, therefore, reactivated the
491 // old code for now. Angus 11 Nov 2002.
492 #if 1
493         vector<string> vec;
494         if (str.empty())
495                 return vec;
496         string keys = rtrim(str);
497         for(;;) {
498                 string::size_type const idx = keys.find(delim);
499                 if (idx == string::npos) {
500                         vec.push_back(ltrim(keys));
501                         break;
502                 }
503                 string const key = trim(keys.substr(0, idx));
504                 if (!key.empty())
505                         vec.push_back(key);
506                 string::size_type const start = idx + delim.size();
507                 keys = keys.substr(start);
508         }
509         return vec;
510 #else
511         boost::char_separator<char> sep(delim.c_str());
512         boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
513         return vector<string>(tokens.begin(), tokens.end());
514 #endif
515 }
516
517
518 // the same vice versa
519 string const getStringFromVector(vector<string> const & vec,
520                                  string const & delim)
521 {
522         string str;
523         int i = 0;
524         for (vector<string>::const_iterator it = vec.begin();
525              it != vec.end(); ++it) {
526                 string item = trim(*it);
527                 if (item.empty())
528                         continue;
529                 if (i++ > 0)
530                         str += delim;
531                 str += item;
532         }
533         return str;
534 }
535
536
537 int findToken(char const * const str[], string const & search_token)
538 {
539         int i = 0;
540
541         while (str[i][0] && str[i] != search_token)
542                 ++i;
543         if (!str[i][0])
544                 i = -1;
545         return i;
546 }
547
548
549 string const externalLineEnding(string const & str)
550 {
551 #if defined(__APPLE__)
552         // The MAC clipboard uses \r for lineendings, and we use \n
553         return subst(str, '\n', '\r');
554 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
555         // Windows clipboard uses \r\n for lineendings, and we use \n
556         return subst(str, "\n", "\r\n");
557 #else
558         return str;
559 #endif
560 }
561
562
563 string const internalLineEnding(string const & str)
564 {
565         string s = subst(str, "\r\n", "\n");
566         return subst(s, '\r', '\n');
567 }
568
569
570 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
571 #if USE_BOOST_FORMAT
572
573 template<>
574 string bformat(string const & fmt, int arg1)
575 {
576         return (boost::format(fmt) % arg1).str();
577 }
578
579
580 template<>
581 string bformat(string const & fmt, long arg1)
582 {
583         return (boost::format(fmt) % arg1).str();
584 }
585
586
587 template<>
588 string bformat(string const & fmt, unsigned int arg1)
589 {
590         return (boost::format(fmt) % arg1).str();
591 }
592
593
594 template<>
595 string bformat<string>(string const & fmt, string arg1)
596 {
597         return (boost::format(fmt) % arg1).str();
598 }
599
600
601 template<>
602 string bformat(string const & fmt, char * arg1)
603 {
604         return (boost::format(fmt) % arg1).str();
605 }
606
607
608 template<>
609 string bformat(string const & fmt, int arg1, int arg2)
610 {
611         return (boost::format(fmt) % arg1 % arg2).str();
612 }
613
614
615 template<>
616 string bformat(string const & fmt, string arg1, string arg2)
617 {
618         return (boost::format(fmt) % arg1 % arg2).str();
619 }
620
621
622 template<>
623 string bformat(string const & fmt, char const * arg1, string arg2)
624 {
625         return (boost::format(fmt) % arg1 % arg2).str();
626 }
627
628
629 template<>
630 string bformat(string const & fmt, string arg1, string arg2, string arg3)
631 {
632         return (boost::format(fmt) % arg1 % arg2 % arg3).str();
633 }
634
635
636 template<>
637 string bformat(string const & fmt,
638                string arg1, string arg2, string arg3, string arg4)
639 {
640         return (boost::format(fmt) % arg1 % arg2 % arg3 % arg4).str();
641 }
642
643 #else
644
645 template<>
646 string bformat(string const & fmt, int arg1)
647 {
648         BOOST_ASSERT(contains(fmt, "%1$d"));
649         string const str = subst(fmt, "%1$d", convert<string>(arg1));
650         return subst(str, "%%", "%");
651 }
652
653
654 template<>
655 string bformat(string const & fmt, long arg1)
656 {
657         BOOST_ASSERT(contains(fmt, "%1$d"));
658         string const str = subst(fmt, "%1$d", convert<string>(arg1));
659         return subst(str, "%%", "%");
660 }
661
662
663 template<>
664 string bformat(string const & fmt, unsigned int arg1)
665 {
666         BOOST_ASSERT(contains(fmt, "%1$d"));
667         string const str = subst(fmt, "%1$d", convert<string>(arg1));
668         return subst(str, "%%", "%");
669 }
670
671
672 template<>
673 string bformat(string const & fmt, string arg1)
674 {
675         BOOST_ASSERT(contains(fmt, "%1$s"));
676         string const str = subst(fmt, "%1$s", arg1);
677         return subst(str, "%%", "%");
678 }
679
680
681 template<>
682 string bformat(string const & fmt, char * arg1)
683 {
684         BOOST_ASSERT(contains(fmt, "%1$s"));
685         string const str = subst(fmt, "%1$s", arg1);
686         return subst(str, "%%", "%");
687 }
688 template<>
689 string bformat(string const & fmt, string arg1, string arg2)
690 {
691         BOOST_ASSERT(contains(fmt, "%1$s"));
692         BOOST_ASSERT(contains(fmt, "%2$s"));
693         string str = subst(fmt, "%1$s", arg1);
694         str = subst(str, "%2$s", arg2);
695         return subst(str, "%%", "%");
696 }
697
698
699 template<>
700 string bformat(string const & fmt, char const * arg1, string arg2)
701 {
702         BOOST_ASSERT(contains(fmt, "%1$s"));
703         BOOST_ASSERT(contains(fmt, "%2$s"));
704         string str = subst(fmt, "%1$s", arg1);
705         str = subst(fmt, "%2$s", arg2);
706         return subst(str, "%%", "%");
707 }
708
709
710 template<>
711 string bformat(string const & fmt, int arg1, int arg2)
712 {
713         BOOST_ASSERT(contains(fmt, "%1$d"));
714         BOOST_ASSERT(contains(fmt, "%2$d"));
715         string str = subst(fmt, "%1$d", convert<string>(arg1));
716         str = subst(str, "%2$d", convert<string>(arg2));
717         return subst(str, "%%", "%");
718 }
719
720
721 template<>
722 string bformat(string const & fmt, string arg1, string arg2, string arg3)
723 {
724         BOOST_ASSERT(contains(fmt, "%1$s"));
725         BOOST_ASSERT(contains(fmt, "%2$s"));
726         BOOST_ASSERT(contains(fmt, "%3$s"));
727         string str = subst(fmt, "%1$s", arg1);
728         str = subst(str, "%2$s", arg2);
729         str = subst(str, "%3$s", arg3);
730         return subst(str, "%%", "%");
731 }
732
733
734 template<>
735 string bformat(string const & fmt,
736                string arg1, string arg2, string arg3, string arg4)
737 {
738         BOOST_ASSERT(contains(fmt, "%1$s"));
739         BOOST_ASSERT(contains(fmt, "%2$s"));
740         BOOST_ASSERT(contains(fmt, "%3$s"));
741         BOOST_ASSERT(contains(fmt, "%4$s"));
742         string str = subst(fmt, "%1$s", arg1);
743         str = subst(str, "%2$s", arg2);
744         str = subst(str, "%3$s", arg3);
745         str = subst(str, "%4$s", arg4);
746         return subst(str, "%%", "%");
747 }
748
749 #endif
750 #endif
751
752 } // namespace support
753 } // namespace lyx