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
184
185
186
187
188
189
190
191
192
|
#include "Util.hpp"
#include <QRegularExpression>
namespace Util {
QString trimQuotes(QString string, bool& trimmed)
{
if(string.isEmpty()) return string;
trimmed = false;
if(string.length() >= 2 && string.at(0) == '"' && string.at(string.length() - 1) == '"')
{
string.remove(0, 1);
string.remove(string.length() - 1, 1);
trimmed = true;
}
return string;
}
QString unescape(const QString &string, const QChar escapeChar)
{
if(string.isEmpty()) return string;
QString ret;
bool escaped = false;
QChar c = string.at(0);
for(int i = 0; i < string.length(); ++i)
{
c = string.at(i);
if(!escaped)
{
if(c == escapeChar)
{
escaped = true;
continue;
}
else
{
ret.append(c);
}
}
else
{
if(unescapeChars.contains(c))
{
ret.append(unescapeChars.value(c));
}
else
{
ret.append(c);
}
escaped = false;
}
}
if(escaped)
{
ret.append(escapeChar);
}
return ret;
}
QString escape(const QString &string, const QChar escapeChar, const QString &escapeChars)
{
QString ret;
for(int i = 0; i < string.length(); ++i)
{
QChar c = string.at(i);
if(c == escapeChar)
{
ret.append(escapeChar);
ret.append(escapeChar);
}
else if(escapeChars.contains(c))
{
ret.append(escapeChar);
ret.append(c);
}
else if(unescapeChars.values().contains(c))
{
ret.append(escapeChar);
ret.append(unescapeChars.key(c));
}
else
{
ret.append(c);
}
}
return ret;
}
QString joinEscape(const QStringList &list, const QChar joinChar, const QChar escapeChar)
{
QString ret;
bool first = true;
foreach(const QString& part, list)
{
if(!first)
{
ret.append(joinChar);
}
else
{
first = false;
}
ret.append(escape(part, escapeChar, joinChar));
}
return ret;
}
QStringList splitEscaped(const QString &joined, const QChar splitChar, const QChar escapeChar)
{
QStringList ret;
int partPos = 0;
bool escaped = false;
for(int i = 0; i < joined.length(); ++i)
{
QChar c = joined.at(i);
if(!escaped)
{
if(c == escapeChar)
{
escaped = true;
}
else if(c == splitChar)
{
ret.append(Util::unescape(joined.mid(partPos, i - partPos), escapeChar));
partPos = i + 1;
}
}
else
{
escaped = false;
}
}
ret.append(Util::unescape(joined.mid(partPos), escapeChar));
return ret;
}
QString& unescapeClonkString(QString&& string)
{
static QRegularExpression escapeExp(R"(\\[0-7]+)");
QRegularExpressionMatch match;
while((match = escapeExp.match(string)).hasMatch())
{
unsigned char c = (unsigned char)match.capturedRef(0).mid(1).toInt(0, 8);
string.replace(match.capturedStart(), match.capturedLength(), c);
}
return string;
}
int indexOfEscaped(const QString& string, const QChar subject, int startPos, const QChar escapeChar)
{
if(string.isEmpty())
{
return -1;
}
if(startPos < 0)
{
startPos += string.length();
}
bool escaped = false;
for(int pos = startPos; pos < string.length(); ++pos)
{
QChar c = string.at(pos);
if(!escaped)
{
if(c == escapeChar)
{
escaped = true;
continue;
}
else
{
if(c == subject)
{
return pos;
}
}
}
else
{
escaped = false;
}
}
return -1;
}
}
|