-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipv4octet.cpp
More file actions
76 lines (60 loc) · 1.4 KB
/
ipv4octet.cpp
File metadata and controls
76 lines (60 loc) · 1.4 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
#include "ipv4.h"
#include <QRegularExpression>
bool IpV4::Octet::fix_empty(QString &octet)
{
bool changed = octet.isEmpty();
if (octet.isEmpty())
{
octet = '0';
changed = true;
}
return changed;
}
int IpV4::Octet::fix_start(QString &octet)
{
int removed_char_count = 0;
while (octet != '0' && octet.startsWith('0'))
{
octet.erase(octet.begin());
++removed_char_count;
}
return removed_char_count;
}
bool IpV4::Octet::is_valid(int value)
{
return value >= min_value && value <= max_value;
}
bool IpV4::Octet::is_valid() const
{
if (m_text.isEmpty())
return false;
if (m_text.contains(QRegularExpression("\\D")))
return false;
int value = int_value();
if (!is_valid(value))
return false;
if (value > 0 && m_text.startsWith("0"))
return false;
if (value == 0 && m_text != "0")
return false;
return true;
}
bool IpV4::Octet::is_prevalid() const
{
if (m_text.isEmpty())
return true;
if (m_text.contains(QRegularExpression("\\D")))
return false;
int value = int_value();
if (!is_valid(value))
return false;
if (value > 0 && m_text.startsWith("0"))
return false;
return true;
}
int IpV4::Octet::int_value() const
{
bool ok;
const unsigned long value = m_text.toULong(&ok);
return ok ? value : min_value-1;
}