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 string const trim(string const & a, char const * p)
479 if (a.empty() || !*p)
482 string::size_type r = a.find_last_not_of(p);
483 string::size_type l = a.find_first_not_of(p);
485 // Is this the minimal test? (lgb)
486 if (r == string::npos && l == string::npos)
489 return a.substr(l, r - l + 1);
493 string const rtrim(string const & a, char const * p)
497 if (a.empty() || !*p)
500 string::size_type r = a.find_last_not_of(p);
502 // Is this test really needed? (Lgb)
503 if (r == string::npos)
506 return a.substr(0, r + 1);
510 string const ltrim(string const & a, char const * p)
514 if (a.empty() || !*p)
517 string::size_type l = a.find_first_not_of(p);
519 if (l == string::npos)
522 return a.substr(l, string::npos);
526 string const split(string const & a, string & piece, char delim)
529 string::size_type i = a.find(delim);
530 if (i == a.length() - 1) {
531 piece = a.substr(0, i);
532 } else if (i != string::npos) {
533 piece = a.substr(0, i);
534 tmp = a.substr(i + 1);
537 tmp = a.substr(i + 1);
545 string const split(string const & a, char delim)
548 string::size_type i = a.find(delim);
549 if (i != string::npos) // found delim
550 tmp = a.substr(i + 1);
556 string const rsplit(string const & a, string & piece, char delim)
559 string::size_type i = a.rfind(delim);
560 if (i != string::npos) { // delimiter was found
561 piece = a.substr(0, i);
562 tmp = a.substr(i + 1);
563 } else { // delimiter was not found
570 // This function escapes 8-bit characters and other problematic
571 // characters that cause problems in latex labels.
572 string const escape(string const & lab)
574 char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
575 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
577 for (string::size_type i = 0; i < lab.length(); ++i) {
578 unsigned char c= lab[i];
579 if (c >= 128 || c == '=' || c == '%') {
581 enc += hexdigit[c>>4];
582 enc += hexdigit[c & 15];
591 /// gives a vector of stringparts which have the delimiter delim
592 vector<string> const getVectorFromString(string const & str,
593 string const & delim)
595 // Lars would like this code to go, but for now his replacement (below)
596 // doesn't fullfil the same function. I have, therefore, reactivated the
597 // old code for now. Angus 11 Nov 2002.
602 string keys = rtrim(str);
604 string::size_type const idx = keys.find(delim);
605 if (idx == string::npos) {
606 vec.push_back(ltrim(keys));
609 string const key = trim(keys.substr(0, idx));
612 string::size_type const start = idx + delim.size();
613 keys = keys.substr(start);
617 boost::char_separator<char> sep(delim.c_str());
618 boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
619 return vector<string>(tokens.begin(), tokens.end());
624 // the same vice versa
625 string const getStringFromVector(vector<string> const & vec,
626 string const & delim)
630 for (vector<string>::const_iterator it = vec.begin();
631 it != vec.end(); ++it) {
632 string item = trim(*it);
643 int findToken(char const * const str[], string const & search_token)
647 while (str[i][0] && str[i] != search_token)
655 docstring const externalLineEnding(docstring const & str)
657 #if defined(__APPLE__)
658 // The MAC clipboard uses \r for lineendings, and we use \n
659 return subst(str, '\n', '\r');
660 #elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
661 // Windows clipboard uses \r\n for lineendings, and we use \n
662 return subst(str, lyx::from_ascii("\n"), lyx::from_ascii("\r\n"));
669 docstring const internalLineEnding(docstring const & str)
671 docstring const s = subst(str,
672 lyx::from_ascii("\r\n"), lyx::from_ascii("\n"));
673 return subst(s, '\r', '\n');
677 #ifndef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
681 docstring bformat(docstring const & fmt, int arg1)
683 return (boost::basic_format<char_type>(fmt) % arg1).str();
688 docstring bformat(docstring const & fmt, long arg1)
690 return (boost::basic_format<char_type>(fmt) % arg1).str();
695 docstring bformat(docstring const & fmt, unsigned int arg1)
697 return (boost::basic_format<char_type>(fmt) % arg1).str();
702 docstring bformat<docstring>(docstring const & fmt, docstring arg1)
704 return (boost::basic_format<char_type>(fmt) % arg1).str();
709 docstring bformat(docstring const & fmt, char * arg1)
711 return (boost::basic_format<char_type>(fmt) % arg1).str();
716 docstring bformat(docstring const & fmt, int arg1, int arg2)
718 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
723 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
725 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
730 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
732 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
737 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
739 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
744 docstring bformat(docstring const & fmt,
745 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
747 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
753 docstring bformat(docstring const & fmt, int arg1)
755 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
756 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
757 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
762 docstring bformat(docstring const & fmt, long arg1)
764 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
765 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
766 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
771 docstring bformat(docstring const & fmt, unsigned int arg1)
773 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
774 docstring const str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
775 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
780 docstring bformat(docstring const & fmt, docstring arg1)
782 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
783 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
784 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
789 docstring bformat(docstring const & fmt, char * arg1)
791 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
792 docstring const str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
793 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
798 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
800 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
801 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
802 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
803 str = subst(str, lyx::from_ascii("%2$s"), arg2);
804 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
809 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
811 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
812 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
813 docstring str = subst(fmt, lyx::from_ascii("%1$s"), lyx::from_ascii(arg1));
814 str = subst(fmt, lyx::from_ascii("%2$s"), arg2);
815 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
820 docstring bformat(docstring const & fmt, int arg1, int arg2)
822 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$d")));
823 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$d")));
824 docstring str = subst(fmt, lyx::from_ascii("%1$d"), convert<docstring>(arg1));
825 str = subst(str, lyx::from_ascii("%2$d"), convert<docstring>(arg2));
826 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
831 docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
833 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
834 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
835 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
836 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
837 str = subst(str, lyx::from_ascii("%2$s"), arg2);
838 str = subst(str, lyx::from_ascii("%3$s"), arg3);
839 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
844 docstring bformat(docstring const & fmt,
845 docstring arg1, docstring arg2, docstring arg3, docstring arg4)
847 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%1$s")));
848 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%2$s")));
849 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%3$s")));
850 BOOST_ASSERT(contains(fmt, lyx::from_ascii("%4$s")));
851 docstring str = subst(fmt, lyx::from_ascii("%1$s"), arg1);
852 str = subst(str, lyx::from_ascii("%2$s"), arg2);
853 str = subst(str, lyx::from_ascii("%3$s"), arg3);
854 str = subst(str, lyx::from_ascii("%4$s"), arg4);
855 return subst(str, lyx::from_ascii("%%"), lyx::from_ascii("%"));
861 } // namespace support