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.
9 * Full author contact details are available in file CREDITS.
12 #ifndef QSTRING_HELPERS_H
13 #define QSTRING_HELPERS_H
15 #include "support/docstring.h"
20 #include <boost/assert.hpp>
25 * toqstr - convert a UTF8 encoded char * into a QString
27 * This should not be used, since all possibly non-ASCII stuff should be
28 * stored in a docstring.
30 inline QString const toqstr(char const * str)
32 return QString::fromUtf8(str);
37 * toqstr - convert a UTF8 encoded std::string into a QString
39 * This should not be used, since all possibly non-ASCII stuff should be
40 * stored in a docstring.
42 inline QString const toqstr(std::string const & str)
44 return toqstr(str.c_str());
48 /// Is \p c a valid utf16 char?
49 inline bool is_utf16(char_type c)
51 // 0xd800 ... 0xdfff is the range of surrogate pairs.
52 return c < 0xd800 || (c > 0xdfff && c < 0x10000);
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).
63 inline char_type qchar_to_ucs4(QChar const & qchar)
65 BOOST_ASSERT(is_utf16(static_cast<char_type>(qchar.unicode())));
66 return static_cast<char_type>(qchar.unicode());
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).
77 inline QChar const ucs4_to_qchar(char_type const ucs4)
79 BOOST_ASSERT(is_utf16(ucs4));
80 return QChar(static_cast<unsigned short>(ucs4));
85 * toqstr - convert a UCS4 encoded docstring into a QString
87 * This is the preferred method of converting anything that possibly
88 * contains non-ASCII stuff to QString.
90 #if QT_VERSION >= 0x040200
91 inline QString const toqstr(docstring const & ucs4)
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());
98 QString const toqstr(docstring const & ucs4);
103 * qstring_to_ucs4 - convert a QString into a UCS4 encoded docstring
105 * This is the preferred method of converting anything that possibly
106 * contains non-ASCII stuff to docstring.
108 docstring const qstring_to_ucs4(QString const & qstr);
112 * fromqstr - convert a QString into a UTF8 encoded std::string
114 * This should not be used except for output to lyxerr, since all possibly
115 * non-ASCII stuff should be stored in a docstring.
117 std::string const fromqstr(QString const & str);
121 #endif // QSTRING_HELPERS_H