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