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