* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
- * \author Lars Gullik Bjønnes
+ * \author Lars Gullik Bjønnes
* \author Jean-Marc Lasgouttes
* \author Dekel Tsur
*
#include "support/textutils.h"
#include <boost/tokenizer.hpp>
-#include "support/assert.h"
+#include "support/lassert.h"
#include <QString>
#include <QVector>
+#include <cstdio>
#include <algorithm>
using namespace std;
}
-
-QString toqstr(char const * str)
-{
- return QString::fromUtf8(str);
-}
-
-QString toqstr(std::string const & str)
-{
- return toqstr(str.c_str());
-}
-
-
-QString toqstr(docstring const & ucs4)
-{
- // If possible we let qt do the work, since this version does not
- // need to be superfast.
- return QString::fromUcs4((uint const *)ucs4.data(), ucs4.length());
-}
-
-QString toqstr(char_type ucs4)
-{
- union { char_type c; uint i; } u = { ucs4 };
- return QString::fromUcs4(&u.i, 1);
-}
-
-docstring qstring_to_ucs4(QString const & qstr)
-{
- if (qstr.isEmpty())
- return docstring();
- QVector<uint> const ucs4 = qstr.toUcs4();
- return docstring((char_type const *)(ucs4.constData()), ucs4.size());
-}
-
-std::string fromqstr(QString const & str)
-{
- return str.isEmpty() ? std::string() : std::string(str.toUtf8());
-}
-
-
/**
* Convert a UCS4 character into a QChar.
* This is a hack (it does only make sense for the common part of the UCS4
}
+bool isLower(char_type c)
+{
+ if (!is_utf16(c))
+ return false;
+ return ucs4_to_qchar(c).isLower();
+}
+
+
bool isAlphaASCII(char_type c)
{
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
bool suffixIs(string const & a, char c)
{
- if (a.empty()) return false;
+ if (a.empty())
+ return false;
return a[a.length() - 1] == c;
}
}
+bool suffixIs(docstring const & a, docstring const & suf)
+{
+ size_t const suflen = suf.length();
+ size_t const alen = a.length();
+ return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
+}
+
+
bool containsOnly(string const & s, string const & cset)
{
return s.find_first_not_of(cset) == string::npos;
}
+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)
+{
+ docstring s = trim(str);
+ 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;
+
+ 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) {
+ // no space found
+ s = s.substr(0, width - 3) + "...";
+ break;
+ }
+ retval.push_back(s.substr(0, i));
+ s = indent + s.substr(i);
+ }
+ if (!s.empty())
+ retval.push_back(s);
+ return retval;
+}
+
+}
+
+
+docstring wrap(docstring const & str, int const ind, size_t const width)
+{
+ docstring s = trim(str);
+ if (s.empty())
+ return docstring();
+
+ vector<docstring> const svec = wrapToVec(str, ind, width);
+ return getStringFromVector(svec, from_ascii("\n"));
+}
+
+
+docstring wrapParas(docstring const & str, int const indent,
+ size_t const width, size_t const maxlines)
+{
+ if (str.empty())
+ return docstring();
+
+ vector<docstring> pars = getVectorFromString(str, from_ascii("\n"), true);
+ vector<docstring> retval;
+
+ vector<docstring>::iterator it = pars.begin();
+ vector<docstring>::iterator en = pars.end();
+ for (; it != en; ++it) {
+ vector<docstring> tmp = wrapToVec(*it, indent, width);
+ size_t const nlines = tmp.size();
+ if (nlines == 0)
+ continue;
+ size_t const curlines = retval.size();
+ if (maxlines > 0 && curlines + nlines >= maxlines) {
+ tmp.resize(maxlines - curlines - 1);
+ tmp.push_back(from_ascii("..."));
+ }
+ retval.insert(retval.end(), tmp.begin(), tmp.end());
+ if (maxlines > 0 && retval.size() >= maxlines)
+ break;
+ }
+ return getStringFromVector(retval, from_ascii("\n"));
+}
+
+
namespace {
template<typename String> vector<String> const
-getVectorFromStringT(String const & str, String const & delim)
+getVectorFromStringT(String const & str, String const & delim, bool keepempty)
{
// Lars would like this code to go, but for now his replacement (below)
// doesn't fullfil the same function. I have, therefore, reactivated the
break;
}
String const key = trim(keys.substr(0, idx));
- if (!key.empty())
+ if (!key.empty() || keepempty)
vec.push_back(key);
size_t const start = idx + delim.size();
keys = keys.substr(start);
#endif
}
+
+template<typename String> const String
+ getStringFromVector(vector<String> const & vec, String const & delim)
+{
+ String str;
+ typename vector<String>::const_iterator it = vec.begin();
+ typename vector<String>::const_iterator en = vec.end();
+ for (; it != en; ++it) {
+ String item = trim(*it);
+ if (item.empty())
+ continue;
+ if (!str.empty())
+ str += delim;
+ str += item;
+ }
+ return str;
+}
+
} // namespace anon
vector<string> const getVectorFromString(string const & str,
- string const & delim)
+ string const & delim,
+ bool keepempty)
{
- return getVectorFromStringT<string>(str, delim);
+ return getVectorFromStringT<string>(str, delim, keepempty);
}
vector<docstring> const getVectorFromString(docstring const & str,
- docstring const & delim)
+ docstring const & delim,
+ bool keepempty)
{
- return getVectorFromStringT<docstring>(str, delim);
+ return getVectorFromStringT<docstring>(str, delim, keepempty);
}
-// the same vice versa
string const getStringFromVector(vector<string> const & vec,
string const & delim)
{
- string str;
- int i = 0;
- for (vector<string>::const_iterator it = vec.begin();
- it != vec.end(); ++it) {
- string item = trim(*it);
- if (item.empty())
- continue;
- if (i++ > 0)
- str += delim;
- str += item;
- }
- return str;
+ return getStringFromVector<string>(vec, delim);
+}
+
+
+docstring const getStringFromVector(vector<docstring> const & vec,
+ docstring const & delim)
+{
+ return getStringFromVector<docstring>(vec, delim);
}
}
-docstring const externalLineEnding(docstring const & str)
-{
-#if defined(__APPLE__)
- // The MAC clipboard uses \r for lineendings, and we use \n
- return subst(str, '\n', '\r');
-#elif defined (_WIN32) || (defined (__CYGWIN__) && defined (X_DISPLAY_MISSING))
- // Windows clipboard uses \r\n for lineendings, and we use \n
- return subst(str, from_ascii("\n"), from_ascii("\r\n"));
-#else
- return str;
-#endif
-}
-
-
-docstring const internalLineEnding(docstring const & str)
-{
- docstring const s = subst(str, from_ascii("\r\n"), from_ascii("\n"));
- return subst(s, '\r', '\n');
-}
-
-
template<>
docstring bformat(docstring const & fmt, int arg1)
{