Repository navigation
Expand file tree
/
Copy pathGlobalFilter.cpp
More file actions
93 lines (80 loc) · 2.63 KB
/
Copy pathGlobalFilter.cpp
File metadata and controls
93 lines (80 loc) · 2.63 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
#include "GlobalFilter.h"
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
namespace GlobalFilter
{
static std::vector<QString> legacyTokenize(const QString& value)
{
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
const QStringList values = value.trimmed().split(" ", QString::SkipEmptyParts);
#else
const QStringList values = value.trimmed().split(" ", Qt::SkipEmptyParts);
#endif
return {values.cbegin(), values.cend()};
}
std::vector<QString> tokenize(const QString& value)
{
std::vector<QString> filters;
int position = 0;
while(position < value.size())
{
while(position < value.size() && value.at(position) == ' ')
++position;
if(position == value.size())
break;
const int termStart = position;
if(value.at(termStart) != '/')
{
while(position < value.size() && value.at(position) != ' ')
++position;
filters.push_back(value.mid(termStart, position - termStart));
continue;
}
bool foundClosingSlash = false;
for(position = termStart + 1; position < value.size(); ++position)
{
if(value.at(position) != '/')
continue;
int backslashes = 0;
for(int previous = position - 1; previous >= termStart && value.at(previous) == '\\'; --previous)
++backslashes;
const bool unescaped = backslashes % 2 == 0;
const bool atTermEnd = position + 1 == value.size() || value.at(position + 1) == ' ';
if(unescaped && !atTermEnd)
return legacyTokenize(value);
if(unescaped)
{
filters.push_back(value.mid(termStart, position - termStart + 1));
++position;
foundClosingSlash = true;
break;
}
}
if(!foundClosingSlash)
return legacyTokenize(value);
}
return filters;
}
void load(std::vector<QString>& filters, QXmlStreamReader& xml)
{
while(xml.readNext() != QXmlStreamReader::EndElement && xml.name() != QT_UNICODE_LITERAL("global_filter"))
{
if(xml.name() == QT_UNICODE_LITERAL("filter"))
{
filters.push_back(xml.attributes().value("value").toString());
xml.skipCurrentElement();
}
}
}
void save(const std::vector<QString>& filters, QXmlStreamWriter& xml)
{
xml.writeStartElement("global_filter");
for(const auto& value : filters)
{
xml.writeStartElement("filter");
xml.writeAttribute("value", value);
xml.writeEndElement();
}
xml.writeEndElement();
}
}