]> git.lyx.org Git - features.git/blob - src/support/qstring_helpers.h
* support/qstring_helpers.h: erase ucs4_to_qstring() method.
[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 const 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         // FIXME: The following cast is not a real conversion but it work
80         // for the ucs2 subrange of unicode. Instead of an assertion we should
81         // return some special characters that indicates that its display is
82         // not supported.
83         BOOST_ASSERT(is_utf16(ucs4));
84         return QChar(static_cast<unsigned short>(ucs4));
85 }
86
87
88 /**
89  * toqstr - convert a UCS4 encoded docstring into a QString
90  *
91  * This is the preferred method of converting anything that possibly
92  * contains non-ASCII stuff to QString.
93  */
94 #if QT_VERSION >= 0x040200
95 inline QString const toqstr(docstring const & ucs4)
96 {
97         // If possible we let qt do the work, since this version does not
98         // need to be superfast.
99         return QString::fromUcs4(reinterpret_cast<uint const *>(ucs4.data()), ucs4.length());
100 }
101 #else
102 QString const toqstr(docstring const & ucs4);
103 #endif
104
105
106 /**
107  * qstring_to_ucs4 - convert a QString into a UCS4 encoded docstring
108  *
109  * This is the preferred method of converting anything that possibly
110  * contains non-ASCII stuff to docstring.
111  */
112 docstring const qstring_to_ucs4(QString const & qstr);
113
114
115 /**
116  * fromqstr - convert a QString into a UTF8 encoded std::string
117  *
118  * This should not be used except for output to lyxerr, since all possibly
119  * non-ASCII stuff should be stored in a docstring.
120  */
121 std::string const fromqstr(QString const & str);
122
123 } // namespace lyx
124
125 #endif // QSTRING_HELPERS_H