]> git.lyx.org Git - lyx.git/blob - src/support/qstring_helpers.cpp
Fix bug 3759 for qt < 4.2
[lyx.git] / src / support / qstring_helpers.cpp
1 /**
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.
5  *
6  * \author Dekel Tsur
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "qstring_helpers.h"
15 #include "unicode.h"
16
17 #include <QVector>
18
19
20 namespace lyx {
21
22 using std::string;
23
24 #if QT_VERSION < 0x040200
25 // We use QString::fromUcs4 in Qt 4.2 and higher
26 QString const toqstr(docstring const & str)
27 {
28         QString s;
29         int i = static_cast<int>(str.size());
30         s.resize(i);
31         for (; --i >= 0;) {
32                 char_type const c = str[i];
33                 if (is_utf16(c))
34                         // Use a simple cast in the common case for speed
35                         // reasons
36                         s[i] = static_cast<unsigned short>(c);
37                 else {
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());
44                         break;
45                 }
46         }
47         return s;
48 }
49 #endif
50
51
52 docstring const qstring_to_ucs4(QString const & qstr)
53 {
54 #if QT_VERSION >= 0x040200
55         QVector<uint> const ucs4 = qstr.toUcs4();
56         return docstring(ucs4.begin(), ucs4.end());
57 #else
58         int const ls = qstr.size();
59         docstring ucs4;
60         for (int i = 0; i < ls; ++i) {
61                 char_type const c = static_cast<char_type>(qstr[i].unicode());
62                 if (is_utf16(c))
63                         // Use a simple cast in the common case for speed
64                         // reasons
65                         ucs4 += c;
66                 else {
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()),
71                                 qstr.size());
72                         // Enable the compiler to do NRVO
73                         ucs4 = docstring(v.begin(), v.end());
74                         break;
75                 }
76         }
77         return ucs4;
78 #endif
79 }
80
81
82 string const fromqstr(QString const & str)
83 {
84         return str.isEmpty() ? string() : string(str.toUtf8());
85 }
86
87 } // namespace lyx