-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexteditstate.cpp
More file actions
136 lines (111 loc) · 3.34 KB
/
texteditstate.cpp
File metadata and controls
136 lines (111 loc) · 3.34 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
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
#include "texteditstate.h"
#include <QStringList>
#include "ipv4.h"
#include "ipv4int.h"
#include "manualdiff.h"
void TextEditState::fixup_wrong_separators()
{
for (const auto spec_ch : QString(IpV4::wrong_octet_separators))
val.replace(spec_ch, IpV4::octet_separator);
}
void TextEditState::fixup_insignificant_zeros()
{
QStringList octets = val.split(IpV4::octet_separator);
if (octets.length() != IpV4::norm_octets_count)
return;
const auto removed_indexes = remove_insignificant_zeros();
shift_removed_pos(removed_indexes);
}
void TextEditState::fixup_manual_changes(const TextEditState &prev)
{
ManualDiff manual(prev, *this);
if (!manual.valid())
return;
if (manual.inserted && manual.ch == '0')
*this = manual.fixup_inserted_zero();
else
*this = manual.fixup_separators_count();
}
bool TextEditState::have_invalid_chars() const
{
const QString text = val;
int available_char_count = text.count(IpV4::octet_separator);
for (const auto digit : QString(IpV4::Octet::availavle_chars))
available_char_count += text.count(digit);
return available_char_count < text.length();
}
bool TextEditState::is_valid() const
{
const QString text = val;
return IpV4(text).is_valid() || IpV4Int(text).is_valid();
}
bool TextEditState::is_prevalid() const
{
const QString text = val;
return text.isEmpty() || IpV4(text).is_prevalid();
}
bool TextEditState::is_invalid() const
{
const QString text = val;
return IpV4Int(text).is_invalid();
}
void TextEditState::move_separator_to(const int new_separator_pos)
{
const int last_separator_pos = val.lastIndexOf(IpV4::octet_separator);
if (last_separator_pos < new_separator_pos)
{// strip end of last octet
val = val.left(new_separator_pos);
pos = val.length();
}
else
{// cut to the closest separator
auto it = val.begin() + new_separator_pos;
while(*it != IpV4::octet_separator)
it = val.erase(it);
pos = new_separator_pos+1;
}
}
QList<int> TextEditState::remove_insignificant_zeros()
{
QList<int> removed_indexes;
auto octets = val.split(IpV4::octet_separator);
const QList<int> octet_removed_chars_cnt = [&octets](){
QList<int> temp;
for (QString& octet : octets)
temp.append(IpV4::Octet::fix_start(octet));
return temp;
}();
int octet_index = 0;
QString::const_iterator it = val.cbegin();
const QString::const_iterator the_end = val.cend();
while (it != the_end)
{
if (*it == IpV4::octet_separator)
{
++it;
++octet_index;
continue;
}
int it_index_in_octet = 0;
while(it != the_end && *it != IpV4::octet_separator)
{
if (it_index_in_octet < octet_removed_chars_cnt[octet_index])
removed_indexes.append(std::distance(val.cbegin(), it));
++it_index_in_octet;
++it;
}
}
val = octets.join(IpV4::octet_separator);
return removed_indexes;
}
void TextEditState::shift_removed_pos(const QList<int> &removed_indexes)
{
int shifted_count = 0;
for (const int removed_char_index : removed_indexes)
{
if (pos + shifted_count <= removed_char_index)
continue;
++shifted_count;
--pos;
}
}