build: Start using std::from_chars

This commit is contained in:
WerWolv
2025-12-03 20:48:22 +01:00
parent c2d70dbd50
commit a0d0621725
7 changed files with 31 additions and 16 deletions

View File

@@ -28,6 +28,8 @@
#include <imgui.h>
#include <wolv/utils/charconv.hpp>
namespace hex {
#if !defined(HEX_MODULE_EXPORT)
@@ -261,7 +263,11 @@ namespace hex {
if (!std::isxdigit(byteString[i]) || !std::isxdigit(byteString[i + 1]))
return {};
result.push_back(std::strtoul(byteString.substr(i, 2).c_str(), nullptr, 16));
auto value = wolv::util::from_chars<u64>(byteString.substr(i, 2), 16);
if (!value.has_value())
return {};
result.push_back(*value);
}
return result;

View File

@@ -34,7 +34,7 @@ namespace hex::plugin::builtin {
template<std::unsigned_integral T, size_t Size = sizeof(T)>
static std::vector<u8> stringToUnsigned(const std::string &value, std::endian endian) requires(sizeof(T) <= sizeof(u64)) {
u64 result = std::strtoull(value.c_str(), nullptr, 0);
const auto result = wolv::util::from_chars<u64>(value).value_or(0);
if (result > std::numeric_limits<T>::max()) return {};
std::vector<u8> bytes(Size, 0x00);
@@ -48,7 +48,7 @@ namespace hex::plugin::builtin {
template<std::signed_integral T, size_t Size = sizeof(T)>
static std::vector<u8> stringToSigned(const std::string &value, std::endian endian) requires(sizeof(T) <= sizeof(u64)) {
i64 result = std::strtoll(value.c_str(), nullptr, 0);
const auto result = wolv::util::from_chars<i64>(value).value_or(0);
if (result > std::numeric_limits<T>::max() || result < std::numeric_limits<T>::min()) return {};
std::vector<u8> bytes(Size, 0x00);
@@ -62,7 +62,7 @@ namespace hex::plugin::builtin {
template<std::floating_point T>
static std::vector<u8> stringToFloat(const std::string &value, std::endian endian) requires(sizeof(T) <= sizeof(long double)) {
T result = std::strtold(value.c_str(), nullptr);
const T result = wolv::util::from_chars<double>(value).value_or(0);
std::vector<u8> bytes(sizeof(T), 0x00);
std::memcpy(bytes.data(), &result, bytes.size());
@@ -362,7 +362,7 @@ namespace hex::plugin::builtin {
[](const std::string &value, std::endian endian) -> std::vector<u8> {
std::ignore = endian;
return hex::crypt::encodeSleb128(std::strtoll(value.c_str(), nullptr, 0));
return hex::crypt::encodeSleb128(wolv::util::from_chars<i64>(value).value_or(0));
}
);
@@ -379,7 +379,7 @@ namespace hex::plugin::builtin {
[](const std::string &value, std::endian endian) -> std::vector<u8> {
std::ignore = endian;
return hex::crypt::encodeUleb128(std::strtoull(value.c_str(), nullptr, 0));
return hex::crypt::encodeUleb128(wolv::util::from_chars<u64>(value).value_or(0));
}
);

View File

@@ -54,7 +54,11 @@ namespace hex::plugin::builtin {
if (!std::isxdigit(byteString[i]) || !std::isxdigit(byteString[i + 1]))
throwNodeError("Invalid byte string format");
result.push_back(std::strtoul(byteString.substr(i, 2).c_str(), nullptr, 16));
auto value = wolv::util::from_chars<u64>(byteString.substr(i, 2), 16);
if (!value.has_value())
throwNodeError("Invalid number value");
result.push_back(*value);
}
return result;

View File

@@ -2,6 +2,7 @@
#include <hex/api/content_registry/pattern_language.hpp>
#include <hex/providers/provider.hpp>
#include <hex/helpers/magic.hpp>
#include <hex/helpers/utils.hpp>
#include <pl/core/evaluator.hpp>
@@ -10,10 +11,12 @@ namespace hex::plugin::builtin {
void registerPatternLanguagePragmas() {
ContentRegistry::PatternLanguage::addPragma("base_address", [](pl::PatternLanguage &runtime, const std::string &value) {
auto baseAddress = strtoull(value.c_str(), nullptr, 0);
auto baseAddress = wolv::util::from_chars<i64>(value);
if (!baseAddress.has_value())
return false;
ImHexApi::Provider::get()->setBaseAddress(baseAddress);
runtime.setDataBaseAddress(baseAddress);
ImHexApi::Provider::get()->setBaseAddress(*baseAddress);
runtime.setDataBaseAddress(*baseAddress);
return true;
});

View File

@@ -4,6 +4,8 @@
#include <hex/ui/imgui_imhex_extensions.h>
#include <charconv>
namespace hex::plugin::builtin {
static u32 digitsNeeded(u32 numDigits, u32 baseX, u32 baseY) {
@@ -40,16 +42,16 @@ namespace hex::plugin::builtin {
switch (base) {
case 10:
number = std::strtoull(buffers[0].c_str(), nullptr, base);
number = wolv::util::from_chars<u64>(buffers[0], base).value_or(0);
break;
case 16:
number = std::strtoull(buffers[1].c_str(), nullptr, base);
number = wolv::util::from_chars<u64>(buffers[1], base).value_or(0);
break;
case 8:
number = std::strtoull(buffers[2].c_str(), nullptr, base);
number = wolv::util::from_chars<u64>(buffers[2], base).value_or(0);
break;
case 2:
number = std::strtoull(buffers[3].c_str(), nullptr, base);
number = wolv::util::from_chars<u64>(buffers[3], base).value_or(0);
break;
default:
return;