3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
9 * Full author contact details are available in file CREDITS.
14 #include "support/lstrings.h"
15 #include "support/lyxlib.h"
16 #include "support/convert.h"
20 #include <boost/tokenizer.hpp>
21 #include <boost/assert.hpp>
23 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
25 #include <boost/format.hpp>
41 #ifndef CXX_GLOBAL_CSTD
51 int compare_no_case(string const & s, string const & s2)
53 string::const_iterator p = s.begin();
54 string::const_iterator p2 = s2.begin();
56 while (p != s.end() && p2 != s2.end()) {
57 int const lc1 = tolower(*p);
58 int const lc2 = tolower(*p2);
60 return (lc1 < lc2) ? -1 : 1;
65 if (s.size() == s2.size())
67 if (s.size() < s2.size())
73 int compare_no_case(docstring const & s, docstring const & s2)
75 docstring::const_iterator p = s.begin();
76 docstring::const_iterator p2 = s2.begin();
78 while (p != s.end() && p2 != s2.end()) {
79 int const lc1 = tolower(*p);
80 int const lc2 = tolower(*p2);
82 return (lc1 < lc2) ? -1 : 1;
87 if (s.size() == s2.size())
89 if (s.size() < s2.size())
96 int ascii_tolower(int c) {
97 if (c >= 'A' && c <= 'Z')
104 int compare_ascii_no_case(string const & s, string const & s2)
106 string::const_iterator p = s.begin();
107 string::const_iterator p2 = s2.begin();
109 while (p != s.end() && p2 != s2.end()) {
110 int const lc1 = ascii_tolower(*p);
111 int const lc2 = ascii_tolower(*p2);
113 return (lc1 < lc2) ? -1 : 1;
118 if (s.size() == s2.size())
120 if (s.size() < s2.size())
126 int compare_no_case(string const & s, string const & s2, unsigned int len)
128 string::const_iterator p = s.begin();
129 string::const_iterator p2 = s2.begin();
131 while (i < len && p != s.end() && p2 != s2.end()) {
132 int const lc1 = tolower(*p);
133 int const lc2 = tolower(*p2);
135 return (lc1 < lc2) ? -1 : 1;
141 if (s.size() >= len && s2.size() >= len)
143 if (s.size() < s2.size())
149 bool isStrInt(string const & str)
151 if (str.empty()) return false;
153 // Remove leading and trailing white space chars.
154 string const tmpstr = trim(str);
155 if (tmpstr.empty()) return false;
157 string::const_iterator cit = tmpstr.begin();
158 if ((*cit) == '-') ++cit;
159 string::const_iterator end = tmpstr.end();
160 for (; cit != end; ++cit) {
161 if (!isdigit((*cit))) return false;
167 bool isStrUnsignedInt(string const & str)
169 if (str.empty()) return false;
171 // Remove leading and trailing white space chars.
172 string const tmpstr = trim(str);
173 if (tmpstr.empty()) return false;
175 string::const_iterator cit = tmpstr.begin();
176 string::const_iterator end = tmpstr.end();
177 for (; cit != end; ++cit) {
178 if (!isdigit((*cit))) return false;
184 bool isStrDbl(string const & str)
186 if (str.empty()) return false;
188 // Remove leading and trailing white space chars.
189 string const tmpstr = trim(str);
190 if (tmpstr.empty()) return false;
191 // if (1 < tmpstr.count('.')) return false;
193 string::const_iterator cit = tmpstr.begin();
194 bool found_dot(false);
195 if ((*cit) == '-') ++cit;
196 string::const_iterator end = tmpstr.end();
197 for (; cit != end; ++cit) {
214 char lowercase(char c)
216 return char(tolower(c));
220 char uppercase(char c)
222 return char(toupper(c));
225 // FIXME for lowercase() and uppercase() function below:
226 // 1) std::tolower() and std::toupper() are templates that
227 // compile fine with char_type. With the test (c >= 256) we
228 // do not trust these function to do the right thing with
230 // 2) these functions use the current locale, which is wrong
231 // if it is not latin1 based (latin1 is a subset of UCS4).
233 char_type lowercase(char_type c)
242 char_type uppercase(char_type c)
253 // since we cannot use std::tolower and std::toupper directly in the
254 // calls to std::transform yet, we use these helper clases. (Lgb)
256 struct local_lowercase {
257 char operator()(char c) const {
262 struct local_uppercase {
263 char operator()(char c) const {
268 struct local_ascii_lowercase {
269 char operator()(char c) const {
270 return ascii_tolower(c);
274 } // end of anon namespace
276 string const lowercase(string const & a)
279 transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
283 string const uppercase(string const & a)
286 transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
291 string const ascii_lowercase(string const & a)
294 transform(tmp.begin(), tmp.end(), tmp.begin(),
295 local_ascii_lowercase());
300 bool prefixIs(string const & a, string const & pre)
302 string::size_type const prelen = pre.length();
303 string::size_type const alen = a.length();
305 if (prelen > alen || a.empty())
308 #if defined(STD_STRING_IS_GOOD)
309 return a.compare(0, prelen, pre) == 0;
311 return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
317 bool suffixIs(string const & a, char c)
319 if (a.empty()) return false;
320 return a[a.length() - 1] == c;
324 bool suffixIs(string const & a, string const & suf)
326 string::size_type const suflen = suf.length();
327 string::size_type const alen = a.length();
332 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
333 string tmp(a, alen - suflen);
334 return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
336 return a.compare(alen - suflen, suflen, suf) == 0;
342 bool containsOnly(string const & s, string const & cset)
344 return s.find_first_not_of(cset) == string::npos;
348 // ale970405+lasgoutt-970425
349 // rewritten to use new string (Lgb)
350 string const token(string const & a, char delim, int n)
352 if (a.empty()) return string();
354 string::size_type k = 0;
355 string::size_type i = 0;
357 // Find delimiter or end of string
359 if ((i = a.find(delim, i)) == string::npos)
363 // i is now the n'th delim (or string::npos)
364 if (i == string::npos) return string();
365 k = a.find(delim, i);
366 // k is now the n'th + 1 delim (or string::npos)
368 return a.substr(i, k - i);
372 docstring const token(docstring const & a, char_type delim, int n)
374 if (a.empty()) return docstring();
376 string::size_type k = 0;
377 string::size_type i = 0;
379 // Find delimiter or end of string
381 if ((i = a.find(delim, i)) == docstring::npos)
385 // i is now the n'th delim (or string::npos)
386 if (i == docstring::npos) return docstring();
387 k = a.find(delim, i);
388 // k is now the n'th + 1 delim (or string::npos)
390 return a.substr(i, k - i);
394 // this could probably be faster and/or cleaner, but it seems to work (JMarc)
395 // rewritten to use new string (Lgb)
396 int tokenPos(string const & a, char delim, string const & tok)
402 while (!str.empty()) {
403 str = split(str, tmptok, delim);
414 template<typename Ch> inline
415 std::basic_string<Ch> const subst_char(std::basic_string<Ch> const & a,
416 Ch oldchar, Ch newchar)
418 typedef std::basic_string<Ch> String;
420 typename String::iterator lit = tmp.begin();
421 typename String::iterator end = tmp.end();
422 for (; lit != end; ++lit)
423 if ((*lit) == oldchar)
429 template<typename String> inline
430 String const subst_string(String const & a,
431 String const & oldstr, String const & newstr)
433 BOOST_ASSERT(!oldstr.empty());
435 typename String::size_type i = 0;
436 typename String::size_type const olen = oldstr.length();
437 while ((i = lstr.find(oldstr, i)) != string::npos) {
438 lstr.replace(i, olen, newstr);
439 i += newstr.length(); // We need to be sure that we dont
440 // use the same i over and over again.
448 string const subst(string const & a, char oldchar, char newchar)
450 return subst_char(a, oldchar, newchar);
454 docstring const subst(docstring const & a,
455 char_type oldchar, char_type newchar)
457 return subst_char(a, oldchar, newchar);
461 string const subst(string const & a,
462 string const & oldstr, string const & newstr)
464 return subst_string(a, oldstr, newstr);
468 docstring const subst(docstring const & a,
469 docstring const & oldstr, docstring const & newstr)
471 return subst_string(a, oldstr, newstr);
475 docstring const trim(docstring const & a, char const * p)
479 if (a.empty() || !*p)
482 docstring s = lyx::from_ascii(p);
483 docstring::size_type r = a.find_last_not_of(s);
484 docstring::size_type l = a.find_first_not_of(s);
486 // Is this the minimal test? (lgb)
487 if (r == docstring::npos && l == docstring::npos)
490 return a.substr(l, r - l + 1);
494 string const trim(string const & a, char const * p)
498 if (a.empty() || !*p)
501 string::size_type r = a.find_last_not_of(p);
502 string::size_type l = a.find_first_not_of(p);
504 // Is this the minimal test? (lgb)
505 if (r == string::npos && l == string::npos)
508 return a.substr(l, r - l + 1);
512 string const rtrim(string const & a, char const * p)
516 if (a.empty() || !*p)
519 string::size_type r = a.find_last_not_of(p);
521 // Is this test really needed? (Lgb)
522 if (r == string::npos)
525 return a.substr(0, r + 1);
529 string const ltrim(string const & a, char const * p)
533 if (a.empty() || !*p)
536 string::size_type l = a.find_first_not_of(p);
538 if (l == string::npos)
541 return a.substr(l, string::npos);
545 string const split(string const & a, string & piece, char delim)
548 string::size_type i = a.find(delim);
549 if (i == a.length() - 1) {
550 piece = a.substr(0, i);
551 } else if (i != string::npos) {
552 piece = a.substr(0, i);
553 tmp = a.substr(i + 1);
556 tmp = a.substr(i + 1);
564 string const split(string const & a, char delim)
567 string::size_type i = a.find(delim);
568 if (i != string::npos) // found delim
569 tmp = a.substr(i + 1);
575 string const rsplit(string const & a, string & piece, char delim)
578 string::size_type i = a.rfind(delim);
579 if (i != string::npos) { // delimiter was found
580 piece = a.substr(0, i);
581 tmp = a.substr(i + 1);
582 } else { // delimiter was not found
589 // This function escapes 8-bit characters and other problematic
590 // characters that cause problems in latex labels.
591 string const escape(string const & lab)
593 char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
594 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
596 for (string::size_type i = 0; i < lab.length(); ++i) {
597 unsigned char c= lab[i];
598 if (c >= 128 || c == '=' || c == '%') {
600 enc += hexdigit[c>>4];
601 enc += hexdigit[c & 15];
610 /// gives a vector of stringparts which have the delimiter delim
611 vector<string> const getVectorFromString(string const & str,
612 string const & delim)
614 // Lars would like this code to go, but for now his replacement (below)
615 // doesn't fullfil the same function. I have, therefore, reactivated the
616 // old code for now. Angus 11 Nov 2002.
621 string keys = rtrim(str);
623 string::size_type const idx = keys.find(delim);
624 if (idx == string::npos) {
625 vec.push_back(ltrim(keys));
628 string const key = trim(keys.substr(0, idx));
631 string::size_type const start = idx + delim.size();
632 keys = keys.substr(start);
636 boost::char_separator<char> sep(delim.c_str());
637 boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
638 return vector<string>(tokens.begin(), tokens.end());
643 // the same vice versa
644 string const getStringFromVector(vector<string> const & vec,
645 string const & delim)
649 for (vector<string>::const_iterator it = vec.begin();
650 it != vec.end(); ++it) {
651 string item = trim(*it);
662 int findToken(char const * const str[], string const & search_token)
666 while (str[i][0] && str[i] != search_token)
674 docstring const externalLineEnding(docstring const & str)
676 #if defined(__APPLE__)
677 // The MAC clipboard uses \r for lineendings, and we use \n
678 return subst(str, '\n', '\r');
679 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
680 // Windows clipboard uses \r\n for lineendings, and we use \n
681 return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
688 docstring const internalLineEnding(docstring const & str)
690 docstring const s = subst(str,
691 lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
692 return subst(s, '\r', '\n');
696 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
700 docstring bformat(docstring const & fmt, int arg1)
702 return (boost::basic_format<char_type>(fmt) % arg1).str();
707 docstring bformat(docstring const & fmt, long arg1)
709 return (boost::basic_format<char_type>(fmt) % arg1).str();
714 docstring bformat(docstring const & fmt, unsigned int arg1)
716 return (boost::basic_format<char_type>(fmt) % arg1).str();
721 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
723 return (boost::basic_format<char_type>(fmt) % arg1).str();
728 docstring bformat(docstring const & fmt, char * arg1)
730 return (boost::basic_format<char_type>(fmt) % arg1).str();
735 docstring bformat(docstring const & fmt, int arg1, int arg2)
737 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
742 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
744 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
749 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
751 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
756 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
758 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
763 docstring bformat(docstring const & fmt,
764 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
766 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
772 docstring bformat(docstring const & fmt, int arg1)
774 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
775 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
776 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
781 docstring bformat(docstring const & fmt, long arg1)
783 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
784 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
785 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
790 docstring bformat(docstring const & fmt, unsigned int arg1)
792 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
793 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
794 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
799 docstring bformat(docstring const & fmt, docstring arg1)
801 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
802 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
803 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
808 docstring bformat(docstring const & fmt, char * arg1)
810 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
811 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
812 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
817 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
819 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
820 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
821 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
822 str = subst(str, lyx::from_ascii("%2$s"), arg2);
823 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
828 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
830 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
831 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
832 docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
833 str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
834 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
839 docstring bformat(docstring const & fmt, int arg1, int arg2)
841 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
842 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
843 docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
844 str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
845 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
850 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
852 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
853 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
854 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
855 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
856 str = subst(str, lyx::from_ascii("%2$s"), arg2);
857 str = subst(str, lyx::from_ascii("%3$s"), arg3);
858 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
863 docstring bformat(docstring const & fmt,
864 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
866 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
867 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
868 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
869 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
870 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
871 str = subst(str, lyx::from_ascii("%2$s"), arg2);
872 str = subst(str, lyx::from_ascii("%3$s"), arg3);
873 str = subst(str, lyx::from_ascii("%4$s"), arg4);
874 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
880 } // namespace support