X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fsupport%2Fqstring_helpers.cpp;h=4f63167b4305f665e9cb04403b346a94054a73ee;hb=4804f637400627ef031bca55b8395d68bf63122a;hp=01c5de4b97ecdaa1b08624c30c68b1603658235d;hpb=1d64673c2202837146e62dfc3bd9c411f81e0dc1;p=lyx.git diff --git a/src/support/qstring_helpers.cpp b/src/support/qstring_helpers.cpp index 01c5de4b97..4f63167b43 100644 --- a/src/support/qstring_helpers.cpp +++ b/src/support/qstring_helpers.cpp @@ -1,87 +1,70 @@ /** - * \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 -#include "qstring_helpers.h" -#include "unicode.h" +#include "support/qstring_helpers.h" -#include +#include "support/debug.h" +#include "support/docstring.h" +#include +#include 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) { - QString s; - int i = static_cast(str.size()); - s.resize(i); - for (; --i >= 0;) { - char_type const c = str[i]; - if (is_utf16(c)) - // Use a simple cast in the common case for speed - // reasons - s[i] = static_cast(c); - else { - // A simple cast is not possible, so we need to use - // the full blown conversion. - std::vector const utf16 = - ucs4_to_utf16(str.data(), str.size()); - // Enable the compiler to do NRVO - s = QString::fromUtf16(&utf16[0], utf16.size()); - break; - } - } - 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 const ucs4 = qstr.toUcs4(); - return docstring(ucs4.begin(), ucs4.end()); -#else - int const ls = qstr.size(); - docstring ucs4; - for (int i = 0; i < ls; ++i) { - char_type const c = static_cast(qstr[i].unicode()); - if (is_utf16(c)) - // Use a simple cast in the common case for speed - // reasons - ucs4 += c; - else { - // A simple cast is not possible, so we need to use - // the full blown conversion. - std::vector const v = utf16_to_ucs4( - reinterpret_cast(qstr.utf16()), - qstr.size()); - // Enable the compiler to do NRVO - ucs4 = docstring(v.begin(), v.end()); - break; - } - } - 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