]> git.lyx.org Git - features.git/blob - src/support/qstring_helpers.cpp
2211af8e4ec1c36645e15cdc179e855dce3f6f18
[features.git] / src / support / qstring_helpers.cpp
1 /**
2  * \file qstring_helper.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Dekel Tsur
7  *
8  * Full author contact details are available in file CREDITS.
9  *
10  * A collection of unicode conversion functions, using iconv.
11  */
12
13 #include <config.h>
14
15 #include "support/docstring.h"
16
17 #include <QString>
18 #include <QVector>
19
20 namespace lyx {
21
22 QString toqstr(char const * str)
23 {
24         return QString::fromUtf8(str);
25 }
26
27 QString toqstr(std::string const & str)
28 {
29         return toqstr(str.c_str());
30 }
31
32
33 QString toqstr(docstring const & ucs4)
34 {
35         // If possible we let qt do the work, since this version does not
36         // need to be superfast.
37         if (ucs4.empty())
38                 return QString();
39         return QString::fromUcs4((uint const *)ucs4.data(), ucs4.length());
40 }
41
42 QString toqstr(char_type ucs4)
43 {
44         union { char_type c; uint i; } u = { ucs4 };
45         return QString::fromUcs4(&u.i, 1);
46 }
47
48 docstring qstring_to_ucs4(QString const & qstr)
49 {
50         if (qstr.isEmpty())
51                 return docstring();
52         QVector<uint> const ucs4 = qstr.toUcs4();
53         return docstring((char_type const *)(ucs4.constData()), ucs4.size());
54 }
55
56 std::string fromqstr(QString const & str)
57 {
58         return str.isEmpty() ? std::string() : std::string(str.toUtf8());
59 }
60
61 } // namespace lyx