-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrchelper.cpp
More file actions
73 lines (62 loc) · 2.29 KB
/
Copy pathcrchelper.cpp
File metadata and controls
73 lines (62 loc) · 2.29 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
// This is a personal academic project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
#include "crchelper.h"
#include <stdexcept>
#include <QRegularExpression>
unsigned int CRCHelper::tryParsePolynom(const QString &text)
{
QString prepared = text.simplified().remove(' ');
if(prepared.isEmpty())
throw std::runtime_error("Пустой полином");
unsigned int polynom = 0;
int matchedCount = 0;
bool converted = false;
bool zeroDegreeFound = false;
QRegularExpression polynomDegreePart("x\\^(\\d{1,2})", QRegularExpression::CaseInsensitiveOption);
auto matched = polynomDegreePart.globalMatch(prepared);
for(; matched.hasNext(); ++matchedCount)
{
const auto i = matched.next().captured(1);
unsigned degree = i.toUInt(&converted, 10);
if(!converted)
throw std::runtime_error(std::string("Ошибка полинома в \"" + i.toStdString() + "\""));
polynom |= (1 << degree);
if(degree == 0)
zeroDegreeFound = true;
}
if(matchedCount != prepared.count('x', Qt::CaseInsensitive))
throw std::runtime_error("Не все части полинома удалось распознать");
const bool hasPlusOneAtTheEnd = prepared.endsWith("+1");
if(hasPlusOneAtTheEnd)
{
if(zeroDegreeFound)
throw std::runtime_error("Ошибка полинома");
else
polynom |= 1;
}
return polynom;
}
std::vector<uint8_t> CRCHelper::tryParceByteArray(const QString& text)
{
if(text.isEmpty())
throw std::runtime_error("Поле исходного текста пусто");
std::vector<uint8_t> result;
QRegularExpression isHex("^(0x)?[0-9a-f]{2}$", QRegularExpression::CaseInsensitiveOption);
bool parseOk = false;
uint8_t temp = 0;
auto tokens = text.split(' ');
for(const auto& i : tokens)
{
if(i.contains(isHex))
{
temp = i.toUInt(&parseOk, 16);
if(parseOk)
{
result.push_back(temp);
continue;
}
}
throw std::runtime_error(std::string("Ошибка разбора \"" + i.toStdString() + "\""));
}
return result;
}