]> git.lyx.org Git - lyx.git/blob - src/support/qstring_helpers.h
'using namespace std' instead of 'using std::xxx'
[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 #include <QVector>
19
20 namespace lyx {
21
22 /**
23  * toqstr - convert a UTF8 encoded char * into a QString
24  *
25  * This should not be used, since all possibly non-ASCII stuff should be
26  * stored in a docstring.
27  */
28 inline QString const toqstr(char const * str)
29 {
30         return QString::fromUtf8(str);
31 }
32
33
34 /**
35  * toqstr - convert a UTF8 encoded std::string into a QString
36  *
37  * This should not be used, since all possibly non-ASCII stuff should be
38  * stored in a docstring.
39  */
40 inline QString const toqstr(std::string const & str)
41 {
42         return toqstr(str.c_str());
43 }
44
45
46 /// Is \p c a valid utf16 char?
47 inline bool is_utf16(char_type c)
48 {
49         // 0xd800 ... 0xdfff is the range of surrogate pairs.
50         return c < 0xd800 || (c > 0xdfff && c < 0x10000);
51 }
52
53
54 /**
55  * toqstr - convert a UCS4 encoded docstring into a QString
56  *
57  * This is the preferred method of converting anything that possibly
58  * contains non-ASCII stuff to QString.
59  */
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
67
68 /**
69  * qstring_to_ucs4 - convert a QString into a UCS4 encoded docstring
70  *
71  * This is the preferred method of converting anything that possibly
72  * contains non-ASCII stuff to docstring.
73  */
74 inline docstring const qstring_to_ucs4(QString const & qstr)
75 {
76         if (qstr.isEmpty())
77                 return docstring();
78         QVector<uint> const ucs4 = qstr.toUcs4();
79         return docstring((char_type const *)(ucs4.constData()), ucs4.size());
80 }
81
82 /**
83  * fromqstr - convert a QString into a UTF8 encoded std::string
84  *
85  * This should not be used except for output to lyxerr, since all possibly
86  * non-ASCII stuff should be stored in a docstring.
87  */
88 inline std::string const fromqstr(QString const & str)
89 {
90         return str.isEmpty() ? std::string() : std::string(str.toUtf8());
91 }
92
93 } // namespace lyx
94
95 #endif // QSTRING_HELPERS_H