]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.C
fix crash when ${HOME} is empty
[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 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
538 #if USE_BOOST_FORMAT
539
540 template<>
541 string bformat(string const & fmt, int arg1)
542 {
543         return (boost::format(fmt) % arg1).str();
544 }
545
546
547 template<>
548 string bformat(string const & fmt, long arg1)
549 {
550         return (boost::format(fmt) % arg1).str();
551 }
552
553
554 template<>
555 string bformat(string const & fmt, unsigned int arg1)
556 {
557         return (boost::format(fmt) % arg1).str();
558 }
559
560
561 template<>
562 string bformat<string>(string const & fmt, string arg1)
563 {
564         return (boost::format(fmt) % arg1).str();
565 }
566
567
568 template<>
569 string bformat(string const & fmt, char * arg1)
570 {
571         return (boost::format(fmt) % arg1).str();
572 }
573
574
575 template<>
576 string bformat(string const & fmt, int arg1, int arg2)
577 {
578         return (boost::format(fmt) % arg1 % arg2).str();
579 }
580
581
582 template<>
583 string bformat(string const & fmt, string arg1, string arg2)
584 {
585         return (boost::format(fmt) % arg1 % arg2).str();
586 }
587
588
589 template<>
590 string bformat(string const & fmt, char const * arg1, string arg2)
591 {
592         return (boost::format(fmt) % arg1 % arg2).str();
593 }
594
595
596 template<>
597 string bformat(string const & fmt, string arg1, string arg2, string arg3)
598 {
599         return (boost::format(fmt) % arg1 % arg2 % arg3).str();
600 }
601
602
603 template<>
604 string bformat(string const & fmt,
605                string arg1, string arg2, string arg3, string arg4)
606 {
607         return (boost::format(fmt) % arg1 % arg2 % arg3 % arg4).str();
608 }
609
610 #else
611
612 template<>
613 string bformat(string const & fmt, int arg1)
614 {
615         BOOST_ASSERT(contains(fmt, "%1$d"));
616         string const str = subst(fmt, "%1$d", convert<string>(arg1));
617         return subst(str, "%%", "%");
618 }
619
620
621 template<>
622 string bformat(string const & fmt, long arg1)
623 {
624         BOOST_ASSERT(contains(fmt, "%1$d"));
625         string const str = subst(fmt, "%1$d", convert<string>(arg1));
626         return subst(str, "%%", "%");
627 }
628
629
630 template<>
631 string bformat(string const & fmt, unsigned int arg1)
632 {
633         BOOST_ASSERT(contains(fmt, "%1$d"));
634         string const str = subst(fmt, "%1$d", convert<string>(arg1));
635         return subst(str, "%%", "%");
636 }
637
638
639 template<>
640 string bformat(string const & fmt, string arg1)
641 {
642         BOOST_ASSERT(contains(fmt, "%1$s"));
643         string const str = subst(fmt, "%1$s", arg1);
644         return subst(str, "%%", "%");
645 }
646
647
648 template<>
649 string bformat(string const & fmt, char * arg1)
650 {
651         BOOST_ASSERT(contains(fmt, "%1$s"));
652         string const str = subst(fmt, "%1$s", arg1);
653         return subst(str, "%%", "%");
654 }
655 template<>
656 string bformat(string const & fmt, string arg1, string arg2)
657 {
658         BOOST_ASSERT(contains(fmt, "%1$s"));
659         BOOST_ASSERT(contains(fmt, "%2$s"));
660         string str = subst(fmt, "%1$s", arg1);
661         str = subst(str, "%2$s", arg2);
662         return subst(str, "%%", "%");
663 }
664
665
666 template<>
667 string bformat(string const & fmt, char const * arg1, string arg2)
668 {
669         BOOST_ASSERT(contains(fmt, "%1$s"));
670         BOOST_ASSERT(contains(fmt, "%2$s"));
671         string str = subst(fmt, "%1$s", arg1);
672         str = subst(fmt, "%2$s", arg2);
673         return subst(str, "%%", "%");
674 }
675
676
677 template<>
678 string bformat(string const & fmt, int arg1, int arg2)
679 {
680         BOOST_ASSERT(contains(fmt, "%1$d"));
681         BOOST_ASSERT(contains(fmt, "%2$d"));
682         string str = subst(fmt, "%1$d", convert<string>(arg1));
683         str = subst(str, "%2$d", convert<string>(arg2));
684         return subst(str, "%%", "%");
685 }
686
687
688 template<>
689 string bformat(string const & fmt, string arg1, string arg2, string arg3)
690 {
691         BOOST_ASSERT(contains(fmt, "%1$s"));
692         BOOST_ASSERT(contains(fmt, "%2$s"));
693         BOOST_ASSERT(contains(fmt, "%3$s"));
694         string str = subst(fmt, "%1$s", arg1);
695         str = subst(str, "%2$s", arg2);
696         str = subst(str, "%3$s", arg3);
697         return subst(str, "%%", "%");
698 }
699
700
701 template<>
702 string bformat(string const & fmt,
703                string arg1, string arg2, string arg3, string arg4)
704 {
705         BOOST_ASSERT(contains(fmt, "%1$s"));
706         BOOST_ASSERT(contains(fmt, "%2$s"));
707         BOOST_ASSERT(contains(fmt, "%3$s"));
708         BOOST_ASSERT(contains(fmt, "%4$s"));
709         string str = subst(fmt, "%1$s", arg1);
710         str = subst(str, "%2$s", arg2);
711         str = subst(str, "%3$s", arg3);
712         str = subst(str, "%4$s", arg4);
713         return subst(str, "%%", "%");
714 }
715
716 #endif
717 #endif
718
719 } // namespace support
720 } // namespace lyx