]> git.lyx.org Git - features.git/blob - src/support/qstring_helpers.h
shuffle stuff around
[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 <QString>
18
19 namespace lyx {
20
21 /**
22  * toqstr - convert a UTF8 encoded char * into a QString
23  *
24  * This should not be used, since all possibly non-ASCII stuff should be
25  * stored in a docstring.
26  */
27 inline QString const toqstr(char const * str)
28 {
29         return QString::fromUtf8(str);
30 }
31
32
33 /**
34  * toqstr - convert a UTF8 encoded std::string into a QString
35  *
36  * This should not be used, since all possibly non-ASCII stuff should be
37  * stored in a docstring.
38  */
39 inline QString const toqstr(std::string const & str)
40 {
41         return toqstr(str.c_str());
42 }
43
44
45 /// Is \p c a valid utf16 char?
46 inline bool is_utf16(char_type c)
47 {
48         // 0xd800 ... 0xdfff is the range of surrogate pairs.
49         return c < 0xd800 || (c > 0xdfff && c < 0x10000);
50 }
51
52
53 /**
54  * toqstr - convert a UCS4 encoded docstring into a QString
55  *
56  * This is the preferred method of converting anything that possibly
57  * contains non-ASCII stuff to QString.
58  */
59 #if QT_VERSION >= 0x040200
60 inline QString const toqstr(docstring const & ucs4)
61 {
62         // If possible we let qt do the work, since this version does not
63         // need to be superfast.
64         return QString::fromUcs4(reinterpret_cast<uint const *>(ucs4.data()), ucs4.length());
65 }
66 #else
67 QString const toqstr(docstring const & ucs4);
68 #endif
69
70
71 /**
72  * qstring_to_ucs4 - convert a QString into a UCS4 encoded docstring
73  *
74  * This is the preferred method of converting anything that possibly
75  * contains non-ASCII stuff to docstring.
76  */
77 docstring const qstring_to_ucs4(QString const & qstr);
78
79
80 /**
81  * fromqstr - convert a QString into a UTF8 encoded std::string
82  *
83  * This should not be used except for output to lyxerr, since all possibly
84  * non-ASCII stuff should be stored in a docstring.
85  */
86 std::string const fromqstr(QString const & str);
87
88 } // namespace lyx
89
90 #endif // QSTRING_HELPERS_H