]> git.lyx.org Git - features.git/blob - src/support/qstring_helpers.h
Remove warnings reported with gcc 4.3:
[features.git] / src / support / qstring_helpers.h
1 // -*- C++ -*-
2 /**
3  * \file qstring_helpers.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Dekel Tsur
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef QSTRING_HELPERS_H
13 #define QSTRING_HELPERS_H
14
15 #include "support/docstring.h"
16
17 #include <QChar>
18 #include <QString>
19
20 #include <boost/assert.hpp>
21
22 namespace lyx {
23
24 /**
25  * toqstr - convert a UTF8 encoded char * into a QString
26  *
27  * This should not be used, since all possibly non-ASCII stuff should be
28  * stored in a docstring.
29  */
30 inline QString const toqstr(char const * str)
31 {
32         return QString::fromUtf8(str);
33 }
34
35
36 /**
37  * toqstr - convert a UTF8 encoded std::string into a QString
38  *
39  * This should not be used, since all possibly non-ASCII stuff should be
40  * stored in a docstring.
41  */
42 inline QString const toqstr(std::string const & str)
43 {
44         return toqstr(str.c_str());
45 }
46
47
48 /// Is \p c a valid utf16 char?
49 inline bool is_utf16(char_type c)
50 {
51         // 0xd800 ... 0xdfff is the range of surrogate pairs.
52         return c < 0xd800 || (c > 0xdfff && c < 0x10000);
53 }
54
55
56 /**
57  * Convert a QChar into a UCS4 character.
58  * This is a hack (it does only make sense for the common part of the UCS4
59  * and UTF16 encodings) and should not be used.
60  * This does only exist because of performance reasons (a real conversion
61  * using iconv is too slow on windows).
62  */
63 inline char_type qchar_to_ucs4(QChar const & qchar)
64 {
65         BOOST_ASSERT(is_utf16(static_cast<char_type>(qchar.unicode())));
66         return static_cast<char_type>(qchar.unicode());
67 }
68
69
70 /**
71  * Convert a UCS4 character into a QChar.
72  * This is a hack (it does only make sense for the common part of the UCS4
73  * and UTF16 encodings) and should not be used.
74  * This does only exist because of performance reasons (a real conversion
75  * using iconv is too slow on windows).
76  */
77 inline QChar const ucs4_to_qchar(char_type const ucs4)
78 {
79         BOOST_ASSERT(is_utf16(ucs4));
80         return QChar(static_cast<unsigned short>(ucs4));
81 }
82
83
84 /**
85  * toqstr - convert a UCS4 encoded docstring into a QString
86  *
87  * This is the preferred method of converting anything that possibly
88  * contains non-ASCII stuff to QString.
89  */
90 #if QT_VERSION >= 0x040200
91 inline QString const toqstr(docstring const & ucs4)
92 {
93         // If possible we let qt do the work, since this version does not
94         // need to be superfast.
95         return QString::fromUcs4(reinterpret_cast<uint const *>(ucs4.data()), ucs4.length());
96 }
97 #else
98 QString const toqstr(docstring const & ucs4);
99 #endif
100
101
102 /**
103  * qstring_to_ucs4 - convert a QString into a UCS4 encoded docstring
104  *
105  * This is the preferred method of converting anything that possibly
106  * contains non-ASCII stuff to docstring.
107  */
108 docstring const qstring_to_ucs4(QString const & qstr);
109
110
111 /**
112  * fromqstr - convert a QString into a UTF8 encoded std::string
113  *
114  * This should not be used except for output to lyxerr, since all possibly
115  * non-ASCII stuff should be stored in a docstring.
116  */
117 std::string const fromqstr(QString const & str);
118
119 } // namespace lyx
120
121 #endif // QSTRING_HELPERS_H