17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 07:29:20 +02:00

refactor(sonar): use std::array for local fixed-size arrays (cpp:S5945)

Arrays that are class members or part of headers are left unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jerrrrycho
2026-10-10 17:44:14 +02:00
parent 312ba58ffa
commit 43fcda05a0
12 changed files with 74 additions and 74 deletions

View File

@@ -16,8 +16,8 @@ static_assert(-1 == ~0, "Two's complement required");
// deserialize little endian uint16
uint16_t sn_utils::ld_uint16(std::istream &s)
{
uint8_t buf[2];
s.read((char*)buf, 2);
std::array<uint8_t, 2> buf{};
s.read(reinterpret_cast<char *>(buf.data()), buf.size());
uint16_t v = static_cast<uint16_t>(buf[1] << 8 | buf[0]);
return v;
}
@@ -25,8 +25,8 @@ uint16_t sn_utils::ld_uint16(std::istream &s)
// deserialize little endian uint32
uint32_t sn_utils::ld_uint32(std::istream &s)
{
uint8_t buf[4];
s.read((char*)buf, 4);
std::array<uint8_t, 4> buf{};
s.read(reinterpret_cast<char *>(buf.data()), buf.size());
uint32_t v = buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0];
return v;
}
@@ -34,8 +34,8 @@ uint32_t sn_utils::ld_uint32(std::istream &s)
// deserialize little endian int32
int32_t sn_utils::ld_int32(std::istream &s)
{
uint8_t buf[4];
s.read((char*)buf, 4);
std::array<uint8_t, 4> buf{};
s.read(reinterpret_cast<char *>(buf.data()), buf.size());
uint32_t v = buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0];
return static_cast<int32_t>(v);
}
@@ -43,8 +43,8 @@ int32_t sn_utils::ld_int32(std::istream &s)
// deserialize little endian uint64
uint64_t sn_utils::ld_uint64(std::istream &s)
{
uint8_t buf[8];
s.read((char*)buf, 8);
std::array<uint8_t, 8> buf{};
s.read(reinterpret_cast<char *>(buf.data()), buf.size());
uint64_t v = (uint64_t)buf[7] << 56 | (uint64_t)buf[6] << 48 |
(uint64_t)buf[5] << 40 | (uint64_t)buf[4] << 32 |
(uint64_t)buf[3] << 24 | (uint64_t)buf[2] << 16 |
@@ -55,8 +55,8 @@ uint64_t sn_utils::ld_uint64(std::istream &s)
// deserialize little endian int64
int64_t sn_utils::ld_int64(std::istream &s)
{
uint8_t buf[8];
s.read((char*)buf, 8);
std::array<uint8_t, 8> buf{};
s.read(reinterpret_cast<char *>(buf.data()), buf.size());
uint64_t v = (uint64_t)buf[7] << 56 | (uint64_t)buf[6] << 48 |
(uint64_t)buf[5] << 40 | (uint64_t)buf[4] << 32 |
(uint64_t)buf[3] << 24 | (uint64_t)buf[2] << 16 |
@@ -67,8 +67,8 @@ int64_t sn_utils::ld_int64(std::istream &s)
// deserialize little endian ieee754 float32
float sn_utils::ld_float32(std::istream &s)
{
uint8_t buf[4];
s.read((char*)buf, 4);
std::array<uint8_t, 4> buf{};
s.read(reinterpret_cast<char *>(buf.data()), buf.size());
uint32_t v = buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0];
return std::bit_cast<float>(v);
}
@@ -76,8 +76,8 @@ float sn_utils::ld_float32(std::istream &s)
// deserialize little endian ieee754 float64
double sn_utils::ld_float64(std::istream &s)
{
uint8_t buf[8];
s.read((char*)buf, 8);
std::array<uint8_t, 8> buf{};
s.read(reinterpret_cast<char *>(buf.data()), buf.size());
uint64_t v = (uint64_t)buf[7] << 56 | (uint64_t)buf[6] << 48 |
(uint64_t)buf[5] << 40 | (uint64_t)buf[4] << 32 |
(uint64_t)buf[3] << 24 | (uint64_t)buf[2] << 16 |
@@ -90,13 +90,13 @@ std::string sn_utils::d_str(std::istream &s)
{
std::string r;
r.reserve(32);
char buf[1];
char c{};
while (true)
{
s.read(buf, 1);
if (buf[0] == 0)
s.read(&c, 1);
if (c == 0)
break;
r.push_back(buf[0]);
r.push_back(c);
}
return r;
}
@@ -141,35 +141,35 @@ uint8_t sn_utils::d_uint8( std::istream& s ) {
void sn_utils::ls_uint16(std::ostream &s, uint16_t v)
{
uint8_t buf[2];
std::array<uint8_t, 2> buf{};
buf[0] = static_cast<uint8_t>(v);
buf[1] = v >> 8;
s.write((char*)buf, 2);
s.write(reinterpret_cast<char const *>(buf.data()), buf.size());
}
void sn_utils::ls_uint32(std::ostream &s, uint32_t v)
{
uint8_t buf[4];
std::array<uint8_t, 4> buf{};
buf[0] = static_cast<uint8_t>(v);
buf[1] = static_cast<uint8_t>(v >> 8);
buf[2] = static_cast<uint8_t>(v >> 16);
buf[3] = v >> 24;
s.write((char*)buf, 4);
s.write(reinterpret_cast<char const *>(buf.data()), buf.size());
}
void sn_utils::ls_int32(std::ostream &s, int32_t v)
{
uint8_t buf[4];
std::array<uint8_t, 4> buf{};
buf[0] = static_cast<uint8_t>(v);
buf[1] = static_cast<uint8_t>(v >> 8);
buf[2] = static_cast<uint8_t>(v >> 16);
buf[3] = v >> 24;
s.write((char*)buf, 4);
s.write(reinterpret_cast<char const *>(buf.data()), buf.size());
}
void sn_utils::ls_uint64(std::ostream &s, uint64_t v)
{
uint8_t buf[8];
std::array<uint8_t, 8> buf{};
buf[0] = static_cast<uint8_t>(v);
buf[1] = static_cast<uint8_t>(v >> 8);
buf[2] = static_cast<uint8_t>(v >> 16);
@@ -178,12 +178,12 @@ void sn_utils::ls_uint64(std::ostream &s, uint64_t v)
buf[5] = static_cast<uint8_t>(v >> 40);
buf[6] = static_cast<uint8_t>(v >> 48);
buf[7] = v >> 56;
s.write((char*)buf, 8);
s.write(reinterpret_cast<char const *>(buf.data()), buf.size());
}
void sn_utils::ls_int64(std::ostream &s, int64_t v)
{
uint8_t buf[8];
std::array<uint8_t, 8> buf{};
buf[0] = static_cast<uint8_t>(v);
buf[1] = static_cast<uint8_t>(v >> 8);
buf[2] = static_cast<uint8_t>(v >> 16);
@@ -192,24 +192,24 @@ void sn_utils::ls_int64(std::ostream &s, int64_t v)
buf[5] = static_cast<uint8_t>(v >> 40);
buf[6] = static_cast<uint8_t>(v >> 48);
buf[7] = v >> 56;
s.write((char*)buf, 8);
s.write(reinterpret_cast<char const *>(buf.data()), buf.size());
}
void sn_utils::ls_float32(std::ostream &s, float t)
{
auto v = std::bit_cast<uint32_t>(t);
uint8_t buf[4];
std::array<uint8_t, 4> buf{};
buf[0] = static_cast<uint8_t>(v);
buf[1] = static_cast<uint8_t>(v >> 8);
buf[2] = static_cast<uint8_t>(v >> 16);
buf[3] = v >> 24;
s.write((char*)buf, 4);
s.write(reinterpret_cast<char const *>(buf.data()), buf.size());
}
void sn_utils::ls_float64(std::ostream &s, double t)
{
auto v = std::bit_cast<uint64_t>(t);
uint8_t buf[8];
std::array<uint8_t, 8> buf{};
buf[0] = static_cast<uint8_t>(v);
buf[1] = static_cast<uint8_t>(v >> 8);
buf[2] = static_cast<uint8_t>(v >> 16);
@@ -218,7 +218,7 @@ void sn_utils::ls_float64(std::ostream &s, double t)
buf[5] = static_cast<uint8_t>(v >> 40);
buf[6] = static_cast<uint8_t>(v >> 48);
buf[7] = v >> 56;
s.write((char*)buf, 8);
s.write(reinterpret_cast<char const *>(buf.data()), buf.size());
}
void sn_utils::s_uint8(std::ostream &s, uint8_t v)