Changeset View
Changeset View
Standalone View
Standalone View
libs/editor/simpleipv4addressvalidator.cpp
Show All 17 Lines | |||||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "simpleipv4addressvalidator.h" | 21 | #include "simpleipv4addressvalidator.h" | ||
22 | 22 | | |||
23 | #include <QStringList> | 23 | #include <QStringList> | ||
24 | #include <QVector> | 24 | #include <QVector> | ||
25 | 25 | | |||
26 | SimpleIpV4AddressValidator::SimpleIpV4AddressValidator(QObject *parent) | 26 | SimpleIpV4AddressValidator::SimpleIpV4AddressValidator(QObject *parent, enum AddressStyle style) | ||
pino: as above, no "enum" | |||||
27 | : QValidator(parent) | 27 | : QValidator(parent) | ||
28 | { | 28 | { | ||
29 | addressStyle = style; | ||||
pino: in constructor initialization list | |||||
29 | } | 30 | } | ||
30 | 31 | | |||
31 | SimpleIpV4AddressValidator::~SimpleIpV4AddressValidator() | 32 | SimpleIpV4AddressValidator::~SimpleIpV4AddressValidator() | ||
32 | { | 33 | { | ||
33 | } | 34 | } | ||
34 | 35 | | |||
35 | QValidator::State SimpleIpV4AddressValidator::validate(QString &address, int &pos) const | 36 | QValidator::State SimpleIpV4AddressValidator::validate(QString &address, int &pos) const | ||
36 | { | 37 | { | ||
37 | if (QValidator::Invalid == checkWithInputMask(address, pos)) { | 38 | if (QValidator::Invalid == checkWithInputMask(address, pos)) { | ||
38 | return QValidator::Invalid; | 39 | return QValidator::Invalid; | ||
39 | } | 40 | } | ||
40 | 41 | | |||
41 | // this list will be filled with tetrad values. It can be used to make | 42 | // this list will be filled with tetrad values. It can be used to make | ||
42 | // some additional correctness checks on the last validation step. | 43 | // some additional correctness checks on the last validation step. | ||
43 | QList<int> tetrads; | 44 | QList<int> tetrads; | ||
44 | 45 | | |||
45 | return checkTetradsRanges(address, tetrads); | 46 | return checkTetradsRanges(address, tetrads); | ||
46 | } | 47 | } | ||
47 | 48 | | |||
48 | QValidator::State SimpleIpV4AddressValidator::checkWithInputMask(QString &value, int &pos) const | 49 | QValidator::State SimpleIpV4AddressValidator::checkWithInputMask(QString &value, int &pos) const | ||
49 | { | 50 | { | ||
50 | QRegExpValidator v(QRegExp(QLatin1String("[0-9, ]{1,3}\\.[0-9, ]{1,3}\\.[0-9, ]{1,3}\\.[0-9, ]{1,3}")), nullptr); | 51 | QRegExpValidator *v; | ||
51 | 52 | | |||
52 | return v.validate(value, pos); | 53 | switch (addressStyle) { | ||
54 | case Base: | ||||
55 | v = new QRegExpValidator(QRegExp(QLatin1String("[0-9, ]{1,3}\\.[0-9, ]{1,3}\\.[0-9, ]{1,3}\\.[0-9, ]{1,3}")), 0); | ||||
56 | break; | ||||
57 | | ||||
58 | case WithCidr: | ||||
59 | v = new QRegExpValidator(QRegExp(QLatin1String("([0-9, ]{1,3}\\.){3,3}[0-9, ]{1,3}/[0-9]{1,2}")), 0); | ||||
60 | break; | ||||
61 | | ||||
62 | case WithPort: | ||||
63 | v = new QRegExpValidator(QRegExp(QLatin1String("([0-9, ]{1,3}\\.){3,3}[0-9, ]{1,3}:[0-9]{1,5}")), 0); | ||||
64 | break; | ||||
65 | } | ||||
66 | return v->validate(value, pos); | ||||
all the regexps and their validators are created every time, and this is going to be super slow; since the type is decided at constructor time and never changed, just create the validator once (alos, this code leaks all the validators) pino: all the regexps and their validators are created every time, and this is going to be super slow… | |||||
53 | } | 67 | } | ||
54 | 68 | | |||
55 | QValidator::State SimpleIpV4AddressValidator::checkTetradsRanges(QString &value, QList<int> &tetrads) const | 69 | QValidator::State SimpleIpV4AddressValidator::checkTetradsRanges(QString &value, QList<int> &tetrads) const | ||
56 | { | 70 | { | ||
57 | QStringList temp; | 71 | QStringList temp; | ||
58 | const QVector<QStringRef> addrParts = value.splitRef(QLatin1Char('.')); | 72 | QVector<QStringRef> addrParts; | ||
73 | QStringList cidrParts; | ||||
74 | QStringList portParts; | ||||
75 | | ||||
76 | switch (addressStyle) { | ||||
77 | case Base: | ||||
78 | addrParts = value.splitRef(QLatin1Char('.')); | ||||
79 | break; | ||||
80 | | ||||
81 | case WithCidr: | ||||
82 | cidrParts = value.split(QLatin1Char('/')); | ||||
83 | addrParts = cidrParts[0].splitRef(QLatin1Char('.')); | ||||
84 | break; | ||||
85 | | ||||
86 | case WithPort: | ||||
87 | portParts = value.split(QLatin1Char(':')); | ||||
88 | addrParts = portParts[0].splitRef(QLatin1Char('.')); | ||||
89 | break; | ||||
90 | } | ||||
91 | | ||||
59 | int i = 0; | 92 | int i = 0; | ||
60 | // fill in the list with invalid values | 93 | // fill in the list with invalid values | ||
61 | tetrads << -1 << -1 << -1 << -1; | 94 | tetrads << -1 << -1 << -1 << -1; | ||
62 | 95 | | |||
63 | // lets check address parts | 96 | // lets check address parts | ||
64 | Q_FOREACH (const QStringRef &part, addrParts) { | 97 | Q_FOREACH (const QStringRef &part, addrParts) { | ||
65 | if (part.isEmpty()) { | 98 | if (part.isEmpty()) { | ||
66 | if (i != (addrParts.size() - 1)) { | 99 | if (i != (addrParts.size() - 1)) { | ||
Show All 12 Lines | |||||
79 | } | 112 | } | ||
80 | 113 | | |||
81 | // correct tetrad value: for example, 001 -> 1 | 114 | // correct tetrad value: for example, 001 -> 1 | ||
82 | temp.append(QString::number(tetrads[i])); | 115 | temp.append(QString::number(tetrads[i])); | ||
83 | 116 | | |||
84 | i++; | 117 | i++; | ||
85 | } | 118 | } | ||
86 | 119 | | |||
120 | | ||||
pino: unneeded empty line change | |||||
87 | // replace input string with the corrected version | 121 | // replace input string with the corrected version | ||
88 | value = temp.join(QLatin1String(".")); | 122 | value = temp.join(QLatin1String(".")); | ||
89 | 123 | | |||
90 | if (i < 4) { | 124 | if (i < 4) { | ||
91 | // not all tetrads are filled... continue | 125 | // not all tetrads are filled... continue | ||
92 | // qCDebug(PLASMA_NM) << "QValidator::Intermediate"; | 126 | // qCDebug(PLASMA_NM) << "QValidator::Intermediate"; | ||
93 | return QValidator::Intermediate; | 127 | return QValidator::Intermediate; | ||
94 | } | 128 | } | ||
95 | else { | 129 | else { | ||
130 | if (addressStyle == WithCidr && cidrParts.size() > 1) { | ||||
131 | value += QLatin1String("/"); | ||||
132 | if (!cidrParts[1].isEmpty()) { | ||||
133 | int cidrValue = cidrParts[1].toInt(); | ||||
134 | if (cidrValue > 32) { | ||||
135 | return QValidator::Invalid; | ||||
136 | } else { | ||||
137 | value += QString::number(cidrValue); | ||||
there is already cidrParts[1] as string, so no need to create it again from the value pino: there is already `cidrParts[1]` as string, so no need to create it again from the value | |||||
138 | return QValidator::Acceptable; | ||||
139 | } | ||||
140 | } | ||||
141 | } else if (addressStyle == WithPort && portParts.size() > 1) { | ||||
142 | value += QLatin1String(":"); | ||||
143 | if (!portParts[1].isEmpty()) { | ||||
144 | int portValue = portParts[1].toInt(); | ||||
145 | if (portValue > 65535) { | ||||
146 | return QValidator::Invalid; | ||||
147 | } else { | ||||
148 | value += QString::number(portValue); | ||||
pino: ditto | |||||
149 | return QValidator::Acceptable; | ||||
150 | } | ||||
151 | } | ||||
152 | } | ||||
153 | | ||||
96 | // qCDebug(PLASMA_NM) << "QValidator::Acceptable"; | 154 | // qCDebug(PLASMA_NM) << "QValidator::Acceptable"; | ||
97 | return QValidator::Acceptable; | 155 | return QValidator::Acceptable; | ||
98 | } | 156 | } | ||
99 | } | 157 | } |
as above, no "enum"