2 * \file qstring_helpers.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "qstring_helpers.h"
24 #if QT_VERSION < 0x040200
25 // We use QString::fromUcs4 in Qt 4.2 and higher
26 QString const toqstr(docstring const & str)
29 int i = static_cast<int>(str.size());
32 char_type const c = str[i];
34 // Use a simple cast in the common case for speed
36 s[i] = static_cast<unsigned short>(c);
38 // A simple cast is not possible, so we need to use
39 // the full blown conversion.
40 std::vector<unsigned short> const utf16 =
41 ucs4_to_utf16(str.data(), str.size());
42 // Enable the compiler to do NRVO
43 s = QString::fromUtf16(&utf16[0], utf16.size());
52 docstring const qstring_to_ucs4(QString const & qstr)
54 #if QT_VERSION >= 0x040200
55 QVector<uint> const ucs4 = qstr.toUcs4();
56 return docstring(ucs4.begin(), ucs4.end());
58 int const ls = qstr.size();
60 for (int i = 0; i < ls; ++i) {
61 char_type const c = static_cast<char_type>(qstr[i].unicode());
63 // Use a simple cast in the common case for speed
67 // A simple cast is not possible, so we need to use
68 // the full blown conversion.
69 std::vector<char_type> const v = utf16_to_ucs4(
70 reinterpret_cast<unsigned short const *>(qstr.utf16()),
72 // Enable the compiler to do NRVO
73 ucs4 = docstring(v.begin(), v.end());
82 string const fromqstr(QString const & str)
84 return str.isEmpty() ? string() : string(str.toUtf8());