1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
22 #include <boost/regex.hpp>
33 #ifndef CXX_GLOBAL_CSTD
42 int compare_no_case(string const & s, string const & s2)
44 string::const_iterator p = s.begin();
45 string::const_iterator p2 = s2.begin();
47 while (p != s.end() && p2 != s2.end()) {
48 int const lc1 = tolower(*p);
49 int const lc2 = tolower(*p2);
51 return (lc1 < lc2) ? -1 : 1;
56 if (s.size() == s2.size())
58 if (s.size() < s2.size())
65 int ascii_tolower(int c) {
66 if (c >= 'A' && c <= 'Z')
73 int compare_ascii_no_case(string const & s, string const & s2)
75 string::const_iterator p = s.begin();
76 string::const_iterator p2 = s2.begin();
78 while (p != s.end() && p2 != s2.end()) {
79 int const lc1 = ascii_tolower(*p);
80 int const lc2 = ascii_tolower(*p2);
82 return (lc1 < lc2) ? -1 : 1;
87 if (s.size() == s2.size())
89 if (s.size() < s2.size())
95 int compare_no_case(string const & s, string const & s2, unsigned int len)
97 string::const_iterator p = s.begin();
98 string::const_iterator p2 = s2.begin();
100 while (i < len && p != s.end() && p2 != s2.end()) {
101 int const lc1 = tolower(*p);
102 int const lc2 = tolower(*p2);
104 return (lc1 < lc2) ? -1 : 1;
110 if (s.size() >= len && s2.size() >= len)
112 if (s.size() < s2.size())
118 bool isStrInt(string const & str)
120 if (str.empty()) return false;
122 // Remove leading and trailing white space chars.
123 string const tmpstr = frontStrip(strip(str));
124 if (tmpstr.empty()) return false;
126 string::const_iterator cit = tmpstr.begin();
127 if ((*cit) == '-') ++cit;
128 string::const_iterator end = tmpstr.end();
129 for (; cit != end; ++cit) {
130 if (!isdigit((*cit))) return false;
136 bool isStrUnsignedInt(string const & str)
138 if (str.empty()) return false;
140 // Remove leading and trailing white space chars.
141 string const tmpstr = frontStrip(strip(str));
142 if (tmpstr.empty()) return false;
144 string::const_iterator cit = tmpstr.begin();
145 string::const_iterator end = tmpstr.end();
146 for (; cit != end; ++cit) {
147 if (!isdigit((*cit))) return false;
153 int strToInt(string const & str)
156 // Remove leading and trailing white space chars.
157 string const tmpstr = frontStrip(strip(str));
158 // Do the conversion proper.
159 return lyx::atoi(tmpstr);
166 unsigned int strToUnsignedInt(string const & str)
168 if (isStrUnsignedInt(str)) {
169 // Remove leading and trailing white space chars.
170 string const tmpstr = frontStrip(strip(str));
171 // Do the conversion proper.
172 return lyx::atoi(tmpstr);
179 bool isStrDbl(string const & str)
181 if (str.empty()) return false;
183 // Remove leading and trailing white space chars.
184 string const tmpstr = frontStrip(strip(str));
185 if (tmpstr.empty()) return false;
186 // if (1 < tmpstr.count('.')) return false;
188 string::const_iterator cit = tmpstr.begin();
189 bool found_dot(false);
190 if ((*cit) == '-') ++cit;
191 string::const_iterator end = tmpstr.end();
192 for (; cit != end; ++cit) {
209 double strToDbl(string const & str)
212 // Remove leading and trailing white space chars.
213 string const tmpstr = frontStrip(strip(str));
214 // Do the conversion proper.
215 return ::atof(tmpstr.c_str());
222 char lowercase(char c)
224 return char(tolower(c));
228 char uppercase(char c)
230 return char(toupper(c));
236 // since we cannot use std::tolower and std::toupper directly in the
237 // calls to std::transform yet, we use these helper clases. (Lgb)
239 struct local_lowercase {
240 char operator()(char c) const {
245 struct local_uppercase {
246 char operator()(char c) const {
251 struct local_ascii_lowercase {
252 char operator()(char c) const {
253 return ascii_tolower(c);
257 } // end of anon namespace
259 string const lowercase(string const & a)
262 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
266 string const uppercase(string const & a)
269 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
274 string const ascii_lowercase(string const & a)
277 transform(tmp.begin(), tmp.end(), tmp.begin(),
278 local_ascii_lowercase());
283 bool prefixIs(string const & a, char const * pre)
287 size_t const l = strlen(pre);
288 string::size_type const alen = a.length();
290 if (l > alen || a.empty())
293 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
294 // Delete this code when the compilers get a bit better.
295 return ::strncmp(a.c_str(), pre, l) == 0;
297 // This is the code that we really want to use
298 // but until gcc ships with a basic_string that
299 // implements std::string correctly we have to
300 // use the code above.
301 return a.compare(0, l, pre, l) == 0;
307 bool prefixIs(string const & a, string const & pre)
309 string::size_type const prelen = pre.length();
310 string::size_type const alen = a.length();
312 if (prelen > alen || a.empty())
315 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
316 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
318 return a.compare(0, prelen, pre) == 0;
324 bool suffixIs(string const & a, char c)
326 if (a.empty()) return false;
327 return a[a.length() - 1] == c;
331 bool suffixIs(string const & a, char const * suf)
335 size_t const suflen = strlen(suf);
336 string::size_type const alen = a.length();
341 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
342 // Delete this code when the compilers get a bit better.
343 string tmp(a, alen - suflen);
344 return ::strncmp(tmp.c_str(), suf, suflen) == 0;
346 // This is the code that we really want to use
347 // but until gcc ships with a basic_string that
348 // implements std::string correctly we have to
349 // use the code above.
350 return a.compare(alen - suflen, suflen, suf) == 0;
356 bool suffixIs(string const & a, string const & suf)
358 string::size_type const suflen = suf.length();
359 string::size_type const alen = a.length();
364 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
365 string tmp(a, alen - suflen);
366 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
368 return a.compare(alen - suflen, suflen, suf) == 0;
374 bool contains(string const & a, string const & b)
378 return a.find(b) != string::npos;
382 bool contains(string const & a, char b)
386 return a.find(b) != string::npos;
390 bool containsOnly(string const & s, string const & cset)
392 return s.find_first_not_of(cset) == string::npos;
396 // ale970405+lasgoutt-970425
397 // rewritten to use new string (Lgb)
398 string const token(string const & a, char delim, int n)
400 if (a.empty()) return string();
402 string::size_type k = 0;
403 string::size_type i = 0;
405 // Find delimiter or end of string
407 if ((i = a.find(delim, i)) == string::npos)
411 // i is now the n'th delim (or string::npos)
412 if (i == string::npos) return string();
413 k = a.find(delim, i);
414 // k is now the n'th + 1 delim (or string::npos)
416 return a.substr(i, k - i);
420 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
421 // rewritten to use new string (Lgb)
422 int tokenPos(string const & a, char delim, string const & tok)
428 while (!str.empty()) {
429 str = split(str, tmptok, delim);
438 bool regexMatch(string const & a, string const & pattern)
440 // We massage the pattern a bit so that the usual
441 // shell pattern we all are used to will work.
442 // One nice thing about using a real regex is that
443 // things like "*.*[^~]" will work also.
444 // build the regex string.
445 string regex(pattern);
446 regex = subst(regex, ".", "\\.");
447 regex = subst(regex, "*", ".*");
448 boost::regex reg(regex);
449 return boost::regex_match(a, reg);
453 string const subst(string const & a, char oldchar, char newchar)
456 string::iterator lit = tmp.begin();
457 string::iterator end = tmp.end();
458 for (; lit != end; ++lit)
459 if ((*lit) == oldchar)
465 string const subst(string const & a,
466 char const * oldstr, string const & newstr)
471 string::size_type i = 0;
472 string::size_type olen = strlen(oldstr);
473 while ((i = lstr.find(oldstr, i)) != string::npos) {
474 lstr.replace(i, olen, newstr);
475 i += newstr.length(); // We need to be sure that we dont
476 // use the same i over and over again.
482 string const subst(string const & a,
483 string const & oldstr, string const & newstr)
486 string::size_type i = 0;
487 string::size_type const olen = oldstr.length();
488 while ((i = lstr.find(oldstr, i)) != string::npos) {
489 lstr.replace(i, olen, newstr);
490 i += newstr.length(); // We need to be sure that we dont
491 // use the same i over and over again.
497 string const strip(string const & a, char const * p)
501 if (a.empty() || !*p) return a;
503 string::size_type i = tmp.find_last_not_of(p);
504 if (i == a.length() - 1) return tmp; // no c's at end of a
505 if (i != string::npos)
506 tmp.erase(i + 1, string::npos);
507 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
508 // Ok This code is now suspect... (Lgb)
509 /// Needed for broken string::find_last_not_of
510 else if (tmp[0] != p[0]) {
511 if (a.length() == 1) return tmp;
512 tmp.erase(1, string::npos);
516 tmp.erase(); // only chars from p in the whole string
521 string const frontStrip(string const & a, char const * p)
525 if (a.empty() || !*p) return a;
527 string::size_type i = tmp.find_first_not_of(p);
534 string const split(string const & a, string & piece, char delim)
537 string::size_type i = a.find(delim);
538 if (i == a.length() - 1) {
539 piece = a.substr(0, i);
540 } else if (i != string::npos) {
541 piece = a.substr(0, i);
542 tmp = a.substr(i + 1);
545 tmp = a.substr(i + 1);
553 string const split(string const & a, char delim)
556 string::size_type i = a.find(delim);
557 if (i != string::npos) // found delim
558 tmp = a.substr(i + 1);
564 string const rsplit(string const & a, string & piece, char delim)
567 string::size_type i = a.rfind(delim);
568 if (i != string::npos) { // delimiter was found
569 piece = a.substr(0, i);
570 tmp = a.substr(i + 1);
571 } else { // delimter was not found
578 // This function escapes 8-bit characters and other problematic
579 // characters that cause problems in latex labels.
580 string const escape(string const & lab)
582 char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
583 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
585 for (string::size_type i = 0; i < lab.length(); ++i) {
586 unsigned char c= lab[i];
587 if (c >= 128 || c == '=' || c == '%') {
589 enc += hexdigit[c>>4];
590 enc += hexdigit[c & 15];
599 /// gives a vector of stringparts which have the delimiter delim
600 vector<string> const getVectorFromString(string const & str,
601 string const & delim)
606 string keys(strip(str));
608 string::size_type const idx = keys.find(delim);
609 if (idx == string::npos) {
610 vec.push_back(frontStrip(keys));
613 string const key = strip(frontStrip(keys.substr(0, idx)));
616 string::size_type const start = idx + delim.size();
617 keys = keys.substr(start);
622 // the same vice versa
623 string const getStringFromVector(vector<string> const & vec,
624 string const & delim)
628 for (vector<string>::const_iterator it = vec.begin();
629 it != vec.end(); ++it) {
630 string item = strip(frontStrip(*it));
631 if (item.empty()) continue;
633 if (i++ > 0) str += delim;