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