-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqmsimplevarexp.cpp
More file actions
183 lines (162 loc) · 5.34 KB
/
qmsimplevarexp.cpp
File metadata and controls
183 lines (162 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "qmsimplevarexp.h"
#include "qmsystem.h"
#include <QCoreApplication>
#include <QDir>
#include <QStandardPaths>
/*!
\class QMSimpleVarExp
\brief Variable expression parsing class.
*/
/*!
Constructs an instance with the default escaping pattern <tt>\w+</tt>.
*/
QMSimpleVarExp::QMSimpleVarExp() : Pattern(QStringLiteral("\\w+")) {
}
/*!
Constructs an instance with the given escaping pattern, should be a regular expression.
*/
QMSimpleVarExp::QMSimpleVarExp(const QString &pattern) : Pattern(pattern) {
}
/*!
Destructor.
*/
QMSimpleVarExp::~QMSimpleVarExp() {
}
/*!
Adds a json object to the searching map, only string values will be included.
*/
void QMSimpleVarExp::addJsonObject(const QJsonObject &obj) {
for (auto it = obj.begin(); it != obj.end(); ++it) {
if (it->isString()) {
Variables.insert(it.key(), it->toString());
}
}
}
/*!
Adds a variant map to the searching map, only string values will be included.
*/
void QMSimpleVarExp::addVariantMap(const QVariantMap &map) {
for (auto it = map.begin(); it != map.end(); ++it) {
if (it->type() == QVariant::String) {
Variables.insert(it.key(), it->toString());
}
}
}
/*!
Adds a variant hash to the searching map, only string values will be included.
*/
void QMSimpleVarExp::addVariantHash(const QVariantHash &hash) {
for (auto it = hash.begin(); it != hash.end(); ++it) {
if (it->type() == QVariant::String) {
Variables.insert(it.key(), it->toString());
}
}
}
/*!
Adds a string map to the searching map.
*/
void QMSimpleVarExp::addMap(const QMap<QString, QString> &map) {
for (auto it = map.begin(); it != map.end(); ++it) {
Variables.insert(it.key(), it.value());
}
}
/*!
Adds a string hash to the searching map.
*/
void QMSimpleVarExp::addHash(const QHash<QString, QString> &hash) {
for (auto it = hash.begin(); it != hash.end(); ++it) {
Variables.insert(it.key(), it.value());
}
}
/*!
Adds a key-value pair to the searching map.
*/
void QMSimpleVarExp::add(const QString &key, const QString &value) {
Variables.insert(key, value);
}
/*!
Clears the searching map.
*/
void QMSimpleVarExp::clear() {
Variables.clear();
}
/*!
\internal
*/
static QString parseExpression(QString s, const QHash<QString, QString> &vars,
const QString &pattern) {
QRegularExpression reg(QStringLiteral(R"((?<!\$)(?:\$\$)*\$\{(%1)\})").arg(pattern));
bool hasMatch;
do {
hasMatch = false;
QString result;
QRegularExpressionMatch match;
int index = 0;
int lastIndex = 0;
while ((index = s.indexOf(reg, index, &match)) != -1) {
hasMatch = true;
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
result += QStringView(s).sliced(lastIndex, index - lastIndex);
#else
result += s.midRef(lastIndex, index - lastIndex);
#endif
const auto &name = match.captured(1);
QString val;
auto it = vars.find(name);
if (it == vars.end()) {
val = name;
} else {
val = it.value();
}
result += val;
index += match.captured(0).size();
lastIndex = index;
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
result += QStringView(s).sliced(lastIndex);
#else
result += s.midRef(lastIndex);
#endif
s = result;
} while (hasMatch);
s.replace(QStringLiteral("$$"), QStringLiteral("$"));
return s;
}
/*!
Parses the given expression.
*/
QString QMSimpleVarExp::parse(const QString &exp) const {
return parseExpression(exp, Variables, Pattern);
}
/*!
Returns a string hash containing common system environment variables.
\li DESKTOP: Desktop location
\li DOCUMENTS: Documents location
\li APPLICATIONS: Applications location
\li HOME: Home location
\li APPDATA: Application data location
\li TEMP: Temporary location
\li ROOT: Filesystem root
\li APPPATH: Application directory path
\li APPNAME: Application name
*/
QHash<QString, QString> QMSimpleVarExp::systemValues() {
return {
{"DESKTOP", QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) },
{"DOCUMENTS", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) },
{"APPLICATIONS", QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation)},
{"HOME", QStandardPaths::writableLocation(QStandardPaths::HomeLocation) },
{"APPDATA", QM::appDataPath() },
{"TEMP", QDir::tempPath() },
{"ROOT", QDir::rootPath() },
{"APPPATH", qApp->applicationDirPath() },
{"APPNAME", qApp->applicationName() },
};
}
/*!
Parses a variable expression using the given variable \c dict .
*/
QString QMSimpleVarExp::evaluate(const QString &s, const QHash<QString, QString> &dict,
const QString &pattern) {
return parseExpression(s, dict, pattern.isEmpty() ? QStringLiteral("\\w+") : pattern);
}