]> git.lyx.org Git - lyx.git/blobdiff - src/support/lstrings.cpp
CMake: fix merged build, seems GCC could not handle the namespaces correctly
[lyx.git] / src / support / lstrings.cpp
index 42be23f4a8f974bda931a479ab5997c7bb56f46b..fef11cf1d4de92c1294d913d37cb590d84f9b825 100644 (file)
@@ -504,7 +504,7 @@ bool prefixIs(docstring const & a, docstring const & pre)
 
 bool suffixIs(string const & a, char c)
 {
-       if (a.empty()) 
+       if (a.empty())
                return false;
        return a[a.length() - 1] == c;
 }
@@ -899,6 +899,16 @@ string const rsplit(string const & a, string & piece, char delim)
 }
 
 
+docstring const rsplit(docstring const & a, char_type delim)
+{
+       docstring tmp;
+       size_t i = a.rfind(delim);
+       if (i != string::npos)
+               tmp = a.substr(i + 1);
+       return tmp;
+}
+
+
 docstring const escape(docstring const & lab)
 {
        char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
@@ -931,37 +941,34 @@ 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, 
-                            size_t const width)
+vector<docstring> wrapToVec(docstring const & str, int ind,
+                           size_t const width)
 {
        docstring s = trim(str);
-       if (s.empty()) 
+       if (s.empty())
                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()) 
+       if (!s.empty())
                retval.push_back(s);
        return retval;
 }
@@ -972,7 +979,7 @@ vector<docstring> wrapToVec(docstring const & str, int const ind,
 docstring wrap(docstring const & str, int const ind, size_t const width)
 {
        docstring s = trim(str);
-       if (s.empty()) 
+       if (s.empty())
                return docstring();
 
        vector<docstring> const svec = wrapToVec(str, ind, width);
@@ -981,22 +988,22 @@ docstring wrap(docstring const & str, int const ind, size_t const width)
 
 
 docstring wrapParas(docstring const & str, int const indent,
-                    size_t const width, size_t const maxlines)
+                   size_t const width, size_t const maxlines)
 {
        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);
-               int const nlines = tmp.size();
+               size_t const nlines = tmp.size();
                if (nlines == 0)
                        continue;
-               int const curlines = retval.size();
+               size_t const curlines = retval.size();
                if (maxlines > 0 && curlines + nlines >= maxlines) {
                        tmp.resize(maxlines - curlines - 1);
                        tmp.push_back(from_ascii("..."));
@@ -1082,14 +1089,14 @@ vector<docstring> const getVectorFromString(docstring const & str,
 
 
 string const getStringFromVector(vector<string> const & vec,
-                                 string const & delim)
+                                string const & delim)
 {
        return getStringFromVector<string>(vec, delim);
 }
 
 
 docstring const getStringFromVector(vector<docstring> const & vec,
-                                    docstring const & delim)
+                                   docstring const & delim)
 {
        return getStringFromVector<docstring>(vec, delim);
 }
@@ -1163,6 +1170,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)
 {