]> git.lyx.org Git - lyx.git/blob - src/support/qstring_helpers.h
b47f5fe5fe0c39c3bd3d78ec3f9859d174662b3c
[lyx.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 inline QString const toqstr(docstring const & ucs4)
60 {
61         // If possible we let qt do the work, since this version does not
62         // need to be superfast.
63         return QString::fromUcs4(reinterpret_cast<uint const *>(ucs4.data()), ucs4.length());
64 }
65
66
67 /**
68  * qstring_to_ucs4 - convert a QString into a UCS4 encoded docstring
69  *
70  * This is the preferred method of converting anything that possibly
71  * contains non-ASCII stuff to docstring.
72  */
73 docstring const qstring_to_ucs4(QString const & qstr);
74
75
76 /**
77  * fromqstr - convert a QString into a UTF8 encoded std::string
78  *
79  * This should not be used except for output to lyxerr, since all possibly
80  * non-ASCII stuff should be stored in a docstring.
81  */
82 std::string const fromqstr(QString const & str);
83
84 } // namespace lyx
85
86 #endif // QSTRING_HELPERS_H