/**
- * \file qstring_helpers.cpp
+ * \file qstring_helper.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Dekel Tsur
- * \author Jürgen Spitzmüller
*
* Full author contact details are available in file CREDITS.
+ *
+ * A collection of unicode conversion functions, using iconv.
*/
#include <config.h>
-#include "qstring_helpers.h"
+#include "support/qstring_helpers.h"
-#include <QVector>
+#include "support/debug.h"
+#include "support/docstring.h"
+#include <QString>
+#include <QVector>
namespace lyx {
-using std::string;
+LyXErr & operator<<(LyXErr & err, QString const & str)
+{
+ return err << fromqstr(str);
+}
+
+
+QString toqstr(char const * str)
+{
+ return QString::fromUtf8(str);
+}
+
+QString toqstr(std::string const & str)
+{
+ return toqstr(str.c_str());
+}
+
-#if QT_VERSION < 0x040200
-// We use QString::fromUcs4 in Qt 4.2 and higher
-QString const toqstr(docstring const & str)
+QString toqstr(docstring const & ucs4)
{
- // This does not properly convert surrogate pairs
- QString s;
- int i = static_cast<int>(str.size());
- s.resize(i);
- for (; --i >= 0;)
- s[i] = ucs4_to_qchar(str[i]);
- return s;
+ // If possible we let qt do the work, since this version does not
+ // need to be superfast.
+ if (ucs4.empty())
+ return QString();
+ return QString::fromUcs4((uint const *)ucs4.data(), ucs4.length());
}
-#endif
+QString toqstr(char_type ucs4)
+{
+ union { char_type c; uint i; } u = { ucs4 };
+ return QString::fromUcs4(&u.i, 1);
+}
-docstring const qstring_to_ucs4(QString const & qstr)
+docstring qstring_to_ucs4(QString const & qstr)
{
-#if QT_VERSION >= 0x040200
+ if (qstr.isEmpty())
+ return docstring();
QVector<uint> const ucs4 = qstr.toUcs4();
- return docstring(ucs4.begin(), ucs4.end());
-#else
- // This does not properly convert surrogate pairs
- int const ls = qstr.size();
- docstring ucs4;
- for (int i = 0; i < ls; ++i)
- ucs4 += qchar_to_ucs4(qstr[i].unicode());
- return ucs4;
-#endif
+ return docstring((char_type const *)(ucs4.constData()), ucs4.size());
}
-
-string const fromqstr(QString const & str)
+std::string fromqstr(QString const & str)
{
- return str.isEmpty() ? string() : string(str.toUtf8());
+ return str.isEmpty() ? std::string() : std::string(str.toUtf8());
}
} // namespace lyx