Multiple fixes

This commit is contained in:
momo5502
2025-01-18 12:39:29 +01:00
parent 5079b750cd
commit cc252447d5
4 changed files with 45 additions and 23 deletions

View File

@@ -2,7 +2,6 @@
#include <span>
#include <string>
#include <cstddef>
#include <sstream>
#include <cwctype>
#include <algorithm>
@@ -47,49 +46,66 @@ namespace utils::string
return to_lower(std::move(str));
}
template <typename Integer>
requires(std::is_integral_v<Integer>)
std::string to_hex_string(const Integer& i)
inline char to_nibble(std::byte value, const bool uppercase = false)
{
std::stringstream stream{};
stream << std::hex << i;
return stream.str();
value = value & static_cast<std::byte>(0xF);
if (value <= static_cast<std::byte>(9))
{
return static_cast<char>('0' + static_cast<char>(value));
}
return static_cast<char>((uppercase ? 'A' : 'a') + (static_cast<char>(value) - 0xA));
}
inline std::string to_hex_string(const void* data, const size_t size)
inline std::pair<char, char> to_hex(const std::byte value, const bool uppercase = false)
{
std::stringstream stream{};
stream << std::hex;
return {to_nibble(value >> 4, uppercase), to_nibble(value, uppercase)};
}
inline std::string to_hex_string(const void* data, const size_t size, const bool uppercase = false)
{
std::string result{};
result.reserve(size * 2);
for (size_t i = 0; i < size; ++i)
{
const auto value = static_cast<const uint8_t*>(data)[i];
stream << value;
const auto value = static_cast<const std::byte*>(data)[i];
const auto [high, low] = to_hex(value, uppercase);
result.push_back(high);
result.push_back(low);
}
return stream.str();
return result;
}
inline std::string to_hex_string(const std::span<std::byte> data)
template <typename Integer>
requires(std::is_integral_v<Integer>)
std::string to_hex_string(const Integer& i, const bool uppercase = false)
{
return to_hex_string(data.data(), data.size());
return to_hex_string(&i, sizeof(Integer), uppercase);
}
inline uint8_t parse_nibble(const char nibble)
inline std::string to_hex_string(const std::span<std::byte> data, const bool uppercase = false)
{
return to_hex_string(data.data(), data.size(), uppercase);
}
inline std::byte parse_nibble(const char nibble)
{
const auto lower = char_to_lower(nibble);
if (lower >= '0' && lower <= '9')
{
return static_cast<uint8_t>(lower - '0');
return static_cast<std::byte>(lower - '0');
}
if (lower >= 'a' && lower <= 'f')
{
return static_cast<uint8_t>(lower - 'a');
return static_cast<std::byte>(lower - 'a');
}
return 0;
return static_cast<std::byte>(0);
}
inline std::vector<std::byte> from_hex_string(const std::string_view str)
@@ -97,13 +113,13 @@ namespace utils::string
const auto size = str.size() / 2;
std::vector<std::byte> data{};
data.resize(size);
data.reserve(size);
for (size_t i = 0; i < size; ++i)
{
const auto high = parse_nibble(str[i * 2 + 0]);
const auto low = parse_nibble(str[i * 2 + 1]);
const auto value = static_cast<std::byte>((high << 4) | low);
const auto value = (high << 4) | low;
data.push_back(value);
}