]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.C
the convert patch
[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         string lstr = a;
357         string::size_type i = 0;
358         string::size_type const olen = oldstr.length();
359         while ((i = lstr.find(oldstr, i)) != string::npos) {
360                 lstr.replace(i, olen, newstr);
361                 i += newstr.length(); // We need to be sure that we dont
362                 // use the same i over and over again.
363         }
364         return lstr;
365 }
366
367
368 string const trim(string const & a, char const * p)
369 {
370         BOOST_ASSERT(p);
371
372         if (a.empty() || !*p)
373                 return a;
374
375         string::size_type r = a.find_last_not_of(p);
376         string::size_type l = a.find_first_not_of(p);
377
378         // Is this the minimal test? (lgb)
379         if (r == string::npos && l == string::npos)
380                 return string();
381
382         return a.substr(l, r - l + 1);
383 }
384
385
386 string const rtrim(string const & a, char const * p)
387 {
388         BOOST_ASSERT(p);
389
390         if (a.empty() || !*p)
391                 return a;
392
393         string::size_type r = a.find_last_not_of(p);
394
395         // Is this test really needed? (Lgb)
396         if (r == string::npos)
397                 return string();
398
399         return a.substr(0, r + 1);
400 }
401
402
403 string const ltrim(string const & a, char const * p)
404 {
405         BOOST_ASSERT(p);
406
407         if (a.empty() || !*p)
408                 return a;
409
410         string::size_type l = a.find_first_not_of(p);
411
412         if (l == string::npos)
413                 return string();
414
415         return a.substr(l, string::npos);
416 }
417
418
419 string const split(string const & a, string & piece, char delim)
420 {
421         string tmp;
422         string::size_type i = a.find(delim);
423         if (i == a.length() - 1) {
424                 piece = a.substr(0, i);
425         } else if (i != string::npos) {
426                 piece = a.substr(0, i);
427                 tmp = a.substr(i + 1);
428         } else if (i == 0) {
429                 piece.erase();
430                 tmp = a.substr(i + 1);
431         } else {
432                 piece = a;
433         }
434         return tmp;
435 }
436
437
438 string const split(string const & a, char delim)
439 {
440         string tmp;
441         string::size_type i = a.find(delim);
442         if (i != string::npos) // found delim
443                 tmp = a.substr(i + 1);
444         return tmp;
445 }
446
447
448 // ale970521
449 string const rsplit(string const & a, string & piece, char delim)
450 {
451         string tmp;
452         string::size_type i = a.rfind(delim);
453         if (i != string::npos) { // delimiter was found
454                 piece = a.substr(0, i);
455                 tmp = a.substr(i + 1);
456         } else { // delimiter was not found
457                 piece.erase();
458         }
459         return tmp;
460 }
461
462
463 // This function escapes 8-bit characters and other problematic
464 // characters that cause problems in latex labels.
465 string const escape(string const & lab)
466 {
467         char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
468                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
469         string enc;
470         for (string::size_type i = 0; i < lab.length(); ++i) {
471                 unsigned char c= lab[i];
472                 if (c >= 128 || c == '=' || c == '%') {
473                         enc += '=';
474                         enc += hexdigit[c>>4];
475                         enc += hexdigit[c & 15];
476                 } else {
477                         enc += c;
478                 }
479         }
480         return enc;
481 }
482
483
484 /// gives a vector of stringparts which have the delimiter delim
485 vector<string> const getVectorFromString(string const & str,
486                                          string const & delim)
487 {
488 // Lars would like this code to go, but for now his replacement (below)
489 // doesn't fullfil the same function. I have, therefore, reactivated the
490 // old code for now. Angus 11 Nov 2002.
491 #if 1
492         vector<string> vec;
493         if (str.empty())
494                 return vec;
495         string keys(rtrim(str));
496         for(;;) {
497                 string::size_type const idx = keys.find(delim);
498                 if (idx == string::npos) {
499                         vec.push_back(ltrim(keys));
500                         break;
501                 }
502                 string const key = trim(keys.substr(0, idx));
503                 if (!key.empty())
504                         vec.push_back(key);
505                 string::size_type const start = idx + delim.size();
506                 keys = keys.substr(start);
507         }
508         return vec;
509 #else
510         boost::char_separator<char> sep(delim.c_str());
511         boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
512         return vector<string>(tokens.begin(), tokens.end());
513 #endif
514 }
515
516
517 // the same vice versa
518 string const getStringFromVector(vector<string> const & vec,
519                                  string const & delim)
520 {
521         string str;
522         int i = 0;
523         for (vector<string>::const_iterator it = vec.begin();
524              it != vec.end(); ++it) {
525                 string item = trim(*it);
526                 if (item.empty())
527                         continue;
528                 if (i++ > 0)
529                         str += delim;
530                 str += item;
531         }
532         return str;
533 }
534
535
536 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
537 #if USE_BOOST_FORMAT
538
539 template<>
540 string bformat(string const & fmt, int arg1)
541 {
542         return (boost::format(fmt) % arg1).str();
543 }
544
545
546 template<>
547 string bformat(string const & fmt, long arg1)
548 {
549         return (boost::format(fmt) % arg1).str();
550 }
551
552
553 template<>
554 string bformat(string const & fmt, unsigned int arg1)
555 {
556         return (boost::format(fmt) % arg1).str();
557 }
558
559
560 template<>
561 string bformat<string>(string const & fmt, string arg1)
562 {
563         return (boost::format(fmt) % arg1).str();
564 }
565
566
567 template<>
568 string bformat(string const & fmt, char * arg1)
569 {
570         return (boost::format(fmt) % arg1).str();
571 }
572
573
574 template<>
575 string bformat(string const & fmt, int arg1, int arg2)
576 {
577         return (boost::format(fmt) % arg1 % arg2).str();
578 }
579
580
581 template<>
582 string bformat(string const & fmt, string arg1, string arg2)
583 {
584         return (boost::format(fmt) % arg1 % arg2).str();
585 }
586
587
588 template<>
589 string bformat(string const & fmt, char const * arg1, string arg2)
590 {
591         return (boost::format(fmt) % arg1 % arg2).str();
592 }
593
594
595 template<>
596 string bformat(string const & fmt, string arg1, string arg2, string arg3)
597 {
598         return (boost::format(fmt) % arg1 % arg2 % arg3).str();
599 }
600
601
602 template<>
603 string bformat(string const & fmt,
604                string arg1, string arg2, string arg3, string arg4)
605 {
606         return (boost::format(fmt) % arg1 % arg2 % arg3 % arg4).str();
607 }
608
609 #else
610
611 template<>
612 string bformat(string const & fmt, int arg1)
613 {
614         BOOST_ASSERT(contains(fmt, "%1$d"));
615         string const str = subst(fmt, "%1$d", convert<string>(arg1));
616         return subst(str, "%%", "%");
617 }
618
619
620 template<>
621 string bformat(string const & fmt, long arg1)
622 {
623         BOOST_ASSERT(contains(fmt, "%1$d"));
624         string const str = subst(fmt, "%1$d", convert<string>(arg1));
625         return subst(str, "%%", "%");
626 }
627
628
629 template<>
630 string bformat(string const & fmt, unsigned int arg1)
631 {
632         BOOST_ASSERT(contains(fmt, "%1$d"));
633         string const str = subst(fmt, "%1$d", convert<string>(arg1));
634         return subst(str, "%%", "%");
635 }
636
637
638 template<>
639 string bformat(string const & fmt, string arg1)
640 {
641         BOOST_ASSERT(contains(fmt, "%1$s"));
642         string const str = subst(fmt, "%1$s", arg1);
643         return subst(str, "%%", "%");
644 }
645
646
647 template<>
648 string bformat(string const & fmt, char * arg1)
649 {
650         BOOST_ASSERT(contains(fmt, "%1$s"));
651         string const str = subst(fmt, "%1$s", arg1);
652         return subst(str, "%%", "%");
653 }
654 template<>
655 string bformat(string const & fmt, string arg1, string arg2)
656 {
657         BOOST_ASSERT(contains(fmt, "%1$s"));
658         BOOST_ASSERT(contains(fmt, "%2$s"));
659         string str = subst(fmt, "%1$s", arg1);
660         str = subst(str, "%2$s", arg2);
661         return subst(str, "%%", "%");
662 }
663
664
665 template<>
666 string bformat(string const & fmt, char const * arg1, string arg2)
667 {
668         BOOST_ASSERT(contains(fmt, "%1$s"));
669         BOOST_ASSERT(contains(fmt, "%2$s"));
670         string str = subst(fmt, "%1$s", arg1);
671         str = subst(fmt, "%2$s", arg2);
672         return subst(str, "%%", "%");
673 }
674
675
676 template<>
677 string bformat(string const & fmt, int arg1, int arg2)
678 {
679         BOOST_ASSERT(contains(fmt, "%1$d"));
680         BOOST_ASSERT(contains(fmt, "%2$d"));
681         string str = subst(fmt, "%1$d", convert<string>(arg1));
682         str = subst(str, "%2$d", convert<string>(arg2));
683         return subst(str, "%%", "%");
684 }
685
686
687 template<>
688 string bformat(string const & fmt, string arg1, string arg2, string arg3)
689 {
690         BOOST_ASSERT(contains(fmt, "%1$s"));
691         BOOST_ASSERT(contains(fmt, "%2$s"));
692         BOOST_ASSERT(contains(fmt, "%3$s"));
693         string str = subst(fmt, "%1$s", arg1);
694         str = subst(str, "%2$s", arg2);
695         str = subst(str, "%3$s", arg3);
696         return subst(str, "%%", "%");
697 }
698
699
700 template<>
701 string bformat(string const & fmt,
702                string arg1, string arg2, string arg3, string arg4)
703 {
704         BOOST_ASSERT(contains(fmt, "%1$s"));
705         BOOST_ASSERT(contains(fmt, "%2$s"));
706         BOOST_ASSERT(contains(fmt, "%3$s"));
707         BOOST_ASSERT(contains(fmt, "%4$s"));
708         string str = subst(fmt, "%1$s", arg1);
709         str = subst(str, "%2$s", arg2);
710         str = subst(str, "%3$s", arg3);
711         str = subst(str, "%4$s", arg4);
712         return subst(str, "%%", "%");
713 }
714
715 #endif
716 #endif
717
718 } // namespace support
719 } // namespace lyx