-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaidenheadLocatorValidator.cpp
73 lines (71 loc) · 2.4 KB
/
MaidenheadLocatorValidator.cpp
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
#include "MaidenheadLocatorValidator.hpp"
MaidenheadLocatorValidator::MaidenheadLocatorValidator (QObject * parent, Length length, Length required)
: QValidator {parent}
{
switch (length)
{
case Length::field:
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})$)"});
break;
case Length::square:
if (Length::field == required)
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})([0-9]{2}){0,1}$)"});
}
else
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})[0-9]{2}$)"});
}
break;
case Length::subsquare:
if (Length::field == required)
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})([0-9]{2}((?<subsquare>[A-Xa-x]{2}){0,1})){0,1}$)"});
}
else if (Length::square == required)
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})[0-9]{2}(?<subsquare>[A-Xa-x]{2}){0,1}$)"});
}
else
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})[0-9]{2}(?<subsquare>[A-Xa-x]{2})$)"});
}
break;
case Length::extended:
if (Length::field == required)
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})([0-9]{2}((?<subsquare>[A-Xa-x]{2}){0,1}([0-9]{2}){0,1})){0,1}$)"});
}
else if (Length::square == required)
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})[0-9]{2}((?<subsquare>[A-Xa-x]{2})([0-9]{2}){0,1}){0,1}$)"});
}
else if (Length::subsquare == required)
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})[0-9]{2}(?<subsquare>[A-Xa-x]{2})([0-9]{2}){0,1}$)"});
}
else
{
re_.setPattern ({R"(^(?<field>[A-Ra-r]{2})[0-9]{2}(?<subsquare>[A-Xa-x]{2})[0-9]{2}$)"});
}
break;
}
}
auto MaidenheadLocatorValidator::validate (QString& input, int& pos) const -> State
{
auto match = re_.match (input, 0, QRegularExpression::PartialPreferCompleteMatch);
auto field = match.captured ("field");
if (field.size ())
{
input.replace (match.capturedStart ("field"), match.capturedLength ("field"), field.toUpper ());
}
auto subsquare = match.captured ("subsquare");
if (subsquare.size ())
{
input.replace (match.capturedStart ("subsquare"), match.capturedLength ("subsquare"), subsquare.toLower ());
}
if (match.hasMatch ()) return Acceptable;
if (!input.size () || match.hasPartialMatch ()) return Intermediate;
pos = input.size ();
return Invalid;
}