]> git.lyx.org Git - features.git/blob - src/support/qstring_helpers.cpp
bb800f3c67eeda9d84897efd9f99b9bac4a7c928
[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/qstring_helpers.h"
16
17 #include "support/debug.h"
18 #include "support/docstring.h"
19 #include "support/qstring_helpers.h"
20
21 #include <QRegExp>
22 #include <QString>
23 #include <QVector>
24
25 namespace lyx {
26
27 LyXErr & operator<<(LyXErr & err, QString const & str)
28 {
29         return err << fromqstr(str);
30 }
31
32
33 QString toqstr(char const * str)
34 {
35         return QString::fromUtf8(str);
36 }
37
38 QString toqstr(std::string const & str)
39 {
40         return toqstr(str.c_str());
41 }
42
43
44 QString toqstr(docstring const & ucs4)
45 {
46         // If possible we let qt do the work, since this version does not
47         // need to be superfast.
48         if (ucs4.empty())
49                 return QString();
50 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
51         return QString::fromStdU32String(reinterpret_cast<std::u32string const &>(ucs4));
52 #else
53         return QString::fromUcs4((uint const *)ucs4.data(), ucs4.length());
54 #endif
55 }
56
57 QString toqstr(char_type ucs4)
58 {
59 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
60         return QString::fromStdU32String(std::u32string(1, ucs4));
61 #else
62         union { char_type c; uint i; } u = { ucs4 };
63         return QString::fromUcs4(&u.i, 1);
64 #endif
65 }
66
67 docstring qstring_to_ucs4(QString const & qstr)
68 {
69         if (qstr.isEmpty())
70                 return docstring();
71         QVector<uint> const ucs4 = qstr.toUcs4();
72         return docstring((char_type const *)(ucs4.constData()), ucs4.size());
73 }
74
75 std::string fromqstr(QString const & str)
76 {
77         return str.isEmpty() ? std::string() : std::string(str.toUtf8());
78 }
79
80 QString charFilterRegExp(QString const & filter)
81 {
82         QString re = ".*";
83         for (QChar const & c : filter) {
84                 if (c.isLower())
85                         re +=  "["+ QRegExp::escape(c) + QRegExp::escape(c.toUpper()) + "]";
86                 else
87                         re +=  QRegExp::escape(c);
88         }
89         return re;
90 }
91
92 QString charFilterRegExpC(QString const & filter)
93 {
94         QString re = "(";
95         for (QChar const & c : filter) {
96                 if (c.isLower())
97                         re +=  "["+ QRegExp::escape(c) + QRegExp::escape(c.toUpper()) + "]";
98                 else
99                         re +=  QRegExp::escape(c);
100         }
101         return re + ")";
102 }
103
104
105
106 } // namespace lyx