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"
23 * toqstr - convert a UTF8 encoded char * into a QString
25 * This should not be used, since all possibly non-ASCII stuff should be
26 * stored in a docstring.
28 inline QString const toqstr(char const * str)
30 return QString::fromUtf8(str);
35 * toqstr - convert a UTF8 encoded std::string into a QString
37 * This should not be used, since all possibly non-ASCII stuff should be
38 * stored in a docstring.
40 inline QString const toqstr(std::string const & str)
42 return toqstr(str.c_str());
46 /// Is \p c a valid utf16 char?
47 inline bool is_utf16(char_type c)
49 // 0xd800 ... 0xdfff is the range of surrogate pairs.
50 return c < 0xd800 || (c > 0xdfff && c < 0x10000);
55 * toqstr - convert a UCS4 encoded docstring into a QString
57 * This is the preferred method of converting anything that possibly
58 * contains non-ASCII stuff to QString.
60 inline QString const toqstr(docstring const & ucs4)
62 // If possible we let qt do the work, since this version does not
63 // need to be superfast.
64 return QString::fromUcs4((uint const *)ucs4.data(), ucs4.length());
68 * toqstr - convert a UCS4 encoded character into a QString
70 * This is the preferred method of converting anything that possibly
71 * contains non-ASCII stuff to QString.
73 inline QString const toqstr(char_type ucs4)
75 union { char_type c; uint i; } u = { ucs4 };
76 return QString::fromUcs4(&u.i, 1);
80 * qstring_to_ucs4 - convert a QString into a UCS4 encoded docstring
82 * This is the preferred method of converting anything that possibly
83 * contains non-ASCII stuff to docstring.
85 inline docstring const qstring_to_ucs4(QString const & qstr)
89 QVector<uint> const ucs4 = qstr.toUcs4();
90 return docstring((char_type const *)(ucs4.constData()), ucs4.size());
94 * fromqstr - convert a QString into a UTF8 encoded std::string
96 * This should not be used except for output to lyxerr, since all possibly
97 * non-ASCII stuff should be stored in a docstring.
99 inline std::string const fromqstr(QString const & str)
101 return str.isEmpty() ? std::string() : std::string(str.toUtf8());
106 #endif // QSTRING_HELPERS_H