]> git.lyx.org Git - lyx.git/blobdiff - src/support/lstrings.cpp
add generic helper class for calling functions in gui thread
[lyx.git] / src / support / lstrings.cpp
index 1c53fd40c1302a9010d93ead772665cb4a049206..ed2b9f9cafffc9b6537186a12eaf260bcb5b0c13 100644 (file)
@@ -315,6 +315,23 @@ bool isStrDbl(string const & str)
 }
 
 
+bool hasDigit(docstring const & str)
+{
+       if (str.empty())
+               return false;
+
+       docstring::const_iterator cit = str.begin();
+       docstring::const_iterator const end = str.end();
+       for (; cit != end; ++cit) {
+               if (*cit == ' ')
+                       continue;
+               if (isdigit((*cit)))
+                       return true;
+       }
+       return false;
+}
+
+
 static bool isHexChar(char_type c)
 {
        return c == '0' ||
@@ -415,6 +432,16 @@ char_type uppercase(char_type c)
 }
 
 
+bool isLowerCase(char_type ch) {
+       return lowercase(ch) == ch;
+}
+
+
+bool isUpperCase(char_type ch) {
+       return uppercase(ch) == ch;
+}
+
+
 namespace {
 
 // since we cannot use tolower and toupper directly in the
@@ -649,6 +676,7 @@ basic_string<Ch> const subst_char(basic_string<Ch> const & a,
        return tmp;
 }
 
+
 /// Substitute all \a oldchar with \a newchar
 docstring const subst_char(docstring const & a,
        docstring::value_type oldchar, docstring::value_type newchar)
@@ -680,6 +708,7 @@ String const subst_string(String const & a,
        return lstr;
 }
 
+
 docstring const subst_string(docstring const & a,
                docstring const & oldstr, docstring const & newstr)
 {
@@ -725,6 +754,19 @@ docstring const subst(docstring const & a,
 }
 
 
+/// Count all occurences of char \a chr inside \a str
+int count_char(docstring const & str, docstring::value_type chr)
+{
+       int count = 0;
+       docstring::const_iterator lit = str.begin();
+       docstring::const_iterator end = str.end();
+       for (; lit != end; ++lit)
+               if ((*lit) == chr)
+                       count++;
+       return count;
+}
+
+
 docstring const trim(docstring const & a, char const * p)
 {
        LASSERT(p, /**/);
@@ -941,7 +983,7 @@ docstring const escape(docstring const & lab)
 namespace {
 
 // this doesn't check whether str is empty, so do that first.
-vector<docstring> wrapToVec(docstring const & str, int const ind,
+vector<docstring> wrapToVec(docstring const & str, int ind,
                            size_t const width)
 {
        docstring s = trim(str);
@@ -949,27 +991,24 @@ vector<docstring> wrapToVec(docstring const & str, int const ind,
                return vector<docstring>();
 
        docstring indent;
-       if (ind < 0)
-               for (int j = 0; j > ind; --j)
-                       indent += " ";
-       else if (ind > 0)
-               for (int j = 0; j < ind; ++j)
-                       s = " " + s;
+       if (ind < 0) {
+               indent.insert(0, -ind, ' ');
+               ind = 0;
+       } else if (ind > 0)
+               s.insert(0, ind, ' ');
 
        vector<docstring> retval;
        while (s.size() > width) {
-               int i = width - 1;
-               // find the last space
-               for (; i >= 0; --i)
-                       if (s[i] == ' ')
-                               break;
-               if (i < 0) {
+               // find the last space within the first 'width' chars
+               size_t const i = s.find_last_of(' ', width - 1);
+               if (i == docstring::npos || i <= size_t(ind)) {
                        // no space found
                        s = s.substr(0, width - 3) + "...";
                        break;
                }
                retval.push_back(s.substr(0, i));
                s = indent + s.substr(i);
+               ind = indent.size();
        }
        if (!s.empty())
                retval.push_back(s);
@@ -996,11 +1035,11 @@ docstring wrapParas(docstring const & str, int const indent,
        if (str.empty())
                return docstring();
 
-       vector<docstring> pars = getVectorFromString(str, from_ascii("\n"), true);
+       vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
        vector<docstring> retval;
 
-       vector<docstring>::iterator it = pars.begin();
-       vector<docstring>::iterator en = pars.end();
+       vector<docstring>::const_iterator it = pars.begin();
+       vector<docstring>::const_iterator const en = pars.end();
        for (; it != en; ++it) {
                vector<docstring> tmp = wrapToVec(*it, indent, width);
                size_t const nlines = tmp.size();
@@ -1173,6 +1212,17 @@ docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
 }
 
 
+template<>
+docstring bformat(docstring const & fmt, docstring arg1, int arg2)
+{
+       LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
+       LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
+       docstring str = subst(fmt, from_ascii("%1$s"), arg1);
+       str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
+       return subst(str, from_ascii("%%"), from_ascii("%"));
+}
+
+
 template<>
 docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
 {